I am currently trying to build a stacking ensemble that consists of both "standard models" and a neural network. The ensemble contains Random Forest, XGBoost, SVM and Catboost. But as soon as I add the neural network I get the error "TypeError: can't pickle _thread.RLock objects". I have tried different versions of Tensorflow (2.0.0, 2.3.0, 1.14, 1.13) but that did not solve the problem. I hope someone can help me with this case.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import RobustScaler
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten
from tensorflow.keras.optimizers import *
rs = 23
dataset = pd.read_csv(url,sep='|')
x = dataset.drop('fraud', axis=1)
y = dataset.fraud
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, stratify=y, random_state=rs)
scaler = StandardScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
Classifiers
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from catboost import CatBoostClassifier
cb_clf = CatBoostClassifier(border_count=14, depth=4, iterations=600, l2_leaf_reg=1, silent= True, learning_rate= 0.02, thread_count=4, random_state=rs)
rf_clf = RandomForestClassifier(n_estimators = 700, criterion = "entropy", min_samples_leaf = 1, min_samples_split = 2, random_state = rs)
svc_clf = SVC(kernel = 'linear', C = 40, random_state = rs)
xg_clf = XGBClassifier(booster="gblinear", eta=0.5, random_state=rs)
DNN
x_train_dnn = np.array(x_train)
x_test_dnn = np.array(x_test)
y_train_dnn = np.array(y_train)
y_test_dnn = np.array(y_test)
def build_nn():
dnn = Sequential()
dnn.add(Dense(128, activation='relu', kernel_initializer='random_normal', input_dim=10))
dnn.add(Dense(128, activation='relu', kernel_initializer='random_normal'))
dnn.add(Dense(1, activation='sigmoid', kernel_initializer='random_normal'))
dnn.compile(optimizer ='adam',loss='binary_crossentropy', metrics =['accuracy'])
return dnn
dnn_clf = keras.wrappers.scikit_learn.KerasClassifier(
build_nn,
epochs=500,
batch_size=32,
verbose=False)
dnn_clf._estimator_type = "classifier"
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
estimators = [("Random Forest", rf_clf),
("XG", xg_clf),
("SVC", svc_clf),
("Catboost", cb_clf),
("DNN", dnn_clf)]
ensemble = StackingClassifier(estimators=estimators, n_jobs=-1, final_estimator=LogisticRegression())
Fitting the Ensemble causes the error
ensemble.fit(x_train, y_train)#fit model to training data
ensemble.score(x_test, y_test)#test our model on the test data
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-14-1c003d476ea2> in <module>()
----> 1 ensemble.fit(x_train, y_train)#fit model to training data
2 ensemble.score(x_test, y_test)#test our model on the test data
6 frames
/usr/lib/python3.6/concurrent/futures/_base.py in __get_result(self)
382 def __get_result(self):
383 if self._exception:
--> 384 raise self._exception
385 else:
386 return self._result
TypeError: can't pickle _thread.RLock objects