0% found this document useful (0 votes)
30 views13 pages

ML Exp1 C36

The document discusses linear regression, including what it is, how it works, and its applications. Linear regression is a machine learning technique for predicting a continuous output variable from one or more input variables. It finds the line of best fit for the input and output data and can then be used to predict future output values based on new input data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views13 pages

ML Exp1 C36

The document discusses linear regression, including what it is, how it works, and its applications. Linear regression is a machine learning technique for predicting a continuous output variable from one or more input variables. It finds the line of best fit for the input and output data and can then be used to predict future output values based on new input data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Prathmesh Gaikwad

TUS3F202128 C36

PART A
(PART A: TO BE REFFERED BY STUDENTS)

Experiment No. 1
A.1 Aim:
To implement Linear Regression.

A.2 Prerequisite:
Python Basic Concepts

A.3 Outcome:
Students will be able to implement Linear Regression.

A.4 Theory:

Machine Learning, being a subset of Artificial Intelligence (AI), has been playing a dominant role in
our daily lives. Data science engineers and developers working in various domains are widely using
machine learning algorithms to make their tasks simpler and life easier.

What is a Regression Problem?

Majority of the machine learning algorithms fall under the supervised learning
category. It is the process where an algorithm is used to predict a result based on the
previously entered values and the results generated from them. Suppose we have an
input variable ‘x’ and an output variable ‘y’ where y is a function of x (y=f{x}).
Supervised learning reads the value of entered variable ‘x’ and the resulting
variable ‘y’ so that it can use those results to later predict a highly accurate output
data of ‘y’ from the entered value of ‘x’. A regression problem is when the resulting
variable contains a real or a continuous value. It tries to draw the line of best fit
from the data gathered from a number of points.
Prathmesh Gaikwad
TUS3F202128 C36

Linear Regression

Linear regression is a quiet and simple statistical regression method used for
predictive analysis and shows the relationship between the continuous variables.
Linear regression shows the linear relationship between the independent variable (X-
axis) and the dependent variable (Y-axis), consequently called linear regression. If
there is a single input variable (x), such linear regression is called simple linear
regression. And if there is more than one input variable, such linear regression is
called multiple linear regression. The linear regression model gives a sloped straight
line describing the relationship within the variables.

To calculate best-fit line linear regression uses a traditional slope-intercept form.

y= Dependent Variable.
x= Independent Variable.
a0= intercept of the line.
a1 = Linear regression coefficient.

Need of a Linear regression

As mentioned above, Linear regression estimates the relationship between a


dependent variable and an independent variable. Let’s understand this with an easy
example:

Let’s say we want to estimate the salary of an employee based on year of experience.
You have the recent company data, which indicates that the relationship between
Prathmesh Gaikwad
TUS3F202128 C36

experience and salary. Here year of experience is an independent variable, and the
salary of an employee is a dependent variable, as the salary of an employee is
dependent on the experience of an employee. Using this insight, we can predict the
future salary of the employee based on current & past information.

A regression line can be a Positive Linear Relationship or a Negative Linear


Relationship.
Prathmesh Gaikwad
TUS3F202128 C36

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No: C36 Name: Prathmesh Krishna Gaikwad


Class: BE-Comps Batch: C2
Date of Experiment: 18/07/2023 Date of Submission: 18/07/2023
Grade:

B.1 Software Code written by student:


import numpy as np
import matplotlib.pyplot as plt

def estimate_coef(x, y):


# number of observations/points
n = np.size(x)

# mean of x and y vector


m_x = np.mean(x)
m_y = np.mean(y)

# calculating cross-deviation and deviation about x


SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x

# calculating regression coefficients


b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x

return (b_0, b_1)

def plot_regression_line(x, y, b):


Prathmesh Gaikwad
TUS3F202128 C36

# plotting the actual points as scatter plot


plt.scatter(x, y, color = "m",
marker = "o", s = 30)

# predicted response vector


