python如何使用线性回归_仅在Python中使用for循环的简单线性回归

我现在做一个线性回归分析。输入变量是Size。产出变量是价格。我将数据集存储在2D数组中。教授们告诉我,使用MPY进行循环分析很容易。

Formula of interation is shown as the picture in the hyperlink。因此,我决定使用以下代码进行计算:#Structure of array (Stored in float), with structure like this [Room, Price]

array = [[4.0, 399.9], [5.0, 329.9], [6.0, 369.0]]

#Set initial value

theta_price = 0

theta_room = 0

stepsize = 0.01

item = 3

#Perform iterations

for looping in range(0, 50): #Loop 50 times

for j in array[0]: #Call the value stored in array[0]

for k in array[1]: #Call the value stored in array[1]

theta_price_1 = theta_price - stepsize * (1 / item) * (sum((theta_price + theta_room * int(j) - int(k)))#Perform iterations of theta 0

theta_room_1 = theta_room - stepsize * (1 / item) * (sum((theta_price + t + theta_room * int(j) - int(k))*int(j)))#Perform iterations of theta 1

#Bring the new theta value to the next loop

theta_price = theta_price_1

theta_room = theta_room_1

print(theta_price,theta_room)#Print the result for every loop

上述代码不起作用,第10行出现错误消息:

^{pr2}$

但是如果我去掉求和函数,它的计算结果是不正确的。因此,我知道它和函数和数组有一些问题,但我不知道如何解决它?在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值