Data Science Explorer

Machine Learning : Polynomial Regression 본문

Machine Learning

Machine Learning : Polynomial Regression

grace21110 2023. 11. 17. 18:59
반응형
  • Polynomial Regression 

It uses relationship between the variables x and y to find the best way to draw a line through the data points.

import numpy
import matplotlib.pyplot as plt

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))

myline = numpy.linspace(1, 22, 100)

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

Result 

 

  • R-Squared

The r-squared value ranges from 0 to 1, where 0 means no relationship, and 1 means 100% related.

Example

import numpy
from sklearn.metrics import r2_score

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))

print(r2_score(y, mymodel(x)))

 

  • Predict Future Values 

Example

import numpy
from sklearn.metrics import r2_score

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))

speed = mymodel(17)
print(speed)

Result 

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

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