Python Forum
Call out data from 2D array to perform calculation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call out data from 2D array to perform calculation
#1
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:

#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 loop
However, 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?
Reply
#2
you don't need zip at all

#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 array])#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 array])#Perform iterations of theta 1
   print(theta_price,theta_room)#Print the result for every loop

you will need zip if you have 2 list and want to combine them:
>>> list1 = [1,2,3]
>>> list2 = [3,4,5]
>>> zip(list1,list2)
[(1, 3), (2, 4), (3, 5)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,234 Jan-13-2023, 01:35 PM
Last Post: perfringo
  At what point while building/testing Logistic Regression do I perform the PCA or MCA? deadendstreet2 0 1,405 Feb-01-2021, 07:02 PM
Last Post: deadendstreet2
  Store data in array kasper1903 4 3,084 Oct-04-2019, 12:43 PM
Last Post: kasper1903
  Using Data from an Array Tomington 1 2,379 Apr-06-2019, 10:42 AM
Last Post: perfringo
  How to perform an ssl renegociation madem 0 1,953 Mar-22-2018, 04:59 PM
Last Post: madem
  2D array element Calculation poonck1 1 3,355 Mar-02-2017, 03:54 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020