admin管理员组文章数量:1305243
I am working on a binary classification problem using a neural network with TensorFlow/Keras and scikit-learn's GridSearchCV for hyperparameter tuning. However, I am encountering an AttributeError when trying to fit the GridSearchCV object. The error occurs at the line grid_result = grid.fit(X_train, y_train). Here is my complete code:
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.pipeline import Pipeline
from scikeras.wrappers import KerasClassifier
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping
import pickle
# Load and preprocess data
data = pd.read_csv('Churn_Modelling.csv')
data = data.drop(['RowNumber', 'CustomerId', 'Surname'], axis=1)
label_encoder_gender = LabelEncoder()
data['Gender'] = label_encoder_gender.fit_transform(data['Gender'])
onehot_encoder_geo = OneHotEncoder(handle_unknown='ignore')
geo_encoded = onehot_encoder_geo.fit_transform(data[['Geography']]).toarray()
geo_encoded_df = pd.DataFrame(geo_encoded, columns=onehot_encoder_geo.get_feature_names_out(['Geography']))
data = pd.concat([data.drop('Geography', axis=1), geo_encoded_df], axis=1)
X = data.drop('Exited', axis=1)
y = data['Exited']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Save encoders and scaler for later use
with open('label_encoder_gender.pkl', 'wb') as file:
pickle.dump(label_encoder_gender, file)
with open('onehot_encoder_geo.pkl', 'wb') as file:
pickle.dump(onehot_encoder_geo, file)
with open('scaler.pkl', 'wb') as file:
pickle.dump(scaler, file)
# Define the model creation function
def create_model(neurons=32, layers=1):
model = Sequential()
model.add(Dense(neurons, activation='relu', input_shape=(X_train.shape[1],)))
for _ in range(layers - 1):
model.add(Dense(neurons, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
modelpile(optimizer='adam', loss="binary_crossentropy", metrics=['accuracy'])
return model
# Create a Keras classifier
model = KerasClassifier(layers=1, neurons=32, build_fn=create_model, verbose=1)
# GridSearch parameters
param_grid = {
'neurons': [16, 32, 64, 128],
'layers': [1, 2],
'epochs': [50, 100]
}
# Perform grid search
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3, verbose=1)
grid_result = grid.fit(X_train, y_train) # Error occurs here
# Print the best parameters
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
I ensured that all libraries (scikeras, tensorflow, scikit-learn) are up to date.
本文标签:
版权声明:本文标题:python - AttributeError: 'super' object has no attribute 'sklearn_tags' when using GridSearchCV 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741796718a2397979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论