DM - Lab Task 9 [0052]
DM - Lab Task 9 [0052]
DM - Lab Task 9 [0052]
1. Import Modules
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import random
2. Loading Dataset
data_iris = load_iris()
label_target = data_iris.target_names
4. Splitting Data
X = data_iris.data
y = data_iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
try:
nn = int(input("Enter number of neighbors: "))
knn = KNeighborsClassifier(nn)
# KNN Model
knn.fit(X_train, y_train)
# Model Evaluation
print("The Score is:", knn.score(X_test, y_test))
prediction = knn.predict([test_data])
print("Predicted output is:", label_target[prediction[0]])
except Exception as e:
print("Please supply valid input......", str(e))