[关闭]
@spiritnotes 2016-02-26T03:59:19.000000Z 字数 1229 阅读 2078

scikit-learn-intro

sklearn


原文

综述

sklern中所有估计称为模型。

可视化

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # 'sepal width (cm)'
  4. x_index = 1
  5. # 'petal length (cm)'
  6. y_index = 2
  7. # this formatter will label the colorbar with the correct target names
  8. formatter = plt.FuncFormatter(lambda i, *args: iris.target_names[int(i)])
  9. plt.scatter(iris.data[:, x_index], iris.data[:, y_index],
  10. c=iris.target, cmap=plt.cm.get_cmap('RdYlBu', 3))
  11. plt.colorbar(ticks=[0, 1, 2], format=formatter)
  12. plt.clim(-0.5, 2.5)
  13. plt.xlabel(iris.feature_names[x_index])
  14. plt.ylabel(iris.feature_names[y_index]);

KNN

  1. from sklearn import neighbors, datasets
  2. iris = datasets.load_iris()
  3. X, y = iris.data, iris.target
  4. # create the model
  5. knn = neighbors.KNeighborsClassifier(n_neighbors=5, weights='uniform')
  6. # fit the model
  7. knn.fit(X, y)
  8. # What kind of iris has 3cm x 5cm sepal and 4cm x 2cm petal?
  9. X_pred = [3, 5, 4, 2]
  10. result = knn.predict([X_pred, ])
  11. print(iris.target_names[result])
  12. print(iris.target_names)
  13. print(knn.predict_proba([X_pred, ]))
  14. from fig_code import plot_iris_knn
  15. plot_iris_knn()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注