Appending Numpy Arrays [duplicate] - python

This question already has answers here:
Appending to numpy arrays
(3 answers)
Closed 3 years ago.
I have a for loop where I create numpy array a. I want to have a results numpy array that I append array a to every loop. So the final structure of the results array should be [a,a,a,etc...], such that I can get into a new array [len(a),Len(a),etc..]
I can't figure out how to do this. I've tried np.append, and can't figure out how to do this (I'm confused what the axis parameter does). I'm new to numpy so any help is appreciated. I don't want to flatten the arrays - I want it to behave like appending a python list to a python list.
Sorry about the formatting - I'm on a phone right now.

The simplest (and quickest) way is to collect the arrays in a list, then use np.concatenate to join them all together.
Example test data
import numpy as np
a = np.random.rand(4,5)
b = np.random.rand(4,5)
c = np.random.rand(4,5)
d = np.random.rand(4,5)
lst = [a,b,c,d]
You can concatenate along either axis — by default it is along the 0-axis.
>>> result = np.concatenate(lst)
>>> result.shape
(16, 5)
...along the 1st axis.
>>> result = np.concatenate(lst, axis=1)
>>> result.shape
(4, 20)
The arrays need to match in the other axis, i.e. if your concatenating them vertically, they must be the same width.

Related

Multidimensional numpy array appending with Python

In Python, I can concatenate two arrays like below,
myArray = []
myArray += [["Value1", "Value2"]]
I can then access the data by indexing the 2 dimensions of this array like so
print(myArray[0][0])
which will output:
Value1
How would I go about achieving the same thing with Numpy?
I tried the numpy append function but that only ever results in single dimensional arrays for me, no matter how many square brackets I put around the value I'm trying to append.
If you know the dimension of the final array then you could use np.vstack
>>> import numpy as np
>>> a = np.array([]).reshape(0,2)
>>> b = np.array([['Value1', 'Value2']])
>>> np.vstack([a,b])
array([['Value1', 'Value2']], dtype='<U32')

How to convert an array containing arrays to only hold one array [duplicate]

This question already has answers here:
Flatten numpy array
(2 answers)
Closed 2 years ago.
My issue is this. I have an array like this :
test = array([[1],[2],[10] .... [-5]])
I want to be able to just convert this to an array that looks like this
test = [1,2,10,..., -5]
The test is a numpy array.
You can use reshape() to change the array into 1D array as:
import numpy as np
test = np.array([[1],[2],[10],[-5]])
test=test.reshape((test.shape[0],)) # test.shape[0] gives the first dimension (the number of rows)
# reshape changes test array to one dimensional array with shape (test.shape[0],)
print(test)

Fill an empty matrix and work with the rows in python [duplicate]

This question already has answers here:
Filling a 2D matrix in numpy using a for loop
(2 answers)
Closed 2 years ago.
I have a function, where I calculate a prediction with the shape 1 x 250. I do this B times. To save all predictions for the B runs I would like to fill an empty B x 250 matrix row wise. Which python method is the best to fill an empty matrix, so that I can access the rows in a simple way (for example to subtract the rows from each other)
There must be something better then the following
indx = np.argsort(y_real[:,1])
print(len(indx)) #250
hat_y_b = []
for b in range(0,B):
y_pred = function()
hat_y_b = np.append(hat_y_b, y_pred[indx,1], axis=0)
I'm not sure I understand your question fully, but here're some general tips on dealing with numpy arrays:
In general, appending things to np arrays is very slow. Instead, allocate the entire array at the start, and fill with array indexing:
import numpy as np
# first, make an array with 250 rows and B cols.
# Each col will be a single prediction vector
all_b_arr = np.zeros((250, B))
# if no lower bound is specified, it is assumed to be 0
for idx in range(B):
# This array indexing means "every row (:), column idx"
all_b_arr[:, idx] = function()

How to change value of remainder of a row in a numpy array once a certain condition is met? [duplicate]

This question already has answers here:
Can NumPy take care that an array is (nonstrictly) increasing along one axis?
(2 answers)
Closed 3 years ago.
I have a 2d numpy array of the form:
array = [[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]]
I'd like to go to each of the rows, iterate over the entries until the value 1 is found, then replace every subsequent value in that row to a 1. The output would then look like:
array = [[0,0,0,1,1], [0,1,1,1,1], [1,1,1,1,1]]
My actual data set is very large, so I was wondering if there is a specialized numpy function that does something like this, or if there's an obvious way to do it that I'm missing.
Thanks!
You can use apply.
import numpy as np
array = np.array([[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]])
def myfunc(l):
i = 0
while(l[i]!=1):
i+=1
return([0]*i+[1]*(len(l)-i))
print(np.apply_along_axis(myfunc, 1, array))

Inserting values to an empty multidim. numpy array [duplicate]

This question already has answers here:
How to add a new row to an empty numpy array
(7 answers)
Closed 5 years ago.
I need to create an empty numpy array of a shape (?, 10, 10, 3). ? means I don't know how many elements will be inserted. Then I have many numpy arrays of a shape (1, 10, 10, 3) which I want to be inserted to the prepared array one by one, so the ? mark will be increasing with inserted elements.
I'm trying all variations of numpy array methods like empty, insert, concatenate, append... but I'm not able to achieve this. Could you please give me a hand with this?
Using np.append works:
import numpy as np
mat = np.empty((0,10,10,3))
array = np.random.rand(1,10,10,3)
mat = np.append(mat, array, axis=0)
mat = np.append(mat, array, axis=0)
print(mat.shape)
>>>(2,10,10,3)

Categories