Delete 1D_CNN_Model - Test.ipynb

parent 76f2beed
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "TPU"
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WXK1h7IS5ua0"
},
"outputs": [],
"source": [
"#Import Libraries\n",
"\n",
"import pandas as pd\n",
"from matplotlib import pyplot as plt\n",
"import numpy as np\n",
"%matplotlib inline\n",
"\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.layers import Flatten\n",
"from sklearn.model_selection import train_test_split\n",
"import time\n",
"\n",
"\n",
"#Data Balancing libraries\n",
"from imblearn.under_sampling import NearMiss\n",
"from imblearn.over_sampling import ADASYN\n",
"\n",
"from imblearn.over_sampling import SMOTE\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"from imblearn.combine import SMOTEENN #Hybrid method\n",
"\n",
"from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, roc_curve, auc\n",
"\n",
"\n",
"#apply standardization\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"#Visual libraries\n",
"import seaborn as sns\n",
"\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.metrics import classification_report"
]
},
{
"cell_type": "code",
"source": [
"data = pd.read_csv('/content/disease_preprocess4.csv')"
],
"metadata": {
"id": "fMhfrc507QwL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"data.head()"
],
"metadata": {
"id": "JryGjddVC8xL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"data.columns"
],
"metadata": {
"id": "F_PjX618F5l6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# define target variable and features\n",
"\n",
"# Defining the features (X) and the target (y)\n",
"\n",
"X = data.drop('HeartDisease', axis=1) # Features\n",
"y = data['HeartDisease'] # Target variable\n",
"\n",
"# Performing the train-test split\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"X_train.shape, X_test.shape, y_train.shape, y_test.shape\n",
"\n",
"print(X_train.head())\n",
"print(y_train.head())"
],
"metadata": {
"id": "Jkmi2N0aC8nZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"##Perform Scaling"
],
"metadata": {
"id": "E2WufmJdz8g_"
}
},
{
"cell_type": "code",
"source": [
"#apply standardization\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"# Create a StandardScaler instance\n",
"scaler = StandardScaler()\n",
"\n",
"\n",
"# Fit the scaler on the training data and transform it\n",
"X_train_scaled = scaler.fit_transform(X_train)\n",
"\n",
"# Use the same scaler to transform the test data\n",
"X_test_scaled = scaler.transform(X_test)"
],
"metadata": {
"id": "Y3FJTGSrUJIh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the count of each class in the before resample data\n",
"print(y_train.value_counts())"
],
"metadata": {
"id": "HZThz9by_GfH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### NearMiss"
],
"metadata": {
"id": "DkcD5UvyWld6"
}
},
{
"cell_type": "code",
"source": [
"# Resample the training data\n",
"\n",
"from imblearn.under_sampling import NearMiss\n",
"\n",
"# Apply NearMiss to Undersample the majority class\n",
"nearmiss = NearMiss(sampling_strategy='auto')\n",
"X_train_nearmiss, y_train_nearmiss = nearmiss.fit_resample(X_train_scaled, y_train)\n"
],
"metadata": {
"id": "w0wWq8wtWoZ3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the count of each class in the resampled data\n",
"print(y_train_nearmiss.value_counts())"
],
"metadata": {
"id": "VvkPNmPaXHKq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_nearmiss.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()"
],
"metadata": {
"id": "mgOGqHJwXHBr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_nearmiss, y_train_nearmiss, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")"
],
"metadata": {
"id": "XqcE7QX_XUfL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
],
"metadata": {
"id": "r2iwyhwYXVQx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
],
"metadata": {
"id": "9lt13HjQXliz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### ADASYN"
],
"metadata": {
"id": "FicbvbTzXsHp"
}
},
{
"cell_type": "code",
"source": [
"# Resample the training data\n",
"\n",
"from imblearn.over_sampling import ADASYN\n",
"\n",
"# Apply NearMiss to Undersample the majority class\n",
"adasyn = ADASYN(sampling_strategy='auto', random_state=23)\n",
"X_train_adasyn, y_train_adasyn = adasyn.fit_resample(X_train_scaled, y_train)\n"
],
"metadata": {
"id": "z_vRKICpX1IB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_adasyn.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()"
],
"metadata": {
"id": "VSFetfoqZA3g"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_adasyn, y_train_adasyn, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")"
],
"metadata": {
"id": "89tJFpG_Xq6I"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
],
"metadata": {
"id": "X_pXi9WqZIRz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
],
"metadata": {
"id": "R4KHz7iEZI97"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### SMOTE"
],
"metadata": {
"id": "yo5CABz5ZMKk"
}
},
{
"cell_type": "code",
"source": [
"# Resample the training data\n",
"\n",
"from imblearn.over_sampling import SMOTE\n",
"\n",
"# Apply NearMiss to Undersample the majority class\n",
"smote = SMOTE(sampling_strategy='auto', random_state=23)\n",
"X_train_smote, y_train_smote = smote.fit_resample(X_train_scaled, y_train)"
],
"metadata": {
"id": "besSIFVZZl3T"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_smote.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()"
],
"metadata": {
"id": "fTSo2sIdZehk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_smote, y_train_smote, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")"
],
"metadata": {
"id": "cLfhhYJqZPJL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
],
"metadata": {
"id": "6CYHP6CXZPD9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
],
"metadata": {
"id": "Zj_sDeLvZO7E"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Random Under Sampler"
],
"metadata": {
"id": "SemurYg5Z8p0"
}
},
{
"cell_type": "code",
"source": [
"# Resample the training data\n",
"\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"\n",
"# Apply NearMiss to Undersample the majority class\n",
"random_under_sampler= RandomUnderSampler(sampling_strategy='auto', random_state=23)\n",
"X_train_random, y_train_random = random_under_sampler.fit_resample(X_train_scaled, y_train)"
],
"metadata": {
"id": "hx80eO5-aBqr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_random.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()"
],
"metadata": {
"id": "6DK0YDZrvofA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_random, y_train_random, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")"
],
"metadata": {
"id": "oO4pTApTvuXw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
],
"metadata": {
"id": "TiuKYZuovzaI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
],
"metadata": {
"id": "xkE0Sy1Lv2Kw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## SMOTE and Random Combined"
],
"metadata": {
"id": "zJtzN8ctUoIo"
}
},
{
"cell_type": "code",
"source": [
"# Resample the training data\n",
"\n",
"from imblearn.over_sampling import SMOTE\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"from imblearn.combine import SMOTEENN # A hybrid method\n",
"\n",
"\n",
"# Apply SMOTE to oversample the minority class\n",
"smote=SMOTE(sampling_strategy='auto', random_state=23)\n",
"X_train_smote, y_train_smote = smote.fit_resample(X_train_scaled, y_train)\n",
"\n",
"# Apply undersampling to the majority class\n",
"under_sampler = RandomUnderSampler(sampling_strategy='auto', random_state=23)\n",
"X_train_combined, y_train_combined = under_sampler.fit_resample(X_train_smote, y_train_smote)\n",
"\n",
"# Train and evaluate your machine learning model using X_train_combined and y_train_combined\n",
"# Evaluate the model on X_test_scaled and y_test\n"
],
"metadata": {
"id": "DX0w3ww2UUFw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the count of each class in the resampled data\n",
"print(y_train.value_counts())"
],
"metadata": {
"id": "9YO5Ql4l_Bl0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Visualization of target variable after resampling\n",
"\n",
"g = sns.countplot(x= y_train_combined,data=data, palette=\"muted\")\n",
"g.set_ylabel(\"Patients\", fontsize=14)\n",
"g.set_xlabel(\"Heart Disease\", fontsize=14)"
],
"metadata": {
"id": "v9-BQ1bpiN3i"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Model Training"
],
"metadata": {
"id": "71z03akwbnSu"
}
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_combined.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()\n"
],
"metadata": {
"id": "0rkXxtX4bqOH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_combined, y_train_combined, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")"
],
"metadata": {
"id": "qcpsJFNQbyEw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
],
"metadata": {
"id": "ZuYctwmBb0Rw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
],
"metadata": {
"id": "FDlrN-l0b1JI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#Import the necessary libraries\n",
"import numpy as np\n",
"from sklearn.metrics import confusion_matrix\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"\n",
"y_pred = model.predict(X_test_scaled) # Replace 'model' with your trained model\n",
"\n",
"# Convert predicted probabilities to binary labels (0 or 1)\n",
"y_pred_binary = (y_pred > 0.5).astype(int)\n",
"\n",
"#compute the confusion matrix.\n",
"cm = confusion_matrix(y_test,y_pred_binary)\n",
"\n",
"#Plot the confusion matrix.\n",
"sns.heatmap(cm,\n",
" annot=True,\n",
" fmt='g',\n",
" xticklabels=['Class 0','Class 1'],\n",
" yticklabels=['Class 0','Class 1'])\n",
"plt.ylabel('Prediction',fontsize=13)\n",
"plt.xlabel('Actual',fontsize=13)\n",
"plt.title('Confusion Matrix',fontsize=17)\n",
"plt.show()"
],
"metadata": {
"id": "EEdrLiFpvdWz"
},
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment