Scikit-learn mesh search with SVM regression

I study cross-validation grid search and stumbled upon this youtube playlist , and the tutorial was also uploaded to github as an ipython laptop. I am trying to recreate the codes in the Search for multiple parameters section at the same time , but instead of using knn, I am using SVM regression. This is my code.

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 

But his issuance of this error

raise the value of ValueError ("X must be the square matrix of the kernel") ValueError: X must be the square matrix of the kernel

+5
source share
1 answer

Remove the 'precomputed' from your parameter space.

kernel='precomputed' can only be used when transmitting a data matrix (n_samples, n_samples) , which represents pairwise similarities for samples instead of the traditional rectangular data matrix (n_samples, n_features) .

For more information about the value of the kernel parameter, see the documentation:

+11
source

Source: https://habr.com/ru/post/1246064/


All Articles