python - 为什么我的 scikit learn 的plot_learning_curve 在 google VM 上运行得不快?

标签 python scikit-learn google-compute-engine google-cloud-datalab lightgbm

我正在运行从 scikit-learn 官方网站借用的一个片段来绘制 learning curve

我的代码非常简单,如下所示:

import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit
from sklearn.metrics import r2_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split


from lightgbm import LGBMRegressor

lgb = LGBMRegressor()
std = StandardScaler()
x = std.fit_transform(df[features])
y = df['total_UPDRS']

title = lgb
cv = ShuffleSplit(n_splits=5, test_size=0.4, random_state=0)
plot_learning_curve(lgb, title, x, y, cv=3, ylim=(0.0, 1.01), n_jobs=16)
plt.show()

我在具有 60GB 内存的 16 个 vCPU 上运行。该过程大约持续了几分钟,然后就死掉了,没有可测量的事件,我不知道设置出了什么问题,因为我可以在 Macbook Pro 的本地 Anaconda 安装上输出图表。 (运行只需 10-15 分钟。)我做错了什么?

最佳答案

更新

我能够在 GCE VM 实例和 Google Cloud Datalab 实例中运行下面的代码,并且使用 Python 2 和 Python 3,没有任何问题。但是,我认为 lightgbm 包存在一些问题。如果我设置 n_jobs=1 它运行得非常快(不到 1 分钟)。如果我将 n_jobs 设置为 16 或任何可用核心数量,它会变得非常慢(持续 10-15 分钟)。也许值得在 Github Repo 中打开一个问题来了解这一点。

(顺便说一句:看到我没有在 Datalab 中使用 %matplotlib 内联命令,看起来不需要它。)

import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit
from sklearn.metrics import r2_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np

!pip install lightgbm

from lightgbm import LGBMRegressor

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):

    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, 
                     color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt

df = pd.DataFrame.from_csv(parkinson data from https://archive.ics.uci.edu/ml/datasets/parkinsons)

lgb = LGBMRegressor(min_data=1,random_state=5, n_jobs=1)
std = StandardScaler()
x = std.fit_transform(df[['MDVP:Fo(Hz)','MDVP:Fhi(Hz)']])
y = df['status']

title = lgb
cv = ShuffleSplit(n_splits=5, test_size=0.4, random_state=0)
plt = plot_learning_curve(lgb, title, x, y, cv=3, ylim=(0.0, 1.01), n_jobs=1)
plt.show()

如果使用 VM 机器而不使用 Jupyter Notebooks 或类似设备: 就我而言,我使用 SSH 访问机器,因此没有用户界面,因此如果我添加 plt.show() 它不会崩溃,但实际上它不会显示任何内容。为了证明它工作正常,我添加了: plt.show() ,而不是 plt.savefig("filename.png") ,它在我的 .py 文件的同一文件夹中成功创建了一个 filename.png 。

我以这种方式导入了 matplotlib (在 this thread 之后)以避免显示错误:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

此外,在出现一些错误后,我将 lgb = LGBMRegressor() 更改为以下内容:lgb = LGBMRegressor(min_data=1)(以下错误声称数据集太小)。

关于python - 为什么我的 scikit learn 的plot_learning_curve 在 google VM 上运行得不快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49666274/

相关文章:

python - 安装 Python 包时选择 pip 与 conda 的具体原因

python - 在 Python 3 中获取两个列表之间的交集

python - 如何从 sklearn SelectKBest 获取实际选择的功能

python - 确定文本是否为英文?

python - 为什么 Google Compute Engine 中的多处理不使用 100% CPU?

Python:没有这样的文件或目录错误(Mac 用户)

python - 使用 Python 在许多文档中搜索许多表达式

python - 无法弄清楚如何清除随机森林中的 NaN

google-cloud-platform - 在 Google Cloud 上运行批处理 python 进程

amazon-ec2 - 内存密集型作业在多核云实例(ec2、gce、rackspace)上的扩展性很差?