3

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?

dawg
  • 98,345
  • 23
  • 131
  • 206
user9891079
  • 119
  • 2
  • 10

4 Answers4

0

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])
leo valdez
  • 259
  • 1
  • 15
  • Great thank you!! However, is it also possible to return the elements of vAgarch as single floats and not in array? Because now they are returned in an array and I need the elements as a stand-alone basis so I can put them into a list? – user9891079 Jun 24 '18 at 18:47
  • 1
    This question might have what you are looking for: [link](https://stackoverflow.com/questions/1966207/converting-numpy-array-into-python-list-structure) However, I suggest you read more about arrays and lists in python first. – leo valdez Jun 24 '18 at 19:22
  • Yeah, I made a mistake. because actually I don't want them in a list, I just want them on a stand-alone basis, so that I can add them to another array.. – user9891079 Jun 24 '18 at 19:41
  • Quick note- python does not have the ++ operator. You can change `count++` to `count+=1` – James Carter Jun 25 '18 at 00:34
0

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

Jayhello
  • 5,931
  • 3
  • 49
  • 56
0

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.

0

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.