热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python线性回归_python向量线性回归

这是我尝试仅使用numpy和线性代数执行线性回归的尝试:deflinear_function(w,x,b):returnnp.dot(w,x)bxnp.arra

这是我尝试仅使用numpy和线性代数执行线性回归的尝试:

def linear_function(w , x , b):

return np.dot(w , x) + b

x = np.array([[1, 1,1],[0, 0,0]])

y = np.array([0,1])

w = np.random.uniform(-1,1,(1 , 3))

print(w)

learning_rate = .0001

xT = x.T

yT = y.T

for i in range(30000):

h_of_x = linear_function(w , xT , 1)

loss = h_of_x - yT

if i % 10000 == 0:

print(loss , w)

w = w + np.multiply(-learning_rate , loss)

linear_function(w , x , 1)

这会导致错误:

ValueError Traceback (most recent call last)

in ()

24 if i % 10000 == 0:

25 print(loss , w)

---> 26 w = w + np.multiply(-learning_rate , loss)

27

28 linear_function(w , x , 1)

ValueError: operands could not be broadcast together with shapes (1,3) (1,2)

这似乎可以减少训练集的维数:

import numpy as np

def linear_function(w , x , b):

return np.dot(w , x) + b

x = np.array([[1, 1],[0, 0]])

y = np.array([0,1])

w = np.random.uniform(-1,1,(1 , 2))

print(w)

learning_rate = .0001

xT = x.T

yT = y.T

for i in range(30000):

h_of_x = linear_function(w , xT , 1)

loss = h_of_x - yT

if i % 10000 == 0:

print(loss , w)

w = w + np.multiply(-learning_rate , loss)

linear_function(w , x , 1)

print(linear_function(w , x[0] , 1))

print(linear_function(w , x[1] , 1))

哪个返回:

[[ 0.68255806 -0.49717912]]

[[ 1.18537894 0. ]] [[ 0.68255806 -0.49717912]]

[[ 0.43605474 0. ]] [[-0.06676614 -0.49717912]]

[[ 0.16040755 0. ]] [[-0.34241333 -0.49717912]]

[ 0.05900769]

[ 1.]

[0.05900769]& [1.]接近培训示例,因此看来此实现是正确的.引发错误的实现有什么问题?我还没有实现2->的维度扩展. 3正确吗?



推荐阅读
author-avatar
黑旦儿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有