-
Notifications
You must be signed in to change notification settings - Fork 0
/
TsneWebApp.py
192 lines (171 loc) · 5.87 KB
/
TsneWebApp.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
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
# author: Ryohei Yamaguchi
import os
import numpy as np
import pandas as pd
import base64
import io
from sklearn.manifold import TSNE
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
from sklearn import datasets
"""global variable"""
# train = pd.DataFrame()
train = pd.DataFrame(datasets.load_wine()["data"])
col_options = [dict(label=x, value=x) for x in train.columns]
test = None
n_iter = 1000
n_components = 3
"""app"""
app = dash.Dash(__name__)
# mathjax = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML'
# app.scripts.append_script({'external_url': mathjax})
app.layout = html.Div(
[
html.H1("t-SNE Visualize"),
html.Div(
[
html.Div(
[
html.H2("train data"),
dcc.Upload(
id="update_train",
children=html.Div([
"Drug and Drop or ",
html.A("Select File")
]),
style={
"width": "95%",
"height": "60px",
"lineHeight": "60px",
"borderWidth": "1px",
"borderStyle": "dashed",
"textAlign": "center",
"borderRadius": "5px"
},
multiple=False
),
],
), # train
html.Div(
[
html.H2("test data"),
dcc.Upload(
id="update_test",
children=html.Div([
"Drug and Drop or ",
html.A("Select File")
]),
style={
"width": "95%",
"height": "60px",
"lineHeight": "60px",
"borderWidth": "1px",
"borderStyle": "dashed",
"textAlign": "center",
"borderRadius": "5px"
},
multiple=False
),
]
), # test
html.Div(
[
html.H2("initial column"),
dcc.Dropdown(id="column", options=col_options)
]
), # initial column
html.Div(
[
html.H2("perplexity"),
dcc.Input(
id="perplexity",
type="number",
min=10,
max=200,
step=10
)
]
), # perplexity Input ==> Slider
html.Div(
[
html.H2("learning rate"),
dcc.Input(
id="learning_rate",
type="number",
min=1,
max=40
)
]
), # learning rate
html.Div(
[
html.H2("calculation"),
html.Button(
"Run",
id="run_button",
n_clicks=0
)
]
), # run
html.Div(
[
html.H2("color"),
dcc.Dropdown(id="color", options=col_options)
]
) # color
],
style={"width": "25%", "float": "left"}
),
dcc.Graph(id="graph", style={"width": "70%", "display": "inline-block"})
]
)
@app.callback(
Output("graph", "figure"),
[
Input("train data", "contents"),
Input("column", "value"),
Input("run_button", "n_clicks"),
Input("perplexity", "value"),
Input("learning_rate", "value"),
Input("color", "value")
]
)
def calc_tsne(train_contents, column, n_clicks, perplexity, learning_rate, color):
global train
if train_contents is not None:
train = contents2dataframe(train_contents)
if perplexity is None:
perplexity = 30
if learning_rate is None:
learning_rate = 10
if n_clicks > 0:
train_reduced = TSNE(
n_components=n_components,
perplexity=perplexity,
learning_rate=learning_rate,
n_iter=n_iter
).fit_transform(train.loc[:, column:])
train_reduced = pd.DataFrame(train_reduced, columns=[f"components {i+1}" for i in range(n_components)])
train_reduced = pd.concat([train.loc[:, column:], train_reduced], axis=1)
x, y, z = "components 1", "components 2", "components 3"
else:
train_reduced = train.copy()
col = train_reduced.columns
x, y, z = col[0], col[1], col[2]
return px.scatter_3d(
train_reduced,
x=x,
y=y,
z=z,
color=color
)
def contents2dataframe(contents):
content_type, content_string = contents.split(",")
decoded = base64.b64decode(content_string)
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")))
return df
app.run_server(port=8045, debug=True)