#pr4 logistic regression


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 LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.read_csv("Bank_Personal_Loan_Modelling.csv")
print("✅ Data Loaded Successfully!\n")
print(data.head())



print("\nMissing Values:\n", data.isnull().sum())


data = data.drop(['ID'], axis=1)
data = data.drop(['ZIP Code'], axis=1)
data = data.drop(['CCAvg'], axis=1)


X = data.drop(['Personal Loan'], axis=1)  
y = data['Personal Loan']   


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled)


X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.25, random_state=42
)

model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)

y_pred = model.predict(X_test)


print("🎯 Accuracy:", round(accuracy_score(y_test, y_pred) * 100, 2), "%")
print("\n📊 Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\n🧾 Classification Report:\n", classification_report(y_test, y_pred))

sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')
plt.title("Confusion Matrix - Logistic Regression")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()