You are on page 1of 9

Introduction to ML

Linear Regression

• Linear regression:
• Linear regression is used for finding linear relationship between target and one or more predictors (inputs
or independent variable) . There are two types of linear regression- Simple and Multiple.
• Simple Linear Regression:
• Simple linear regression is useful for finding relationship between two continuous variables. One is
predictor or independent variable and other is response or dependent variable. It looks for statistical
relationship but not deterministic relationship. Relationship between two variables is said to be
deterministic if one variable can be accurately expressed by the other. For example, using temperature in
degree Celsius it is possible to accurately predict Fahrenheit. Statistical relationship is not accurate in
determining relationship between two variables. For example, relationship between height and weight.
• The core idea is to obtain a line that best fits the data. The best fit line is the one for which total prediction
error (all data points) are as small as possible. Error is the distance between the point to the regression
line.
Introduction to ML

Linear Regression

• Regression is a data science task of predicting the value of target (numerical values) by building a model
based on one or more predictors (numerical and categorical variables).

• 1. Frequency Table
» Decision Tree
• 2. Covariance Matrix
» Multiple Linear Regression
• 3. Similarity Function
» K Nearest Neighbors
• 4. Others
» Artificial Neural Network and Support Vector Machine
Introduction to ML

Linear Regression

Linear Regression Use cases-


Some Uses of Linear Regression are:
• Sales of a product: pricing, performance, and risk parameters
•Generating insights on consumer behavior, profitability, and other business factors
•Evaluation of trends; making estimates, and forecasts
•Determining marketing effectiveness, pricing, and promotions on sales of a product
•Assessment of risk in financial services and insurance domain
•Studying engine performance from test data in automobiles
•Calculating causal relationships between parameters in biological systems
•Conducting market research studies and customer survey results analysis
Introduction to ML

Linear Regression Using Scikit Learn (sklearn)

• We know that the equation of a line is given by y=mx+b, where m is the slope and b is the intercept. Our
goal is to find the best values of slope (m) and intercept (b) to fit our data. Linear regression uses the
ordinary least squares method to fit our data points. Import Statement;
• 1. from sklearn import linear_model
• We have the height and weight data of some people. Let's use this data to do linear regression and try to
predict the weight of other people.
height=[[4.0],[4.5],[5.0],[5.2],[5.4],[5.8],[6.1],[6.2],[6.4],[6.8]]
weight=[ 42 , 44 , 49, 55 , 53 , 58 , 60 , 64 , 66 , 69]
print("height weight")
for row in zip(height, weight):
print(row[0][0],"->",row[1])
Introduction to ML

Linear Regression Using Scikit Learn (sklearn)

• Import statement to plot graph using matplotlib:

import matplotlib.pyplot as plt

Plotting the height and weight data:

plt.scatter(height,weight,color='black')

plt.xlabel("height")
plt.ylabel("weight")

Output:
Introduction to ML

Linear Regression Using Scikit Learn (sklearn)


Introduction to ML

Linear Regression Using Scikit Learn (sklearn)

• Declaring the linear regression function and call the fit method to learn from data:

reg=linear_model.LinearRegression()
reg.fit(height,weight)
• Slope and intercept:

m=reg.coef_[0]

b=reg.intercept_
print("slope=",m, "intercept=",b)
• Output:
• …….
Introduction to ML

Linear Regression Using Scikit Learn (sklearn)

plt.scatter(height,weight,color='black')

predicted_values = [reg.coef_ * i + reg.intercept_ for i in height]


plt.plot(height, predicted_values, 'b')
plt.xlabel("height")
plt.ylabel("weight")

Output:
Introduction to ML

Linear Regression Using Scikit Learn (sklearn)

You might also like