-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr2.py
181 lines (141 loc) · 5.11 KB
/
pr2.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from typing import Any
import pandas as pd
import time
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
class P2:
data: pd.DataFrame
data2: pd.DataFrame
first_1000: Any
first_labels_1000: Any
@staticmethod
def t1():
P2.data = pd.read_csv('q1data.csv')
@staticmethod
def t2():
print('t2:')
P2.data.info()
print('t2:\n', P2.data.head())
print('\n')
if P2.data.isnull().values.any():
P2.data = P2.data.dropna()
@staticmethod
def t3():
import plotly.graph_objs as go
fig = go.Figure()
fig.add_trace(go.Bar(
x=P2.data.six_regions,
y=P2.data.life_exp,
marker=dict(
color=list(range(len(P2.data.country))),
coloraxis='coloraxis',
line=dict(color='black', width=2)
)
))
fig.update_layout(
title='Population in the Six Regions',
title_x=0.5,
title_font_size=20,
xaxis_title='Six Regions',
xaxis_title_font_size=16,
xaxis_tickfont_size=14,
yaxis_title='Population',
yaxis_title_font_size=16,
yaxis_tickfont_size=14,
width=1400,
height=700,
margin=dict(l=0, r=0, t=30, b=0)
)
fig.update_xaxes(showline=True, tickangle=-45, linewidth=2, linecolor='black', gridcolor='ivory')
fig.update_yaxes(showline=True, tickangle=-45, linewidth=2, linecolor='black', gridcolor='ivory')
fig.show()
@staticmethod
def t4():
import plotly.express as px
colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen']
fig = px.pie(P2.data, values='population', names='six_regions')
fig.update_traces(
hoverinfo='label+percent',
textinfo='value',
textfont_size=20,
marker=dict(colors=colors, line=dict(color='#000000', width=2))
)
fig.update_layout(title='Population and Countries', title_x=0.5)
fig.show()
@staticmethod
def t5():
def plot(x, y, title, x_label, y_label):
plt.rc('axes', axisbelow=True)
px = 1 / plt.rcParams['figure.dpi'] # pixel in inches
plt.subplots(figsize=(1920*px, 700*px))
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.scatter(x, y, color='white', edgecolor='black', linewidth=2, zorder=2)
plt.plot(x, y, color='red', zorder=1)
plt.grid(linewidth=2, color='mistyrose')
plt.show()
plot(P2.data.income, P2.data.life_exp, 'Income vs Life Expectancy', 'Income', 'Life Expectancy')
plot(P2.data.income, P2.data.population, 'Income vs Population', 'Income', 'Population')
@staticmethod
def t6():
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import TSNE
P2.data2 = pd.read_csv('mnist_train.csv')
print('t6:', P2.data2.head())
labels = P2.data2['label']
labels_dropped = P2.data2.drop('label', axis=1)
standard = StandardScaler().fit_transform(labels_dropped)
start_time = time.time()
P2.first_1000 = standard[0:1000, :]
P2.first_labels_1000 = labels[0:1000]
def plot(perplexity):
model = TSNE(n_components=2, perplexity=perplexity, random_state=123)
tsne_features = model.fit_transform(P2.first_1000)
tsne_data = np.vstack((tsne_features.T, P2.first_labels_1000)).T
tsne_df = pd.DataFrame(data=tsne_data, columns=['X', 'Y', 'class_type'])
sns.scatterplot(data=tsne_df, x='X', y='Y', hue='class_type', palette='bright')
plt.title(f'Perplexity {perplexity}')
plt.show()
plot(5)
plot(25)
plot(50)
end_time = time.time()
elapsed_time = end_time - start_time
print(f't6: Elapsed time: {elapsed_time} seconds')
@staticmethod
def t7():
import umap
start_time = time.time()
def plot(n_neighbors, min_dist):
embedding = umap.UMAP(
n_neighbors=n_neighbors,
min_dist=min_dist,
random_state=123,
metric='correlation'
).fit_transform(P2.first_1000)
umap_data = np.vstack((embedding.T, P2.first_labels_1000)).T
umap_df = pd.DataFrame(data=umap_data, columns=['X', 'Y', 'class_type'])
plt.title(f'n_neighbors={n_neighbors}, min_dist={min_dist}')
sns.scatterplot(data=umap_df, x='X', y='Y', hue='class_type', palette='bright')
plt.show()
plot(5, 0.1)
plot(5, 0.5)
plot(25, 0.1)
end_time = time.time()
elapsed_time = end_time - start_time
print(f't7: Elapsed time: {elapsed_time} seconds')
plot(25, 0.5)
plot(50, 0.5)
plot(50, 1.0)
if __name__ == '__main__':
import warnings
warnings.filterwarnings('ignore')
P2.t1()
P2.t2()
P2.t3()
P2.t4()
P2.t5()
P2.t6()
P2.t7()