matplotlib 多项式回归曲线

1bqhqjot  于 2023-10-24  发布在  其他
关注(0)|答案(3)|浏览(107)

我试图为我的数据创建一个2度的回归曲线。当我创建我的图表时,我得到了一个有趣的锯齿形东西:

但我想把我的数据建模成一条实际的曲线,看起来就像散点图的连接版本。

有什么建议/更好的方法吗?

  1. degree = 2
  2. p = np.poly1d(np.polyfit(data['input'],y, degree))
  3. plt.plot(data['input'], p(data['input']), c='r',linestyle='-')
  4. plt.scatter(data['input'], p(data['input']), c='b')

在这里,数据[['input']是与y具有相同维度的列向量。
编辑:我也试过这样做:

  1. X, y = np.array(data['input']).reshape(-1,1), np.array(data['output'])
  2. lin_reg=LinearRegression(fit_intercept=False)
  3. lin_reg.fit(X,y)
  4. poly_reg=PolynomialFeatures(degree=2)
  5. X_poly=poly_reg.fit_transform(X)
  6. poly_reg.fit(X_poly,y)
  7. lin_reg2=LinearRegression(fit_intercept=False)
  8. lin_reg2.fit(X_poly,y)
  9. X_grid=np.arange(min(X),max(X),0.1)
  10. X_grid=X_grid.reshape((len(X_grid),1))
  11. plt.scatter(X,y,color='red')
  12. plt.plot(X,lin_reg2.predict(poly_reg.fit_transform(X)),color='blue')
  13. plt.show()

这给了我这个图表。

散点图是我的数据,蓝色之字形是一条二次曲线,用来模拟数据。帮助?

4xy9mtcn

4xy9mtcn1#

在你的图中,你只需要用直线从一点到另一点进行绘制(其中y值是来自polyfit函数的近似y)。
我会跳过polyfit函数(因为你有你感兴趣的所有y值),只需要用B样条函数scipy插值data['input']y,然后用你感兴趣的x范围绘制新的y值。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.interpolate as interp

仅从点到点绘制(锯齿形)

  1. x = np.array([1, 2, 3, 4])
  2. y = np.array([75, 0, 25, 100])
  3. plt.plot(x, y)

插值点

  1. x_new = np.linspace(1, 4, 300)
  2. a_BSpline = interp.make_interp_spline(x, y)
  3. y_new = a_BSpline(x_new)
  4. plt.plot(x_new, y_new)

试试这个,然后用你的数据调整!:)

展开查看全部
svmlkihl

svmlkihl2#

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.linear_model import LinearRegression
  7. from sklearn.preprocessing import PolynomialFeatures
  8. #improve degree = 3
  9. p_reg = PolynomialFeatures(degree = 3)
  10. X_poly = p_reg.fit_transform(X)
  11. #again create new linear regression obj
  12. reg2 = LinearRegression()
  13. reg2.fit(X_poly,y)
  14. plt.scatter(X, y, color = 'b')
  15. plt.xlabel('Level')
  16. plt.ylabel('Salary')
  17. plt.title("Truth or Bluff")
  18. # predicted values
  19. plt.plot(X, reg2.predict(X_poly), color='r')
  20. plt.show()

With Degree 3
With Degree 4

展开查看全部
agxfikkp

agxfikkp3#

因为你没有给我们看你的数据,我假设你的自变量不是单调的。
下面是我的代码
1.它计算两个数据向量,其中 y = y(x)x 不是单调的
1.它计算一个拟合数据的二次多项式
1.它在上图的左侧绘制了 x与y 的散点图和 x与y=p(x) 的线图,其中 y 是根据最佳拟合多项式计算的
1.它排序 x → xs
1.在上图的右边部分,它再次绘制了相同的散点图和 xs vs y=p(xs) 的线图。
.

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. np.random.seed(0)
  4. # x is not monotonic
  5. x = np.random.random(60)
  6. # y is quadratic with a quartic "error"
  7. y = 0.3+1.2*x-1.4*(x-0.5)**2 - x**4/3
  8. # find the 2nd degree poly1d object that best fits data
  9. p = np.poly1d(np.polyfit(x, y, 2))
  10. fig, (ax0, ax1) = plt.subplots(1,2,figsize=(8,4),layout='constrained')
  11. s=6 ; lw=0.75
  12. # what you did?
  13. ax0.scatter(x, y, c='k', s=s)
  14. ax0.plot(x, p(x), lw=lw)
  15. # what you want?
  16. xs = np.sort(x)
  17. ax1.scatter(x, y, c='k', s=s)
  18. ax1.plot(xs, p(xs), lw=lw)
  19. #
  20. plt.show()
展开查看全部

相关问题