Iris Flower Classification

 

Iris Flower Classification Project using Machine Learning


Machine learning is almost everywhere nowadays. It’s become more and more necessary day by day. From the recommendation of what to buy to recognizing a person, robotics, everywhere is machine learning. So in this project, we’ll create the “Hello World” of machine learning which means Iris flower classification.
Iris flower classification is a very popular machine learning project. The iris dataset contains three classes of flowers, Versicolor, Setosa, Virginica, and each class contains 4 features, ‘Sepal length’, ‘Sepal width’, ‘Petal length’, ‘Petal width’. The aim of the iris flower classification is to predict flowers based on their specific features.

What is machine learning?

Machine learning is about learning to predict something or extracting knowledge from data. ML is a part of artificial intelligence. ML algorithms build a model based on sample data or known as training data and based upon the training data the algorithm can predict something on new data.

Categories of Machine Learning :


Supervised machine learning:  Supervised machine learning are types of machine learning that are trained on well-labeled training data. Labeled data means the training data is already tagged with the correct output.

Unsupervised machine learning: Unlike supervised learning, unsupervised learning doesn’t have any tagged data. It learned patterns from untagged data. Basically, it creates a group of objects based on the input data/features.

Semi-supervised machine learning: Semi-supervised learning falls between supervised and unsupervised learning. It has a small amount of tagged data and a large amount of untagged data.

Applications of Machine Learning:


1. Speech Recognition: Speech recognition uses NLP (Natural Language Processing) to process human speech into written format and vice versa. Some examples are – Google Assistant, Alexa, Siri.

2. Recommendation Engine: Using the past behavior of a human’s search data the recommendation engine can produce new data to cross-sell products to customers. For example – Amazon product recommendations, Spotify music recommendations.

3. Chatbot: Chatbots are used to give customer services without any human agent. It takes questions from users and based on the question it gives an answer as a response.

In this project, we’ll solve the problem using a supervised learning approach. We’ll use an algorithm called “Support vector machine”.

Support vector machine: A support vector machine (also known as a support vector network) is a supervised machine learning algorithm that analyzes data for classification and regression. SVMs are one of the most robust classifications methods.

SVM approximates a separate line (Hyperplane) between two classes.


SVM algorithm finds the points closest to the line from both classes. These points are known as support vectors. Then it computes the distance between the line and support vectors. This distance is called the margin. The main goal is to maximize the margin. The hyperplane which has the maximum margin is known as the optimal hyperplane.




SVM mainly supports binary classification natively. For multiclass classification, It separates the data for binary classification and utilizes the same principle by breaking down multi-classification problems into multiple binary classification problems.


Prerequisites:


1. Numpy- 1.19.3

2. Matplotlib- 3.3.2

3. Seaborn – 0.11.1

4. Pandas – 1.2.4

5. Scikit-learn – 0.24.2

Download Iris Flower Classificatio ProjectCode

Please download the source code of iris flower classification with opencv:

Steps to Classify Iris Flower:

1. Load the data
2. Analyze and visualize the dataset
3. Model training.
4. Model Evaluation.
5. Testing the model.

Step 1 – Load the data:





First, we’ve imported some necessary packages for the project.

  • Numpy will be used for any computational operations.
  • We’ll use Matplotlib and seaborn for data visualization.
  • Pandas help to load data from various sources like local storage, database, excel file, CSV file, etc.
  • Next, we load the data using pd.read_csv() and set the column name as per the iris data information.
  • Pd.read_csv reads CSV files. CSV stands for comma separated value.
  • df.head() only shows the first 5 rows from the data set table.
  • All the numerical values are in centimeters.

Step 2 – Analyze and visualize the dataset:

Let’s see some information about the dataset.


From this description, we can see all the descriptions about the data, like average length and width, minimum value, maximum value, the 25%, 50%, and 75% distribution value, etc.


Let’s visualize the dataset.
  • To visualize the whole dataset we used the seaborn pair plot method. It plots the whole dataset’s information.


  • From this visualization, we can tell that iris-setosa is well separated from the other two flowers.
  • And iris virginica is the longest flower and iris setosa is the shortest.
Now let’s plot the average of each feature of each class.


  • Here we separated the features from the target value.
  • Np.average calculates the average from an array.
  • Here we used two for loops inside a list. This is known as list comprehension.
  • List comprehension helps to reduce the number of lines of code.
  • The Y_Data is a 1D array, but we have 4 features for every 3 classes. So we reshaped Y_Data to a (4, 3) shaped array.
  • Then we change the axis of the reshaped matrix.
  • We used matplotlib to show the averages in a bar plot.
  • Here we can clearly see the verginica is the longest and setosa is the shortest flower.

Step 3 – Model training:


  • Using train_test_split we split the whole data into training and testing datasets. Later we’ll use the testing dataset to check the accuracy of the model.
  • Here we imported a support vector classifier from the scikit-learn support vector machine.
  • Then, we created an object and named it svn.
  • After that, we feed the training dataset into the algorithm by using the svn.fit() method.

Step 4 – Model Evaluation:


  • Now we predict the classes from the test dataset using our trained model.
  • Then we check the accuracy score of the predicted classes.
  • accuracy_score() takes true values and predicted values and returns the percentage of accuracy.

Output:
0.9666666666666667

The accuracy is above 96%.


  • The classification report gives a detailed report of the prediction.
  • Precision defines the ratio of true positives to the sum of true positive and false positives.
  • Recall defines the ratio of true positive to the sum of true positive and false negative.
  • F1-score is the mean of precision and recall value.
  • Support is the number of actual occurrences of the class in the specified dataset.

Step 5 – Testing the model:


  • Here we take some random values based on the average plot to see if the model can predict accurately.

Output:

Prediction of Species: [‘Iris-setosa’ ‘Iris-versicolor’ ‘Iris-virginica’]

It looks like the model is predicting correctly because the setosa is shortest and virginica is the longest and versicolor is in between these two.

  • We can save the model using pickle format.
  • And again we can load the model in any other program using pickle and use it using model.predict to predict the iris data.

Summary:

In this project, we learned to train our own supervised machine learning model using Iris Flower Classification Project with Machine Learning. Through this project, we learned about machine learning, data analysis, data visualization, model creation, etc.

project code :project code

JUPYTER NOTEBOOK: Notebook


Comments