如何在给定代码场景下计算ANN的测试集准确率?
Fixing ANN Test Accuracy Calculation in Your Code
Hey there! Let's get your ANN's test accuracy sorted out. I notice a couple of key adjustments needed in your code, plus we'll fill in the missing pieces to calculate accuracy properly.
Key Issues to Address
- You're using
KerasRegressorfor a classification task: Accuracy is a metric for classification problems (predicting categories), butKerasRegressoris designed for regression (predicting continuous values). For classification, you needKerasClassifierinstead. - Missing data split: To evaluate test set accuracy, you need to separate your dataset into training and unseen test subsets.
- Incomplete model setup: Your model needs an output layer and loss function tailored to classification (not regression).
Corrected Code Example
Here's a revised version of your code that calculates test set accuracy correctly. I'll assume you're working on a binary classification task (adjust for multi-class if needed):
import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier # Switched from KerasRegressor from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.pipeline import Pipeline from sklearn.metrics import accuracy_score # Load your dataset (replace with your actual data path/loading logic) data = pd.read_csv("your_dataset.csv") X = data.drop("target_column", axis=1).values # Features y = data["target_column"].values # Target labels # Split data into training (80%) and test (20%) sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Define your classification ANN model def create_classifier(): model = Sequential() model.add(Dense(12, input_dim=X_train.shape[1], activation='relu')) # Input + hidden layer model.add(Dense(8, activation='relu')) # Second hidden layer # Binary classification output layer (adjust for multi-class if needed) model.add(Dense(1, activation='sigmoid')) # Compile with classification-specific loss and metrics model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model # Create a pipeline with scaling and the classifier estimator = Pipeline([ ('scaler', StandardScaler()), # Normalize features ('classifier', KerasClassifier(build_fn=create_classifier, epochs=100, batch_size=5, verbose=0)) ]) # Train the model on training data estimator.fit(X_train, y_train) # Predict labels for the test set y_pred = estimator.predict(X_test) # Calculate and print test set accuracy test_accuracy = accuracy_score(y_test, y_pred) print(f"Test Set Accuracy: {test_accuracy:.4f}")
Additional Notes
- Multi-class adjustment: If you're working with more than 2 classes, modify the output layer to use
Dense(n_classes, activation='softmax')and change the loss function tocategorical_crossentropy(ensure your target labels are one-hot encoded, or usesparse_categorical_crossentropyfor integer labels). - Regression check: If you're actually doing regression (predicting continuous values), accuracy isn't the right metric—use metrics like Mean Absolute Error (MAE) or R-squared instead.
- Python 3.5 compatibility: Since you're using Python 3.5, make sure you install compatible library versions: Keras ≤ 2.3.1, scikit-learn ≤ 0.20.3, and TensorFlow ≤ 1.15 (newer versions dropped support for Python 3.5).
内容的提问来源于stack exchange,提问作者Kashish Goyal
相关产品推荐
相关产品推荐

