All notes
AI & Learning

Neural Network Classification Using Scikit-Learn

Artificial neural networks are machine learning models that learn relationships between input features and target outputs through interconnected layers of neurons. A Multi-Layer Perceptron (MLP) is a feedforward neural network that typically contains an input layer, one or more hidden layers, and an output layer

Neural Network Classification Using Scikit-Learn Introduction Artificial neural networks are machine learning models that learn relationships between input features and target outputs through interconnected layers of neurons. A Multi-Layer Perceptron (MLP) is a feedforward neural network that typically contains an input layer, one or more hidden layers, and an output layer. During training, the network adjusts its internal weights to reduce prediction error. Scikit-learn provides MLPClassifier, enabling neural-network classification in Python through a consistent machine learning interface (Lee, 2019; Scikit-learn developers, n.d.-a). Neural networks evolved from early perceptron models to more advanced multilayer networks, trained with backpropagation and other optimization techniques (Chopra, 2023). This assignment uses an MLP classifier in two experiments. Task 1 analyzes an existing Python program that classifies Iris flowers and examines how varying the number of neurons in a hidden layer affects classification performance. Task 2 develops a neural network model to classify wine types using 13 chemical attributes from the Wine dataset provided by scikit-learn. The Wine experiment also standardizes features before training and evaluates the resulting classifier on unseen test data. Task 1: Analysis of MLP Model Using the Iris Dataset Question 1: What Is the Purpose of This Code? The purpose of the provided Python program is to build, train, and evaluate a neural network classifier using the Iris dataset. The dataset contains four numerical input features and three target classes. The program loads the data with load_iris(), stores the input features in X and target labels in y, and divides the observations into training and testing subsets with train_test_split(). With test_size=0.3, 70% of the data is used for training, and 30% is reserved for testing. Keeping a separate test set allows evaluation of the classifier on observations not used to fit the model.

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42 )

The network is created with hidden_layer_sizes=(10,), which specifies a single hidden layer with 10 neurons. max_iter=1000 allows up to 1,000 training iterations, while random_state=42 makes the experiment reproducible. The model is trained with mlp.fit(X_train, y_train) and then predicts the classes of the test observations using mlp.predict(X_test). Finally, accuracy_score() compares the predicted class labels with the correct labels. MLPClassifier trains iteratively using backpropagation; by default, the Adam optimizer is used to update model parameters (Scikit-learn developers, n.d.-a). Question 2: Result with hidden_layer_sizes=(10,) The original code was executed with a single hidden layer of 10 neurons. The actual test accuracy obtained was 0.98. Because the Iris dataset contains 150 observations and 30% of the data was assigned to the test set, the model was evaluated on 45 test observations. An accuracy of 0.98 therefore means that nearly all test samples were classified correctly.

task1-b.png

Figure 1. Output with hidden_layer_sizes=(10,) showing Accuracy = 0.98.

The program also produced a ConvergenceWarning stating that the stochastic optimizer reached the maximum of 1,000 iterations before the optimization criterion was fully satisfied. This warning does not mean that the program failed. The neural network was trained and produced predictions, but further iterations might still have reduced the training objective. Despite the warning, the model achieved high test accuracy. Question 3: Result with hidden_layer_sizes=(30,) The hidden-layer size was then changed from 10 neurons to 30 neurons while the other settings remained unchanged. The modified model achieved an accuracy of 1.00. In this experiment, increasing the hidden-layer size improved test accuracy from 0.98 to 1.00, indicating that all 45 observations in this test split were classified correctly.

Figure 2. Output with hidden_layer_sizes=(30,) showing Accuracy = 1.00.

task1-c.png

A larger hidden layer gives an MLP greater capacity to represent nonlinear relationships, but this result should not be interpreted as evidence that more neurons always produce a better model. Additional neurons increase model complexity and computational requirements and can also increase the risk of overfitting. Therefore, the appropriate conclusion is that the 30-neuron configuration performed better for this fixed train-test split, not that it is universally superior. The convergence warning still appeared, demonstrating that high test accuracy and full optimization convergence are not the same concept. Question 4: Printing the Predicted Classes The statement shown in the assignment, print("Result obtained by " : y_pred), contains a Python syntax error because a colon cannot be used in that position to separate the string from the variable. To perform the requested experiment, the statement was corrected to the following valid Python syntax: print("Result obtained by Eric Ji:", y_pred) The corrected statement was placed immediately after y_pred = mlp.predict(X_test). It printed my name followed by the NumPy array of predicted class labels. The values 0, 1, and 2 represent the three target classes in the Iris dataset, and the array contains one prediction for each of the 45 test samples. Printing y_pred does not change the trained model or the predictions; it only displays the results that the model has already generated.

