机器学习——多元线性回归(随机生成数据)

我参考的博客之类的(对原理解释的很棒)
one: https://www.cnblogs.com/huayecai/p/10784906.html
two: https://www.cnblogs.com/xuanku/p/dl_linear.html

具体对python高级库了解很少,最简单用numpy实现
参考了同校同级孩子讲解,b站upID:小渣不是嘤嘤怪)

import numpy as np
import random

learn_rate = 1e-6
P = 10
N = 1000
loss = 1e9  

x_ = np.random.randint(0,10,size=(N,P))
w_ = np.random.randint(1,5,size=(P,1))
b_ = np.random.rand()
y_ = np.array([(np.matmul(np.transpose(w_),x_[i])+b_) for i in range(N)])
#print(y_.shape)

w = np.random.rand(P,1)
b = np.random.rand()

def loss_f(W,B):
    return np.mean((np.array([(np.matmul(np.transpose(W),x_[i])+B) for i in range(N)]-y_))**2)

def w_gradient(W,B):
    return np.matmul(np.matmul(np.transpose(x_),x_),W)-np.matmul(np.transpose(x_),(y_-B))

def b_gradient(W,B):
    return np.mean(np.array([(np.matmul(np.transpose(W),x_[i])+B) for i in range(N)]-y_ ))

while loss >1:
    w = w - learn_rate*w_gradient(w,b)
    b = b - learn_rate*b_gradient(w,b)
    loss = loss_f(w,b)
    print(loss)
    print("--------------------")

print(w,b)
print(w_,b_)
#b偏差较大 = . = ,有想法再回来改咯

机器学习——多元线性回归(随机生成数据)_第1张图片

第二个代码,我的矩阵数乘还有点问题,同上,有想法再修改啦


import numpy as np

learn_rate = 1e-6
loss = 1e9
P = 5
N = 10

x = np.random.randint(0,10,size=(N,P))
a = np.ones(1)
x_ = np.insert(x,P,values= a,axis=1)
x_T = np.transpose(x)
w_ = np.random.randint(1,7,size=(P+1,1))
y_ = np.array([np.matmul(np.transpose(w_),x_[i]) for i in range(N)])

w = np.random.rand(P+1,1)

def loss_f(W):
    return np.mean((np.array([np.matmul(np.transpose(W),x_[i]) for i in range(N)])-y_)**2)

def w_gradient(W):
    return np.matmul(x_T,np.matmul(x_,W))-np.matmul(y_,x_T)

while loss > 1:
    w = w - learn_rate*w_gradient(w)
    loss = loss_f(w)
    print(loss)
    print("-------------------")

print(w_)
print(w)

你可能感兴趣的:(机器学习,python)