For sum of all the elements along an axis, the following function returns the sum of array elements over the specified axis:
Sum(arr, axis, dtype, out).
The parameters for the function include:
Arr as the input array
Axis along which the sum value will be computed. Otherwise, arr will be regarded as flattened, meaning that all the axis will be considered. Where axis = 0, it means along the column. When axis = 1, the function will work along the row. Out stands for different array where the result will be placed. For this, array must be allocated the same dimensions as the anticipated output. Below is an example of sum function:
# 1D array
arr = [20, 2, .2, 10, 4]
print(“\nSum of arr : “, np.sum(arr))
print(“Sum of arr(uint8) : “, np.sum(arr, dtype = np.uint8))
print(“Sum of arr(float32) : “, np.sum(arr, dtype = np.float32))
print (“\nIs np.sum(arr).dtype == np.uint : “,
np.sum(arr).dtype == np.uint)
print (“Is np.sum(arr).dtype == np.float : “,
np.sum(arr).dtype == np.float)
Output
Sum of arr : 36.2
Sum of arr(uint8) : 36
Sum of arr(float32) : 36.2
Is np.sum(arr).dtype == np.uint : False
Is np.sum(arr).dtype == np.uint : True
Arithmetic Mean Function
Arithmetic mean function is suitable for computing the average of a list of numbers. The function returns the mean data set, given as parameters. The average is given by dividing the sum of the numbers with the count of the numbers in the list.
Set of numbers: [n10, n20, n30, n40, n50]Sum of data-set = (n10 + n20 + n30 + n40 + n50)Number of data generated = 5Average or arithmetic mean = (n10 + n20 + n30 + n40 + n50) / 5
data1 = [1, 3, 4, 5, 7, 9, 2]
x = statistics.mean(data1)
# Printing the mean
print(“Mean is :”, x)
Output
Mean is: 4.428571428571429
Std, var Standard Deviation, Variance
Standard deviation indicates the measure of spread and variation of a data set. The function is given by stdev( [data-set], xbar )
sample = [1, 2, 3, 4, 5]
# Prints standard deviation
# xbar is set to default value of 1
print(“Standard Deviation of sample is % s ”
% (statistics.stdev(sample)))
Output:
Standard Deviation of the sample is 1.5811388300841898
Min, Max Minimum, and Maximum
Min, max Minimum and maximum function calculates the maximum and minimum of the values passed in the argument. For the maximum, the function is max(j,k,l,..,key,default)
# Python code to illustrate the functioning of
# max()
# printing the maximum of 8,24,40,36,98
print(“Maximum of 8,24,40,36 and 98 is : “,end=””)
print (max(8,24,40,36,98 ) )
Output:
Maximum of 8,24,40,36 and 98 is 98.
For the minimum, the function is expressed as
min(j,k,l,..,key,default)
# printing the minimum of 8,24,40,36,98
print(“Minimum of 8,24,40,36 and 98 is : “,end=””)
print (min(8,24,40,36,98 ) )
Output:
Maximum of 8,24,40,36 and 98 is 8.
Argmin and Argmax
Argmin, argmax returns indices of the minimum and maximum elements of an array in a given axis, expressed as argmax(array, axis = None, out = None)
array = geek.arrange(12).reshape(3, 4)
print(“INPUT ARRAY : \n”, array)
# No axis mentioned, so works on entire array
print(“\nMax element : “, geek.argmax(array))
# returning Indices of the max element
# as per the indices
print(“\nIndices of Max element : “, geek.argmax(array, axis=0))
print(“\nIndices of Max element : “, geek.argmax(array, axis=1))
Output:
Input array: [[ 10 11 12 13] [ 14 15 16 17] [ 18 19 20 21]]Minimum element: 10Max element: 21Indices of Max element : [2 2 2 2]Indices of Max element : [3 3 3]Cumsum Functions
Cumsum function is applied when calculating cumulative sum of array elements over a specific axis,
expressed as cumsum(arr, axis=None, dtype=None, out=None) in_arr = geek.array([[2, 4, 6], [1, 3, 5]]) print (“Input array : “, in_arr) out_sum = geek.cumsum(in_arr) print (“cumulative sum of array elements: “, out_sum) |
Output:
Input array : [[2 4 6]
[1 3 5]]
Cumulative sum of array elements: [ 2 6 12 13 16 21]
References
Deitel, P., & Deitel, H. (2020). Introduction to data science: Measures of central tendency – Mean, median and mode. In Intro to python for computer science and data science. Pearson Education.
Sayantan, J. (2018). Cumsum () in Python. Retrieved from https://www.geeksforgeeks.org/numpy-cumsum-in-python/
Wedin, O. (2008). Data filtering methods. Retrieved from https://cordis.europa.eu/docs/projects/cnect/5/215455/080/deliverables/ROADIDEA-D3-1-Data-filtering-methods-V1-1.pdf