Supervised Learning - Classification Week 8 Challenge
2.
|
PROS |
CONS |
|
It is pretty intuitive and simple |
K-NN slow algorithm |
|
KNN has no assumptions |
KNN can handle large no of input variables |
|
KNN has No Training Step |
Imbalanced data causes problems: |
|
Can be used both for Classification and Regression |
- Apply knn to the “Surface defects in stainless steel plates” and identify the differences
syntax for KNN
from sklearn.neighbors import KNeighborsClassifier
KNN = KNeighborsClassifier(n_neighbors = 40)
Now to run the same code by replacing SVM with KNN.
from sklearn import datasets, linear_model
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
# Read the dataset
df = pd.read_csv('faults.csv')
df.drop('TypeOfSteel_A400', axis='columns', inplace=True)
df.head()
print(df.info())
print(df.head(5))
# Seperating the values seperatly of the faulty ones from the good ones
X = df.iloc[:, :26].values
y = df.iloc[:, -1].values
#Fault columns
y1 = df.iloc[:, 26].values
y2 = df.iloc[:, 27].values
y3 = df.iloc[:, 28].values
y4 = df.iloc[:, 29].values
y5 = df.iloc[:, 30].values
y6 = df.iloc[:, 31].values
y7 = df.iloc[:, 32].values
# Assigning output for returning for the faulty ones
for n in range(len(y)):
if y1[n] == 1:
y[n] = 1
elif y2[n] == 1:
y[n] = 2
elif y3[n] == 1:
y[n] = 3
elif y4[n] == 1:
y[n] = 4
elif y5[n] == 1:
y[n] = 5
elif y6[n] == 1:
y[n] = 6
elif y7[n] == 1:
y[n] = 7
else :
y[n] = 0
print(y)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 4)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.neighbors import KNeighborsClassifier
KNN = KNeighborsClassifier(n_neighbors = 5)
KNN.fit(X_train,y_train)
y_pred = KNN.predict(X_test)
print("The score is: ", KNN.score(X_test, y_test))
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
import seaborn as sn
plt.figure(figsize=(10,7))
sn.heatmap(cm, annot = True)
plt.xlabel('predicted')
plt.ylabel('actual')
plt.show()
print("The score is: ", KNN.score(x_test, y_test))
print('accuracy_score')
The Output
The accuracy score is

The heatmap is

Comparing with SVM
The accuracy score is

The heatmap is

So based on the results both are having differences with their results with SVM having a slightly higher accuracy percentage.