Skip to content

Instantly share code, notes, and snippets.

@johnwcothran
Created February 20, 2018 22:05
Show Gist options
  • Save johnwcothran/88d049d4d53e389c9b589fa9a7934cd2 to your computer and use it in GitHub Desktop.
Save johnwcothran/88d049d4d53e389c9b589fa9a7934cd2 to your computer and use it in GitHub Desktop.
import pandas as pd
# Part 1 functions
def mean(arr):
return sum(arr) / len(arr)
def isEven (arr):
return arr % 2 == 0
def half (arr):
if isEven(len(arr)):
return int(len(arr)/2) - 1
else:
return int(len(arr)/2)
def median (arr):
sortedList = sorted(arr)
index = half(sortedList)
if isEven(len(sortedList)):
return mean([ sortedList[index], sortedList[index + 1] ])
else:
return sortedList[index]
# Part 2 functions
def square(x):
return x * x
def sumOfSquaredDifferences (arr):
xBar = mean(arr)
differences = map(lambda x: x - xBar, arr)
squaredDifferences = map(square, differences)
return sum(squaredDifferences)
def variance (arr):
n = len(arr)
return sumOfSquaredDifferences(arr) / (n-1)
def sqrt (x):
return x**(1/2)
def stDev (arr):
return sqrt(variance(arr))
# data
data = [
{"name": "John", "distance": 5602, "high-speed-running": 504},
{"name": "Mike", "distance": 5242, "high-speed-running": 622},
{"name": "Chad", "distance": 4825, "high-speed-running": 453},
{"name": "Phil", "distance": 611, "high-speed-running": 500},
{"name": "Tyler", "distance": 5436, "high-speed-running": 409}
]
df = pd.DataFrame(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment