You can multiply a tuple (n,) by the number of dimensions you want. e.g.:
>>> import numpy as np
>>> N=2
>>> np.zeros((N,)*1)
array([ 0., 0.])
>>> np.zeros((N,)*2)
array([[ 0., 0.],
[ 0., 0.]])
>>> np.zeros((N,)*3)
array([[[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.]]])
Answer from mgilson on Stack OverflowYou can multiply a tuple (n,) by the number of dimensions you want. e.g.:
>>> import numpy as np
>>> N=2
>>> np.zeros((N,)*1)
array([ 0., 0.])
>>> np.zeros((N,)*2)
array([[ 0., 0.],
[ 0., 0.]])
>>> np.zeros((N,)*3)
array([[[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.]]])
>>> sh = (10, 10, 10, 10)
>>> z1 = zeros(10000).reshape(*sh)
>>> z1.shape
(10, 10, 10, 10)
While above is not wrong, it's just excessive.
Initializing a 3D Numpy array with random values in Python - Python - Data Science Dojo Discussions
How to append 3d numpy array to a 4d array
Sounds like what you really need is a python list of 3D numpy arrays. Appending to a numpy array is possible with np.append or np.concat, but it's very expensive because it forces the entire array to be remade. Is there any reason you want a 4D array?
More on reddit.comCan someone explain what does this np.pad mean?
Pad adds values to your mas, before and after each axis (each touple in your npad for each axis), for example: a = [1, 2, 3, 4, 5] np.pad(a, (2,3), 'constant', constant_values=(0, 0)) array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])
Look, two zeros on left and three on right You can get more examples in official doc: http://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html Btw in your task, if you want add 4 zeros to each side (I don't now why) you should use something like this: np.pad(image, ((4, 4), (4, 4)), 'constant', constant_values=0)
Also, you can do it by hand (in cycle) for understanding
More on reddit.comHow can I find unique elements along one axis of a numpy array?
The way I would do it in pure numpy:
np.unique(["{}{}".format(i, j) for i,j in arr])If you want to count, just add a len().
Explanation of the code:
["{}{}".format(i, j) for i, j in arr]It creates the following array: ["00", "01", "11", "01", "02", "12"]
This pseudo-flattened array can be then used to check which pairs are unique and which ones are not, using the np.unique() function. It is slightly "hacky", and this method has the following caveats:
-
You have to know in advance how many items are in each row of your array.
-
The number of items in each row must be the same.
You can get around those two caveats in the following way:
Change the code in the following way:
np.unique(["".join(map(str, i)) for i in arr])
This code will iterate over each row of your array, convert the elements into strings, and join the elements of this list. The syntax is a bit more confusing to a beginner (because of the map and counter-intuitive "".join() function), but it is more flexible. However, you might still run into an issue if the elements of your array cannot be converted to a string (e.g. complex objects, or NaN values).
If you are open to using another library, you can do it more flexibly in pandas.
import pandas as pd df = pd.DataFrame(arr) unique_arr = df.drop_duplicates().values #To return an array rather than a DataFrame object
This second method will return all the unique rows in your array as well.
More on reddit.com