TypeError:向堆栈集合添加神经网络时,无法pickle_thread.RLock对象

2024-04-23 16:49:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试建立一个由“标准模型”和神经网络组成的叠加集成。 该集成包括随机森林、XGBoost、SVM和Catboost。但一旦我添加了神经网络,我就得到了错误“TypeError:cannotpickle\u thread.RLock objects”。 我尝试过Tensorflow的不同版本(2.0.0、2.3.0、1.14、1.13),但这并没有解决问题。我希望有人能帮我处理这个案子

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)

分类器

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())

拟合集合会导致错误

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

Tags: fromtestimportmodeltensorflownptrainrandom