Files
hpos-data/scripts/logistic.py
Pritimay Sarkar 2bca96a0f6 add cm plot
2023-10-01 15:10:18 +05:30

67 lines
2.0 KiB
Python

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import pickle
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_excel('/Users/apple/Downloads/21092023-July_Sept.xlsx', sheet_name="op")
data = data.dropna()
print(data)
data.plot()
label_encoder = LabelEncoder()
categorical_cols = ['Gender', 'Caste', 'Category', 'Marital Status', 'Blood Group']
for col in categorical_cols:
data[col] = label_encoder.fit_transform(data[col])
X = data[['Age', 'Gender', 'Caste', 'Category', 'Marital Status']]
y = data['Test Result']
corr = X.corr()
print(corr)
sm.graphics.plot_corr(corr, xnames=list(corr.columns))
plt.show()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
classification_report_result = classification_report(y_test, y_pred)
#sns.heatmap(pd.DataFrame(classification_report_result).iloc[:-1, :].T, annot=True)
# Calculate the confusion matrix
confusion = confusion_matrix(y_test, y_pred)
# Plot the confusion matrix using Seaborn
plt.figure(figsize=(8, 6))
sns.heatmap(confusion, annot=True, fmt='d', cmap='Blues', linewidths=0.5)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
print(f"Accuracy: {accuracy}")
print("Classification Report:")
print(classification_report_result)
# with open('logistic_regression_model.pkl', 'wb') as model_file:
# pickle.dump(model, model_file)
# with open('logistic_regression_model.pkl', 'rb') as model_file:
# loaded_model = pickle.load(model_file)
# new_data = pd.DataFrame({'Age': [30], 'Gender': ['MALE'], 'Caste': ['SC'], 'Category': [''], 'Marital Status': ['Single']})
# predicted_result = loaded_model.predict(new_data)
# print(predicted_result)