Which is the simplest way to make a polynomial regression with sklearn?

后端 未结 2 793
渐次进展
渐次进展 2020-12-21 18:07

I have some data that doesn\'t fit a linear regression,

In fact should fit \'exactly\' a quadratic function:

P = R*I**2 

2条回答
  •  囚心锁ツ
    2020-12-21 19:14

    You can use numpy's polyfit.

    import numpy as np
    from matplotlib import pyplot as plt
    X = np.linspace(0, 100, 50)
    Y = 23.24 + 2.2*X + 0.24*(X**2) + 10*np.random.randn(50) #added some noise
    coefs = np.polyfit(X, Y, 2)
    print(coefs)
    p = np.poly1d(coefs)
    plt.plot(X, Y, "bo", markersize= 2)
    plt.plot(X, p(X), "r-") #p(X) evaluates the polynomial at X
    plt.show()
    

    Out:

    [  0.24052058   2.1426103   25.59437789]
    

提交回复
热议问题