gpt4 book ai didi

python - k 折叠交叉验证不会终止,卡在 cross_val_score

转载 作者:行者123 更新时间:2023-12-01 09:18:00 26 4
gpt4 key购买 nike

我正在尝试运行 kfold 交叉验证。但由于某种原因,它卡在这里,它不会从这里终止 accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)我不明白有什么问题。以及我该如何解决它。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:,1:]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)


import keras
from keras.models import Sequential #Required to initialize the ANN
from keras.layers import Dense #Build layers of ANN
from keras.layers import Dropout


# Evaluating the ANN
import keras
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score

from keras.models import Sequential #Required to initialize the ANN
from keras.layers import Dense #Build layers of ANN

def build_classifier(): # Builds the architecture, or the classifier
classifier = Sequential()
classifier.add(Dense(activation = 'relu', input_dim = 11, units = 6, kernel_initializer = 'uniform'))# add layers
classifier.add(Dense(activation = 'relu', units = 6, kernel_initializer = 'uniform'))# add layers
classifier.add(Dense(activation = 'sigmoid', units = 1, kernel_initializer = 'uniform'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier, batch_size = 10, nb_epoch = 100)
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)
mean = accuracies.mean()
variance = accuracies.std()
<小时/>

编辑
我在 Windows 10 上使用 Anaconda 和 python 3.6。
数据集:Drive Link for dataset
当我设置 n_jobs = 1 时它工作得很好,但当 n_jobs = -1 时它不起作用

最佳答案

既然您已经设置了n_jobs = -1,那么所有CPU都会按照提到的文档here使用。但是,您必须了解,利用所有 CPU 并不一定会导致执行时间减少,因为:

  • 创建资源并将其分配给新线程会产生开销。
  • 此外,可能还存在其他瓶颈,例如数据太大而无法同时广播到所有线程、线程抢占 RAM(或其他资源等)、数据如何推送到每个线程等.
  • Python 中的多线程也有各种缺点,请参阅 herehere .

您可以查看 GridSearchCV 和并行化的类似问题 here in this answer .

此外,正如 @ncfith 提到的,当前没有解决此问题的方法。

引用文献

关于python - k 折叠交叉验证不会终止,卡在 cross_val_score,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com