我最近发现要求我有.fit()
经过培训的scikit-learn
SVC
分类器实例并需要 .predict()
< / strong>很多实例。
是否有办法通过任何.predict()
内置工具仅对此scikit-learn
方法进行并行化?
from sklearn import svm
data_train = [[0,2,3],[1,2,3],[4,2,3]]
targets_train = [0,1,0]
clf = svm.SVC(kernel='rbf', degree=3, C=10, gamma=0.3, probability=True)
clf.fit(data_train, targets_train)
# this can be very large (~ a million records)
to_be_predicted = [[1,3,4]]
clf.predict(to_be_predicted)
如果有人确实知道解决方案,如果你能分享它,我会非常高兴。
答案 0 :(得分:2)
这可能是错误的,但这样的事情应该可以解决问题。基本上,将数据分成块并在joblib.Parallel
循环中分别在每个块上运行模型。
from sklearn.externals.joblib import Parallel, delayed
n_cores = 2
n_samples = to_be_predicted.shape[0]
slices = [
(n_samples*i/n_cores, n_samples*(i+1)/n_cores))
for i in range(n_cores)
]
results = np.vstack( Parallel( n_jobs = n_cores )(
delayed(clf.predict)( to_be_predicted[slices[i_core][0]:slices[i_core][1]
for i_core in range(n_cores)
))
答案 1 :(得分:2)
上面的工作示例...
from joblib import Parallel, delayed
from sklearn import svm
data_train = [[0,2,3],[1,2,3],[4,2,3]]
targets_train = [0,1,0]
clf = svm.SVC(kernel='rbf', degree=3, C=10, gamma=0.3, probability=True)
clf.fit(data_train, targets_train)
to_be_predicted = np.array([[1,3,4], [1,3,4], [1,3,5]])
clf.predict(to_be_predicted)
n_cores = 3
parallel = Parallel(n_jobs=n_cores)
results = parallel(delayed(clf.predict)(to_be_predicted[i].reshape(-1,3))
for i in range(n_cores))
np.vstack(results).flatten()
array([1, 1, 0])