Iris Flowers Classification ML Project with Code
In this code, we first load the iris dataset using the load_iris()
function from scikit-learn. We then split the data into training and testing sets using the train_test_split()
function.
Next, we scale the features using the StandardScaler()
function from scikit-learn. We then train the KNN model with n_neighbors = 3
using the KNeighborsClassifier()
function.
After training the model, we predict the species of the iris flowers in the testing set using the predict()
function. Finally, we evaluate the performance of the model using the accuracy_score()
, precision_score()
, recall_score()
, and f1_score()
functions from scikit-learn.
Note that the values of the metrics may vary slightly depending on the random seed used for splitting the dataset.
# Import necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the iris dataset
iris = load_iris()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# Scale the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train the KNN model
k = 3
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
# Predict the species of the iris flowers in the testing set
y_pred = knn.predict(X_test)
# Evaluate the performance of the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
print('Accuracy:', accuracy)
print('Precision:', precision)
print('Recall:', recall)
print('F1 Score:', f1)
Machine learning is an application of artificial intelligence (AI) that allows systems to learn and improve from experience without being explicitly programmed. One of the most popular machine learning algorithms is classification, which involves categorizing data into distinct classes based on their features. In this article, we will discuss the Iris Flowers Classification ML Project, which is a classic example of a classification problem.
The Iris Flowers Classification ML Project involves classifying iris flowers into one of three species based on their sepal length, sepal width, petal length, and petal width. The three species are Iris Setosa, Iris Versicolor, and Iris Virginica. This project is widely used in machine learning education and is often the first project that beginners tackle.
The dataset for this project is available in the scikit-learn library, which is a popular machine learning library for Python. The dataset contains 150 samples of iris flowers, with 50 samples from each of the three species. Each sample has four features: sepal length, sepal width, petal length, and petal width. The goal of the project is to create a machine learning model that can accurately predict the species of an iris flower based on its four features.
There are many machine learning algorithms that can be used for classification problems, such as logistic regression, decision trees, and support vector machines. For the Iris Flowers Classification ML Project, we will use the k-nearest neighbors (KNN) algorithm, which is a simple and effective algorithm for classification.
The KNN algorithm works by finding the k closest points to a new data point in the feature space and classifying the new data point based on the majority class of the k neighbors. The value of k is a hyperparameter that needs to be tuned to achieve optimal performance.
To implement the Iris Flowers Classification ML Project, we first need to load the dataset from the scikit-learn library. We then need to split the dataset into training and testing sets. The training set is used to train the KNN model, while the testing set is used to evaluate the performance of the model.
Next, we need to preprocess the data by scaling the features to have zero mean and unit variance. This is important because the KNN algorithm is sensitive to the scale of the features.
After preprocessing the data, we can train the KNN model on the training set. We can then use the model to predict the species of the iris flowers in the testing set. We can evaluate the performance of the model using metrics such as accuracy, precision, recall, and F1 score.
The Iris Flowers Classification ML Project is a great way to learn about machine learning and classification algorithms. It is a classic example of a classification problem and is widely used in machine learning education. By implementing this project, you will gain a deeper understanding of machine learning concepts and techniques.
Comments
Post a Comment