import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
# Step 1: Load training and testing data
folder = r"E:\Kiran\DSI\ML and NN\Lab\Program 12"
train = pd.read_csv(folder + r"\optdigits1.csv", header=None)
test = pd.read_csv(folder + r"\optdigits.csv", header=None)
X_train = train.iloc[:, :-1].values
y_train = train.iloc[:, -1].values
X_test = test.iloc[:, :-1].values
y_test = test.iloc[:, -1].values
# Step 2: Scale the input features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Step 3: Create the models
sgd_model = MLPClassifier(
hidden_layer_sizes=(64,),
solver='sgd',
learning_rate_init=0.01,
momentum=0,
max_iter=1000,
random_state=42
)
momentum_model = MLPClassifier(
hidden_layer_sizes=(64,),
solver='sgd', learning_rate_init=0.01,
momentum=0.9,
max_iter=1000,
random_state=42
)
# Step 4: Train the models
sgd_model.fit(X_train, y_train)
momentum_model.fit(X_train, y_train)
# Step 5: Predict and calculate accuracy
sgd_pred = sgd_model.predict(X_test)
momentum_pred = momentum_model.predict(X_test)
print("SGD Accuracy:",
round(accuracy_score(y_test, sgd_pred) * 100, 2), "%")
print("SGD Epochs:", sgd_model.n_iter_)
print("SGD Final Loss:", round(sgd_model.loss_, 4))
print("\nSGD with Momentum Accuracy:",
round(accuracy_score(y_test, momentum_pred) * 100, 2), "%")
print("Momentum Epochs:", momentum_model.n_iter_)
print("Momentum Final Loss:", round(momentum_model.loss_, 4))
# Step 6: Plot training loss
plt.plot(sgd_model.loss_curve_, label="SGD")
plt.plot(momentum_model.loss_curve_, label="SGD with Momentum")
plt.xlabel("Epoch")
plt.ylabel("Training Loss")
plt.title("SGD vs SGD with Momentum")
plt.legend()
plt.grid()
plt.show()