title: Applying K-fold cross validation to ANN
body: <p>I developed an ANN based on a Machine Learning course that goes as follows:</p>
  <pre><code>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 &gt; 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)
  </code></pre>
  <p>However, when trying to add kfold crossvalidation like so</p>
  <pre><code>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()
  </code></pre>
  <p>I get the follow error:</p>
  <pre><code>TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator &lt;tensorflow.python.keras.engine.sequential.Sequential object at 0x000001A52F049F88&gt; does not.
  </code></pre>
  <p>When I try using accuracy for scoring as</p>
  <pre><code>accuracies = cross_val_score(estimator = ann,scoring = &quot;accuracy&quot;, X = X_train, y = y_train, cv = 10)
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>Cannot clone object '&lt;tensorflow.python.keras.engine.sequential.Sequential object at 0x000001A52F049F88&gt;' (type &lt;class 'tensorflow.python.keras.engine.sequential.Sequential'&gt;): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.
  </code></pre>