NumPy statistical functions
NumPy provides many statistical functions for finding the minimum element, maximum element, percentile, standard deviation, and variance in an array.
numpy.amin() and numpy.amax()
numpy.amin() is used to compute the minimum value of elements in an array along a specified axis.
numpy.amin(a, axis=None, out=None, keepdims=, initial=, where=)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the minimum. If not provided, the minimum of the entire array is returned. It can be an integer indicating the axis index, or a tuple indicating multiple axes.out: Optional parameter specifying where to store the result.keepdims: Optional parameter; if True, the result array keeps the same number of dimensions as the input. If False (default), axes of dimension 1 are removed after computation.initial: Optional parameter specifying an initial value from which to compute the minimum over the array elements.where: Optional parameter, a boolean array specifying that only elements satisfying the condition are considered.
numpy.amax() is used to compute the maximum value of elements in an array along a specified axis.
numpy.amax(a, axis=None, out=None, keepdims=, initial=, where=)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the maximum. If not provided, the maximum of the entire array is returned. It can be an integer indicating the axis index, or a tuple indicating multiple axes.out: Optional parameter specifying where to store the result.keepdims: Optional parameter; if True, the result array keeps the same number of dimensions as the input. If False (default), axes of dimension 1 are removed after computation.initial: Optional parameter specifying an initial value from which to compute the maximum over the array elements.where: Optional parameter, a boolean array specifying that only elements satisfying the condition are considered.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
3
,
7
,
5
]
,
[
8
,
4
,
3
]
,
[
2
,
4
,
9
]
]
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Call the amin() function:
'
)
(
np
.
amin
(
a
,
1
)
)
(
'
\n
'
)
(
'
Call the amin() function again:
'
)
(
np
.
amin
(
a
,
0
)
)
(
'
\n
'
)
(
'
Call the amax() function:
'
)
(
np
.
amax
(
a
)
)
(
'
\n
'
)
(
'
Call the amax() function again:
'
)
(
np
.
amax
(
a
,
axis
=
0
)
)
The output is:
我们的数组是:
[[3 7 5]
[8 4 3]
[2 4 9]]
Call the amin() function:
[3 3 2]
Call the amin() function again:
[2 4 3]
Call the amax() function:
9
Call the amax() function again:
[8 7 9]
numpy.ptp()
numpy.ptp() The function computes the difference between the maximum and minimum values in the array (maximum - minimum).
numpy.ptp(a, axis=None, out=None, keepdims=, initial=, where=)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the peak-to-peak value. If not provided, the peak-to-peak value of the entire array is returned. It can be an integer indicating the axis index, or a tuple indicating multiple axes.out: Optional parameter specifying where to store the result.keepdims: Optional parameter; if True, the result array keeps the same number of dimensions as the input. If False (default), axes of dimension 1 are removed after computation.initial: Optional parameter specifying an initial value from which to compute the peak-to-peak value over the array elements.where: Optional parameter, a boolean array specifying that only elements satisfying the condition are considered.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
3
,
7
,
5
]
,
[
8
,
4
,
3
]
,
[
2
,
4
,
9
]
]
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Call the ptp() function:
'
)
(
np
.
ptp
(
a
)
)
(
'
\n
'
)
(
'
Call the ptp() function along axis 1:
'
)
(
np
.
ptp
(
a
,
axis
=
1
)
)
(
'
\n
'
)
(
'
Call the ptp() function along axis 0:
'
)
(
np
.
ptp
(
a
,
axis
=
0
)
)
The output is:
我们的数组是:
[[3 7 5]
[8 4 3]
[2 4 9]]
Call the ptp() function:
7
Call the ptp() function along axis 1:
[4 5 7]
// Call ptp() function along axis 0:
[6 3 6]
numpy.percentile()
A percentile is a measure used in statistics indicating the percentage of observations below a given value. The function numpy.percentile() accepts the following parameters.
numpy.percentile(a, q, axis)
Parameter Description:
- a: input array
- q: the percentile to compute, between 0 and 100
- axis: the axis along which to compute the percentile
First, clarify the percentile:
The p-th percentile is a value such that at least p% of data items are less than or equal to it, and at least (100-p)% of data items are greater than or equal to it.
For example: college entrance exam scores are often reported as percentiles. Suppose a candidate's raw score in the Chinese section of the entrance exam is 54. Relative to other students taking the same exam, it is not easy to know how his score compares. But if the raw score of 54 corresponds exactly to the 70th percentile, we know that about 70% of students scored lower than him and about 30% scored higher.
Here p = 70.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
10
,
7
,
4
]
,
[
3
,
2
,
1
]
]
)
(
'
Our array is:
'
)
(
a
)
(
'
Call the percentile() function:
'
)
The 50th percentile is the median of a after sorting
(
np
.
percentile
(
a
,
50
)
)
axis is 0, computed along columns
(
np
.
percentile
(
a
,
50
,
axis
=
0
)
)
axis is 1, computed along rows
(
np
.
percentile
(
a
,
50
,
axis
=
1
)
)
Keep dimensions unchanged
(
np
.
percentile
(
a
,
50
,
axis
=
1
,
keepdims
=
True
)
)
The output is:
我们的数组是:
[[10 7 4]
[ 3 2 1]]
Calling the percentile() function:
3.5
[6.5 4.5 2.5]
[7. 2.]
[[7.]
[2.]]
numpy.median()
The numpy.median() function is used to compute the median of the elements in array a
numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the median. If not provided, the median of the entire array is computed. It can be an integer indicating the axis index, or a tuple indicating multiple axes.out: Optional parameter specifying where to store the result.overwrite_input: Optional parameter; if True, allows the use of the input array's memory during computation. This may improve performance in some cases but may modify the contents of the input array.keepdims: Optional parameter; if True, the result array keeps the same number of dimensions as the input. If False (default), axes of dimension 1 are removed after computation.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
30
,
65
,
70
]
,
[
80
,
95
,
10
]
,
[
50
,
90
,
60
]
]
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Call the median() function:
'
)
(
np
.
median
(
a
)
)
(
'
\n
'
)
(
'
Call the median() function along axis 0:
'
)
(
np
.
median
(
a
,
axis
=
0
)
)
(
'
\n
'
)
(
'
Call the median() function along axis 1:
'
)
(
np
.
median
(
a
,
axis
=
1
)
)
The output is:
我们的数组是:
[[30 65 70]
[80 95 10]
[50 90 60]]
Call the median() function:
65.0
Call the median() function along axis 0:
[50. 90. 60.]
Call the median() function along axis 1:
[65. 80. 60.]
numpy.mean()
The numpy.mean() function returns the arithmetic mean of the elements in an array; if an axis is provided, it is computed along that axis.
The arithmetic mean is the sum of elements along the axis divided by the number of elements.
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the mean. If not provided, the mean of the entire array is computed. It can be an integer indicating the axis index, or a tuple indicating multiple axes.dtype: Optional parameter specifying the data type of the output. If not provided, an appropriate data type is chosen based on the input data type.out: Optional parameter specifying where to store the result.keepdims: Optional parameter; if True, the result array keeps the same number of dimensions as the input. If False (default), axes of dimension 1 are removed after computation.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
,
3
]
,
[
3
,
4
,
5
]
,
[
4
,
5
,
6
]
]
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Call the mean() function:
'
)
(
np
.
mean
(
a
)
)
(
'
\n
'
)
(
'
Call the mean() function along axis 0:
'
)
(
np
.
mean
(
a
,
axis
=
0
)
)
(
'
\n
'
)
(
'
Call the mean() function along axis 1:
'
)
(
np
.
mean
(
a
,
axis
=
1
)
)
The output is:
我们的数组是:
[[1 2 3]
[3 4 5]
[4 5 6]]
Call the mean() function:
3.6666666666666665
# Call mean() function along axis 0:
[2.66666667 3.66666667 4.66666667]
# Call mean() function along axis 1:
[2. 4. 5.]
numpy.average()
The numpy.average() function computes the weighted average of elements in an array based on the respective weights given in another array.
This function can accept an axis parameter. If no axis is specified, the array is flattened.
A weighted average multiplies each value by its corresponding weight, sums them to get a total, and then divides by the total number of units.
Consider the array [1,2,3,4] and the corresponding weights [4,3,2,1]; the weighted average is computed by summing the products of corresponding elements and dividing the sum by the sum of the weights.
加权平均值 = (1*4+2*3+3*2+4*1)/(4+3+2+1)
Function syntax:
numpy.average(a, axis=None, weights=None, returned=False)
Parameter Description:
a: The input array, which can be a NumPy array or an array-like object.axis: Optional parameter specifying the axis along which to compute the weighted average. If not provided, the weighted average of the entire array is computed. It can be an integer indicating the axis index, or a tuple indicating multiple axes.weights: Optional parameter specifying the weights for the corresponding data points. If no weight array is provided, equal weights are used by default.returned: Optional parameter; if True, returns both the weighted average and the sum of weights.
Example
import
numpy
as
np
a
=
np
.
array
(
[
1
,
2
,
3
,
4
]
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Call the average() function:
'
)
(
np
.
average
(
a
)
)
(
'
\n
'
)
When no weights are specified, it is equivalent to the mean function
wts
=
np
.
array
(
[
4
,
3
,
2
,
1
]
)
(
'
Call the average() function again:
'
)
(
np
.
average
(
a
,
weights
=
wts
)
)
(
'
\n
'
)
If the returned parameter is set to true, the sum of weights is returned
(
'
Sum of weights:
'
)
(
np
.
average
(
[
1
,
2
,
3
,
4
]
,
weights
=
[
4
,
3
,
2
,
1
]
,
returned
=
True
)
)
The output is:
我们的数组是:
[1 2 3 4]
Call the average() function:
2.5
Call the average() function again:
2.0
Sum of weights:
(2.0, 10.0)
In multidimensional arrays, the axis to use for computation can be specified.
Example
import
numpy
as
np
a
=
np
.
arange
(
6
)
.
reshape
(
3
,
2
)
(
'
Our array is:
'
)
(
a
)
(
'
\n
'
)
(
'
Modified array:
'
)
wt
=
np
.
array
(
[
3
,
5
]
)
(
np
.
average
(
a
,
axis
=
1
,
weights
=
wt
)
)
(
'
\n
'
)
(
'
Modified array:
'
)
(
np
.
average
(
a
,
axis
=
1
,
weights
=
wt
,
returned
=
True
)
)
The output is:
我们的数组是:
[[0 1]
[2 3]
[4 5]]
Modified array:
[0.625 2.625 4.625]
Modified array:
(array([0.625, 2.625, 4.625]), array([8., 8., 8.]))
Standard deviation
Standard deviation is a measure of the dispersion of a set of data around its mean.
Standard deviation is the arithmetic square root of variance.
The standard deviation formula is as follows:
std = sqrt(mean((x - x.mean())**2))
If the array is [1,2,3,4], its mean is 2.5. Thus, the squared differences are [2.25,0.25,0.25,2.25], and the square root of their mean divided by 4, i.e., sqrt(5/4), gives 1.1180339887498949.
Example
import
numpy
as
np
(
np
.
std
(
[
1
,
2
,
3
,
4
]
)
)
The output is:
1.1180339887498949
Variance
Variance in statistics (sample variance) is the average of the squared differences between each sample value and the mean of all sample values, i.e., mean((x - x.mean())** 2).
In other words, standard deviation is the square root of variance.
Example
import
numpy
as
np
(
np
.
var
(
[
1
,
2
,
3
,
4
]
)
)
The output is:
1.25