1

I want to create 2D array by multiple 1D array (1,7680) to have multiple number of arrays under each other creating 2D array (n,7680)

Any help would be appreciated

code

y=[]
t=0
movement=int(S*256)
if(S==0):
    movement=_SIZE_WINDOW       
while data.shape[1]-(t*movement+_SIZE_WINDOW) > 0:
    for i in range(0, 22):
        start = t*movement
        stop = start+_SIZE_WINDOW
        signals[i,:]=data[i,start:stop]
        y=np.append(signals[i,:],y)

     t=t+1
6
  • 1
    Please explain in more detail. Commented Jun 21, 2020 at 21:36
  • You wrote np.empty(()). Are you using numpy? If that is true maybe you would need to add the numpy tag to your question. Commented Jun 21, 2020 at 21:37
  • @AnnZen I want to create 2D array by multiple 1D array (1,7680) to have multiple number of arrays under each other creating 2D array Commented Jun 21, 2020 at 21:40
  • 2
    Appending arrays to other arrays one by one is an extremely inefficient way to use NumPy, due to the quadratic amount of copying involved. Build up a list of arrays one by one and then convert to a single array at the end, or preallocate the whole array up front and fill in chunks one by one. Commented Jun 21, 2020 at 21:41
  • @user2357112supportsMonica How can I do that ?? I want to create 2D array by multiple 1D array to have multiple number of arrays under each other creating 2D array Commented Jun 21, 2020 at 21:44

1 Answer 1

1

If the shape of the arrays you want to create is well defined the easiest and optimal way to do so is to create an empty array like this:

array_NxM = np.empty((N,M))

This will create an empty array with the desired shape, then you can fill the array by iterating through its elements.

Creating an array by appending 1d arrays is definitely not optimal but an acceptable way to do so would be to create a list, appending 1d arrays to it and then cast the list to a numpy array like this:

array_NxM = []
for i in range(M):
    array_NxM.append(array_1xM)
array_NxM = np.array(array_NxM)

The worst way to do this is definitely to use np.append. If possible always avoid appending to a numpy array as this operations leads to a full copy in memory of the array and a read/write of it.

Sign up to request clarification or add additional context in comments.

4 Comments

I add this to my codearray_NxM = [] for J in range(7681): array_NxM.append(data[i,start:stop]) array_NxM = np.array(array_NxM) but I got this error array_NxM.append(data[i,start:stop]) AttributeError: 'numpy.ndarray' object has no attribute 'append'
You cannot use append to a numpy array. Just use the first solution, it will produce what you need
I don't know the shape of the array to use this array_NxM = np.empty((N,M)) .
What is the first solution ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.