通过numpy实现回归分析过程

#回归分析通过numpy的基本实现过程

'''
回归模型主体
'''
import numpy as np
def linear_loss(x,y,w,b):
    num_train = x.shape[0]
    num_feature = x.shape[1]
    #模型公式
    y_hat = np.dot(x,w)+b#np.dot,针对两个矩阵,如果满足第一个矩阵的
    #行数和第二个矩阵的列数相同,则进行矩阵相乘
    #损失函数
    loss = np.num((y_hat-y)**2)/num_train
    #参数的偏导
    dw = np.dot(x.T,(y_hat-y))/num_train
    db = np.sum((y_hat-y))/num_train
    return y_hat, loss, dw,db
# 参数初始化:
def initialize_params(dims):
    w=np.zeros((dims,1))
    b=0
    return w,b

#基于梯度下降的模型训练过程:
def linear_train(x,y,learning_rate,epochs):
    w,b = initialize_params(x.shape[1])
    loss_list = []
    for i in range(1,epochs):
        #计算当前预测值、损失和参数偏导
        y_hat,loss,dw,db = linear_loss(x,y,w,b)
        loss_list.append(loss)
        #基于梯度下降的参数更新过程
        w += -learning_rate * dw
        b += -learning_rate * db  
        #打印迭代次数和损失
        if i%10000==0:
            print('epoch %d loss %f'%(i,loss))
            #保存参数
            params = {
                'w':w,
                'b':b
                }
    #保存梯度
    grads = {
        'dw':dw,
        'db':db}
    return loss_list,loss,params,grads
#以sklearn 中的diabetes 数据集为例进行训练
from sklearn.datasets import load_diabetes
from sklearn.utils import shuffle
import numpy as np
diabetes = load_diabetes()
data = diabetes.data
target = diabetes.target
#打乱数据
x,y = shuffle(data,target,random_state = 13)#随机排列
x=x.astype(np.float32)#对数据类型进行转换
#训练集与测试集的简单划分
offset = int(x.shape[0]*0.9)
x_train,y_train = x[:offset],y[offset:]
x_test,y_test = x[offset:],y[offset:]
y_train=y_train.reshape((-1,1))
y_test = y_test.reshape((-1,1))#转化为一列

#执行训练
loss_list,loss,params,grads = linear_train(x_train,y_train,0.001,100000)
#定义预测函数对测试集结果进行预测:
def predict(x,params):
    w=params['w']
    b=params['b']
    y_pred = np.dot(x,w)+b
    return y_pred
y_pred = predict(x_test,params)
#通过matplotlib进行画图
import matplotlib.pyplot as plt
f = predict(x_test,params)
plt.scatter(range(x_test.shape[0]),y_test)
plt.plot(f,color = 'darkorange')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
plt.plot(loss_list,color = 'blue')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.show()
p=int(x.shape[0]*0.9)
x_train,y_train=x[:p],y[:p]
x_test,y_test = x[p:],y[p:]
cost_list,params,grads = linear_train(x_train,y_train,0.001,100000)
y_prediction = predict(x_test,params)
print(y_prediction)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值