Figure 3. Output showing the predicted class array and “Result obtained by Eric Ji.” task1-d.png Task 2: Neural Network Classification of Wine Types Dataset Features and Target Variable For Task 2, the Wine dataset was loaded directly from sklearn.datasets using load_wine(). The dataset contains 178 samples and 13 numerical features describing chemical characteristics of wine, with a three-class target variable (Scikit-learn developers, n.d.-b). The 13 features are alcohol, malic_acid, ash, alcalinity_of_ash, magnesium, total_phenols, flavanoids, nonflavanoid_phenols, proanthocyanins, color_intensity, hue, od280/od315_of_diluted_wines, and proline. The target labels are class_0, class_1, and class_2, encoded numerically as 0, 1, and 2. Complete Python Code for Wine Classification

Task 2: Wine Classification Using a Neural Network

from sklearn.datasets import load_wine from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPClassifier from sklearn.metrics import accuracy_score, classification_report

Load the Wine dataset from scikit-learn

wine = load_wine()

X contains the 13 chemical features

X = wine.data

y contains the target class labels: 0, 1, and 2

y = wine.target

Display the feature names and target classes

print("Feature names:") for feature in wine.feature_names: print(feature)

print("\nTarget classes:") print(wine.target_names)

print("\nNumber of samples:", X.shape[0]) print("Number of features:", X.shape[1])

Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.30, random_state=42, stratify=y )

Standardize the features

Fit the scaler only on the training data to avoid data leakage

scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test)

Create the neural network classifier

mlp = MLPClassifier( hidden_layer_sizes=(30,), max_iter=1000, random_state=42 )

Train the model

mlp.fit(X_train_scaled, y_train)

Make predictions on the test set

y_pred = mlp.predict(X_test_scaled)

Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print("\nPredicted classes:") print(y_pred)

print("\nActual classes:") print(y_test)

print(f"\nWine Classification Accuracy: {accuracy:.2f}")

print("\nClassification Report:") print(classification_report( y_test, y_pred, target_names=wine.target_names )) Steps and Interpretation of the Results The dataset was divided into 70% training data and 30% testing data. The stratify=y option was used so that the three wine classes remained proportionally represented in both subsets. Before training, the numerical features were standardized using StandardScaler. Standardization is useful because the 13 Wine features have very different numerical scales. StandardScaler centers each feature around its mean and scales it according to its standard deviation, helping optimization algorithms work with features on more comparable numerical scales (Scikit-learn developers, n.d.-c). The scaler was fitted only to the training data with fit_transform(), and the learned transformation was then applied to the test data with transform(). This prevents information from the test set from influencing the preprocessing parameters used during training. The MLP classifier used one hidden layer with 30 neurons and a maximum of 1,000 iterations. The trained Wine model achieved an overall test accuracy of 0.96. The 30% test subset contained 54 observations, and comparison of the predicted and actual arrays showed that 52 of the 54 samples were classified correctly. The detailed classification report showed precision, recall, and F1-score of 1.00 for class_0, 0.95 for class_1, and 0.93 for class_2. The macro-average and weighted-average scores were both approximately 0.96, which is consistent with the overall accuracy. These results indicate that the model learned useful relationships between the chemical measurements and the three wine classes. Training Loss Curve Figure 4 shows the loss curve recorded during training of the Wine MLP classifier. The loss decreased rapidly during the early iterations and then declined more gradually, approaching a very low value after roughly 400 iterations. This pattern indicates that the optimization process reduced the training error substantially at the beginning and then made smaller improvements as training progressed. The loss curve describes the training objective, so it should be considered together with the independent test accuracy of 0.96 when evaluating the model.

Figure 4. Loss Curve During MLP Training on the Wine Dataset. Conclusion loss_curve.png This assignment demonstrated neural-network-based multiclass classification using Python and scikit-learn. In Task 1, the Iris MLP with 10 hidden neurons achieved an accuracy of 0.98, while increasing the hidden layer to 30 neurons produced an accuracy of 1.00 for the same data split and random state. The experiment showed that architecture can influence performance and that additional neurons do not guarantee better generalization in every situation. Printing y_pred further demonstrated that a classifier produces one predicted target class for each test observation. In Task 2, an MLP classifier was developed for the Wine dataset using all 13 chemical features. StandardScaler was applied after the train-test split so that feature values with very different numerical scales could be processed more effectively without leaking test-set information into training. The model achieved 0.96 accuracy, correctly classifying 52 of 54 test observations. The classification results showed strong performance across all three wine classes. Together, the two experiments illustrate important neural network concepts including hidden-layer architecture, feature preprocessing, model training, prediction, and classification metrics.

References Chopra, D. (2023). Introduction to machine learning with Python. Bentham Science Publishers. Lee, W.-M. (2019). Python machine learning. https://ebookcentral.proquest.com/lib/univ-people-ebooks/detail.action?docID=5747364 Scikit-learn developers. (n.d.-a). MLPClassifier. https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html Scikit-learn developers. (n.d.-b). The Wine recognition dataset. https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_wine.html
Scikit-learn developers. (n.d.-c). StandardScaler. https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html

Back to the notebook