将错误作为1个位置参数。输入。在深度学习模型中

发布于 2025-01-23 05:54:33 字数 3019 浏览 0 评论 0原文

我遇到了这个错误:

需要一个位置参数,输入

这是该行的输入:

kfolds = cross_val_score(model, X, y, cv = 3)

要求是二进制分类模型。我们需要预测0或1中的结果。我使用了深度学习模型。使用的功能为2个位置参数。但是它显示了另一种位置论点。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2025-01-30 05:54:33

cross_val_score参数已正确输入。

但是,评分参数不存在。
根据语法,需要提及评分参数。

因此,我建议您尝试以下代码并继续进行。

scores = cross_val_score(model, X, y, cv=5, scoring='neg_root_mean_squared_error')

这将避免丢失位置参数的错误。

The cross_val_score parameters have been correctly entered.

However, the scoring parameter is absent .
As per the syntax, the scoring parameter if not available, needs to be mentioned.

Hence, i suggest you to please try the code below and proceed.

scores = cross_val_score(model, X, y, cv=5, scoring='neg_root_mean_squared_error')

This would avoid the error of missing positional argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文