GridSearchCV
具体实例:
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 15:30:30 2015
@author: Chaofn
"""
import numpy as np
from sklearn import datasets
from sklearn import cross_validation
from sklearn.svm import SVR
from sklearn.grid_search import GridSearchCV
#Laod sample data
boston=datasets.load_boston()
"""
We can now quickly sample a training set while holding out 40% of
the data for testing (evaluating) our predictor
"""
x_train,x_test,y_train,y_test=cross_validation.train_test_split(
boston.data,boston.target,test_size=0.3,random_state=0)
#Fit regression model
svr=GridSearchCV(SVR(kernel='rbf',gamma=0.1),cv=5,
param_grid={"C":[1e0,1e1,1e2,1e3],
"gamma":np.logspace(-2,2,5)})
svr.fit(x_train,y_train)
#Predict
predict_targets=svr.predict(x_test)
#Evalution
n_test_samples=len(y_test)
error=np.linalg.norm(predict_targets-y_test,ord=1)/n_test_samples
print("Mean Absolute Error is:%.3f" %(error))
pcc=np.corrcoef(predict_targets,y_test)[0,1]
print ("Pearson Correlation Coefficient: %.4f" %(pcc))