# apply functions to 3d numpy array

• Last Update :
• Techknowledgy :
```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)```

Suggestion : 2

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

Suggestion : 3

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

Suggestion : 4

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

Suggestion : 5

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[13]: data1 = [6, 7.5, 8, 0, 1]

In[14]: arr1 = np.array(data1)

In[15]: arr1
Out[15]: array([6., 7.5, 8., 0., 1.])```
```In[27]: arr1 = np.array([1, 2, 3], dtype = np.float64)

In[28]: arr2 = np.array([1, 2, 3], dtype = np.int32)

In[29]: arr1.dtype In[30]: arr2.dtype
Out[29]: dtype('float64') Out[30]: dtype('int32')```
```In[45]: arr = np.array([
[1., 2., 3.],
[4., 5., 6.]
])

In[46]: arr
Out[46]:
array([
[1., 2., 3.],
[4., 5., 6.]
])

In[47]: arr * arr In[48]: arr - arr
Out[47]: Out[48]:
array([
[1., 4., 9.], array([
[0., 0., 0.],
[16., 25., 36.]
])[0., 0., 0.]
])```
```In[51]: arr = np.arange(10)

In[52]: arr
Out[52]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In[53]: arr[5]
Out[53]: 5

In[54]: arr[5: 8]
Out[54]: array([5, 6, 7])

In[55]: arr[5: 8] = 12

In[56]: arr
Out[56]: array([0, 1, 2, 3, 4, 12, 12, 12, 8, 9])```
```In[75]: arr[1: 6]
Out[75]: array([1, 2, 3, 4, 64])```
```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]
])```

Suggestion : 6

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.]
]
]```