SVM非线性内核:
- 多项式 poly
- 径向基函数 rbf
- Sigmod
- 高级定制核
一、对比linear和rbf内核的性能区别
#比较几种核及参数,挑选出性能最好的svm模型
#载入数据
from sklearn import datasets
digits = datasets.load_digits()
X,y = digits.data, digits.target
#数据区分测试集和训练集,并将数据标准化
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.preprocessing import MinMaxScaler
# We keep 30% random examples for test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
# We scale the data in the range [-1,1]
scaling = MinMaxScaler(feature_range=(-1, 1)).fit(X_train)
X_train = scaling.transform(X_train)
X_test = scaling.transform(X_test)
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
import numpy as np
learning_algo = SVC(class_weight='balanced', random_state=101)
search_space = [{'kernel': ['linear'], 'C': np.logspace(-3, 3, 7)},
{'kernel': ['rbf'], 'degree':[2,3,4], 'C':np.logspace(-3, 3, 7), 'gamma': np.logspace(-3, 2, 6)}]
gridsearch = GridSearchCV(learning_algo, param_grid=search_space, scoring='accuracy', refit=True, cv=10, n_jobs=-1)
gridsearch.fit(X_train,y_train)
cv_performance = gridsearch.best_score_
test_performance = gridsearch.score(X_test, y_test)
print ('Cross-validation ac