Complete Coding example with K-Nearest Neighbor (KNN) Algorithm


 #Load and Explore Dataset

import pandas as pd from google.colab import drive drive.mount('/content/drive') #Mounted at /content/drive #Dataset Loading # Load the training and testing datasets df = pd.read_csv('/content/drive/MyDrive/Machine Learning Lab/loan_approval_dataset.csv') # Display the first 5 rows of the dataset df.head()



#Check for missing values and dataset summary # Check for missing values and basic statistics print(df.isnull().sum()) print("\nDataset Summary:\n") print(df.describe()) #Split Features and Labels # Separate features and target X = df.drop('Loan_Approved', axis=1) y = df['Loan_Approved'] #Split the Data into Training and Testing Sets from sklearn.model_selection import train_test_split # Split the data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) #Feature Scaling (Standardization) from sklearn.preprocessing import StandardScaler # Initialize the scaler scaler = StandardScaler() # Fit on training data, transform both training and testing X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) #Train KNN Classifier and Make Predictions from sklearn.neighbors import KNeighborsClassifier # Initialize and train the KNN model knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X_train_scaled, y_train) # Make predictions on the test set y_pred = knn.predict(X_test_scaled) #Evaluate Model Performance from sklearn.metrics import accuracy_score, classification_report, confusion_matrix # Evaluate the model print("Accuracy:", accuracy_score(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))


# Get confusion matrix cm = confusion_matrix(y_test, y_pred) # Assign values TN, FP, FN, TP = cm.ravel() # Print the results print("True Positives (TP):", TP) print("True Negatives (TN):", TN) print("False Positives (FP):", FP) print("False Negatives (FN):", FN)

#True Positives (TP): 4 #True Negatives (TN): 4 #False Positives (FP): 7 #False Negatives (FN): 5

No comments:

Post a Comment