Related Tags

python
community creator

# np.tile, np.where, and np.trim_zeros in Numpy Python

Numpy is a module that uses the several functions it contains to improve the code’s speed and several other features. The NumPy module is created using Python and C, but it allows us to reach C++ speeds. It is mainly used for working with arrays.

Here some of the cool functions that NumPy provides:

• np.tile
• np.where
• np.trim_zeros

## np.tile

np.tile creates a new array in which the original array can be repeated as many times as we want.

import numpy as np
arr = [1,2,3,4]
rep = 2
print(np.tile(arr, rep))

## np.where

np.where is used to find the indices of an array element. In a 2d array, it can find the array the element is in.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])

print(np.where(arr == 4))

## np.trim_zeros

np.trim_zeros is used to remove any extra zeroes that may be trailing, leading, or both.

import numpy as np
a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
print(np.trim_zeros(a))
array([1, 2, 3, 0, 2, 1])

RELATED TAGS

python
community creator

CONTRIBUTOR 