clip
The clip function allows limiting the values inside an array of number. It's ideal to cap and floor data during a data-cleaning.
# Python code example: given an array of floats we want to limit its values between 0 and 100
import numpy as np
ar=[-10,0,10,20,30.4,50,70,80.2,100,210,2333]
ar2=np.clip(ar,0,100)
print(ar2)
Output:
[ 0. 0. 10. 20. 30.4 50. 70. 80.2 100. 100. 100. ]
To learn more about this function, please check
Here.
meshgrid
The meshgrid function is aimed at building coordinate sets based on input vectors.
It returns a tuple of coordinate matrices based on these vectors.
This function is ideal for building parameter grids in ML like it's discussed here.
# Python code example: given two array of data generate the mesghgrid
import numpy as np
a=[0 ,1, 2, 3]
b=[2024, 2025]
x,y=np.meshgrid(a,b)
print("x:n "+str(x))
print("y:n "+str(y))
Output:
x:
[[0 1 2 3]
[0 1 2 3]]
y:
[[2024, 2024, 2024, 2024]
[2025, 2025, 2025, 2025]]
The result is a combination of vectors that represents the different x and y coordinates of the points generated based on the two input vectors. In this example, the combination of the two input vectors generates 8 different points: (0,2024),(1,2024),...(2,2025),(3,2025).
To learn more about this function please check Here.
unique_inverse
The unique_inverse function takes one list as input and output 2 lists: one with the list of unique items contained in the input array, the other with the index of each of the unique values in the original list.
# Python code example: example of the outputs of the unique_inverse function
import numpy as np
ar=[1,2,3,1,2,3,3,2,1,2]
unique,location=np.unique_inverse(ar)
print('Input:n')
print('t Original list: '+ str(ar))
print('Output:n')
print('t : '+ str(unique))
Output:
Input:
Original list: [1, 2, 3, 1, 2, 3, 3, 2, 1, 2]
Output:
Unique values: [1 2 3]
Unique values locations: [0 1 2 0 1 2 2 1 0 1]
This function could be useful to build a compression algorithm aimed as synthetizing data. The first output being the dictionary and the second one the map of the dictionary data in the original "message".
To learn more about this function please check Here.
vectorize
The vectorize function is aimed at vectorizing functions in numpy. It takes one argument as input, the reference to the function to be vectorized, and output a new function reference, that can be applied directy to numpy arrays.
# Python code example: example of function vectorizing in numpy
import numpy as np
def myfunction(x):
return 4*x**2-3
vectMyFunction=np.vectorize(myfunction)
ar=[1,2,3,4]
res=vectMyFunction(ar)
print('Input:n')
print('t Original list: '+ str(ar))
print('Output:n')
print('t Result: '+ str(res))
Output:
Input:
Original list: [1, 2, 3, 4]
Output:
Result: [1 13 33 61]
Compared to array manipulation, this function brings better performance and clarity to the code, as there's no need for explicit loops on the arrays.
To learn more about this function please check Here.
roll
The roll function shifts the elements of an array along a specified axis. It is very useful for data
manipulation, image processing, and signal processing.
# Python code example: example of function roll in numpy
import numpy as np
# Create an array
array = np.array([1, 2, 3, 4, 5])
# Roll the array to the left by two positions
rolled_array = np.roll(array, -3)
# Print the rolled array
print('Input:n')
print('t Original list: '+ str(ar))
print('Output:n')
print('t Result: '+ str(res))
Output:
Input:
Original list: [1, 2, 3, 4,5]
Output:
Result: [4 5 1 2 3]
To learn more about this function please check here.
where
The where function from the numpy library is used to create an array, by applying a condition to an input array. It is very useful for data processing, feature engineering and ML modeling tasks in general.
# Python code example: example of function where in numpy
import numpy as np
# Create an array
array = np.array([1, 2, 3, 4, 5])
# Select elements greater than 3
# "Greater than 3" as GT3 and "Less than or equal to 3" as LTE3
result = np.where(arr > 3, "GT3", "LTE3")
# Print the filter result
print('Input:n')
print('t Original list: '+ str(ar))
print('Output:n')
print('t Result: '+ str(res))
Output:
Input:
Original list: [1, 2, 3, 4,5]
Output:
Result: ['LTE 3' 'LTE 3' 'LTE 3' 'GT 3' 'GT 3']
Compared to array manipulation, this function brings better performance and clarity to the code, as there's no need for explicit loops on the arrays.
To learn more about this function please check here.