我正在执行网格搜索以识别最佳SVM参数。我正在使用ipython和sklearn。代码很慢,只能在一个核心上运行。如何种植并利用多个核心?谢谢
random_state = np.random.RandomState(10)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=.2,random_state=random_state)
model_to_set = OneVsRestClassifier(svm.SVC(kernel="linear"))
parameters = {
"estimator__C": [1, 2, 4, 8, 16, 32],
"estimator__kernel": ["linear", "rbf"],
"estimator__gamma":[1, 0.1, 1e-2, 1e-3, 1e-4],
}
model_tuning = GridSearchCV(model_to_set, param_grid=parameters)
model_tuning.fit(X_train, y_train)
print model_tuning.best_score_
print model_tuning.best_params_
print "Time passed: ", "{0:.1f}".format(time.time()-t), "sec"
答案 0 :(得分:5)
col1 col2 col3
0 w a U
1 1 2 A
2 2 NaN B
个参数
n_jobs :int,默认= 1
并行运行的作业数。改变了 版本0.17:升级到joblib 0.9.3。
答案 1 :(得分:3)
默认情况下,GridSearchCV使用1个作业搜索估算器的指定参数值。
因此,您需要通过修改以下行明确设置所需的并行作业数:
model_tuning = GridSearchCV(model_to_set, param_grid=parameters)
进入以下内容以允许并行运行作业:
model_tuning = GridSearchCV(model_to_set, param_grid=parameters, n_jobs=4)
答案 2 :(得分:0)
尝试使用sparkcontext在多台计算机上并行化网格搜索。 通过使用spark-sklearn库,您只需单行更改即可使程序更快地运行
from spark_sklearn import GridSearchCV
还要确保正确初始化spark上下文(" sc ")。那么你需要做的就是
gs = GridSearchCV(sc, classifier, param_grid)
gs.fit(X, y)
对于scikit-learn中的标准数字数据集,864候选设置的网格搜索花费了318.92秒。使用4台机器只需122.03秒
您可以找到有关上述here
的更多信息