我正在学习交叉验证网格搜索,发现了这个youtube播放列表,教程也作为ipython笔记本上传到了github。我试图在同时搜索多个参数部分重新创建代码,但我使用的不是knn,而是SVM回归。这是我的代码
from sklearn.datasets import load_iris
from sklearn import svm
from sklearn.grid_search import GridSearchCV
import matplotlib.pyplot as plt
import numpy as np
iris = load_iris()
X = iris.data
y = iris.target
k=['rbf', 'linear','poly','sigmoid','precomputed']
c= range(1,100)
g=np.arange(1e-4,1e-2,0.0001)
g=g.tolist()
param_grid=dict(kernel=k, C=c, gamma=g)
print param_grid
svr=svm.SVC()
grid = GridSearchCV(svr, param_grid, cv=5,scoring='accuracy')
grid.fit(X, y)
print()
print("Grid scores on development set:")
print()
print grid.grid_scores_
print("Best parameters set found on development set:")
print()
print(grid.best_params_)
print("Grid best score:")
print()
print (grid.best_score_)
# create a list of the mean scores only
grid_mean_scores = [result.mean_validation_score for result in grid.grid_scores_]
print grid_mean_scores
但它给出了这个错误
提升ValueError("X应该是平方核矩阵")ValueError:X应该是一个方核矩阵
从参数空间中删除'precomputed'
。
CCD_ 2只能在传递代表样本的成对相似性的CCD_ 3数据矩阵而不是传统的(n_samples, n_features)
矩形数据矩阵时使用。
有关内核参数的含义的更多详细信息,请参阅文档:
- http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
- http://scikit-learn.org/stable/modules/svm.html#svm-内核