Skip to content

Instantly share code, notes, and snippets.

@kurianbenoy
Last active May 25, 2018 03:13
Show Gist options
  • Select an option

  • Save kurianbenoy/36f0f8b653177c3dd224f80d954254e3 to your computer and use it in GitHub Desktop.

Select an option

Save kurianbenoy/36f0f8b653177c3dd224f80d954254e3 to your computer and use it in GitHub Desktop.
knn_iris.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
knn = KNeighborsClassifier(n_neighbors=20)
knn.fit(X_train,y_train)
pred = knn.predict(X_test)
print(pred)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, pred)
print(cm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment