0% found this document useful (0 votes)
3 views4 pages

Exp-3 - Implement Classification Using MLP

The document outlines an experiment to implement classification using a Multilayer Perceptron (MLP) on the Breast Cancer dataset. It details the steps for data preparation, model training, and performance evaluation using accuracy, classification reports, and confusion matrices. The provided code demonstrates the implementation process, including data scaling, model fitting, and visualization of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Exp-3 - Implement Classification Using MLP

The document outlines an experiment to implement classification using a Multilayer Perceptron (MLP) on the Breast Cancer dataset. It details the steps for data preparation, model training, and performance evaluation using accuracy, classification reports, and confusion matrices. The provided code demonstrates the implementation process, including data scaling, model fitting, and visualization of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exp No: 3

Implement classification using MLP.


(08/09/25)

Aim:

Implement classification using MLP.

Objective:

1. Based on a dataset (e.g., Breast Cancer data), we will implement a Multilayer


Perceptron (MLP) for classification by first preparing the data through
scaling and then training the model.

2. Based on the trained model, we will predict the class labels for a test set and
evaluate the model's performance using metrics such as accuracy, a
classification report, and a confusion matrix to assess its ability to correctly
classify instances.

Program Code:

import 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, classification_report, confusion_matrix

import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()

X = cancer.data

y = cancer.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)

mlp = MLPClassifier(

hidden_layer_sizes=(100, 50),

max_iter=1000,

activation='relu',

solver='adam',

random_state=42

mlp.fit(X_train_scaled, y_train)

y_pred = mlp.predict(X_test_scaled)

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy * 100:.2f}%")

print("Classification Report:")

print(classification_report(y_test, y_pred, target_names=cancer.target_names))

print("Confusion Matrix:")

cm = confusion_matrix(y_test, y_pred)

plt.figure(figsize=(8, 6))

sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',


xticklabels=cancer.target_names, yticklabels=cancer.target_names)

plt.title('Confusion Matrix')

plt.xlabel('Predicted')
plt.ylabel('Actual')

plt.show()

plt.figure(figsize=(8, 6))

plt.plot(mlp.loss_curve_)

plt.title('Loss Curve during Training')

plt.xlabel('Epoch')

plt.ylabel('Loss')

plt.grid(True)

plt.show()

You might also like