﻿ NumPy: Compute the mean, standard deviation, and variance of a given array along the second axis - w3resource # NumPy: Compute the mean, standard deviation, and variance of a given array along the second axis

## NumPy Statistics: Exercise-7 with Solution

Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis.

From Wikipedia: There are several kinds of means in various branches of mathematics (especially statistics).
For a data set, the arithmetic mean, also called the mathematical expectation or average, is the central value of a discrete set of numbers: specifically, the sum of the values divided by the number of values. The arithmetic mean of a set of numbers is typically denoted by $\overline{x}$, pronounced "$x$ bar". If the data set were based on a series of observations obtained by sampling from a statistical population, the arithmetic mean is the sample mean (denoted $\overline{x}$) to distinguish it from the mean of the underlying distribution.
In probability and statistics, the population mean, or expected value, are a measure of the central tendency either of a probability distribution or of the random variable characterized by that distribution. In the case of a discrete probability distribution of a random variable $X$, the mean is equal to the sum over every possible value weighted by the probability of that value; that is, it is computed by taking the product of each possible value $x$ of $X$ and its probability $p\left(x\right)$, and then adding all these products together, giving . An analogous formula applies to the case of a continuous probability distribution. Not every probability distribution has a defined mean; see the Cauchy distribution for an example. Moreover, for some distributions the mean is infinite.

Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis.

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(6)
print("\nOriginal array:")
print(x)
r1 = np.mean(x)
r2 = np.average(x)
assert np.allclose(r1, r2)
print("\nMean: ", r1)
r1 = np.std(x)
r2 = np.sqrt(np.mean((x - np.mean(x)) ** 2 ))
assert np.allclose(r1, r2)
print("\nstd: ", 1)
r1= np.var(x)
r2 = np.mean((x - np.mean(x)) ** 2 )
assert np.allclose(r1, r2)
print("\nvariance: ", r1)



Sample Output:

Original array:
[0 1 2 3 4 5]

Mean:  2.5

std:  1

variance:  2.9166666666666665


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Returns the symmetric difference between two lists, after applying the provided function to each list element of both

Example:

def tips_symmetric_difference_by(p, q, fn):
_p, _q = set(map(fn, p)), set(map(fn, q))
return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p]
from math import floor
print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))


Output:

[2.4, 6.8]