Lab#10 Ai
Lab#10 Ai
Lab#10 Ai
Objective
A) Write a python program to implement Simple Linear Regression and plot
the graph.
B) Implementation of Logistic Regression for iris using sklearn.
Theory
Simple Linear Regression is a statistical method that is used to model the linear relationship
between a dependent variable (often denoted as y) and an independent variable (often
denoted as x). The goal of Simple Linear Regression is to find the best-fitting straight line
through the data that minimizes the sum of the squared residuals (i.e., the differences
between the predicted values and the actual values of y).
The equation for a simple linear regression model can be written as:
y = β0 + β1x + ε
where:
y is the dependent variable (the one being predicted)
x is the independent variable (the one used to make the prediction)
β0 is the y-intercept (the value of y when x = 0)
β1 is the slope of the line (the amount that y changes for each unit increase in x)
ε is the error term (the part of y that is not explained by the linear relationship with
x)
The goal of Simple Linear Regression is to estimate the values of β0 and β1 that result in
the best-fitting line through the data. This is typically done using the method of least
squares, which involves minimizing the sum of the squared residuals between the predicted
values and the actual values of y.
Once the parameters β0 and β1 have been estimated, the regression line can be used to
predict the value of y for any given value of x. The quality of the regression model can be
assessed using various metrics, such as the coefficient of determination (R-squared), which
measures the proportion of the variation in y that is explained by the linear relationship with
x.
Simple Linear Regression is a powerful tool for modeling and predicting the behavior of a
single dependent variable based on a single independent variable. It is widely used in a
variety of fields, including economics, finance, marketing, and engineering, among others.
Code
# Importing necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
Logistic Regression is a statistical method used for classification problems where the response
variable is categorical. It estimates the probability of an event occurring (such as the probability
of a flower being a particular species) based on one or more predictor variables (such as the petal
length and width of a flower). Logistic Regression is often used in machine learning and data
analysis applications to build predictive models.
The iris dataset is a well-known dataset that contains measurements for three species of iris
flowers: setosa, versicolor, and virginica. It consists of 150 observations, with 50 observations
for each species. Each observation includes measurements of the sepal length, sepal width, petal
length, and petal width of the flower.
To implement Logistic Regression for the iris dataset using scikit-learn, we first need to load the
dataset using the load_iris function. This function returns an object containing the data and
target variables. We can split the dataset into training and testing sets using the train_test_split
function.
Next, we create a Logistic Regression model using the LogisticRegression class from scikit-
learn. We fit the model to the training data using the fit method. Once the model is trained, we
can use it to predict the classes of the testing data using the predict method.
Finally, we can evaluate the performance of the model by comparing the predicted classes to the
true classes using a metric such as accuracy. In this implementation, we are using the
accuracy_score function from scikit-learn to calculate the accuracy of the model.
Logistic Regression is a powerful tool for building predictive models for classification problems,
and the iris dataset is a common example used to demonstrate its use in machine learning
applications. The implementation using scikit-learn is straightforward and can be easily adapted
to other classification problems.
Code
# Importing necessary libraries
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
In this program, we are using the iris dataset which is included in scikit-learn. We are splitting
the dataset into training and testing sets using the train_test_split function from scikit-learn. We
are creating a Logistic Regression model, fitting it to the training data, and predicting the classes
for the testing data using the predict method. We are then calculating the accuracy of the model
using the accuracy_score function from scikit-learn.
Note that in this example, we are using all the features in the iris dataset to train the model. You
can modify the code to select only a subset of features if desired.
Exercise:
B. Create a Python program to implement Logistic Regression on the iris dataset using
scikit-learn and print the confusion matrix.