1
I’m trying to use the scikit-Learn library RFE on models I created using tensorflow, but when I try to train I get TypeError: can't pickle _thread._local objects
. Follow the code and error below:
import tensorflow as tf
import pandas as pd
from sklearn.feature_selection import RFE
data = {'atributo1':[1,2,3,4,5],'atributo2':[1,2,3,4,5],'atributo3':[1,2,3,4,5],'atributo4':[1,2,3,4,5], 'target':[1,0,1,0,1]}
base = pd.DataFrame(data)
n_hidden1 = 100
n_hidden2 = 50
n_outputs = 2
def create_model():
model = tf.keras.Sequential([tf.keras.layers.Dense(n_hidden1,activation = 'relu'),
tf.keras.layers.Dense(n_hidden2,activation = 'relu'),
tf.keras.layers.Dense(n_outputs,activation = 'softmax')])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
model = tf.keras.wrappers.scikit_learn.KerasClassifier(build_fn=create_model(), batch_size = 10, epochs = 20)
rank = RFE(estimator=model,verbose=1,n_features_to_select=2)
rank.fit(base.drop('target',axis=1),base['target'])
> runfile('C:/Users/panto/.spyder-py3/temp.py', wdir='C:/Users/panto/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-5-4d89fbeba90e>", line 1, in <module>
runfile('C:/Users/panto/.spyder-py3/temp.py', wdir='C:/Users/panto/.spyder-py3')
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/panto/.spyder-py3/temp.py", line 25, in <module>
rank.fit(base.drop('target',axis=1),base['target'])
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\feature_selection\rfe.py", line 144, in fit
return self._fit(X, y)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\feature_selection\rfe.py", line 179, in _fit
estimator = clone(self.estimator)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py", line 64, in clone
new_object_params[name] = clone(param, safe=False)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py", line 55, in clone
return copy.deepcopy(estimator)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Users\panto\AppData\Local\Continuum\anaconda3\lib\copy.py", line 169, in deepcopy
rv = reductor(4)
TypeError: can't pickle _thread._local objects
You need to stare at the model before the function
RFE
, nay?– Lucas
No, I’ve used RFE with templates created in sklearn itself and it worked like this, it serves precisely to stare several times and find the rank of attributes.
– Pedro Antonio
Try to make a minimum repeatable example. Then it would be easier to understand what is happening. See instructions here: https://answall.com/help/minimal-reproducible-example
– Lucas