简体   繁体   English

将 K 折交叉验证应用于 ANN

[英]Applying K-fold cross validation to ANN

I developed an ANN based on a Machine Learning course that goes as follows:我基于机器学习课程开发了一个人工神经网络,内容如下:

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

dataset = pd.read_excel('CHURN DATA (2).xlsx')
dataset.replace([np.inf, -np.inf], np.nan, inplace=True)
dataset = dataset.fillna(0)

X = dataset.iloc[:, 2:45].values
y = dataset.iloc[:, 45].values

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[:, 1] = le.fit_transform(X[:,1])
X[:, 2] = le.fit_transform(X[:,2])
X[:, 3] = le.fit_transform(X[:,3])

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(),[0])], remainder = 'passthrough')
X = np.array(ct.fit_transform(X))

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units = 43, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 43, activation = 'relu'))

ann.add(tf.keras.layers.Dense(units = 1, activation = 'sigmoid'))
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
ann.fit(X_train, y_train, batch_size = 256, epochs = 100)

y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

However, when trying to add kfold crossvalidation like so但是,当尝试像这样添加 kfold 交叉验证时

from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(ann, X = X_train, y = y_train, cv = 10)
mean = accuracies.mean()
variance = accuracies.std()

I get the follow error:我收到以下错误:

TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator <tensorflow.python.keras.engine.sequential.Sequential object at 0x000001A52F049F88> does not.

When I try using accuracy for scoring as当我尝试使用准确性进行评分时

accuracies = cross_val_score(estimator = ann,scoring = "accuracy", X = X_train, y = y_train, cv = 10)

I get the following error:我收到以下错误:

Cannot clone object '<tensorflow.python.keras.engine.sequential.Sequential object at 0x000001A52F049F88>' (type <class 'tensorflow.python.keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

The error message says it all.错误消息说明了一切。 You can't just pass a Keras model in Sklearn.您不能只在 Sklearn 中传递 Keras model。 There is a Keras wrapper for Sklearn, so both can indeed be used together. Sklearn 有一个 Keras 包装器,因此两者确实可以一起使用。 It's tensorflow.keras.wrappers.scikit_learn.KerasClassifier .它是tensorflow.keras.wrappers.scikit_learn.KerasClassifier

Here's a reproducible example with the MNIST:这是 MNIST 的可重现示例:

import tensorflow as tf
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score

(X_train, y_train), (_, _) = tf.keras.datasets.mnist.load_data()
X_train = X_train[..., None]

def build_model():
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
        tf.keras.layers.Dense(32, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')])
    model.compile(loss='sparse_categorical_crossentropy', 
        optimizer='adam', metrics=['accuracy'])
    return model

model = build_model()

history = model.fit(X_train, y_train, epochs=1)

keras_clf = KerasClassifier(build_model)

accuracies = cross_val_score(estimator=keras_clf, scoring="accuracy", 
    X=X_train, y=y_train, cv=5)

print(accuracies)
array([0.74008333, 0.65      , 0.71075   , 0.561     , 0.66683333])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM