Important pandas as pd
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, confusion_matrix, classification_report
# Step 1: Load the Iris dataset
file_path = r"E:\Kiran\DSI\ML and NN\Lab\Program 9\iris\iris.csv"
data = pd.read_csv(file_path)
print("First 5 records:")
print(data.head())
# Step 2: Separate input and output
X = data[['sepal_length',
'sepal_width',
'petal_length',
'petal_width']]
y = data['species']
# Step 3: Split the dataset into training and testing
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
# Step 4: Standardize the input data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Step 5: Create the MLP model
model = MLPClassifier(
hidden_layer_sizes=(10,),
activation='relu',
solver='adam',
max_iter=1000,
random_state=42
)
# Step 6: Train the model
model.fit(X_train, y_train)
# Step 7: Predict the test data
y_pred = model.predict(X_test)
# Step 8: Display actual and predicted values
print("\nActual Output:")
print(y_test.to_numpy())
print("\nPredicted Output:")
print(y_pred)
# Step 9: Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("\nAccuracy:")
print(accuracy)
print("\nAccuracy Percentage:")
print(accuracy * 100, "%")
# Step 10: Display confusion matrix
cm = confusion_matrix(y_test, y_pred)
print("\nConfusion Matrix:")
print(cm)
# Step 11: Display classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred))