Feb-28-2017, 01:25 PM
I am now doing a linear regression analysis. The input variable is Size. The output variable is Price. I store the set of data in 2D array. I know using NumPy and SciPy is easy to conduct analysis but it only allows me to use for loop only to perform iterations. Therefore, I decided the following code to perform the calculation:
To correct the error, I modified some of the code as
#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 theta_price = theta_price - stepsize * (1 / item) * (sum([theta_price + theta_room * int(j) - int(k) for j, k in zip(array[0], array[1])]))#Perform iterations of theta 0 theta_room = theta_room - stepsize * (1 / item) * (sum([(theta_price + theta_room * int(j) - int(k)) * int(j) for j, k in zip(array[0], array[1])]))#Perform iterations of theta 1 theta_price = theta_price_1 theta_room = theta_room_1 print(theta_price,theta_room)#Print the result for every loopHowever, the calculation is not what I am expected. I think it is because of the
zip(array[0], array[1])which stores j as 4.0, 399.9 and k 5.0, 329.9 not j = 4.0, 5.0, 6.0 and k = 399.9, 329.9, 369.0.
To correct the error, I modified some of the code as
for looping in range(0, 50): #Loop 50 times for looping in range(0, (item-1)): theta_price = theta_price - stepsize * (1 / item) * (sum([theta_price + theta_room * int(j) - int(k) for j, k in zip(array[p][0], array[p][1])]))#Perform iterations of theta 0
Error:zip argument #1 must support iteration
Therefore, how to correct the code to call out the data I want for calculation?