题目:
求解思路:
类似于一次线性回归,将方程使用矩阵进行表示,然后利用最小二乘法构建闭式解
import numpy as np
from matplotlib import pyplot as ply
from tqdm import tqdm
test_pth = "C:/Users/Dell/Desktop/MLexp/exp2/data/experiment_02_testing_set.csv"
train_pth = "C:/Users/Dell/Desktop/MLexp/exp2/data/experiment_02_training_set.csv"
train_set = np.loadtxt(train_pth,delimiter=',', dtype=float)
test_set = np.loadtxt(test_pth,delimiter=',', dtype=float)
class Linear :
def __init__(self) :
self.weight = np.mat(np.random.random((3,1)))
def backword (self , input , y_train) :
X_train_augmented = np.c_[input ** 2, input, np.ones_like(input)]
self.weight = np.linalg.inv(X_train_augmented.T.dot(X_train_augmented)).dot(X_train_augmented.T).dot(y_train)
model = Linear()
X_train = train_set[:, 0].reshape(-1,1)
X_test = test_set[:, 0].reshape(-1,1)
y_train = train_set[:, 1].reshape(-1,1)
y_test = test_set[:, 1].reshape(-1,1)
model.backword(X_train , y_train)
X_train_augmented = np.c_[X_train**2, X_train, np.ones_like(X_train)]
X_test_augmented = np.c_[X_test**2, X_test, np.ones_like(X_test)]
y_pred_train = X_train_augmented.dot(model.weight)
train_loss = np.mean((y_train - y_pred_train)**2)
print("train loss : " , train_loss)
y_pred_test = X_test_augmented.dot(model.weight)
test_loss = np.mean((y_test - y_pred_test)**2)
print("test loss : " , test_loss)
fig = plt.Figure(figsize=(20,8) , dpi = 80)
x = train_set[:,0]
y = train_set[:,1]
plt.scatter(x,y, label = 'train_set')
x = test_set[:,0]
y = test_set[:,1]
plt.scatter(x,y, label = 'test_set')
X = np.insert(X_train , X_train.shape[0] , X_test , axis=0).reshape(-1,)
sorted_indices = np.argsort(X)
X = X[sorted_indices].reshape(-1,1)
x_label = np.c_[X**2, X, np.ones_like(X)]
pre_y = x_label.dot(model.weight)
x_label = np.insert(X_train , X_train.shape[0] , X_test , axis=0).reshape(-1,)
sorted_indices = np.argsort(x_label)
x_label = x_label[sorted_indices].reshape(-1,1)
plt.plot(x_label , pre_y , label = 'Fitted Model' , c = 'g' , linewidth=3)
plt.xlabel('x')
plt.ylabel('y')
plt.title("Linear Regression : y = wx^2 + wx + b")
plt.legend()
plt.show()
如有错误,请在评论区指出