Search code examples
pythonnumpyscikit-learnknn

K-Nearest Neighbor in Sci-Kit Learn


I am trying to practice using Sci-Kit Learn to do a K-Nearest Neighbor prediction model using the Iris data set. This is what I have written:

import sklearn
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import seaborn as sns
iris = datasets.load_iris()

X = iris.data
y = iris.target


knn =KNeighborsClassifier(n_neighbors=6)


knn.fit(X, y)

This is my output>>> KNeighborsClassifier(n_neighbors=6)

However, I think I should be getting: KNeighborsClassifer(algorithm = 'auto', leaf_size =30, metric ='minkowski, metric_params=None, n_jobs=1, n_neighbors=6, p=2, weights='uniform')

Also, I tried to predict the target value based on a new array of X values (X_new) as below:

X_new = np.array([[5.6,2.8,3.9,1.1],[5.7,2.6,3.8,1.3],[4.7,3.2,1.3,0.2]])

Pred = knn.predict(X_new)

print(Pred)

However, it didn't provide an output of anything at all. Any assistance/advice would be appreciated!


Solution

  • I think your code works fine considering I ran it on Google Colab (link to the notebook - https://colab.research.google.com/drive/1FROuNe4NMD6D2HCCEtz6TePlCccbGFZm?usp=sharing). Do check this out maybe you try reproducing the error.