a = np.arange(12).reshape((4, 3))
def sum(array):
return np.sum(array)
np.apply_along_axis(sum, 1, a) >>>
array([3, 12, 21, 30])
import numpy as np # Example of an image.2 x2x3 a = np.array([ [ [1, 2, 3], [4, 5, 6] ], [ [7, 8, 9], [10, 11, 12] ] ]) # Our function.This swap first and last items of 3 - item array def rgb_to_bgr(pixel): pixel[0], pixel[2] = pixel[2], pixel[0] return pixel x, y, z = a.shape[0], a.shape[1], a.shape[2] a = a.reshape(x * y, z) a = np.apply_along_axis(rgb_to_bgr, 1, a) a = a.reshape(x, y, z) print(a)
This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.,Execute func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.,Apply a function to 1-D slices along the given axis.,The output array. The shape of out is identical to the shape of arr, except along the axis dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar out will have one fewer dimensions than arr.
Ni, Nk = a.shape[: axis], a.shape[axis + 1: ] for ii in ndindex(Ni): for kk in ndindex(Nk): f = func1d(arr[ii + s_[: , ] + kk]) Nj = f.shape for jj in ndindex(Nj): out[ii + jj + kk] = f[jj]
Ni, Nk = a.shape[: axis], a.shape[axis + 1: ] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[..., ] + kk] = func1d(arr[ii + s_[: , ] + kk])
>>> def my_func(a):
...""
"Average first and last element of a 1-D array"
""
...
return (a[0] + a[-1]) * 0.5 >>>
b = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]) >>>
np.apply_along_axis(my_func, 0, b)
array([4., 5., 6.]) >>>
np.apply_along_axis(my_func, 1, b)
array([2., 5., 8.])
>>> b = np.array([ [8, 1, 7], [4, 3, 9], [5, 2, 6] ]) >>> np.apply_along_axis(sorted, 1, b) array([ [1, 7, 8], [3, 4, 9], [2, 5, 6] ])
>>> b = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) >>> np.apply_along_axis(np.diag, -1, b) array([ [ [1, 0, 0], [0, 2, 0], [0, 0, 3] ], [ [4, 0, 0], [0, 5, 0], [0, 0, 6] ], [ [7, 0, 0], [0, 8, 0], [0, 0, 9] ] ])
Last Updated : 05 Sep, 2020
Output:
shape of arr: (5, 4, 3) shape of load_original_arr: (5, 4, 3) Yes, both the arrays are same
Note: There are a lot of functions for changing the shapes of arrays in numpy flatten, ravel and also for rearranging the elements rot90, flip, fliplr, flipud etc. These fall under Intermediate to Advanced section of numpy.,The shape of an array is the number of elements in each dimension.,By reshaping we can add or remove dimensions or change number of elements in each dimension.,Meaning that you do not have to specify an exact number for one of the dimensions in the reshape method.
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.(4, 3)
The easiest way to create an array is to use the array function. This accepts any sequence-like object (including other arrays) and produces a new NumPy array containing the passed data. For example, a list is a good candidate for conversion:,Whenever you see “array”, “NumPy array”, or “ndarray” in the text, with few exceptions they all refer to the same thing: the ndarray object.,NumPy array indexing is a rich topic, as there are many ways you may want to select a subset of your data or individual elements. One-dimensional arrays are simple; on the surface they act similarly to Python lists:,As a simple example, suppose we wished to evaluate the function sqrt(x^2 + y^2) across a regular grid of values. The np.meshgrid function takes two 1D arrays and produces two 2D matrices corresponding to all pairs of (x, y) in the two arrays:
In[83]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
In[84]: data = np.random.randn(7, 4)
In[85]: names
Out[85]:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'],
dtype = '|S4')
In[86]: data
Out[86]:
array([
[-0.048, 0.5433, -0.2349, 1.2792],
[-0.268, 0.5465, 0.0939, -2.0445],
[-0.047, -2.026, 0.7719, 0.3103],
[2.1452, 0.8799, -0.0523, 0.0672],
[-1.0023, -0.1698, 1.1503, 1.7289],
[0.1913, 0.4544, 0.4519, 0.5535],
[0.5994, 0.8174, -0.9297, -1.2564]
])
JavaScript seems to be disabled in your browser. You must have JavaScript enabled in your browser to utilize the functionality of this website.
import numpy as np
the_3d_array = np.zeros((2, 2, 2))
print(the_3d_array)
[ [ [0. 0.] [0. 0.] ] [ [0. 0.] [0. 0.] ] ]