scikit learn - sklearn GridSearchCV, SelectKBest, and SVM -
i trying make classifier uses feature selection via functioni have written, golub, returns 2 np arrays selectkbest requires. want link svm classifier linear , and optimize on possible combinations of k , c. however, have tried far has not succeeded , not sure why. code follows:
import numpy np sklearn import cross_validation sklearn import svm sklearn.feature_selection import selectkbest sklearn.pipeline import make_pipeline, pipeline sklearn.grid_search import gridsearchcv golub_mod import golub class svm_golub_linear: def __init__(self,x,y): self.x=x self.y=y def golub_svm(self): x=self.x y=self.y kbest=selectkbest(golub,k=1) k_vals=np.linspace(100,1000,10,dtype=int) k_vals=k_vals.tolist() c_vals=[0.00001,0.0001,0.001,0.01,.1,1,10,100,1000] clf=svm.linearsvc(penalty='l2') steps=[('feature_selection',kbest),('svm_linear',clf)] pipeline=make_pipeline(steps) params=dict(feature_selection__k=k_vals, svm_linear__c=c_vals) best_model=gridsearchcv(pipeline,param_grid=params) self.model=best_model.fit(x,y) print(best_mod.best_params_) def svmpredict(self,x_n): y_vals=self.model.predict(x_n) return y_vals
when try run this:
model=svm_golub_linear(x,y) model.golub_svm()
i following error:
typeerror: last step of chain should implement fit '[('feature_selection', selectkbest(k=1, score_func=<function golub @ 0x105f2c398>)), ('svm_linear', linearsvc(c=1.0, class_weight=none, dual=false, fit_intercept=true, intercept_scaling=1, loss='squared_hinge', max_iter=1000, multi_class='ovr', penalty='l2', random_state=none, tol=0.0001, verbose=0))]' (type <type 'list'>) doesn't)
i not understand because linearsvc have fit method.
in code above if replace
pipeline=make_pipeline(steps)
to
pipeline=pipeline(steps)
the code works is.
Comments
Post a Comment