y_pred = b[0] + b[1]*x

# plotting the regression line


plt.plot(x, y_pred, color = "g")
# putting labels
plt.xlabel('x')
plt.ylabel('y')
# function to show plot
plt.show()

def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))

# plotting regression line


plot_regression_line(x, y, b)
if __name__ == "__main__":
main()
Prathmesh Gaikwad
TUS3F202128 C36

B.2 Input and Output:


Prathmesh Gaikwad
TUS3F202128 C36
Prathmesh Gaikwad
TUS3F202128 C36

B.3 Observations and learning:


Linear regression is a data analysis technique that predicts the value of unknown data by
using another related and known data value. It mathematically models the unknown or dependent
variable and the known or independent variable as a linear equation.

B.4 Conclusion:
Hence, we successfully studied & implemented Linear Regression.
B.5 Question of Curiosity (Handwritten any 3)
1. Explain in detail Multivariate Linear Regression.
2. Explain the concepts behind linear regression.
3. Write a short note on linear regression.
Prathmesh Gaikwad
TUS3F202128 C36
Prathmesh Gaikwad
TUS3F202128 C36
Prathmesh Gaikwad
TUS3F202128 C36
Prathmesh Gaikwad
TUS3F202128 C36

4. What is the difference between simple linear regression and multiple linear regression?
Ans:
Simple linear regression:
Simple linear regression has only one x and one y variable.
One independent variable.
Multi collinearity problem cannot exist.
The coefficient of determination is the square of the correlation coefficient between x and y

Multi Linear Regression:


Multiple linear regression has one y and two or more x variables.
More than one independent variable.
Multi collinearity problem can exist.
The coefficient of determination is not simply the square of r.

5. How do you create a linear regression dataset in python?


Ans:
Simple Linear Regression in Python
Step 1: Importing the dataset
Step 2: Data pre-processing
Step 3: Splitting the test and train sets
Step 4: Fitting the linear regression model to the training set
Step 5: Predicting test results
Step 6: Visualizing the test results
Prathmesh Gaikwad
TUS3F202128 C36

6. What are the applications of Linear Regression.


Ans:
Market analysis
You can use a regression model to determine how products perform in the market by establishing
the relationships between several quantitative variables, such as social media engagement, pricing
and number of sales. This information allows you to utilise specific marketing strategies to
maximise sales and increase revenue. For example, you can use a simple linear model to ascertain
how price affects sales and use it to evaluate the strength between the two variables.

Financial analysis
Financial analysts use linear models to evaluate a company's operational performance and forecast
returns on investment. They also use it in the capital asset pricing model, which studies the
relationship between the expected investment returns and the associated market risks. It shows
companies if an investment has a fair price and contributes to decisions on whether or not to invest
in the asset.

Sports analysis
This involves sports analysts using statistics to determine a team's or player's performance in a
game. They can use this information to compare teams and players and provide essential
information to their followers. They can also use this data to predict game attendance based on the
status of the teams playing and the market size, so they can advise team managers on game venues
and ticket prices that can maximise profits.

Environmental health
Specialists in this field use this regression model to evaluate the relationship between natural
elements, such as soil, water and air. An example is the relationship between the amount of water
and plant growth. This can help environmentalists predict the effects of air or water pollution on
environmental health.

Medicine
Medical researchers can use this regression model to determine the relationship between
independent characteristics, such as age and body weight, and dependent ones, such as blood
pressure. This can help reveal the risk factors associated with diseases. They can use this
information to identify high-risk patients and promote healthy lifestyles.

Marks scored by students based on number of hours studied (ideally)- Here marks scored in
exams are independent and the number of hours studied is independent.

Predicting crop yields based on the amount of rainfall- Yield is a dependent variable while the
measure of precipitation is an independent variable.

Predicting the Salary of a person based on years of experience- Therefore, Experience becomes
the independent while Salary turns into the dependent variable.

You might also like