Home > Software engineering >  How to append np.arrays() in a for-loop?
How to append np.arrays() in a for-loop?

Time:05-14

This application is in analysis of astrophysical data, but the problem is purely a question of programming/syntax. I want to go through my 53 bins of data (each one 1D galaxy spectrum at different radii of the galaxy) and perform some algorithm to determine kinematical parameters such as rotational velocity and velocity dispersion for each bin/radius. Can somebody tell me what I did wrong in the for-loop, I wanted to get 53 values in my radius array but only have 2 (presumably the first and the last one). I thought I would start with the first value radius_center and then using np.append() would append the next value for the radius at each loop iteration to the array, so I would end up with 53 values in the array at the end.

for bin in (1, 53):
gal_data = np.loadtxt('C:/Users/bins/' str(bin) '_upper.txt')
loglam_gal = gal_data[0, :]
flux_gal = gal_data[1, :]
gh_moments = fcq_application(loglam_temp, flux_temp, loglam_gal, flux_gal)[0]
z = fcq_application(loglam_temp, flux_temp, loglam_gal, flux_gal)[1]

radius = gal_data[2, 2]
radius_arr = np.append(radius_center, radius)

EDIT: Following @Jérôme Richard I did everything with lists, which I initialized with the starting value before the for-loop. Problem is now I have 3 values in my list after running through the loop and not 53 and I am not sure why.. Here is the code:

for bin in (1, 53):
    gal_data = np.loadtxt('C:/Users/reich/OneDrive/Dokumente/Uni/Bachelorarbeit/Python/bins/' str(bin) '_upper.txt')
    loglam_gal = gal_data[0, :]
    flux_gal = gal_data[1, :]
    gh_moments, z, *_ = fcq_application(loglam_temp, flux_temp, loglam_gal, flux_gal)

    radius = gal_data[2, 2]
    radius_list.append(radius)
    vel_rot = np.abs(vel_rot_center-gh_moments[0])
    vel_rot_list.append(vel_rot)
    vel_disp = gh_moments[1]
    vel_disp_list.append(vel_disp)
    h3 = gh_moments[2]
    h3_list.append(h3)
    h4 = gh_moments[3]
    h4_list.append(h4)
    z_list.append(z)
    signal_to_noise = gal_data[3, 3]
    signal_to_noise_list.append(signal_to_noise)

CodePudding user response:

for bin in (1,53): is going to treat (1,53) as a tuple of elements containing only 1 and 53, to actually go through the range you need for bin in range(1,54) (54 instead of 53 because it is non-inclusive of the last element).

radius_arr = np.empty(53) # pre-allocate a numpy array of length 53
# btw if you wanted radius_center to be the first value, you'd want to do
# radius_arr = np.empty(54)
# radius_arr[0] = radius_center
# then in the for loop use radius_arr[i] = radius instead of radius_arr[i-1]

for i in range(1,54):
     gal_data = np.loadtxt('C:/Users/bins/' str(bin) '_upper.txt')
     loglam_gal = gal_data[0, :]
     flux_gal = gal_data[1, :]
     gh_moments = fcq_application(loglam_temp, flux_temp, loglam_gal, flux_gal)[0]
     z = fcq_application(loglam_temp, flux_temp, loglam_gal, flux_gal)[1]
     radius = gal_data[2, 2]

     radius_arr[i-1] = radius

For general knowledge if you did want to use append you could use something similar to this:

radius_arr = np.array([radius_center]) # this is very inefficient
radius_list = [radius_center] # this is better
for i in range(1,54):
     ...
     radius_arr = radius_arr.append(radius) # this is very inefficient
     radius_list.append(radius) # doing it with a list first and appending the values is better

radius_list = np.array(radius_list) # can convert a list to a numpy array

python tuple

  • Related