To construct a Multi-Layer Perceptron to predict heart attack using Python
- Import the required libraries: numpy, pandas, MLPClassifier, train_test_split, StandardScaler, accuracy_score, and matplotlib.pyplot.
- Load the heart disease dataset from a file using pd.read_csv().
- Separate the features and labels from the dataset using data.iloc values for features (X) and data.iloc[:, -1].values for labels (y).
- Split the dataset into training and testing sets using train_test_split().
- Normalize the feature data using StandardScaler() to scale the features to have zero mean and unit variance.
- Create an MLPClassifier model with desired architecture and hyperparameters, such as hidden_layer_sizes, max_iter, and random_state.
- Train the MLP model on the training data using mlp.fit(X_train, y_train). The model adjusts its weights and biases iteratively to minimize the training loss.
- Make predictions on the testing set using mlp.predict(X_test).
- Evaluate the model's accuracy by comparing the predicted labels (y_pred) with the actual labels (y_test) using accuracy_score().
- Print the accuracy of the model.
- Plot the error convergence during training using plt.plot() and plt.show().
Developed By: Y SHAVEDHA
Register No. 212221230095
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
data=pd.read_csv("heart.csv")
X=data.iloc[:, :-1].values #features
Y=data.iloc[:, -1].values #labels
X_train,X_test,y_train,y_test=train_test_split(X,Y,test_size=0.2,random_state=42)
scaler=StandardScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.transform(X_test)
mlp=MLPClassifier(hidden_layer_sizes=(100,100),max_iter=1000,random_state=42)
training_loss=mlp.fit(X_train,y_train).loss_curve_
y_pred=mlp.predict(X_test)
accuracy=accuracy_score(y_test,y_pred)
print("Accuracy",accuracy)
plt.plot(training_loss)
plt.title("MLP Training Loss Convergence")
plt.xlabel("Iteration")
plt.ylabel("Training Losss")
plt.show()
![image](https://private-user-images.githubusercontent.com/93427376/281733031-0f5ac0c2-2f62-41b1-a410-2fda3ba4f556.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNjI4MjcsIm5iZiI6MTcyMjE2MjUyNywicGF0aCI6Ii85MzQyNzM3Ni8yODE3MzMwMzEtMGY1YWMwYzItMmY2Mi00MWIxLWE0MTAtMmZkYTNiYTRmNTU2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDEwMjg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNlYmRjMWFjM2JmMzE4NWJjOTg2MzY3NWFmMWUyNDNlMGIwZDZmYTExOGY0YjBmYmIyNDVlZGIwMGRhMTE4MGUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.WSeXQSFUJ-S1BD0vAkVYJik-aJYddavTyGyf-xDbUdU)
![image](https://private-user-images.githubusercontent.com/93427376/281733109-40a192f3-3dce-4b50-aefe-5172c816b091.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNjI4MjcsIm5iZiI6MTcyMjE2MjUyNywicGF0aCI6Ii85MzQyNzM3Ni8yODE3MzMxMDktNDBhMTkyZjMtM2RjZS00YjUwLWFlZmUtNTE3MmM4MTZiMDkxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDEwMjg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWMxMTVlYmU0MDhlMGQxM2FiMzQ5NTFiZmUwOWMyZmYzYjZiYjI1YTdjOGVhMGY1NmNlZThkYzJiYmNiYzk3Y2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.PLfrSaGGHQ_mOAN5b2aCC5USTp6OHbxr8gTUJDS9UFA)
Thus, an ANN with MLP is constructed and trained to predict the heart attack using python.