Logistic Regression on iris dataset

0

In this article we will see how can we implement the Logistic Regression model on iris dataset . As we know that Logistic Regression is a mostly used machine learning algorithm for discrete classes. Suppose you have given the data about the flowers and you have to find which category the flower belongs then you can use this algorithm.

So today we are going to implement this Logistic Regression on iris dataset.

1. First of all we will import all the necessary libraries for this.


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

2. Now we create a variable to assign the data.


data=load_iris()

3. Now we create 2 other variables for training purpose X which has all the input values and Y which has target data.


X=data.data
y=data.target

4. Now we will be splitting our data in four parts (X_train,X_test,Y_train,Y_test)


x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)

5. Now we will import Logistic Regression from Sklearn library and create a variable model and assign value of it.


#importing the logisticRegression
from sklearn.linear_model import LogisticRegression
model=LogisticRegression()

6. Now we will fit our model with the data. In this process our model is trained.


model.fit(x_train,y_train)

7. Now we can use our test data and predict the values. FOr that we will create a variable y_pred and our model will mae prediction for each tuple of x_test.


y_pred=model.predict(x_test)

8. To calculate the accuracy we will import accuracy score from sklearn library.


from sklearn.metrics import accuracy_score
acc=accuracy_score(y_test,y_pred)

9. We will print the accuracy.


print(acc)

So in this way we can implement the logistic regression using iris dataset.













Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !