-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (39 loc) · 1.48 KB
/
utils.py
File metadata and controls
54 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# stats functions
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
def z_score(data):
"""Return the z-score of each element in data"""
mean_val = np.mean(data)
std_val = np.std(data)
return [(x - mean_val) / std_val for x in data]
def mse(y, y_hat):
"""Return the mean squared error of y and y_hat"""
return np.mean((y - y_hat) ** 2)
def mape(y, y_hat):
"""Return the mean absolute percentage error of y and y_hat"""
return np.mean(np.abs((y - y_hat) / y))
def check_normality(data):
"""check if data is normally distributed using visualizations and shapiro-wilk test."""
# ensure data is a numpy array for consistency
data = np.array(data)
# visualizations: histogram and q-q plot
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
# histogram
axs[0].hist(data, bins=20, color='skyblue', edgecolor='black')
axs[0].set_title('histogram')
axs[0].set_xlabel('value')
axs[0].set_ylabel('frequency')
# q-q plot
stats.probplot(data, dist="norm", plot=axs[1])
axs[1].set_title('q-q plot')
plt.tight_layout()
plt.show()
# shapiro-wilk test
shapiro_stat, p_value = stats.shapiro(data)
print(f"shapiro-wilk test: statistic = {shapiro_stat:.4f}, p-value = {p_value:.4f}")
if p_value > 0.05:
print("result: data is likely normally distributed (p > 0.05).")
else:
print("result: data is likely not normally distributed (p ≤ 0.05).")