scikit learn 模塊 調參 pipeline+girdsearch 數據舉例:文檔分類數據集 fetch_20newsgroups
#-*- coding: UTF-8 -*- import numpy as np from sklearn.pipeline import Pipeline from sklearn.linear_model import SGDClassifier from sklearn.grid_search import GridSearchCV from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extraction.text import TfidfTransformer from sklearn.datasets import fetch_20newsgroups from sklearn import metrics 獲取待分類的文本數據源 categories = ['comp.graphics', 'comp.os.ms-windows.misc','comp.sys.ibm.pc.hardware','comp.sys.mac.hardware','comp.windows.x']; newsgroup_data = fetch_20newsgroups(subset = 'train',categories = categories) X,Y=np.array(newsgroup_data.data),np.array(newsgroup_data.target) Xtrain,Ytrain,Xtest,Ytest =X[0:2400],Y[0:2400],X[2400:],Y[2400:] #Pipeline主要用於將三個需要串行的模塊串在一起,后一個模型處理前一個的結果''' #vect主要用於去音調、轉小寫、去停頓詞->tdidf主要用於計詞頻->clf分類模型''' pipeline_obj = Pipeline([('vect',CountVectorizer()),('tfidf',TfidfTransformer()),('clf',SGDClassifier()),]) print "pipeline:",'\n', [name for name, _ in pipeline_obj.steps],'\n' #定義需要遍歷的所有候選參數的字典,key_name需要用__分隔模型名和模型內部的參數名''' parameters = { 'vect__max_df': (0.5, 0.75),'vect__max_features': (None, 5000, 10000), 'tfidf__use_idf': (True, False),'tfidf__norm': ('l1', 'l2'), 'clf__alpha': (0.00001, 0.000001), 'clf__n_iter': (10, 50) } print "parameters:",'\n',parameters,'\n' #GridSearchCV用於尋找vectorizer詞頻統計, tfidftransformer特征變換和SGD classifier分類模型的最優參數 grid_search = GridSearchCV( pipeline_obj, parameters, n_jobs = 1,verbose=1 ) print 'grid_search','\n',grid_search,'\n' #輸出所有參數名及參數候選值 grid_search.fit(Xtrain,Ytrain),'\n'#遍歷執行候選參數,尋找最優參數 best_parameters = dict(grid_search.best_estimator_.get_params())#get實例中的最優參數 for param_name in sorted(parameters.keys()): print("\t%s: %r" % (param_name, best_parameters[param_name])),'\n'#輸出最有參數結果 pipeline_obj.set_params(clf__alpha = 1e-05,clf__n_iter = 50,tfidf__use_idf = True,vect__max_df = 0.5,vect__max_features = None) #將pipeline_obj實例中的參數重寫為最優結果''' print pipeline_obj.named_steps #用最優參數訓練模型''' pipeline_obj.fit(Xtrain,Ytrain) pred = pipeline_obj.predict(Xtrain) print '\n',metrics.classification_report(Ytrain,pred) pred = pipeline_obj.predict(Xtest) print '\n',metrics.classification_report(Ytest,pred)
執行結果:總共有96個參數排列組合候選組,每組跑3次模型進行交叉驗證,共計跑模型96*3=288次。
調參前VS調參后:
#參考
#http://blog.csdn.net/mmc2015/article/details/46991465
# http://blog.csdn.net/abcjennifer/article/details/23884761
# http://scikit-learn.org/stable/modules/pipeline.html
# http://blog.csdn.net/yuanyu5237/article/details/44278759