This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def range_(x): | |
return max(x) - min(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def quantile(x, p): | |
p_index = int(p*len(x)) | |
return sorted(x)[p_index] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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) |