我构建了一个具有两列X和Y的数据集,并对该数据使用了1、2和7阶的拟合模型(如下代码所示)。
我想创建一个图,使得只有回归线出现,而原始数据作为黑色散点出现,但是当我绘制每个拟合模型时,它同时包含回归线和原始数据。指定的颜色。这意味着原始数据散点不会变成黑色,并且图例非常混乱。我该如何解决?
此外,我想将每次拟合的预测值(1、2和7阶)添加为训练DataFrame中的其他列。最有效的方法是什么?
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
X = np.linspace(0, 18, num=10)
Y = (0.05*X**2)+4*np.sin(X)+1.15*X+2*np.random.randn()
combi = {'X':X, 'Y':Y}
train = pd.DataFrame(combi)
print(train)
linear = np.polyfit(X, Y, 1)
linear_fn = np.poly1d(linear)
#linear_fn is now a function which takes in X and returns an estimate for Y
quadratic = np.polyfit(X, Y, 2)
quadratic_fn = np.poly1d(quadratic)
#quadratic_fn is now a function which takes in X and returns an estimate for Y
order7 = np.polyfit(X, Y, 7)
order7_fn = np.poly1d(order7)
#order7_fn is now a function which takes in X and returns an estimate for Y
plt.scatter(X,Y, color = 'black', label = 'Original Data')
plt.plot(X, Y, 'yo', X, linear_fn(X), '--k', color='red', label='Linear')
plt.plot(X, Y, 'yo', X, quadratic_fn(X), '--k', color = 'green', label='Quadratic')
plt.plot(X, Y, 'yo', X, order7_fn(X), '--k', color = 'purple', label='Order 7')
plt.title('Training Data - Fits')
plt.legend()
plt.show()