For loop for an array

user9891079 Source

I have the array vAgarch and I am trying to extract each element from that, so I have the following code now:

vAgarch = [0.05, 0.03, 0.04, 0.05, 0.03, 0.04]
vAgarch = np.array(vAgarch)

# Extract each element from array vAgarch
dA1garch = np.fabs(vAgarch[0])
dA2garch = np.fabs(vAgarch[1])
dA3garch = np.fabs(vAgarch[2])
dA4garch = np.fabs(vAgarch[3])
dA5garch = np.fabs(vAgarch[4])
dA6garch = np.fabs(vAgarch[5])

However, this could be easier right? My array will consist of 40 elements later on and I think this code can be simplified with a for loop. I tried several for loops, but so far I have no success. Is there someone who can help me with simplifying this code?

pythonarraysloopsnumpy

Answers

answered 3 weeks ago leo valdez #1

It is possible to use a loop if you don't use separate variable names for dAgarch.

vAgarch= [0.05, 0.03, 0.04, 0.05, 0.03, 0.04]
dAgarch=np.zeros(vAgarch.size)
vAgarch= np.array(vAgarch)

# Extract each element from array vAgarch
count =0
while(count<dAgarch.size):
    dAgarch[count]= np.fabs(vAgarch[count])
    count+=1

Or as you ask for loop

for count in range(0,dAgarch.size):
    dAgarch[count]= np.fabs(vAgarch[count])

answered 3 weeks ago Jayhello #2

There is no need to use a for loop,numpy fabs parameters can be array_like, so you can just pass list to it. As below code(I have change some elements in vAgarch to minus):

def test_s():
    vAgarch = [-0.05, 0.03, -0.04, 0.05, 0.03, 0.04]
    print vAgarch
    # [-0.05, 0.03, -0.04, 0.05, 0.03, 0.04]

    # equivalent to a for loop
    arr2 = np.fabs(vAgarch)
    print arr2   # arr2 is a new array, all its elements is plus
    # [ 0.05  0.03  0.04  0.05  0.03  0.04]

    print vAgarch[0]
    # -0.05
    print arr2[0]
    # 0.05

you can also see tutorial in https://docs.scipy.org/doc/numpy/reference/generated/numpy.fabs.html.

Or if you use pycharm,you can go to the function definition for example:

enter image description here

enter image description here

answered 3 weeks ago Kobee Raveendran #3

Is there a specific reason you want everything in a standalone variable? I may be missing your point, but I'll propose something else if I'm understanding what you want.

As Jayhello mentioned, you can supply your original numpy array to np.fabs() directly. You stated in a comment that you did not want a list as you wanted to put these values in another list as well, but this is also possible with lists, using np.append(). Let's say the array that you want to add them to is master_array, you could do something like this:

import numpy as np

# other code here

new_arr = np.fabs(vAgarch)
master_array = np.append(master_array, new_arr)

This will append each of the elements from the vAgarch list to the other list after calling fabs() on them. If you'd like to add specific elements of the array only, you can just reference them by index (i.e. new_arr[2]) rather than creating separate variables for every single element.

answered 3 weeks ago user9986701 #4

Just

arr = np.fabs(vAgarch)

is enough. In numpy almost all the function parameter can be array_like(list or numpy array), and returns array.

So there is no need to use for loop.I recommend you to learn more about numpy array operation.

comments powered by Disqus