Data Science Explorer

Machine Learning: Linear Regression 본문

Machine Learning

Machine Learning: Linear Regression

grace21110 2023. 11. 15. 21:00
반응형
  • Linear Regression 

It uses the relationship between the data-points to draw a straight line through all them. 

 

Example 

import matplotlib.pyplot as plt
from scipy import stats

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

slope, intercept, r, p, std_err = stats.linregress(x, y)

def myfunc(x):
  return slope * x + intercept

mymodel = list(map(myfunc, x))

plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()

 

Let me kindly explain this code! 

 

Step #1:

slope, intercept, r, p, std_err = stats.linregress(x, y)

The linregress function from scipy.stats is used to perform linear regression on the data. It gives the slope, intercept, correlation coefficient(r), p-value, and standard error of the regression. 

 

Step #2:

def myfunc(x):
    return slope * x + intercept

This defines a linear function (myfunc) based on the slope and intercept obtained from the linear regression. 

 

Step #3: 

mymodel = list(map(myfunc, x))

The map function applies the linear function to each element in the x list, generating predicted y values for the linear regression line.

 

And as the result, you will get to see this graph. 

 

 

  • R for Relationship 

The relationship between x axis and y axis - the coefficient of correlation - is called r. It ranges from -1 to 1, where 0 means on relationship and 1 (and -1) means 100 percent related. 

 

Example 

from scipy import stats
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

slope, intercept, r, p, std_err = stats.linregress(x, y)

print(r)
-0.76

The result shows there is a relationship but not perfect. 

 

  • Predict Future Values 

Example 

Predict the speed of a 5 years old car.

 

from scipy import stats

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

slope, intercept, r, p, std_err = stats.linregress(x, y)

def myfunc(x):
  return slope * x + intercept

speed = myfunc(10)

print(speed)
94.3495217071376

'Machine Learning' 카테고리의 다른 글

Machine Learning: Train/Test  (0) 2023.11.18
Machine Learning : Polynomial Regression  (0) 2023.11.17
Machine Learning: Percentiles  (0) 2023.11.14
Machine Learning : Standard Deviation  (0) 2023.11.13
Mean Median Mode  (0) 2023.11.12