Skip to content

Commit

Permalink
add numpy lib to calculate standard deviation
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMac committed Jun 15, 2019
1 parent e14a13c commit 2d616f1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions P38_data_sd_analysis.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy
import numpy as np

temperature=[31,32,32,31,28,29,31,38,32,31,30,29,30,31,26]

sorted_temp=sorted(temperature)
mean_temp=numpy.mean(temperature)
mean_temp=np.mean(temperature)

print ('original temperature are :',temperature)

print ('sorted temperature are :',sorted_temp)

print ('mean of temperature is: ', mean_temp)

print ('median of temperature is: ', numpy.median(temperature))
print ('median of temperature is: ', np.median(temperature))

print ('\n')

Expand All @@ -23,18 +23,22 @@
num_items=len(temperature)
product=1.

print ('=======method-1 to calculate standard deviation, step by step=======')

for temp in temperature:
diff=temp-mean_temp
sqrt_diff=diff**2
sqrt_diff_list.append(sqrt_diff)
average_sqrt_diff=numpy.mean(sqrt_diff_list)
average_sqrt_diff=np.mean(sqrt_diff_list)
product*=temp

standard_deviation = numpy.sqrt(average_sqrt_diff)
standard_deviation = np.sqrt(average_sqrt_diff)
geometric_mean = product ** (1./num_items)

print ('Standard Deviation = ',standard_deviation)
print ('\n')

print ('Geometric Mean = ',geometric_mean)

print ('=======method-2 to calculate standard deviation, using numpy directly =======')

print ('Standard Deviation = ',np.std(temperature))

0 comments on commit 2d616f1

Please sign in to comment.