Supervised Learning - Classification Week 8 Challenge
1.
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, log_loss
# Load the dataset
data = pd.read_csv(""C:\Users\venka\Downloads\Mechanical_datasets-20230715T140641Z-001.zip.csv",encoding="unicode_escape")
# Split the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(data.drop("Other_Faults", axis=1), data["Other_Faults"], test_size=0.25)
# Train the KNN classifier
clf = KNeighborsClassifier(n_neighbors=5, p=2, weights="uniform")
clf.fit(X_train, y_train)
# Evaluate the performance of the classifier
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
auc = roc_auc_score(y_test, y_pred)
log_loss = log_loss(y_test, y_pred)
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1:", f1)
print("AUC:", auc)
print("Log loss:", log_loss)
conclusion:
The output of the code will be the accuracy, precision, recall, F1 score, AUC, and log loss of the KNN classifier. The results will vary depending on the hyperparameters that are used.
To compare the performance of the KNN classifier with the SVM classifier, we can calculate the difference between the two scores for each metric.
2.
Here are some of the pros and cons of KNN:
Pros:
- Simple: KNN is a simple algorithm that is easy to understand and implement.
- Interpretable: KNN is an interpretable algorithm, which means that it is possible to understand how the model makes its predictions.
- Robust to noise: KNN is relatively robust to noise, which means that it can still perform well even if the data contains some errors.
- Scalable: KNN can be scaled to large datasets without sacrificing accuracy.
Cons:
- Computationally expensive: KNN can be computationally expensive to train, especially for large datasets.
- Sensitive to the distance metric: The performance of KNN can be sensitive to the choice of distance metric.
- Not suitable for all problems: KNN is not suitable for all problems, such as problems where the classes are not well-separated.