Challenge

Supervised Learning - Classification Week 8 Challenge

2. KNN is simple algorithm and easy to implement

other than n_neighbours hyperparameter it doesnt require any parameters to work with dataset

Model doesnt require training step

Can be used for both classification and regression problem

If the size of dataset grows efficieny reduces significantly

Small K value sensitive to Noise points

Larger k value works well but might include points from other classed

Imbalace of data is major concern in KNN, for instance if training dataset has more category A data out of A and B, then the result wil prone more to Category A

1.

import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

df = pd.read_csv('faults.csv')
X1 = df

y_Output = df[["Pastry","Z_Scratch","K_Scatch","Stains","Dirtiness","Bumps","Other_Faults"]]
x_Input = X1.drop(['Pastry','Z_Scratch','K_Scatch','Stains','Dirtiness','Bumps','Other_Faults'],axis='columns')

y = []
for i in range(y_output.shape[0]):
    if y_output["Pastry"].values[i] == 1:
        y.append(0)
    elif y_output["Z_Scratch"].values[i] == 1:
        y.append(1)
    elif y_output["K_Scatch"].values[i] == 1:
        y.append(2)
    elif y_output["Stains"].values[i] == 1:
        y.append(3)
    elif y_output["Dirtiness"].values[i] == 1:
        y.append(4)
    elif y_output["Bumps"].values[i] == 1:
        y.append(5)
    else:
        y.append(6)
        
y_op_1d=np.array(y)

X_train, X_test, y_train, y_test = train_test_split(x_Input,y_op_1d, test_size=0.2, random_state=1)

knn = KNeighborsClassifier(n_neighbors=10)
knn.fit(X_train, y_train)
knn.predict(X_test)[:10]
knn.score(X_test, y_test)
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.