0
我創建了一個類似於sklearn gridsearch的小代碼,它在一組超參數上訓練模型(在下面的代碼中爲X和y),使用一組超參數有關驗證數據的幾個指標(Xt,yt_class),並將結果存儲在pandas DataFrame中。Python,在多個CPU上運行循環
from sklearn.grid_search import ParameterGrid
from sklearn.metrics import precision_score,f1_score
grid = {'C':[1,10.0,50,100.0],'gamma':[0.00001,0.0001,0.001,0.01,0.1]}
param_grid = ParameterGrid(grid)
results = pd.DataFrame(list(param_grid))
precision = []
f1 = []
for params in param_grid:
model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params)
model.fit(X,y)
precision.append(precision_score(yt_class, model.predict(Xt), average='weighted'))
f1.append(f1_score(yt_class, model.predict(Xt), average='weighted'))
print(params)
print(precision_score(yt_class, model.predict(Xt), average='weighted'))
print(f1_score(yt_class, model.predict(Xt), average='weighted'))
results['precision'] = precision
results['f1'] = f1
現在我努力讓自己在多個CPU我的循環運行,我嘗試以下基本的例子多處理模塊,但作爲新的Python和總體規劃的無法弄清楚它的作品在我的情況。什麼不起作用
例子:
import multiprocessing as mp
pool = mp.Pool(processes=8)
def get_scores(param_grid):
precision = []
f1 = []
for params in param_grid:
model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params)
model.fit(X,y)
model.predict(Xt)
precision.append(precision_score(yt_class, model.predict(Xt), average='weighted'))
f1.append(f1_score(yt_class, model.predict(Xt), average='weighted'))
return precision,f1
scores = pool.apply(get_scores,param_grid)
我拿到後出現以下錯誤運行代碼(運行我的代碼後出現同樣的錯誤):'TypeError:get_scores()需要1個位置參數,但是有20個被賦予' –
對,它應該是map_異步,而不是apply_async –