ORIGINAL DATA
|
|
train_test_split()
|
┌─────────┴─────────┐
↓ ↓
TRAINING DATA TESTING DATA
| |
┌─────┴─────┐ ┌─────┴─────┐
↓ ↓ ↓ ↓
X_train y_train X_test y_test
| | | |
Inputs Answers Inputs Actual Answers
| | |
└─────┬─────┘ |
↓ ↓
TRAIN PREDICT
MODEL |
| ↓
| y_pred
| |
└─────────────┘
↓
Compare
y_pred vs y_test
↓
Accuracy
Before running the program, install scikit-learn if it is not available:
!pip install scikit-learn
# Import required libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
| Function | Purpose |
|---|---|
| train_test_split( ) | Divides data into training and testing sets |
| KNeighborsClassifier( ) | Implements k-NN algorithm |
| accuracy_score( ) | Calculates model accuracy |
The IRIS dataset contains information about flowers. Each flower has four features: Sepal length , Sepal width , Petal length , Petal width
The flowers belong to three classes:
iris = load_iris()
X_iris_features = iris.data
y_iris_labels = iris.target
X_iris_features
y_iris_labels # 0- Iris Setosa 1- Iris Versicolor 2-Iris Virginica
# Names of flower classes
class_names = iris.target_names
class_names
# Split the dataset into training and testing data
# 80% data is used for training
# 20% data is used for testing
X_train, X_test, y_train, y_test = train_test_split(
X_iris_features,
y_iris_labels,
test_size=0.2,
random_state=42
)
### X_train Training input data
### y_train Training output/target data
### X_test Testing input data
### y_test contains the actual correct answers for X_test
# Here k = 3 means the algorithm checks the
# 3 nearest neighbours to classify a new data point
knn = KNeighborsClassifier(n_neighbors=3)
New Flower
Nearest neighbours:
Majority = Setosa
Prediction = Setosa
The algorithm studies the training examples and learns the relationship between:
Flower measurements → Flower type
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
print("----- Prediction Results -----")
for i in range(len(y_test)):
actual = class_names[y_test[i]]
predicted = class_names[y_pred[i]]
print("\nTest Data:", X_test[i])
print("Actual Class :", actual)
print("Predicted Class:", predicted)
if actual == predicted:
print("Result: Correct Prediction")
else:
print("Result: Wrong Prediction")
Accuracy = (Correct Predictions / Total Predictions ) * 100
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of k-NN Model:", accuracy * 100, "%")
print("Dimensions of y_test:", y_test.shape)
print("Dimensions of y_pred:", y_pred.shape)
print(y_test)
print(y_pred)
cm = confusion_matrix(
y_test,
y_pred
)
print(cm)
| Predicted | |||
|---|---|---|---|
| Setosa | Versicolor | Virginica | |
| Actual | |||
| Setosa | 10 | 0 | 0 |
| Versicolor | 0 | 9 | 0 |
| Virginica | 0 | 0 | 11 |
# Import required libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Load the IRIS dataset
iris = load_iris()
# Features of flowers
X = iris.data
# Target classes (species)
y = iris.target
# Names of flower classes
class_names = iris.target_names
# Split the dataset into training and testing data
# 80% data is used for training
# 20% data is used for testing
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
# Create k-NN classifier
# Here k = 3 means the algorithm checks the
# 3 nearest neighbours to classify a new data point
knn = KNeighborsClassifier(n_neighbors=3)
# Train the model
knn.fit(X_train, y_train)
# Make predictions using test data
y_pred = knn.predict(X_test)
# Display correct and wrong predictions
print("----- Prediction Results -----")
for i in range(len(y_test)):
actual = class_names[y_test[i]]
predicted = class_names[y_pred[i]]
print("\nTest Data:", X_test[i])
print("Actual Class :", actual)
print("Predicted Class:", predicted)
if actual == predicted:
print("Result: Correct Prediction")
else:
print("Result: Wrong Prediction")
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("\n------------------------------")
print("Accuracy of k-NN Model:", accuracy * 100, "%")
cm = confusion_matrix(
y_test,
y_pred
)
print(cm)