Numpy statistical functions

Numpy provides various statistical functions which are used to perform some statistical data analysis. In this section of the tutorial, we will discuss the statistical functions provided by the numpy.

Finding the minimum and maximum elements from the array

The numpy.amin() and numpy.amax() functions are used to find the minimum and maximum of the array elements along the specified axis respectively.

Consider the following example.

Example

snippet
import numpy as np

a = np.array([[2,10,20],[80,43,31],[22,43,10]])

print("The original array:\n")
print(a)


print("\nThe minimum element among the array:",np.amin(a))
print("The maximum element among the array:",np.amax(a))

print("\nThe minimum element among the rows of array",np.amin(a,0))
print("The maximum element among the rows of array",np.amax(a,0))

print("\nThe minimum element among the columns of array",np.amin(a,1))
print("The maximum element among the columns of array",np.amax(a,1))

Output:

Output
The original array: [[ 2 10 20] [80 43 31] [22 43 10]] The minimum element among the array: 2 The maximum element among the array: 80 The minimum element among the rows of array [ 2 10 10] The maximum element among the rows of array [80 43 31] The minimum element among the columns of array [ 2 31 10] The maximum element among the columns of array [20 80 43]

numpy.ptp() function

The name of the function numpy.ptp() is derived from the name peak-to-peak. It is used to return the range of values along an axis. Consider the following example.

Example

snippet
import numpy as np

a = np.array([[2,10,20],[80,43,31],[22,43,10]])

print("Original array:\n",a)

print("\nptp value along axis 1:",np.ptp(a,1))

print("ptp value along axis 0:",np.ptp(a,0))

Output:

Output
Original array: [[ 2 10 20] [80 43 31] [22 43 10]] ptp value along axis 1: [18 49 33] ptp value along axis 0: [78 33 21]

numpy.percentile() function

The syntax to use the function is given below.

snippet
numpy.percentile(input, q, axis)

It accepts the following parameters.

  1. input: It is the input array.
  2. q: It is the percentile (1-100) which is calculated of the array element.
  3. axis: It is the axis along which the percentile is to be calculated.

Consider the following example.

Example

snippet
import numpy as np

a = np.array([[2,10,20],[80,43,31],[22,43,10]])

print("Array:\n",a)

print("\nPercentile along axis 0",np.percentile(a, 10,0))

print("Percentile along axis 1",np.percentile(a, 10, 1))

Output:

Output
Array: [[ 2 10 20] [80 43 31] [22 43 10]] Percentile along axis 0 [ 6. 16.6 12. ] Percentile along axis 1 [ 3.6 33.4 12.4]

Calculating median, mean, and average of array items

The numpy.median() function:

Median is defined as the value that is used to separate the higher range of data sample with a lower range of data sample. The function numpy.median() is used to calculate the median of the multi-dimensional or one-dimensional arrays.

The numpy.mean() function:

The mean can be calculated by adding all the items of the arrays dividing by the number of array elements. We can also mention the axis along which the mean can be calculated.

The numpy.average() function:

The numpy.average() function is used to find the weighted average along the axis of the multi-dimensional arrays where their weights are given in another array.

Consider the following example.

Example

snippet
import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

print("Array:\n",a)

print("\nMedian of array along axis 0:",np.median(a,0))
print("Mean of array along axis 0:",np.mean(a,0))
print("Average of array along axis 1:",np.average(a,1))
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +