【问题标题】:cross_val_predict is not completing. No error messagecross_val_predict 未完成。没有错误信息
【发布时间】:2020-10-14 15:54:54
【问题描述】:

我正在尝试在 MNIST 示例数据集上使用 KNearestNeighbors。

当尝试使用 cross_val_predict 时,无论我离开多长时间,脚本都会继续运行。

我有什么遗漏/做错了吗?

感谢任何反馈。

from sklearn.datasets import fetch_openml
import numpy as np
mnist = fetch_openml('mnist_784', version=1) #Imports the dataset into the notebook

X, y = mnist["data"], mnist["target"]

y=y.astype(np.uint8)
X=X.astype(np.uint8)#For machine learning models to understand the output must be casted to an interger not a string.

X.shape, y.shape

y=y.astype(np.uint8) #For machine learning models to understand the output must be casted to an interger not a string.
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:] #Separate the data into training and testing sets

from sklearn.neighbors import KNeighborsClassifier

knn_clf = KNeighborsClassifier()
knn_clf.fit(X_train, y_train)

from sklearn.model_selection import cross_val_predict
from sklearn.metrics import f1_score

y_train_knn_pred = cross_val_predict(knn_clf, X_train, y_train, cv=3)

f1_score(y_train, y_train_knn_pred, average="macro")

【问题讨论】:

    标签: python numpy scikit-learn knn


    【解决方案1】:

    使用n_jobs=-1

    用于进行计算的 CPU 数量。无意味着 1 除非 在 joblib.parallel_backend 上下文中。 -1 表示使用所有处理器

    from sklearn.datasets import fetch_openml
    import numpy as np
    mnist = fetch_openml('mnist_784', version=1) #Imports the dataset into the notebook
    
    X, y = mnist["data"], mnist["target"]
    y=y.astype(np.uint8)
    X=X.astype(np.uint8)#For machine learning models to understand the output must be casted to an interger not a string.
    
    
    y=y.astype(np.uint8) #For machine learning models to understand the output must be casted to an interger not a string.
    X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:] #Separate the data into training and testing sets
    
    from sklearn.neighbors import KNeighborsClassifier
    
    knn_clf = KNeighborsClassifier(n_jobs=-1) # HERE
    knn_clf.fit(X_train, y_train) # this took seconds on my macbook pro
    
    from sklearn.model_selection import cross_val_predict
    from sklearn.metrics import f1_score
    
    y_train_knn_pred = cross_val_predict(knn_clf, X_train, y_train, cv=3, n_jobs=-1) # AND HERE
    
    f1_score(y_train, y_train_knn_pred, average="macro")
    

    【讨论】:

    • 仅在 KNeighborsClassifier 中使用 n_jobs=-1 还不够吗?在将它用于模型和分数后,我曾经看到过奇怪的问题。
    • 我在cross_val_predictKNeighborsClassifier 中都使用n_jobs=-1。我从未见过奇怪的问题
    • 我也在使用 Macbook pro。 Cross_val_predict 即使使用所有处理器也需要很长时间。你的运行时间是多少? (棒球场图)
    • Cross_val_predict 对我和通常每个人来说也很慢。最快的是knn_clf = KNeighborsClassifier(n_jobs=-1)
    【解决方案2】:

    我认为混淆来自于 KNN 算法拟合调用比预测快得多这一点。来自另一个 SO 帖子:

    Why is cross_val_predict so much slower than fit for KNeighborsClassifier?

    KNN 也被称为惰性算法,因为在拟合过程中它确实 只是保存输入数据,特别是没有学习 全部。

    在预测期间,实际距离计算发生在每个 测试数据点。因此,您可以理解,在使用时 cross_val_predict,KNN 必须在验证数据点上进行预测, 这使得计算时间更长!

    因此,当您查看输入的大小时,需要大量的计算能力。数据。使用多个 CPU 或最小化维度可能很有用。

    如果您想使用多个 CPU 内核,您可以将参数“n_jobs”传递给cross_val_predictKNeighborsClassifier,以设置要使用的内核数量。将其设置为 -1 以使用所有可用的内核

    【讨论】:

    • 您的解释很有道理,感谢您链接该帖子。
    猜你喜欢
    • 2014-03-24
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2017-06-21
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多