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

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

from sklearn.preprocessing import OneHotEncoder,LabelEncoder,StandardScaler

enc1=LabelEncoder()
enc2=LabelEncoder()
X[:,1] = enc1.fit_transform(X[:,1])
X[:,2] = enc2.fit_transform(X[:,2])

one = OneHotEncoder(categorical_features=[1])
X=one.fit_transform(X).toarray()

X = X[:,1:]

from sklearn.model_selection import train_test_split
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,random_state=0,test_size=0.2)

scale = StandardScaler()
scale.fit_transform(Xtrain)
scale.transform(Xtest)

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from keras.layers import Dense

def func1():
    net = Sequential()
    net.add(Dense(input_dim=11,units=6,activation="relu",kernel_initializer='uniform'))
    net.add(Dense(units=6,activation="relu",kernel_initializer='uniform'))
    net.add(Dense(units=1,activation="sigmoid",kernel_initializer='uniform'))
    net.compile(optimizer='adam',metrics=['accuracy'],loss='binary_crossentropy')

    return net

classfier = KerasClassifier(build_fn=func1(),batch_size=10, epochs=100)
cross = cross_val_score(estimator=classfier, X=Xtrain, y=Ytrain, cv=10 , n_jobs=-1)

引发错误:
Traceback (most recent call last):

  File "<ipython-input-7-e80e82960eb9>", line 1, in <module>
    cross = cross_val_score(estimator=classfier, X=Xtrain, y=Ytrain, cv=10 , n_jobs=-1)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 342, in cross_val_score
    pre_dispatch=pre_dispatch)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in cross_validate
    for train, test in cv.split(X, y, groups))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 620, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
    self.items = list(iterator_slice)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in <genexpr>
    for train, test in cv.split(X, y, groups))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\base.py", line 62, in clone
    new_object_params[name] = clone(param, safe=False)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\base.py", line 53, in clone
    return copy.deepcopy(estimator)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)

TypeError: can't pickle _thread.lock objects

我该如何解决?

最佳答案

更改此行:

classfier = KerasClassifier(build_fn=func1, batch_size=10, epochs=100, verbose=0)

请注意,func1,不称为。从文档中:

关于python - Keras:TypeError:无法使用KerasClassifier腌制_thread.lock对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48303166/

10-12 19:32