Transforming ndarray objects

Transform the ndarray based on minimum and maximum values: 

 

  • The clip() method of ndarray transforms an ndarray object in such a way that the values are in between the maximum and minimum values inclusive of the values specified.

 

  • At least one of the parameters - min or max should be passed to the clip() method for it to work without any error.

 

  • If there are any values below the minimum value they will be replaced by the value as specified the min parameter.

 

  • If there are any values above the maximum value they will be replaced by the value as specified the max parameter.

 

Example – numpy.ndarray.clip:

import numpy as np

 

# Create a 2 dimensional array

array_2D = np.array([[9,8,6],

                      [5,4,1],

                      [11,42,10],

                      ])

 

# Minimum value is 5 and maximum value is 10

clippedArray = array_2D.clip(5, 10)

 

print("Original Array:")

print(array_2D)

 

print("Clipped Array:")

print(clippedArray)

 

Output:

Original Array:

[[ 9  8  6]

 [ 5  4  1]

 [11 42 10]]

Clipped Array:

[[ 9  8  6]

 [ 5  5  5]

 [10 10 10]]

 

Transform the n-dimensional array into a one-dimensional array: 

 

  • The flatten() method of numpy.ndarray transforms any n-dimensional array represented by the ndarray into a one dimensional array.

 

  • The transformation from n-dimensions to one dimension is done as specified by the parameter order.

 

  • The order parameter of flatten() method specifies whether the transformation is based on column major i.e, Fortran Style - denoted by literal 'F' or row major i.e, C Style -  denoted by literal 'F'.

 

  • The default order is based on row major.

 

  • ravel() method can also be used to transform an n-dimensional array into a one dimensional array. Please note that ravel() may not always return a copy that represents the one-dimensional array.

 

Example:

import numpy as np

 

# Create a 2-dimensional array

array_2d = np.array([[5, 10, 15, 20],

                    [25, 30, 35, 40]])

 

print("Original Array")

print(array_2d)

 

# Make a one dimensional array - default is row major

rowMajor = array_2d.flatten()

print("Flattened 2-dimensional array as 1-dimensional array - transformed using row major:")

print(rowMajor)

 

# Make a one dimensional array - column major

colMajor = array_2d.flatten('F')

print("Flattened 2-dimensional array as 1-dimensional array - transformed using column major:")

print(colMajor)

 

Output:

Original Array

[[ 5 10 15 20]

 [25 30 35 40]]

Flattened 2-dimensional array as 1-dimensional array - transformed using row major

[ 5 10 15 20 25 30 35 40]

Flattened 2-dimensional array as 1-dimensional array - transformed using column major

[ 5 25 10 30 15 35 20 40]

 

Change the shape of an array:

  • The reshape() method of ndarray takes a parameter shape as an int or a tuple of ints and transforms the shape of an n-dimensional array.
  • The returned object may not be a copy and could be a view of the original array.

Example:

import numpy as np

 

# Create a 2-dimensional array

array_3d = np.array([[[5, 10, 15, 20],

                     [25, 30, 35, 40]],

 

                    [[45, 50, 65, 70],

                     [75, 80, 85, 90]]])

 

print("Original Array:")

print(array_3d)

 

print("Dimensions of the original Array:")

print(array_3d.ndim)

 

print("Shape of the original Array:")

print(array_3d.shape)

 

reshaped1 = array_3d.reshape((2,2,2,2))

print("4d array - Reshaped using row major:")

print(reshaped1)

 

reshaped2 = array_3d.reshape((2,2,2,2), order='F')

print("4d array - Reshaped using column major:")

print(reshaped2)

 

reshaped3 = array_3d.reshape((2,8))

print("2d array - Reshaped using row major:")

print(reshaped3)

 

print("2d array - Reshaped using column major:")

reshaped4 = array_3d.reshape((2,8), order='F')

print(reshaped4)

 

reshaped5 = array_3d.reshape((16))

print("1d array - Reshaped using row major:")

print(reshaped5)

 

print("1d array - Reshaped using column major:")

reshaped6 = array_3d.reshape((16), order='F')

print(reshaped6)

 

Output:

Original Array:

[[[ 5 10 15 20]

  [25 30 35 40]]

 

 [[45 50 65 70]

  [75 80 85 90]]]

Dimensions of the original Array:

3

Shape of the original Array:

(2, 2, 4)

4d array - Reshaped using row major:

[[[[ 5 10]

   [15 20]]

 

  [[25 30]

   [35 40]]]

 

 

 [[[45 50]

   [65 70]]

 

  [[75 80]

   [85 90]]]]

4d array - Reshaped using column major:

[[[[ 5 15]

   [10 20]]

 

  [[25 35]

   [30 40]]]

 

 

 [[[45 65]

   [50 70]]

 

  [[75 85]

   [80 90]]]]

2d array - Reshaped using row major:

[[ 5 10 15 20 25 30 35 40]

 [45 50 65 70 75 80 85 90]]

2d array - Reshaped using column major:

[[ 5 25 10 30 15 35 20 40]

 [45 75 50 80 65 85 70 90]]

1d array - Reshaped using row major:

[ 5 10 15 20 25 30 35 40 45 50 65 70 75 80 85 90]

1d array - Reshaped using column major:

[ 5 45 25 75 10 50 30 80 15 65 35 85 20 70 40 90]


Copyright 2024 © pythontic.com