Aggregation in Numpy
What is Aggregation?
It combines
the multiple values together and forms a single value. The value depends on the
operation.
Let us implement some aggregate functions in numpy using
python.
Implementation:
This
program includes the numpy object. It creates an array of elements.
It has following functions to provide aggregate functions.
Sum : adds all elements together.
Mean: finds the mean value.
Median: finds the middle highest value.
Min: it gives you the minimum value.
Max: it gives you the maximum value.
Std: it calculates the standard deviation.
Argmin: it displays index of minimum value.
Argmax: it gives you the index of maximum value.
Code:
import numpy as np1
s_arr = np1.array([11,12,13,14,15,16,17])
print("The original array is:",s_arr)
a_sum = np1.sum(s_arr)
a_mean=np1.mean(s_arr)
a_min =np1.min(s_arr)
a_max = np1.max(s_arr)
a_std =np1.std(s_arr)
a_argmin = np1.argmin(s_arr)
a_argmax = np1.argmax(s_arr)
print("The sum of all elements:",a_sum)
print("The mean value of the array:",a_mean)
print("The minimum value of the array:",a_min)
print("The maximum value of the array:",a_max)
print("The standard deviation:",a_std)
print("The index of the minimum value:",a_min)
print("The index of the maximum value:",a_max)
Output:
The original array is: [11 12 13 14 15 16 17]
The sum of all elements: 98
The mean value of the array: 14.0
The minimum value of the array: 11
The maximum value of the array: 17
The standard deviation: 2.0
The index of the minimum value: 11
The index of the maximum value: 17
Let us create some other functions
Row_wise aggregation and Column-wise aggregation:
These
functions calculate row-wise and column-wise aggregation.
Code:
import numpy as np1
arr2 = np1.array([
[3, 2, 1],
[6, 5, 4]
])
print("Column wise Aggregation:",np1.sum(arr2, axis=0))
print("Row wise aggregation:",np1.sum(arr2, axis=1))
Output:
Column wise Aggregation: [9 7 5]
Row wise aggregation: [ 6 15]
This is the way of using Aggregation in python using Numpy. Hope,
you understood the code. Keep coding!!!
Comments
Post a Comment