In this article of Iris Flowers Classification, we will be dealing with Logistic Regression Machine Learning Algorithm. First, we will see logistic Regression, and then we will understand the working of an algorithm with the Iris flowers dataset. We all know about Iris Dataset, and it contains features of different flower species. Independent features in this dataset are Sepal Length, Sepal Width, Petal Length, and Petal Width. All these lengths were in centimeters. And Dependent feature, which will be the output for the model, is Species. It contains the name of the species to which that particular flower with those measurements belongs.
This Iris dataset is the first dataset that any data science student work on.
Before going into creating a machine learning model, let us understand Logistic Regression first.
Logistic Regression
Logistic Regression is a supervised machine learning model used mainly for categorical data, and it is a classification algorithm. Seeing the name logistic regression, you may think it will be a regression algorithm. But the fact is that it is a classification algorithm, and it is a generalization of the linear regression model.
Logistic Regression is used to find the relationship between dependent and independent variables. This is done by using a logistic regression equation. This is a very easy to implement, understand, and also easy method to train the model.
To understand it more, think of an example of your email. You will be getting many emails, and in them, some are spam. Using this logistic Regression, we can find whether the mail is spam or ham. It will classify the emails and label them as spam or ham, like 0 or 1.
The logistic Regression model will take the mail content as input, and then it will analyze it and finally generate a label. If it is spam, it will give 1 as spam, and if it is a ham, then it will give 0, saying that it is not spam.
Working with Dataset
Before creating the model and training it, we have to preprocess the dataset. Preprocessing means converting the dataset into an understandable format before using it for any machine learning algorithms. It includes data transformation, data reduction, data cleaning, and many more.
Let us build a machine learning model using logistic Regression. For this, we will take the iris flowers dataset.
Let us start by importing some important basic libraries.
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.simplefilter("ignore")
matplotlib and seaborn are used for visualizations and warnings; we can ignore all the warnings we encounter.
Import the dataset from your local desktop. Use pandas for it. Enter the path to the dataset file in the read_csv method. It will import the iris dataset.
#Import iris dataset df=pd.read_csv(r'C:UsersAdminDownloadsIris.csv')
Let us view the data frame.
df
df.info()
If there are any missing values, then modify them before using the dataset. For modifying you can use the fillna() method. It will fill null values.
#checking for null values df.isnull().sum()
We can see that all values are 0. It means that there are no null values over the entire data frame.
To view the column names in the data frame, use columns
df.columns
Index(['Id', 'SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species'], dtype='object')
View the statistical description of the dataset.
It contains variables like count, mean, standard deviation, minimum value, maximum value, and percentiles of all the columns such as Id, Sepal length, sepal width, petal length, and petal width. Use describe() method to view it.