# -*- coding: utf-8 -*-
"""Knn sklearn.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1TGRllLvnGkfEqdZ6pQSh5QMJsAzyysm6
"""

########################################################IRIS  DATASET ###############################
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
############################### USING SKLEARN Loading dataset
iris=datasets.load_iris()
####################################### features and labels
x=iris.data
y=iris.target


################################################################## USE iris.DESCR to know about iris datasets

import sklearn.model_selection as model_selection
xtrain,xtest,ytrain,ytest=model_selection.train_test_split(x,y,test_size=0.2,random_state=4)

krange=range(1,10)
scores={}
s_list=[]
for k in krange:
  clf=KNeighborsClassifier(n_neighbors=k)
  clf.fit(xtrain,ytrain)
  ypred=clf.predict(xtest)
  scores[k]=accuracy_score(ytest,ypred)
  s_list.append(accuracy_score(ytest,ypred))

import matplotlib.pyplot as plt
plt.plot(krange,s_list)
plt.xlabel('value of k for knn')
plt.ylabel('Testing accuracy')

knn=KNeighborsClassifier()
knn.fit(x,y)
classes={0:'setosa',1:'versicolor',2:'virginica'}
x_new=[[3,4,5,2],[5,4,2,2]]
y_predict=knn.predict(x_new)
print(classes[y_predict[0]])
print(classes[y_predict[1]])


af7a1e352ad9ec7a23c11f009dee11ad21cd0049
<mask token>

<mask token>
for k in krange:
    clf = KNeighborsClassifier(n_neighbors=k)
    clf.fit(xtrain, ytrain)
    ypred = clf.predict(xtest)
    scores[k] = accuracy_score(ytest, ypred)
    s_list.append(accuracy_score(ytest, ypred))
<mask token>
plt.plot(krange, s_list)
plt.xlabel('value of k for knn')
plt.ylabel('Testing accuracy')
<mask token>
knn.fit(x, y)
<mask token>
print(classes[y_predict[0]])
print(classes[y_predict[1]])

<mask token>
iris = datasets.load_iris()
x = iris.data
y = iris.target
<mask token>
xtrain, xtest, ytrain, ytest = model_selection.train_test_split(x, y,
    test_size=0.2, random_state=4)
krange = range(1, 10)
scores = {}
s_list = []
for k in krange:
    clf = KNeighborsClassifier(n_neighbors=k)
    clf.fit(xtrain, ytrain)
    ypred = clf.predict(xtest)
    scores[k] = accuracy_score(ytest, ypred)
    s_list.append(accuracy_score(ytest, ypred))
<mask token>
plt.plot(krange, s_list)
plt.xlabel('value of k for knn')
plt.ylabel('Testing accuracy')
knn = KNeighborsClassifier()
knn.fit(x, y)
classes = {(0): 'setosa', (1): 'versicolor', (2): 'virginica'}
x_new = [[3, 4, 5, 2], [5, 4, 2, 2]]
y_predict = knn.predict(x_new)
print(classes[y_predict[0]])
print(classes[y_predict[1]])

<mask token>
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
iris = datasets.load_iris()
x = iris.data
y = iris.target
import sklearn.model_selection as model_selection
xtrain, xtest, ytrain, ytest = model_selection.train_test_split(x, y,
    test_size=0.2, random_state=4)
krange = range(1, 10)
scores = {}
s_list = []
for k in krange:
    clf = KNeighborsClassifier(n_neighbors=k)
    clf.fit(xtrain, ytrain)
    ypred = clf.predict(xtest)
    scores[k] = accuracy_score(ytest, ypred)
    s_list.append(accuracy_score(ytest, ypred))
import matplotlib.pyplot as plt
plt.plot(krange, s_list)
plt.xlabel('value of k for knn')
plt.ylabel('Testing accuracy')
knn = KNeighborsClassifier()
knn.fit(x, y)
classes = {(0): 'setosa', (1): 'versicolor', (2): 'virginica'}
x_new = [[3, 4, 5, 2], [5, 4, 2, 2]]
y_predict = knn.predict(x_new)
print(classes[y_predict[0]])
print(classes[y_predict[1]])

# -*- coding: utf-8 -*-
"""Knn sklearn.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1TGRllLvnGkfEqdZ6pQSh5QMJsAzyysm6
"""

########################################################IRIS  DATASET ###############################
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
############################### USING SKLEARN Loading dataset
iris=datasets.load_iris()
####################################### features and labels
x=iris.data
y=iris.target


################################################################## USE iris.DESCR to know about iris datasets

import sklearn.model_selection as model_selection
xtrain,xtest,ytrain,ytest=model_selection.train_test_split(x,y,test_size=0.2,random_state=4)

krange=range(1,10)
scores={}
s_list=[]
for k in krange:
  clf=KNeighborsClassifier(n_neighbors=k)
  clf.fit(xtrain,ytrain)
  ypred=clf.predict(xtest)
  scores[k]=accuracy_score(ytest,ypred)
  s_list.append(accuracy_score(ytest,ypred))

import matplotlib.pyplot as plt
plt.plot(krange,s_list)
plt.xlabel('value of k for knn')
plt.ylabel('Testing accuracy')

knn=KNeighborsClassifier()
knn.fit(x,y)
classes={0:'setosa',1:'versicolor',2:'virginica'}
x_new=[[3,4,5,2],[5,4,2,2]]
y_predict=knn.predict(x_new)
print(classes[y_predict[0]])
print(classes[y_predict[1]])

