Python Machine Learning Masterclass: K-Neighbors Project on the Iris Dataset – Part 5

Posted by


Welcome to part five of the Python Machine Learning Masterclass series on the K-Neighbors project using the Iris dataset. In this tutorial, we will continue our work with the K-Neighbors algorithm to classify the Iris dataset. We will also perform model evaluation to determine the effectiveness of our model.

To get started, make sure you have the necessary libraries installed. If you haven’t already installed scikit-learn, matplotlib, and numpy, you can do so by running the following commands in your terminal:

pip install scikit-learn
pip install matplotlib
pip install numpy

Now, let’s load the Iris dataset and split it into training and testing sets. We will use 70% of the data for training and 30% for testing.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

Next, let’s train our K-Neighbors model with the training data. We will use three neighbors for this example.

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)

Now that we have trained our model, let’s make predictions on the test data and evaluate its performance.

y_pred = knn.predict(X_test)

To evaluate the performance of our model, we can calculate the accuracy, precision, recall, and F1 score.

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')

print(f"Accuracy: {accuracy}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")

You can also visualize the performance of our model by plotting a confusion matrix.

from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='g')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

Lastly, we can tune our model by experimenting with different values for the number of neighbors and other hyperparameters. You can also try different algorithms or preprocessing techniques to improve the performance of the model.

That’s it for part five of the Python Machine Learning Masterclass on the K-Neighbors project using the Iris dataset. I hope you found this tutorial helpful and that you are now able to build and evaluate machine learning models with the K-Neighbors algorithm. Stay tuned for more tutorials in this series!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x