ExNo 08ml
ExNo 08ml
CO4
functions
Task8: Create a perceptron with appropriate number of inputs and outputs using Medical data.
Train it using fixed increment learning algorithm until no change in weights is
required. Output the final weights.
Platform: Google co-lab, Language: Python
Background:
Healthcare providers are increasingly leveraging machine learning to enhance patient care and
identify individuals at risk of specific health conditions. In this use case, we focus on predicting
the risk of stroke using a dataset that includes various health-related features.
Objective:
The primary objective is to develop a predictive model that can assist healthcare professionals
in identifying individuals who may be at a higher risk of experiencing a stroke. Early
identification of high-risk individuals allows for targeted interventions and preventive
measures.
Data:
The dataset used in this use case contains information about individuals, including their age,
presence of hypertension, heart disease, average glucose level, and body mass index (bmi). The
dataset also includes an indicator variable for stroke (1 for individuals who experienced a stroke
and 0 for those who did not).
Algorithm:
2. Data Preprocessing:
- Fill missing values in the dataset with the mean of their respective columns.
- Select relevant features (age, hypertension, heart disease, avg_glucose_level, bmi) and the
target variable (stroke).
- Split the dataset into input features (X) and the target variable (y).
- Split the data into training and testing sets.
- Standardize the features using StandardScaler.
3. Model Training:
- Create a Perceptron model with specified hyperparameters (max_iter, eta0, random_state).
- Train the Perceptron model on the standardized training data.
4. Model Evaluation:
- Make predictions on the test set using the trained model.
- Calculate and print the accuracy of the model.
- Output the final weights of the perceptron.
5. Visualization:
- Create a confusion matrix and plot it as a heatmap using Seaborn.
- Generate an ROC curve and plot it.
- Create a Precision-Recall curve and plot it.
Program:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score, confusion_matrix,
roc_curve, auc, precision_recall_curve
import seaborn as sns
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')
data = pd.read_csv(file_path)
print(data.head())
data = data.fillna(data.mean())
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
perceptron.fit(X_train, y_train)
y_pred = perceptron.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')
final_weights = perceptron.coef_
print(f'Final Weights: {final_weights}')
plt.figure(figsize=(12, 4))
plt.subplot(1, 3, 1)
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues',
xticklabels=['No Stroke', 'Stroke'], yticklabels=['No Stroke',
'Stroke'])
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.subplot(1, 3, 2)
fpr, tpr, thresholds = roc_curve(y_test,
perceptron.decision_function(X_test))
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC =
{roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc='lower right')
plt.subplot(1, 3, 3)
precision, recall, _ = precision_recall_curve(y_test,
perceptron.decision_function(X_test))
plt.plot(recall, precision, color='green', lw=2, label='Precision-
Recall curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.tight_layout()
plt.savefig('/content/drive/MyDrive/ML Lab/ExNo10/Figure.png',dpi=500)
plt.show()
stroke
0 1
1 1
2 1
3 1
4 1
Accuracy: 0.9158512720156555
Final Weights: [[ 0.18583003 -0.42796629 0.188951 -0.19687049
0.06610429]]