我想知道是否有人能帮助我理解如何将这段代码(线性回归)转换为多项式回归。我试图不使用太多预制函数,以确保我理解我的操作。
# Importing Necessary Libraries
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (20.0, 10.0)
# Reading Data
data = pd.read_csv('test.csv')
print(data.shape)
data.head()
# Collecting X and Y
X = data['a'].values
Y = data['b'].values
# Mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
# Total number of values
m = len(X)
# Using the formula to calculate b1 and b2
numer = 0
denom = 0
for i in range(m):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
b1 = numer / denom
b0 = mean_y - (b1 * mean_x)
# Print coefficients
print(b1, b0)
max_x = np.max(X) + 100
min_x = np.min(X) - 100
# Calculating line values x and y
x = np.linspace(min_x, max_x, 1000)
y = b0 + b1 * x
# Ploting Line
plt.plot(x, y, color='#58b970', label='Regression Line')
# Ploting Scatter Points
plt.scatter(X, Y, c='#ef5423', label='Scatter Plot')
plt.xlabel('a')
plt.ylabel('b')
plt.legend()
plt.show()
现在我希望将这段代码“升级”,使其作为三次多项式回归(ax^3 + bx² ...)运行。有人能帮助我吗?谢谢。