logistic model on cleaned data till 23oct

- 61.71 percent accuracy
This commit is contained in:
Pritimay Sarkar
2023-10-24 21:38:48 +05:30
parent 2c4288e98d
commit 1cf6324326

86
scripts/logistic2.py Normal file
View File

@@ -0,0 +1,86 @@
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
import os
curdir = os.getcwd()
path_delim = '/'
data = pd.read_excel(curdir + path_delim + 'data/tests_24_10_2023_20_53_cleaned.xlsx', sheet_name="data")
data = data.dropna()
print(data)
data.plot()
label_encoder = LabelEncoder()
categorical_cols = ['deviceId', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']
for col in categorical_cols:
data[col] = label_encoder.fit_transform(data[col])
X = data[['deviceId', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']]
y = data['classificationResult']
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)
# Accuracy: 0.6171938361719383
# Classification Report:
# precision recall f1-score support
# Inconclusive. Repeat with test with lower volume of blood 0.00 0.00 0.00 6
# Inconclusive. Very low Absorbance - Repeat test with Higher Blood Volume 0.17 0.05 0.07 21
# Negative Borderline. Repeat Test 0.00 0.00 0.00 167
# Normal 0.69 0.89 0.78 749
# Positive for Sickle Cell. HPLC for Confirmation 0.00 0.00 0.00 41
# Sickle Cell Disease 0.28 0.24 0.26 38
# Sickle Cell Trait 0.37 0.39 0.38 211
# accuracy 0.62 1233
# macro avg 0.22 0.22 0.21 1233
# weighted avg 0.49 0.62 0.55 1233
# 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)