import pandas as pd from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier 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 = ['calculatedRatio', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample'] # for col in categorical_cols: # data[col] = label_encoder.fit_transform(data[col]) X = data[['calculatedRatio', 'led1Buffer', 'led1Sample', 'led2Buffer', 'led2Sample']] y = data['classificationResult'] print(X) 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) # Change the classifier to KNeighborsClassifier model = KNeighborsClassifier(n_neighbors=5) # You can adjust the number of neighbors as needed 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) # 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.7502027575020276 # Classification Report: # precision recall f1-score support # Inconclusive. Repeat with test with lower volume of blood 0.50 0.17 0.25 6 # Inconclusive. Very low Absorbance - Repeat test with Higher Blood Volume 0.50 0.48 0.49 21 # Negative Borderline. Repeat Test 0.51 0.51 0.51 167 # Normal 0.85 0.92 0.88 749 # Positive for Sickle Cell. HPLC for Confirmation 0.21 0.17 0.19 41 # Sickle Cell Disease 0.67 0.32 0.43 38 # Sickle Cell Trait 0.66 0.55 0.60 211 # accuracy 0.75 1233 # macro avg 0.56 0.45 0.48 1233 # weighted avg 0.74 0.75 0.74 1233 # Save the KNeighborsClassifier model with open('kneighbors_classifier_model.pkl', 'wb') as model_file: pickle.dump(model, model_file) with open('kneighbors_classifier_model.pkl', 'rb') as model_file: loaded_model = pickle.load(model_file) # Define new_data as needed for prediction # new_data = pd.DataFrame({'calculatedRatio': [0.17500836], 'led1Buffer': [24843.33], 'led1Sample': [19678], 'led2Buffer': [26715.33], 'led2Sample': [13842.67]}) # new_data = pd.DataFrame({'calculatedRatio': [0.231057205], 'led1Buffer': [23776.33], 'led1Sample': [16286], 'led2Buffer': [26401.67], 'led2Sample': [6952.67]}) new_data = pd.DataFrame({'calculatedRatio': [0.175881142], 'led1Buffer': [24256], 'led1Sample': [16303], 'led2Buffer': [27016.33], 'led2Sample': [4612.67]}) # new_data = new_data.apply(lambda col: label_encoder.transform(col)) print(new_data) predicted_result = loaded_model.predict(new_data) print(predicted_result)