python 中bayes模型超參數並行網格搜索 程序分析


from sklearn.datasets import fetch_20newsgroups

import numpy as np

news = fetch_20newsgroups(subset='all')



from sklearn.cross_validation import train_test_split

X_train,X_test,y_train,y_test = train_test_split(news.data[:3000],
                                                 news.target[:3000],test_size=0.25,random_state=33)

from sklearn.svm import SVC

from sklearn.feature_extraction.text  import  TfidfVectorizer

#導入 pipeline

from sklearn.pipeline import Pipeline

#使用pipeline 簡化系統搭建流程,將文本抽取與分類器模型串聯起來

clf = Pipeline([('vect',TfidfVectorizer(stop_words='english',analyzer='word')),('svc',SVC())])


parameters = {'svc__gamma': np.logspace(-2, 1, 4), 'svc__C': np.logspace(-1, 1, 3)}  

#從sklearn.grid_search 中導入網絡搜索模塊GridSearchCV

from sklearn.grid_search import GridSearchCV

if __name__ == '__main__':
#將12組參數組合以及初始化的pipline包括3折交叉驗證的要求全部告知GridSearchCV  務必注意refit=true這樣一個設定

  gs = GridSearchCV(clf,parameters,verbose=2,refit = True,cv=3,n_jobs = -1)
#執行單線程網搜索

  gs.fit(X_train,y_train)
 # %time_=gs.best_params_,gs.best_score_
gs.best_params_,gs.best_score_
print(gs.score(X_test,y_test))

程序運行出現如下錯誤:

  File "D:\Python35\demo\gridsearch_model_parallel.py", line 42
    %time_ =gs.best_params_,gs.best_score_
    ^
SyntaxError: invalid syntax

去掉%time_符號,程序正常運行



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章