-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml7.py
More file actions
25 lines (20 loc) · 797 Bytes
/
ml7.py
File metadata and controls
25 lines (20 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_iris
# Importing iris dataset from sklearn and splitting input and output
iris = load_iris()
X = iris.data
y = iris.target
# Splitting the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Creating and training the KNN model
knn_model = KNeighborsClassifier(n_neighbors=3)
knn_model.fit(X_train, y_train)
# Making predictions on the test set
y_pred = knn_model.predict(X_test)
# Calculating and printing the accuracy
acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)