Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DM - Lab Task 9 [0052]

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Lab Task 9

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

3. Displaying Sample Data

print("Sample Data from Iris Dataset")


print("*" * 30)
for _ in range(10):
rn = random.randint(0, 120)
print(data_iris.data[rn], "===>", label_target[data_iris.target[rn]])

Sample Data from Iris Dataset


******************************
[5.1 3.3 1.7 0.5] ===> setosa
[4.6 3.1 1.5 0.2] ===> setosa
[5.4 3.4 1.7 0.2] ===> setosa
[5.5 2.6 4.4 1.2] ===> versicolor
[5.1 3.8 1.5 0.3] ===> setosa
[5.6 2.7 4.2 1.3] ===> versicolor
[6.3 2.5 4.9 1.5] ===> versicolor
[5.8 2.7 5.1 1.9] ===> virginica
[5.6 2.5 3.9 1.1] ===> versicolor
[5.1 3.5 1.4 0.3] ===> setosa

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)

print("The Training dataset length: ", len(X_train))


print("The Testing dataset length: ", len(X_test))

The Training dataset length: 105


The Testing dataset length: 45

5. KNN Model & Model Evaluation & User Input

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))

test_data = input("Enter Test Data (comma-separated): ").split(",")


test_data = [float(value) for value in test_data]

prediction = knn.predict([test_data])
print("Predicted output is:", label_target[prediction[0]])

except Exception as e:
print("Please supply valid input......", str(e))

The Score is: 0.9777777777777777


Predicted output is: setosa
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like