'getting error as 1 positional argument required. inputs. in deep learning model
I am getting this error:
one positional argument is required, Inputs
on this row:
kfolds = cross_val_score(model, X, y, cv = 3)
the requirement is a binary classification model. we need to predict the outcome which in 0 or 1. I have used deep learning model. used function with 2 positional arguments. but it is showing one more positional arguments are required.
# importing the required the libraries
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV
from keras.layers import Dense # Dense layers are "fully connected" layers
from keras.models import Sequential # Documentation: https://keras.io/models/sequential/
from keras.layers import Flatten
from keras.utils.np_utils import to_categorical
from keras.optimizers import SGD, Adam
from keras.callbacks import EarlyStopping
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
df = pd.read_csv('example_Data.csv')
df = df.dropna()
#print(df.head())
y=df['target']
#print(target.head())
X = df.drop(['target'],axis=1)
#print(X)
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42)
print(X_train.shape)
print(y_train.shape)
print(X_train.shape[1])
# Create an Adam optimizer with the given learning rate
def create_model(learning_rate, activation):
# Create an Adam optimizer with the given learning rate
opt = Adam(lr = learning_rate)
# Create your binary classification model
model = Sequential()
model.add(Dense(1600, input_shape = (X_train.shape[1],), activation = activation))
model.add(Dense(800, activation = activation))
model.add(Dense(1, activation = 'sigmoid'))
# Compile your model with your optimizer, loss, and metrics
model.compile(optimizer = opt, loss = 'binary_crossentropy', metrics = ['accuracy'])
return model
from keras.wrappers.scikit_learn import KerasClassifier
# Create a KerasClassifier
model = KerasClassifier(build_fn = create_model)
# Define the parameters to try out
params = {'activation': ['relu', 'tanh'], 'batch_size': [32, 128, 256],
'epochs': [50, 100, 200], 'learning_rate': [0.1, .01, .001]}
# Create a randomize search cv object passing in the parameters to try
random_search = RandomizedSearchCV(model, param_distributions = params, cv = KFold(3))
# Create a KerasClassifier
random_search.fit(X_train, y_train)
print(random_search.best_params_)
#random_search
#{'learning_rate': 0.01, 'epochs': 100, 'batch_size': 256, 'activation': 'tanh'}
from keras.wrappers.scikit_learn import KerasClassifier
# Create a KerasClassifier
model = KerasClassifier(build_fn = create_model(learning_rate = 0.01, activation = 'tanh'),
epochs = 100,
batch_size = 256, verbose = 0)
# Calculate the accuracy score for each fold
kfolds = cross_val_score(model, X, y, cv = 3)
# Print the mean accuracy
print('The mean accuracy was:', kfolds.mean())
# Print the accuracy standard deviation
print('With a standard deviation of:', kfolds.std())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
