使用多进程-Pool-和-sklearn-,代码运行但核心未显示任何工作

我试图对大约31000行和1000列的数据进行机器学习。这需要很长时间,所以我想我可以并行化这个任务,于是我把它做成了一个函数,并尝试在Windows 10上的Jupyter Notebook中使用该工具。但它只是运行了,当我在任务管理器中查看我的核心时,它们并没有工作。代码有问题吗,还是它根本不受支持?

from sklearn.model_selection import train_test_splitX_dev, X_test, y_dev, y_test = train_test_split(X, y, test_size=0.2, random_state=1)from sklearn.model_selection import KFoldfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.preprocessing import Imputerfrom sklearn.metrics import accuracy_scorefrom multiprocessing import Poolfrom datetime import datetime as dtdef tree_paralel(x):    tree = DecisionTreeClassifier(criterion="gini", max_depth= x, random_state=1)      accuracy_ = []    for train_idx, val_idx in kfolds.split(X_dev, y_dev):        X_train, y_train, = X_dev.iloc[train_idx], y_dev.iloc[train_idx]        X_val, y_val = X_dev.iloc[val_idx], y_dev.iloc[val_idx]         X_train = pd.DataFrame(im.fit_transform(X_train),index = X_train.index)        X_val = pd.DataFrame(im.transform(X_val), index = X_val.index)        tree.fit(X_train, y_train)        y_pred = tree.predict(X_val)        accuracy_.append(accuracy_score(y_val, y_pred))    print("This was the "+str(x)+" iteration", (dt.now() - start).total_seconds())    return accuracy_

然后使用多进程工具:

kfolds = KFold(n_splits=10)accuracy = []im = Imputer()p = Pool(5)input_ = range(1,11)output_ = []start = dt.now()for result in p.imap(tree_paralel, input_):    output_.append(result)p.close()print("Time:", (dt.now() - start).total_seconds())

回答:

这是在使用交互式Python时已知的问题。
引用multiprocessing文档中使用工作者池部分的说明:

注意:此包中的功能要求子进程可以导入__main__模块。这在编程指南中有覆盖,但在这里指出这一点是值得的。这意味着某些示例,如multiprocessing.pool.Pool示例,在交互式解释器中将无法工作

另见多进程编程指南

顺便说一句,我没有明白你需要通过代码实现什么。使用GridSearchCV并设置n_jobs=5不就能解决你的问题(并且大大简化你的代码)吗?

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注