108 lines
2.1 KiB
Python
108 lines
2.1 KiB
Python
|
#!/bin/python3
|
||
|
import numpy as np
|
||
|
|
||
|
# import pandas as pd
|
||
|
import time
|
||
|
|
||
|
# Print the current time
|
||
|
print(time.strftime("%Y-%m-%d %H:%M:%S"))
|
||
|
|
||
|
a = {1, 2, 3}
|
||
|
assert 1 in a, "1 is not in a"
|
||
|
assert 4 not in a, "4 is in a"
|
||
|
|
||
|
b = {"a": 1, "b": 2, "c": 3}
|
||
|
assert "a" in b, "a is not in b"
|
||
|
assert "d" not in b, "d is in b"
|
||
|
|
||
|
c = a.union(b.values())
|
||
|
assert 1 in c, "1 is not in c"
|
||
|
|
||
|
# Xor
|
||
|
assert 1 ^ 1 == 0
|
||
|
assert 1 ^ 0 == 1
|
||
|
assert 0 ^ 1 == 1
|
||
|
assert 0 ^ 0 == 0
|
||
|
|
||
|
a = 0b1010 # 10
|
||
|
a = a ^ 0b1010 # 10
|
||
|
assert a == 0, "a should be 0"
|
||
|
|
||
|
# Poisson distribution
|
||
|
# print(np.random.poisson(5, 1))
|
||
|
# print(np.random.binomial(10, 0.5, 10))
|
||
|
|
||
|
|
||
|
# Probability mass function of poisson distribution
|
||
|
def pmf_poisson(k, l):
|
||
|
return (l**k) * np.exp(-l) / np.math.factorial(k)
|
||
|
|
||
|
|
||
|
def mean(l):
|
||
|
return sum(l) / len(l)
|
||
|
|
||
|
|
||
|
def median(l):
|
||
|
l.sort()
|
||
|
n = len(l)
|
||
|
if n % 2 == 0:
|
||
|
return (l[n // 2 - 1] + l[n // 2]) / 2
|
||
|
else:
|
||
|
return l[n // 2]
|
||
|
|
||
|
def mode(l):
|
||
|
return max(set(l), key=l.count)
|
||
|
|
||
|
|
||
|
assert median([1, 2, 3, 4]) == 2.5
|
||
|
assert median([1, 2, 3, 4, 5]) == 3
|
||
|
|
||
|
quantile = np.quantile
|
||
|
# def quantile(l, q):
|
||
|
|
||
|
assert quantile([1, 2, 3, 4, 5], 0.5) == 3
|
||
|
assert quantile([1, 2, 3, 4, 5], 0.25) == 2
|
||
|
assert quantile([1, 2, 3, 4, 5], 0.75) == 4
|
||
|
|
||
|
|
||
|
def whisker_plot(l):
|
||
|
q1 = quantile(l, 0.25) # Lower quartile
|
||
|
q3 = quantile(l, 0.75) # Upper quartile
|
||
|
iqr = q3 - q1 # Interquartile range
|
||
|
lw = q1 - 1.5 * iqr # Lower whisker
|
||
|
uw = q3 + 1.5 * iqr # Upper whisker
|
||
|
return lw, q1, q3, uw
|
||
|
|
||
|
|
||
|
def outliers(l):
|
||
|
lw, q1, q3, uw = whisker_plot(l)
|
||
|
return [x for x in l if x < lw or x > uw]
|
||
|
|
||
|
|
||
|
values = [16, 18, 28, 13, 50, 31, 25, 22, 18, 23, 29, 38]
|
||
|
|
||
|
assert median(values) == 24
|
||
|
# assert quantile(values, 0.25) == 18
|
||
|
# assert quantile(values, 0.75) == 30
|
||
|
|
||
|
print(quantile(values, 0.25))
|
||
|
print(quantile(values, 0.75))
|
||
|
|
||
|
|
||
|
print(whisker_plot(values))
|
||
|
print(outliers(values))
|
||
|
|
||
|
# Plot the box diagram
|
||
|
import matplotlib.pyplot as plt
|
||
|
|
||
|
# plt.boxplot(values, vert=False)
|
||
|
# plt.show()
|
||
|
|
||
|
|
||
|
# Plot the poisson distribution when lambda = 5
|
||
|
# import matplotlib.pyplot as plt
|
||
|
|
||
|
# s = np.random.poisson(3, 10000)
|
||
|
# count, bins, ignored = plt.hist(s, 10, density=True)
|
||
|
# plt.show()
|