私は現在、線形回帰分析を行っています。入力変数はSizeです。出力変数はPriceです。私は2D配列にデータのセットを格納します。私はNumPyを使って分析するのは簡単ですが、私の教授はforループを使って反復を実行するように指示しました。 Formula of interation is shown as the picture in the hyperlink。したがって、私は計算を実行するには、次のコードを決めた:forループを使用した単純な線形回帰
#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のエラーメッセージが表示されて機能していなかった。
'int' object is not iterable
しかし、私はsum関数を削除した場合、それは動作します計算結果が正しくない。したがって、私はそれがサム関数と配列にいくつかの問題があることを知っていますが、私はそれを解決する方法を知らない?
あなたのケースでは、 'it'は' x'と 'y'、' array [0] 'と' arrary [1] 'に適用されるべきです。 – zsrkmyn