Home > Back-end >  How to append to a Numpy array using a for-loop
How to append to a Numpy array using a for-loop

Time:09-28

I'm trying to learn how to work with Numpy arrays in python and working on a task where the goal is to append certain values from a square function to an np array.

To be specific, trying to append to the array in such a way that the result looks like this.

[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25](....)

In other words kind of like using a for loop to append to a nested list kind of like this:

N = 101

def f(x):
    return x**2

list1 = []
for i in range(N 1):
    list1.append([i])
    list1[i].append(f(i))

print(list1)

When I try to do this similarly whit Numpy arrays like below:

import numpy as np

N = 101
x_min = 1
x_max = 10

y = np.zeros(N)
x = np.linspace(x_min,x_max, N)

def f(x):
    return x**2

for i in y:
    np.append(y,f(x))
print(y)

I get the following output:

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0.]

... which is obviously wrong

Arrays as a datatype are quite new to me, so I would massively appreciate it if anyone could help me out.

Best regards from a rookie who is motivated to learn and welcome all help.

CodePudding user response:

It is kind of un-numpy-thonic (if that's a thing) to mix and match numpy arrays with vanilla python operations like for loops and appending. If I were to do this in pure numpy I would first start with your original array

>>> import numpy as np
>>> N = 101
>>> values = np.arange(N)
>>> values
array([  0,   1,   2,   ...,  99, 100])

then I would generate your squared array to create your 2D result

>>> values = np.array([values, values**2])
>>> values.T
array([[    0,     0],
       [    1,     1],
       [    2,     4],
       ...
       [   98,  9604],
       [   99,  9801],
       [  100, 10000]])

CodePudding user response:

Numpy gets its speed advantages in two primary ways:

  1. Faster execution of large numbers of repeated operations (i.e. without Python for loops)

  2. Avoiding moving data in memory (i.e. re-allocating memory space).

It's impossible to implement an indefinite append operation with Numpy arrays and still get both of these advantages. So don't do it!

I can't see in your example why an append is necessary because you know the size of the result array in advance (N).

Perhaps what you are looking for instead is vectorized function execution and assignment:

y[:] = f(x)
print(y)

(Instead of your for loop.)

This produces:

[  1.       1.1881   1.3924   1.6129   1.8496   2.1025   2.3716   2.6569
   2.9584   3.2761   3.61     3.9601   4.3264   4.7089   5.1076   5.5225
   5.9536   6.4009   6.8644   7.3441   7.84     8.3521   8.8804   9.4249
   9.9856  10.5625  11.1556  11.7649  12.3904  13.0321  13.69    14.3641
...etc.

Or, to get a similar output to your first bit of code:

y = np.zeros((N, 2))
y[:, 0] = x
y[:, 1] = f(x)

CodePudding user response:

You could simply broadcast the operation and column_stack them.

col1 = np.arange(N)
col2 = col1 **2
list1 = np.column_stack((col1,col2))
  • Related