List standard deviation

I want to find the mean and standard deviation of 1, 2, ... digits of several (Z) lists. For example, I have

A_rank=[0.8,0.4,1.2,3.7,2.6,5.8] B_rank=[0.1,2.8,3.7,2.6,5,3.4] C_Rank=[1.2,3.4,0.5,0.1,2.5,6.1] # etc (up to Z_rank )... 

Now I want to take the average value and std *_Rank[0] , the average value and std *_Rank[1] , etc.
(i.e. the average and std of the 1st digit from all lists (A..Z) _rank.
average and std 2nd digit from all lists (A..Z) _rank.
average and std 3rd digits ...; etc).

+77
python list standard-deviation
Mar 13 '13 at 15:38
source share
8 answers

Since Python 3.4 / PEP450 there is a statistics module in the standard library that has a stdev method to calculate the standard deviation of iterations like yours:

 >>> A_rank = [0.8, 0.4, 1.2, 3.7, 2.6, 5.8] >>> import statistics >>> statistics.stdev(A_rank) 2.0634114147853952 
+104
02 Feb '14 at 0:27
source share

I would put A_Rank et al into a 2D NumPy array, and then use numpy.mean() and numpy.std() to calculate the means and standard deviations:

 In [17]: import numpy In [18]: arr = numpy.array([A_rank, B_rank, C_rank]) In [20]: numpy.mean(arr, axis=0) Out[20]: array([ 0.7 , 2.2 , 1.8 , 2.13333333, 3.36666667, 5.1 ]) In [21]: numpy.std(arr, axis=0) Out[21]: array([ 0.45460606, 1.29614814, 1.37355985, 1.50628314, 1.15566239, 1.2083046 ]) 
+92
Mar 13 '13 at 15:42
source share

Here is some clean Python code that you can use to calculate the mean and standard deviation.

All of the code below is based on the statistics module in Python 3.4+.

 def mean(data): """Return the sample arithmetic mean of data.""" n = len(data) if n < 1: raise ValueError('mean requires at least one data point') return sum(data)/n # in Python 2 use sum(data)/float(n) def _ss(data): """Return sum of square deviations of sequence data.""" c = mean(data) ss = sum((xc)**2 for x in data) return ss def stddev(data, ddof=0): """Calculates the population standard deviation by default; specify ddof=1 to compute the sample standard deviation.""" n = len(data) if n < 2: raise ValueError('variance requires at least two data points') ss = _ss(data) pvar = ss/(n-ddof) return pvar**0.5 

Note: to improve accuracy when summing floating point numbers, the statistics module uses the user-defined function _sum and not the built-in sum that I used instead.

Now we have, for example:

 >>> mean([1, 2, 3]) 2.0 >>> stddev([1, 2, 3]) # population standard deviation 0.816496580927726 >>> stddev([1, 2, 3], ddof=1) # sample standard deviation 0.1 
+44
Jan 03 '15 at 18:48
source share

In Python 2.7.1, you can calculate the standard deviation using numpy.std() for:

  • Population std . Just use numpy.std() with no extra arguments other than your data list.
  • Std example : you need to pass ddof (i.e. Delta Degrees Degree of Freedom) to 1, as shown in the following example:

numpy.std (<your-list>, ddof = 1 )

In calculations, the divisor N is used - ddof , where N is the number of elements. By default, ddof is zero.

It computes the std pattern, not std.

+21
Jul 12 '15 at 9:22
source share

In python 2.7 you can use NumPy numpy.std() gives the standard deviation of the population .

In Python 3.4, statistics.stdev() returns the standard standard deviation. The pstdv() function is the same as numpy.std() .

+9
Apr 24 '14 at 16:15
source share

pure python code:

 from math import sqrt def stddev(lst): mean = float(sum(lst)) / len(lst) return sqrt(float(reduce(lambda x, y: x + y, map(lambda x: (x - mean) ** 2, lst))) / len(lst)) 
+4
Jun 08 '17 at 14:45
source share

Other answers explain how to make std dev in python enough, but no one explains how to do the fancy workaround you described.

I'm going to assume that AZ is the whole population. If you do not see Ome , answer the question of how to draw a conclusion from the sample.

So, to get the standard deviation / average of the first digit of each list, you need something like the following:

 #standard deviation numpy.std([A_rank[0], B_rank[0], C_rank[0], ..., Z_rank[0]]) #mean numpy.mean([A_rank[0], B_rank[0], C_rank[0], ..., Z_rank[0]]) 

To shorten the code and generalize it to any nth digit, use the following function that I generated for you:

 def getAllNthRanks(n): return [A_rank[n], B_rank[n], C_rank[n], D_rank[n], E_rank[n], F_rank[n], G_rank[n], H_rank[n], I_rank[n], J_rank[n], K_rank[n], L_rank[n], M_rank[n], N_rank[n], O_rank[n], P_rank[n], Q_rank[n], R_rank[n], S_rank[n], T_rank[n], U_rank[n], V_rank[n], W_rank[n], X_rank[n], Y_rank[n], Z_rank[n]] 

Now you can just get stdd and the value of all n-th places from AZ as follows:

 #standard deviation numpy.std(getAllNthRanks(n)) #mean numpy.mean(getAllNthRanks(n)) 
+3
May 22 '17 at 16:11
source share

Using python, here are a few methods:

 import statistics as st n = int(input()) data = list(map(int, input().split())) 

Approach 1 - using the function

 stdev = st.pstdev(data) 

Approach 2: calculate the variance and take its square root

 variance = st.pvariance(data) devia = math.sqrt(variance) 

Approach 3: using basic math

 mean = sum(data)/n variance = sum([((x - mean) ** 2) for x in X]) / n stddev = variance ** 0.5 print("{0:0.1f}".format(stddev)) 

Remarks:

  • variance calculates population variance
  • pvariance calculates the variance of the entire population
  • similar differences between stdev and pstdev
0
Apr 14 '19 at 6:49
source share



All Articles