机器学习实验-实验二 (最新)

题目:

求解思路:

类似于一次线性回归,将方程使用矩阵进行表示,然后利用最小二乘法构建闭式解

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()

如有错误,请在评论区指出

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值