我想跟进上一个问题(Recursive Feature Elimination on Keras Models),因为我遇到了障碍。 我目前正在尝试实现以下内容(并非所有代码都在这里以便于阅读):
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.feature_selection import RFE
k_model = KerasClassifier(build_fn=model, epochs=epochs,
batch_size=bs, verbose=0) #model is a standard Keras MLP
selector = RFE(k_model, step=1)
然而,这似乎是功能性的,在我尝试适合模型的下一行:
selector = selector.fit(x_train, y_train)
我收到以下错误:
TypeError: can't pickle _thread.RLock objects
任何想法/帮助将不胜感激。 编辑:
# =============================================================================
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils, plot_model
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier as ABC
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.feature_selection import RFE
import numpy as np
import time
# =============================================================================
# Functions:
# MLP(x, y)
# =============================================================================
def MLP(x, y):
s_time = time.clock()
# ATR, MOM, RSI, OBV
# keep_features = ['ATR', 'MOM', 'RSI', 'OBV']
# drop_features = list(set(list(x)).difference(keep_features))
# x.drop(drop_features, axis=1, inplace=True)
# x = x.as_matrix()
# y = y.as_matrix()
# num_features = np.array((5,10,15))
num_classes = 3
epochs = 1
bs = 10
np.random.seed(7)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33)
# y_train = np_utils.to_categorical(y_train, num_classes)
# y_test = np_utils.to_categorical(y_test, num_classes)
# create model
model = Sequential()
model.add( Dense(50, input_shape=(x_train.shape[1],), activation='tanh'))
model.add( Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#
# =============================================================================
# plot_model(model, to_file='../3_Deliverables/Final Paper/data/keras_model.png')
# =============================================================================
# Port Keras Framework into SK-Learn
k_model = KerasClassifier(build_fn=model, epochs=epochs, batch_size=bs, verbose=0)
temp = k_model
selector = RFE(temp, step=1)
out = selector.fit(x_train, y_train)
# evaluate the model
# scores = model.evaluate(x_test, y_test)
# print("\n%s: %.2f%%" % (indicators.metrics_names[1], scores[1]*100))
e_time = time.clock()
print('\n Total Time: ', e_time-s_time)
错误堆栈:
File "C:\Miniconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 705, in runfile
execfile(filename, namespace)
File "C:\Miniconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Schifman Ben/2_Software/project.py", line 51, in <module>
pred.MLP(x_all, y_all)
File "C:\Users\Schifman Ben\2_Software\prediction.py", line 50, in MLP
out = selector.fit(x_train, y_train)
File "C:\Miniconda3\lib\site-packages\sklearn\feature_selection\rfe.py",
line 134, in fit
return self._fit(X, y)
File "C:\Miniconda3\lib\site-packages\sklearn\feature_selection\rfe.py",
line 169, in _fit
estimator = clone(self.estimator)
File "C:\Miniconda3\lib\site-packages\sklearn\base.py", line 62, in clone
new_object_params[name] = clone(param, safe=False)
File "C:\Miniconda3\lib\site-packages\sklearn\base.py", line 53, in clone
return copy.deepcopy(estimator)
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Miniconda3\lib\copy.py", line 169, in deepcopy
rv = reductor(4)
TypeError: can't pickle _thread.RLock objects
答案 0 :(得分:0)
KerasClassifier build_fn
需要一个指向函数的指针,该函数返回model
,而不是模型本身。
所以改变你的代码,如:
def MLP(x, y):
...
...
...
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33)
# y_train = np_utils.to_categorical(y_train, num_classes)
# y_test = np_utils.to_categorical(y_test, num_classes)
# This is what you need
# create model
def create_model():
model = Sequential()
model.add(Dense(50, input_shape=(x_train.shape[1],), activation='tanh'))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
...
...
# Port Keras Framework into SK-Learn
k_model = KerasClassifier(build_fn=create_model, epochs=epochs, batch_size=bs, verbose=0)
...
...
但之后你会收到另一个关于feature_importances_
的错误,因为RFE
与KerasClassifier不兼容。
有关详细信息,请参阅此问题:Feature selection on a keras model