File size: 3,698 Bytes
c6b92c7
072885d
 
 
12dcaf7
072885d
 
 
5554539
 
 
 
 
 
b827309
 
5554539
 
 
 
 
6e73e6e
5554539
6e73e6e
 
3f3bffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import bertopic
import plotly.express as px
import matplotlib as mp

st.set_page_config(page_title="Topic Modeling with Bertopic")

from datasets import load_dataset

st.markdown("""
https://github.com/pinecone-io/examples/tree/master/learn/algos-and-libraries/bertopic
""")

# data = load_dataset('jamescalam/python-reddit')
data = load_dataset("awacke1/LOINC-Panels-and-Forms")





from datasets import load_dataset

geo = load_dataset('jamescalam/world-cities-geo', split='train')
st.write(geo)






import plotly.express as px

palette = ['#1c17ff', '#faff00', '#8cf1ff', '#000000', '#030080', '#738fab']

fig = px.scatter_3d(
    x=geo['x'], y=geo['y'], z=geo['z'],
    color=geo['continent'],
    custom_data=[geo['country'], geo['city']],
    color_discrete_sequence=palette
)
fig.update_traces(
    hovertemplate="\n".join([
        "city: %{customdata[1]}",
        "country: %{customdata[0]}"
    ])
)


fig.write_html("umap-earth-3d.html", include_plotlyjs="cdn", full_html=False)


import numpy as np

geo_arr = np.asarray([geo['x'], geo['y'], geo['z']]).T
geo_arr = geo_arr / geo_arr.max()

st.markdown(geo_arr[:5])


import umap


colors = geo['continent']
c_map = {
    'Africa': '#8cf1ff',
    'Asia': '#1c17ff',
    'Europe': '#faff00',
    'North America': '#738fab',
    'Oceania': '#030080',
    'South America': '#000000'
}
for i in range(len(colors)):
    colors[i] = c_map[colors[i]]
colors[:5]



import matplotlib.pyplot as plt
import seaborn as sns
from tqdm.auto import tqdm

fig, ax = plt.subplots(3, 3, figsize=(14, 14))
nns = [2, 3, 4, 5, 10, 15, 30, 50, 100]
i, j = 0, 0
for n_neighbors in tqdm(nns):
    fit = umap.UMAP(n_neighbors=n_neighbors)
    u = fit.fit_transform(geo_arr)
    sns.scatterplot(x=u[:,0], y=u[:,1], c=colors, ax=ax[j, i])
    ax[j, i].set_title(f'n={n_neighbors}')
    if i < 2: i += 1
    else: i = 0; j += 1


target = geo['continent']
t_map = {
    'Africa': 0,
    'Asia': 1,
    'Europe': 2,
    'North America': 3,
    'Oceania': 4,
    'South America': 5
}
for i in range(len(target)):
    target[i] = t_map[target[i]]

fig, ax = plt.subplots(3, 3, figsize=(14, 14))
nns = [2, 3, 4, 5, 10, 15, 30, 50, 100]
i, j = 0, 0
for n_neighbors in tqdm(nns):
    fit = umap.UMAP(n_neighbors=n_neighbors)
    u = fit.fit_transform(geo_arr, y=target)
    sns.scatterplot(x=u[:,0], y=u[:,1], c=colors, ax=ax[j, i])
    ax[j, i].set_title(f'n={n_neighbors}')
    if i < 2: i += 1
    else: i = 0; j += 1

import umap

fit = umap.UMAP(n_neighbors=50, min_dist=0.5)
u = fit.fit_transform(geo_arr)

fig = px.scatter(
    x=u[:,0], y=u[:,1],
    color=geo['continent'],
    custom_data=[geo['country'], geo['city']],
    color_discrete_sequence=palette
)
fig.update_traces(
    hovertemplate="\n".join([
        "city: %{customdata[1]}",
        "country: %{customdata[0]}"
    ])
)

fig.write_html("umap-earth-2d.html", include_plotlyjs="cdn", full_html=False)

import pandas as pd

umapped = pd.DataFrame({
    'x': u[:,0],
    'y': u[:,1],
    'continent': geo['continent'],
    'country': geo['country'],
    'city': geo['city']
})

umapped.to_csv('umapped.csv', sep='|', index=False)

from sklearn.decomposition import PCA

pca = PCA(n_components=2)  # this means we will create 2-d space
p = pca.fit_transform(geo_arr)
fig = px.scatter(
    x=p[:,0], y=p[:,1],
    color=geo['continent'],
    custom_data=[geo['country'], geo['city']],
    color_discrete_sequence=palette
)
fig.update_traces(
    hovertemplate="\n".join([
        "city: %{customdata[1]}",
        "country: %{customdata[0]}"
    ])
)

fig.write_html("pca-earth-2d.html", include_plotlyjs="cdn", full_html=False)