Created
July 18, 2020 06:15
-
-
Save amansk2050/5d68592e4872ea2bbc68b6ec60847de9 to your computer and use it in GitHub Desktop.
Knn classification in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
dataset = pd.read_csv("/kaggle/input/iris-flower-dataset/IRIS.csv") | |
dataset.head() | |
Features=["sepal_length","sepal_width","petal_length","petal_width"] | |
X=dataset[Features] | |
y=dataset.species | |
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) | |
from sklearn.preprocessing import StandardScaler | |
scaler= StandardScaler() | |
caler.fit(X_train) | |
X_train = scaler.transform(X_train) | |
X_test = scaler.transform(X_test) | |
#for k=8 | |
from sklearn.neighbors import KNeighborsClassifier | |
classifier = KNeighborsClassifier(n_neighbors = 8) | |
classifier.fit(X_train, y_train) | |
y_pred = classifier.predict(X_test) | |
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score | |
result = confusion_matrix(y_test, y_pred) | |
print("Confusion Matrix:") | |
print(result) | |
result1 = classification_report(y_test, y_pred) | |
print("Classification Report:",) | |
print (result1) | |
result2 = accuracy_score(y_test,y_pred) | |
print("Accuracy:",result2) | |
#for k=5 | |
from sklearn.neighbors import KNeighborsClassifier | |
classifier = KNeighborsClassifier(n_neighbors = 5) | |
classifier.fit(X_train, y_train) | |
y_pred = classifier.predict(X_test) | |
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score | |
result = confusion_matrix(y_test, y_pred) | |
print("Confusion Matrix:") | |
print(result) | |
result1 = classification_report(y_test, y_pred) | |
print("Classification Report:",) | |
print (result1) | |
result2 = accuracy_score(y_test,y_pred) | |
print("Accuracy:",result2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment