asf
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.preprocessing import LabelEncoder, StandardScaler from sklearn.metrics import confusion_matrix # Load dataset file_path = "healthcare-dataset-stroke-data.csv" data = pd.read_csv(file_path) # Handle missing values data['bmi'] = data['bmi'].fillna(data['bmi'].mean()) # Drop unnecessary column data = data.drop(['id'], axis=1) # Encode categorical columns label_encoders = {} for column in ['gender', 'ever_married', 'work_type', 'Residence_type', 'smoking_status']: le = LabelEncoder() data[column] = le.fit_transform(data[column]) label_encoders[column] = le # Split features and target X = data.drop(['stroke'], axis=1) y = data['stroke'] # Scale the features scaler = StandardScaler() X = scaler.fit_transform(X) # Split into training and testing data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y) # Initialize and train Naive Bayes model model = GaussianNB() model.fit(X_train, y_train) # Predict the test data y_pred = model.predict(X_test) # Calculate confusion matrix Confusion_Matrix = confusion_matrix(y_test, y_pred) print("The confusion matrix is as follows: ") print(Confusion_Matrix) # Extract True Positives, False Positives, False Negatives, True Negatives True_positives = Confusion_Matrix[1, 1] False_positives = Confusion_Matrix[0, 1] False_negatives = Confusion_Matrix[1, 0] True_negatives = Confusion_Matrix[0, 0] # Print confusion matrix values print("True Positives:", True_positives) print("False Positives:", False_positives) print("False Negatives:", False_negatives) print("True Negatives:", True_negatives) # Calculate Precision Precision = True_positives / (True_positives + False_positives) if (True_positives + False_positives) > 0 else 0 print("Precision:", Precision) # Calculate Recall Recall = True_positives / (True_positives + False_negatives) if (True_positives + False_negatives) > 0 else 0 print("Recall:", Recall) # Calculate F1-Score F1_Score = 2 * (Precision * Recall) / (Precision + Recall) if (Precision + Recall) > 0 else 0 print("F1-Score:", F1_Score)
Leave a Comment