python - Cross validation and standaridization in skitlearn -


i find accuracy of sklearn classifier k-cross validation. can estimate accuracy without cross-validation. however, how can improve code cross validation , apply standardscaler @ same time?

from sklearn.datasets import load_iris sklearn.cross_validation import train_test_split sklearn.neighbors import kneighborsclassifier sklearn import metrics sklearn.cross_validation import cross_val_score sklearn.preprocessing import standardscaler sklearn import svm sklearn.pipeline import pipeline iris = load_iris() x = iris.data y = iris.target x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=4) pipe_lrsvc = pipeline([('scaler', standardscaler()), ('clf', svm.linearsvc())]) pipe_lrsvc.fit(x_train, y_train) y_pred = pipe_lrsvc.predict(x_test) print(metrics.accuracy_score(y_test, y_pred)) 

simply use pipeline estimator input cross_val_score:

cross_val_score(pipe_lrsvc, iris.data, iris.target, cv=5) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -