test model with sample data

This commit is contained in:
Pritimay Sarkar
2023-11-08 14:04:39 +05:30
parent 58f0fa0665
commit 0ae09130be

View File

@@ -2,85 +2,91 @@ import pandas as pd
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, roc_auc_score
import pickle import pickle
import statsmodels.api as sm import statsmodels.api as sm
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import seaborn as sns import seaborn as sns
import os import os
curdir = os.getcwd() # curdir = os.getcwd()
path_delim = '/' # path_delim = '/'
data = pd.read_excel(curdir + path_delim + 'data/tests_24_10_2023_20_53_cleaned.xlsx', sheet_name="data") # data = pd.read_excel(curdir + path_delim + 'data/tests_24_10_2023_20_53_cleaned.xlsx', sheet_name="data")
data = data.dropna() # data = data.dropna()
print(data) # # print(data)
data.plot() # data.plot()
label_encoder = LabelEncoder() # label_encoder = LabelEncoder()
categorical_cols = ['deviceId', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample'] # categorical_cols = ['calculatedRatio', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']
for col in categorical_cols: # # for col in categorical_cols:
data[col] = label_encoder.fit_transform(data[col]) # # data[col] = label_encoder.fit_transform(data[col])
X = data[['deviceId', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']] # X = data[['calculatedRatio', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']]
y = data['classificationResult'] # y = data['classificationResult']
corr = X.corr() # corr = X.corr()
print(corr) # print(corr)
sm.graphics.plot_corr(corr, xnames=list(corr.columns)) # sm.graphics.plot_corr(corr, xnames=list(corr.columns))
plt.show() # # plt.show()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression() # model = LogisticRegression()
model.fit(X_train, y_train) # model.fit(X_train, y_train)
y_pred = model.predict(X_test) # y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred) # accuracy = accuracy_score(y_test, y_pred)
classification_report_result = classification_report(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) # # sns.heatmap(pd.DataFrame(classification_report_result).iloc[:-1, :].T, annot=True)
# Calculate the confusion matrix # # Calculate the confusion matrix
confusion = confusion_matrix(y_test, y_pred) # confusion = confusion_matrix(y_test, y_pred)
# Plot the confusion matrix using Seaborn # # Plot the confusion matrix using Seaborn
plt.figure(figsize=(8, 6)) # plt.figure(figsize=(8, 6))
sns.heatmap(confusion, annot=True, fmt='d', cmap='Blues', linewidths=0.5) # sns.heatmap(confusion, annot=True, fmt='d', cmap='Blues', linewidths=0.5)
plt.xlabel('Predicted') # plt.xlabel('Predicted')
plt.ylabel('Actual') # plt.ylabel('Actual')
plt.title('Confusion Matrix') # plt.title('Confusion Matrix')
plt.show() # plt.show()
print(f"Accuracy: {accuracy}") # print(f"Accuracy: {accuracy}")
print("Classification Report:") # print("Classification Report:")
print(classification_report_result) # print(classification_report_result)
# Accuracy: 0.6171938361719383 # # dataset: [6163 rows x 15 columns]
# Classification Report: # # Accuracy: 0.6593673965936739
# precision recall f1-score support # # Classification Report:
# # precision recall f1-score support
# Inconclusive. Repeat with test with lower volume of blood 0.00 0.00 0.00 6 # # 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 # # Inconclusive. Very low Absorbance - Repeat test with Higher Blood Volume 0.88 0.71 0.79 21
# Negative Borderline. Repeat Test 0.00 0.00 0.00 167 # # Negative Borderline. Repeat Test 0.11 0.01 0.01 167
# Normal 0.69 0.89 0.78 749 # # Normal 0.80 0.88 0.84 749
# Positive for Sickle Cell. HPLC for Confirmation 0.00 0.00 0.00 41 # # 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 Disease 0.25 0.16 0.19 38
# Sickle Cell Trait 0.37 0.39 0.38 211 # # Sickle Cell Trait 0.37 0.62 0.46 211
# accuracy 0.62 1233
# macro avg 0.22 0.22 0.21 1233
# weighted avg 0.49 0.62 0.55 1233
# # accuracy 0.66 1233
# # macro avg 0.34 0.34 0.33 1233
# # weighted avg 0.58 0.66 0.61 1233
# with open('logistic_regression_model.pkl', 'wb') as model_file: # with open('logistic_regression_model.pkl', 'wb') as model_file:
# pickle.dump(model, model_file) # pickle.dump(model, model_file)
# with open('logistic_regression_model.pkl', 'rb') as model_file: with open('logistic_regression_model.pkl', 'rb') as model_file:
# loaded_model = pickle.load(model_file) loaded_model = pickle.load(model_file)
# new_data = pd.DataFrame({'Age': [30], 'Gender': ['MALE'], 'Caste': ['SC'], 'Category': [''], 'Marital Status': ['Single']}) # new_data = pd.DataFrame({'calculatedRatio': [0.126976079], 'led1Buffer': [24313.67], 'led1Sample': [20531], 'led2Buffer': [26565], 'led2Sample': [9975.33]}) #Normal
# predicted_result = loaded_model.predict(new_data) # new_data = pd.DataFrame({'calculatedRatio': [0.17500836], 'led1Buffer': [24843.33], 'led1Sample': [19678], 'led2Buffer': [26715.33], 'led2Sample': [13842.67]}) #SCT
# print(predicted_result) # new_data = pd.DataFrame({'calculatedRatio': [0.251395102], 'led1Buffer': [24244], 'led1Sample': [17475], 'led2Buffer': [27059.67], 'led2Sample': [9258.33]}) #SCD
# new_data = pd.DataFrame({'calculatedRatio': [0.251061035], 'led1Buffer': [24172], 'led1Sample': [19345], 'led2Buffer': [26918], 'led2Sample': [12964.33]})
# new_data = pd.DataFrame({'calculatedRatio': [0.242851779], 'led1Buffer': [25087.33], 'led1Sample': [20170.67], 'led2Buffer': [26578.33], 'led2Sample': [12724.67]})
# new_data = pd.DataFrame({'calculatedRatio': [0.189778691], 'led1Buffer': [23209.33], 'led1Sample': [17672], 'led2Buffer': [19850.33], 'led2Sample': [10360.33]}) #SCT
new_data = pd.DataFrame({'calculatedRatio': [0.149174719], 'led1Buffer': [24122], 'led1Sample': [18368.33], 'led2Buffer': [21733.33], 'led2Sample': [9114.67]}) #Normal
predicted_result = loaded_model.predict(new_data)
print(predicted_result)