Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy statistical functions

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:

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:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

3

,

7

,

5

]

,

[

8

,

4

,

3

]

,

[

2

,

4

,

9

]

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Call the amin() function:

'

)

print

(

np

.

amin

(

a

,

1

)

)

print

(

'

\n

'

)

print

(

'

Call the amin() function again:

'

)

print

(

np

.

amin

(

a

,

0

)

)

print

(

'

\n

'

)

print

(

'

Call the amax() function:

'

)

print

(

np

.

amax

(

a

)

)

print

(

'

\n

'

)

print

(

'

Call the amax() function again:

'

)

print

(

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:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

3

,

7

,

5

]

,

[

8

,

4

,

3

]

,

[

2

,

4

,

9

]

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Call the ptp() function:

'

)

print

(

np

.

ptp

(

a

)

)

print

(

'

\n

'

)

print

(

'

Call the ptp() function along axis 1:

'

)

print

(

np

.

ptp

(

a

,

axis

=

1

)

)

print

(

'

\n

'

)

print

(

'

Call the ptp() function along axis 0:

'

)

print

(

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:

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

]

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

Call the percentile() function:

'

)

The 50th percentile is the median of a after sorting

print

(

np

.

percentile

(

a

,

50

)

)

axis is 0, computed along columns

print

(

np

.

percentile

(

a

,

50

,

axis

=

0

)

)

axis is 1, computed along rows

print

(

np

.

percentile

(

a

,

50

,

axis

=

1

)

)

Keep dimensions unchanged

print

(

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:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

30

,

65

,

70

]

,

[

80

,

95

,

10

]

,

[

50

,

90

,

60

]

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Call the median() function:

'

)

print

(

np

.

median

(

a

)

)

print

(

'

\n

'

)

print

(

'

Call the median() function along axis 0:

'

)

print

(

np

.

median

(

a

,

axis

=

0

)

)

print

(

'

\n

'

)

print

(

'

Call the median() function along axis 1:

'

)

print

(

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:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

3

,

4

,

5

]

,

[

4

,

5

,

6

]

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Call the mean() function:

'

)

print

(

np

.

mean

(

a

)

)

print

(

'

\n

'

)

print

(

'

Call the mean() function along axis 0:

'

)

print

(

np

.

mean

(

a

,

axis

=

0

)

)

print

(

'

\n

'

)

print

(

'

Call the mean() function along axis 1:

'

)

print

(

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:

Example

import

numpy

as

np

a

=

np

.

array

(

[

1

,

2

,

3

,

4

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Call the average() function:

'

)

print

(

np

.

average

(

a

)

)

print

(

'

\n

'

)

When no weights are specified, it is equivalent to the mean function

wts

=

np

.

array

(

[

4

,

3

,

2

,

1

]

)

print

(

'

Call the average() function again:

'

)

print

(

np

.

average

(

a

,

weights

=

wts

)

)

print

(

'

\n

'

)

If the returned parameter is set to true, the sum of weights is returned

print

(

'

Sum of weights:

'

)

print

(

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

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Modified array:

'

)

wt

=

np

.

array

(

[

3

,

5

]

)

print

(

np

.

average

(

a

,

axis

=

1

,

weights

=

wt

)

)

print

(

'

\n

'

)

print

(

'

Modified array:

'

)

print

(

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

print

(

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

print

(

np

.

var

(

[

1

,

2

,

3

,

4

]

)

)

The output is:

1.25