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

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)

Related

Is there a numpy function to find an array in multi dimensional array?

I have a numpy array with n row and p columns.
I want to check if a given row is in my array and find the index.
For exemple I have a numpy array like this :
[[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0],....]
I want to check if this array [6,0,5,8,2,1] is in my numpy array or and where.
Is there a numpy function for that ?
I'm sorry for asking naive question but I'm quite confuse right now.
You can use == and .all(axis=1) to match entire rows, then use numpy.where() to get the index:
import numpy as np
a = np.array([[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0], [6,0,5,8,2,1]])
b = np.array([6,0,5,8,2,1])
print(np.where((a==b).all(axis=1)))
Output:
(array([5], dtype=int32),)

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)

Getting the top N values and their coordinates from a 2D Numpy array [duplicate]

This question already has answers here:
Efficient way to take the minimum/maximum n values and indices from a matrix using NumPy
(3 answers)
Closed 2 years ago.
I have a 2D numpy array "bigrams" of shape (851, 851) with float values inside. I want to get the top ten values from this array and I want their coordinates.
I know that np.amax(bigrams) can return the single highest value, so that's basically what I want but then for the top ten.
As a numpy-noob, I wrote some code using a loop to get the top values per row and then using np.where() to get the coordinates, but i feel there must be a smarter way to solve this..
You can flatten and use argsort.
idxs = np.argsort(bigrams.ravel())[-10:]
rows, cols = idxs//851, idxs%851
print(bigrams[rows,cols])
An alternative would be to do a partial sorting with argpartition.
partition = np.argpartition(bigrams.ravel(),-10)[-10:]
max_ten = bigrams[partition//851,partition%851]
You will get the top ten values and their coordinates, but they won't be sorted. You can sort this smaller array of ten values later if you want.

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))

Appending Numpy Arrays [duplicate]

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.

Categories