Skip to content

Instantly share code, notes, and snippets.

View albertyumol's full-sized avatar
:octocat:
just bash-ing around

Albert 'Bash' Yumol albertyumol

:octocat:
just bash-ing around
  • Manila, Philippines
View GitHub Profile
@albertyumol
albertyumol / standard_deviation.py
Created October 23, 2019 01:32
Calculate the standard deviation.
import numpy as np
def standard_deviation(x):
x_bar = np.mean(x)
N = len(x)
deviation_mean = [x_i - x_bar for x_i in x]
return np.sqrt(np.dot(deviation_mean, deviation_mean) / N)
@albertyumol
albertyumol / interquartile_range.py
Created October 23, 2019 01:12
Calculate the interquartile range in Python.
def quantile(x, p):
p_index = int(p*len(x))
return sorted(x)[p_index]
def interquartile_range(x):
return quantile(x, 0.75) - quantile(x, 0.25)
@albertyumol
albertyumol / variance.py
Created October 23, 2019 00:45
Calculate the variance in Python.
import numpy as np
def variance(x):
x_bar = np.mean(x)
N = len(x)
deviation_mean = [x_i - x_bar for x_i in x]
return np.dot(deviation_mean, deviation_mean) / N
@albertyumol
albertyumol / range.py
Created October 22, 2019 23:15
Calculate the range in Python.
def range_(x):
return max(x) - min(x)
@albertyumol
albertyumol / quantile.py
Created October 22, 2019 23:14
Calculate quantile in Python.
def quantile(x, p):
p_index = int(p*len(x))
return sorted(x)[p_index]
@albertyumol
albertyumol / mode.py
Created October 22, 2019 23:02
Calculating mode in Python.
from collections import Counter
def mode(x):
counts = Counter(x)
max_count = max(counts.values())
return [i for i, j in counts.items() if j == max_count]
@albertyumol
albertyumol / eskwelabs_exam_scores.py
Created October 22, 2019 03:00
Distribution of Scores from the Eskwelabs Entrance Exam
import random
import pandas as pd
import matplotlib.pyplot as plt
random.seed( 30 ) #for replicability
n = 100
y = []
for i in range(n):
y += [random.randint(25, 50)]
@albertyumol
albertyumol / median.py
Created October 22, 2019 02:08
Calculating mean in Python.
def median(x):
sorted_ = sorted(x)
N = len(x)
middle_value = N // 2
if n % 2 != 0: #CASE 1: if N is odd
M = sorted_[middle_value]
else: #CASE 2: if N is odd
M = (sorted_[(middle_value) - 1] + sorted_[middle_value]) / 2
return M
@albertyumol
albertyumol / mean.py
Created October 21, 2019 23:00
Calculating mean in Python.
#assuming that x is a list of values and is not empty
#because we are not allowed to divide by 0
def mean(x):
return sum(x) / len(x)
### Set county location values, drough level values, marker sizes (according to county size), colormap and title
x, y = m(dr_m.nth(0).longitude.tolist(), dr_m.nth(0).latitude.tolist())
colors = (dr_m.nth(0).code).tolist()
sizes = (dr_m.nth(0).count1*80).tolist()
cmap = plt.cm.plasma#autumn_r
#cmap = plt.cm.hot_r
sm = ScalarMappable(cmap=cmap)
plt.title('Social Movement (Year-Month): '+dr_m.nth(0).event_date.iloc[0].strftime('%Y-%m'))
### Display the scatter plot and its colorbar (0-5)