# Python numpy Aggregate Functions

In the Python numpy module, we have many aggregate functions or statistical functions to work with a single-dimensional or multi-dimensional array. The Python numpy aggregate functions are sum, min, max, mean, average, product, median, standard deviation, variance, argmin, argmax, percentile, cumprod, cumsum, and corrcoef.

To demonstrate these Python numpy aggregate functions, we use the below-shown arrays.

```import numpy as np
arr1 = np.array([10, 20, 30, 40, 50])
arr1

arr2 = np.array([[0, 10, 20], [30, 40, 50], [60, 70, 80]])
arr2

arr3 = np.array([[14, 6, 9, -12, 19, 72],[-9, 8, 22, 0, 99, -11]])
arr3```

## Python numpy Aggregate Functions Examples

The list of available Python numpy aggregate functions with an example of each.

### Python numpy sum

Python numpy sum function calculates the sum of values in an array.

```arr1.sum()
arr2.sum()
arr3.sum()```

This Python numpy sum function allows you to use an optional argument called an axis. This Python numpy Aggregate Function helps to calculate the sum of a given axis. For example, axis = 0 returns the sum of each column in a Numpy array.

```arr2.sum(axis = 0)
arr3.sum(axis = 0)```

axis = 1 returns the sum of each row in an array

```arr2.sum(axis = 1)
arr3.sum(axis = 1)```

You don’t have to use this axis name inside those Python array sum parentheses. I mean, arr2.sum(axis = 1) is same as arr2.sum(1).

```arr2.sum(0)
arr2.sum(1)
arr3.sum(0)
arr3.sum(1)```

### Python numpy average

Python numpy average function returns the average of a given array.

```np.average(arr1)
np.average(arr2)
np.average(arr3)```

Average of x and Y axis

```np.average(arr2, axis = 0)
np.average(arr2, axis = 1)```

Calculate numpy array Average without using the axis name.

```np.average(arr3, 0)
np.average(arr3, 1)```

### Python numpy prod

Python numpy prod function finds the product of all the elements in a given array. This numpy prod function returns 1 for an empty array.

```np.prod([])
np.prod(arr1)
np.prod(arr2) # any number multiply by zero gives zero```

This time we are using a two-dimensional array.

```x = np.array([[1, 2, 3], [4, 5, 6]])
np.prod(x)

y = np.random.randint(1, 10, size = (5, 5))
np.prod(y)```

Next, we would like to calculate the product of all the numbers on the X-axis and Y-axis separately.

```np.prod(x, axis = 0)
np.prod(x, axis = 1)
np.prod(y, axis = 0)
np.prod(y, axis = 1)```

Find the numpy array product without using the axis name.

```np.prod(x, 1)
np.prod(y, 1)```

### Python numpy min

The Python numpy min function returns the minimum value in an array or a given axis.

```arr1.min()
arr2.min()
arr3.min()```

We used the min function to find the numpy array minimum value in the X and Y-axis.

```arr2.min(axis = 0)
arr2.min(axis = 1)
arr3.min(0)
arr3.min(1)```

### Python Array minimum

Unlike the min function, this Python array minimum function accepts two arrays. Next, numpy array minimum performs one to one comparison of each array item in one array with other and returns an array of minimum values.

```import numpy as np

a = np.array([1, 20, 5, 22, 8, 15])
print(a)

b = np.array([12, 9, 3, 42, 6, 33])
print(b)

print('\n-----Minimum Array----')
print(np.minimum(a, b))```

This time we are applying the Python array minimum function on randomly generated 5 * 5 matrixes.

```import numpy as np
x = np.random.randint(1, 10, size = (5, 5))
print(x)
print()

y = np.random.randint(1, 10, size = (5, 5))
print(y)

print('\n-----Minimum Array----')
print(np.minimum(x, y))```

### Python numpy max

The Python numpy max function returns the maximum number from a given array or in a given axis.

```arr1.max()
arr2.max()
arr3.max()```

Find the maximum value in the X and Y-axis using the numpy max function.

```arr2.max(axis = 0)
arr2.max(axis = 1)
arr3.max(0)
arr3.max(1)```

### Python numpy Array maximum

Unlike max function, this Python numpy array maximum function accepts two arrays as an argument. Next, this function compares each array item with others in one to one way and returns an array of maximum values.

```import numpy as np

a = np.array([1, 20, 5, 22, 8, 15])
print(a)

b = np.array([12, 9, 3, 42, 6, 33])
print(b)

print('\n-----Maximum Array----')
print(np.maximum(a, b))```

This time we generated two 5 * 5 matrix of random numbers. Next, we used this Python array maximum function on them.

```import numpy as np

x = np.random.randint(1, 10, size = (5, 5))
print(x)
print()
y = np.random.randint(1, 10, size = (5, 5))
print(y)

print('\n-----Maximum Array----')
print(np.maximum(x, y))```

### Python numpy mean

Python numpy mean function returns the mean or average of a given array or in a given axis. The mathematical formula for this numpy mean is the sum of all the items in an array / total array of elements.

```arr1.mean()
arr2.mean()
arr3.mean()```

Mean value of x and Y-axis (or each row and column)

```arr2.mean(axis = 0)
arr2.mean(axis = 1)```

We are calculating the numpy array Mean without using the axis name.

```arr3.mean(0)
arr3.mean(1)```

### Python numpy median

The Python numpy median function return the median of an array or an axis.

```np.median(arr1)
np.median(arr2)
np.median(arr3)

# Median of x and Y axis
np.median(arr2, axis = 0)
np.median(arr2, axis = 1)

# Find median with axis number.
np.median(arr3, 0)
np.median(arr3, 1)```

### Python numpy var function

The Python numpy var function returns the variance of a given array or in a given axis. The formula for this Python numpy var is : (item1 – mean)2 + …(itemN – mean)2 / total items

```arr1.var()
arr2.var()
arr3.var()

x.var()
y.var()```

### Python numpy std

The Python numpy std function returns the standard deviation of a given array or in a given axis. The formula behind this is the numpy array square root of variance.

```arr1.std()
arr2.std()
arr3.std()

x.std()
y.std()```

### Python numpy cumsum

The Python numpy cumsum function returns the cumulative sum of a given array or in a given axis.

```arr1.cumsum()
arr2.cumsum()
arr3.cumsum()```

Finding the cumulative sum of x and Y axis

```arr2.cumsum(axis = 0)
arr2.cumsum(axis = 1)

# Cumulative Sum of an array by axis without using the axis name.
arr3.cumsum(0)
arr3.cumsum(1)```

### Python numpy cumprod

The Python numpy cumprod function returns the cumulative product of a given array or in a given axis.

```arr1.cumprod()
arr2.cumprod()
arr3.cumprod()

# Let me find the cumulative product of array elements in x and Y axis
arr2.cumprod(axis = 0)
arr2.cumprod(axis = 1)```

We are finding the Cumulative Product of the third array by axis without using the axis name.

```arr3.cumprod(0)
arr3.cumprod(1)```

### Python numpy percentile

The Python numpy percentile function finds the percentile (based on the given value) of an array or an axis.

```np.percentile(arr1, 10)
np.percentile(arr1, 30)
np.percentile(arr1, 50)
np.percentile(arr1, 75)
np.percentile(arr1, 100)

np.percentile(arr2, 50)
np.percentile(arr3, 50)

# Let me find the percentile of x and Y axis
np.percentile(arr2, 50, axis = 0)
np.percentile(arr2, 50, axis = 1)

# Cumulative Product of third array by axis without using the axis name.
np.percentile(arr3, 75, 0)
np.percentile(arr3, 75, 1)```

### Python numpy argmin

The Python numpy argmin returns the index position of the minimum value in a given array or a given axis. To demonstrate this numpy argmin and the argmax function, we declared two more arrays of random values

```arr4 = np.random.randint(9, size = (9))
arr4

arr5 = np.random.randint(25, size = (5, 5))
arr5```

Next, we are using this Python numpy argmin function on those arrays

```arr4.argmin()
arr5.argmin()
arr3.argmin()

# Let me find the index position of minimum value in arr5 x and Y axis
arr5.argmin(axis = 0)
arr5.argmin(axis = 1)

# Index of minimum values in third array by axis.
arr3.argmin(0)
arr3.argmin(1)```

### Python numpy argmax

The Python numpy argmax returns the index position of the maximum value in a given array or a given axis. Here, we used the numpy.argmax function on arr3, arr4, and arr5.

```arr4.argmax()
arr5.argmax()
arr3.argmax()```

Let us find the index position of the maximum value in arr3 and arr5 by X and Y-axis.

```arr3.argmax(axis = 0)
arr3.argmax(axis = 1)

# Index of the maximum values in an array arr5 by axis.
arr5.argmax(0)
arr5.argmax(1)```

### Python numpy corrcoef

Python numpy corrcoef function find and returns the correlation coefficient of an array.

```np.corrcoef(arr1)
np.corrcoef(arr2)
np.corrcoef(arr3)

np.corrcoef(x)
np.corrcoef(y)```