forked from AIPI510/aipi510-fall24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathta5_deftones.py
121 lines (91 loc) · 3.23 KB
/
ta5_deftones.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
# Set a seed for reproducibility
np.random.seed(0)
def get_mean(data):
'''
gets the mean from a numpy array
Inputs:
data(np.ndarray): input data
Returns:
the mean of data
'''
return np.mean(data)
def get_median(data):
'''
gets the median from a numpy array
Inputs:
data(np.ndarray): input data
Returns:
the median of data
'''
return np.median(data)
def get_mode(data):
'''
gets the mode from a numpy array
Inputs:
data(np.ndarray): input data
Returns:
the mode of data
'''
return stats.mode(data).mode
def plot_symmetric_dist(loc, scale, size):
'''
creates and shows a plot for a symmetric normal distribution
Inputs:
loc(float): the mean for which to center the distribution
scale(float): the standard deviation of the distribution
size(int): the number of datapoints for the distribution
'''
# Generate a large normally distributed dataset
data = np.random.normal(loc=loc, scale=scale, size=size)
# Calculate mean, median, and mode
mean = get_mean(data)
median = get_median(data)
mode = get_mode(np.round(data))
# Plot the distribution with mean, median, and mode
plt.figure(figsize=(10, 6))
hist = sns.histplot(data, kde=True, color='skyblue', bins=30)
hist.lines[0].set_color('blue')
# Add vertical lines for mean, median, and mode
plt.axvline(mean, color='red', linestyle='--', label=f'Mean: {mean:.2f}')
plt.axvline(median, color='green', linestyle='--', label=f'Median: {median:.2f}')
plt.axvline(mode, color='blue', linestyle='--', label=f'Mode: {mode:.2f}')
# title the plot, give it a legend and show the plot
plt.title('Perfectly Symmetric Normal Distribution')
plt.legend()
plt.show()
def plot_skewed_dist(a, size, plot_title):
'''
creates and shows a plot for a skewed distribution
Inputs:
a(float): the skewness of the distribution
size(int): the number of datapoints for the distribution
plot_title(str): what to title the plot
'''
# Generate a large skewed dataset
data = stats.skewnorm.rvs(a=a, size=size)
# Calculate mean, median, and mode
mean = get_mean(data)
median = get_median(data)
mode = get_mode(np.round(data, 1))
# Plot the distribution with mean, median, and mode
plt.figure(figsize=(10, 6))
hist = sns.histplot(data, kde=True, color='skyblue', bins=30)
hist.lines[0].set_color('blue')
# Add vertical lines for mean, median, and mode
plt.axvline(mean, color='red', linestyle='--', label=f'Mean: {mean:.2f}')
plt.axvline(median, color='green', linestyle='--', label=f'Median: {median:.2f}')
plt.axvline(mode, color='blue', linestyle='--', label=f'Mode: {mode:.2f}')
# title the plot, give it a legend and show the plot
plt.title(plot_title)
plt.legend()
plt.show()
def main():
plot_symmetric_dist(0, 1, 1000000)
plot_skewed_dist(5, 100000, 'Distribution Skewed to the Right')
plot_skewed_dist(-5, 100000, 'Distribution Skewed to the Left')
if __name__ == "__main__":
main()