作用:简化语法。

准备数据

  1. import numpy as np
  2. from sklearn import datasets
  3. from sklearn.model_selection import train_test_split
  4. # 载入数据
  5. digits = datasets.load_digits()
  6. X = digits.data
  7. y = digits.target
  8. # 分割数据
  9. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)

网格搜索语法

超参数kp结合的搜索

参数设置

  1. from sklearn.model_selection import GridSearchCV
  2. # 设置两组参数
  3. param_grid = [
  4. {
  5. 'weights': ['uniform'],
  6. 'n_neighbors': [i for i in range(1, 11)]
  7. },
  8. {
  9. 'weights': ['distance'],
  10. 'n_neighbors': [i for i in range(1, 11)],
  11. 'p': [i for i in range(1, 6)]
  12. }
  13. ]

建模

  1. knn_clf = KNeighborsClassifier()
  2. from sklearn.model_selection import GridSearchCV
  3. grid_search = GridSearchCV(knn_clf, param_grid)
  4. grid_search.fit(X_train, y_train)

image.png

更多超参数

n_jobs: 代表CPU核数,-1代表使用所有核