思路和一元一样,只是没想到写这个小东西也会浪费这么多时间
思路请看《吴恩达机器学习》,对初学者及其友好
其中,x_data,和 y 列表可根据需要自行修改
import numpy as np
import pandas as pd
x_data = np.ones((1,20)).reshape(5,4)
y = np.array([[1,2,3,4]])
#传入
## 1 1 1 1 1.。。。。。。。。1 //第一行乘以 theta_0 全为1
##x1_1,x1_2,x1_3,.................x1_m //共m组数据
# x2_1,x2_2,......................x2_m
def duoyuanhuigui(x_data,y):
x_data = np.vstack([np.ones((1,x_data.shape[1]),dtype=float),x_data]) #第一行加上了 111111111111111111111
theta = np.ones((1, x_data.shape[0]), dtype=float)
##theta * x_data = [y1,y2,y3,.............,y_m]
## d theta_j = 1/m * [(yp_1-y1)*x[j,1]+(yp_2-y2)*x[j,2]+.....
hang = x_data.shape[0] #行数,即有几组数据
lie = x_data.shape[1]
print(hang,lie)
d_theta_temp = np.zeros(shape = (1,hang))
a = 0.1 #学习率
i = 1
while( (d_theta_temp != np.zeros(shape=(1,hang))).any and i<10000): #i作为循环上限
y_propredict = np.matmul(theta, x_data)
for j in range(hang):
d_theta_temp[0,j] = (np.sum((y_propredict-y)*x_data[j,:]) / hang)
theta -= a * d_theta_temp
i+=1
print(theta)
duoyuanhuigui(x_data,y)



