Skip to content

Commit bfae068

Browse files
committed
2 parents 6c1d0cb + e3cbba5 commit bfae068

3 files changed

Lines changed: 166 additions & 53 deletions

File tree

notebooks/Clusters.html

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/T2 - 2 - Data Cleaning - Funciones de distribución de probabilidad.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@
253253
"metadata": {},
254254
"source": [
255255
"* Generamos dos números aleatorios uniforme x e y entre 0 y 1 en total 1000 veces.\n",
256-
"* Calcularemos x^2 + y^2\n",
257-
" * Si el valor es inferior a 1 -> estamos dentro del círculo.\n",
258-
" * Si el valor es superior a 1 -> estamos fuera del círculo.\n",
256+
"* Calcularemos $z = x^2 + y^2$:\n",
257+
" * Si $z < 1 \\rightarrow$ estamos dentro del círculo.\n",
258+
" * Si $z \\geq 1 \\rightarrow$ estamos fuera del círculo.\n",
259259
"* Calculamos el número total de veces que están dentro del círculo y lo dividimos entre el número total de intentos para obtener una aproximación de la probabilidad de caer dentro del círculo.\n",
260260
"* Usamos dicha probabilidad para aproximar el valor de π.\n",
261261
"* Repetimos el experimento un número suficiente de veces (por ejemplo 100), para obtener (100) diferentes aproximaciones de π. \n",

notebooks/T6 - 3 - K-Means.ipynb

Lines changed: 156 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,36 @@
2424
{
2525
"data": {
2626
"text/plain": [
27-
"array([[0.24327622, 0.60318554, 0.72408892],\n",
28-
" [0.10584919, 0.56236851, 0.05975853],\n",
29-
" [0.30887799, 0.29100802, 0.67885865],\n",
30-
" [0.18020585, 0.65903446, 0.462176 ],\n",
31-
" [0.85023048, 0.74522111, 0.5739342 ],\n",
32-
" [0.57896199, 0.86265495, 0.20816652],\n",
33-
" [0.02019233, 0.27361025, 0.3371139 ],\n",
34-
" [0.54054302, 0.69888469, 0.71992822],\n",
35-
" [0.36972618, 0.61941294, 0.42553518],\n",
36-
" [0.07721486, 0.93915327, 0.48527109],\n",
37-
" [0.13592702, 0.91944956, 0.28000776],\n",
38-
" [0.01037749, 0.05480406, 0.50893094],\n",
39-
" [0.51850712, 0.91240845, 0.42468522],\n",
40-
" [0.27740363, 0.1100795 , 0.96982913],\n",
41-
" [0.81763758, 0.49553161, 0.97659639],\n",
42-
" [0.46373359, 0.23367186, 0.64050321],\n",
43-
" [0.83707915, 0.94965509, 0.62440261],\n",
44-
" [0.2289729 , 0.60970231, 0.66829221],\n",
45-
" [0.55967859, 0.02828568, 0.29046767],\n",
46-
" [0.72264686, 0.45068418, 0.14720924],\n",
47-
" [0.77373106, 0.75260356, 0.63793063],\n",
48-
" [0.25593398, 0.65074944, 0.61219348],\n",
49-
" [0.67460808, 0.92390334, 0.39246455],\n",
50-
" [0.99425515, 0.0578658 , 0.39302936],\n",
51-
" [0.12052208, 0.55358792, 0.64855949],\n",
52-
" [0.94208241, 0.21071778, 0.0786513 ],\n",
53-
" [0.25667821, 0.24529227, 0.58563222],\n",
54-
" [0.78307821, 0.46458176, 0.03342848],\n",
55-
" [0.0631332 , 0.95642914, 0.81468177],\n",
56-
" [0.93696675, 0.10054874, 0.69450692]])"
27+
"array([[0.64794608, 0.58978154, 0.457586 ],\n",
28+
" [0.86418797, 0.93960306, 0.91205676],\n",
29+
" [0.45517333, 0.83186738, 0.85112973],\n",
30+
" [0.90980972, 0.74137858, 0.28493695],\n",
31+
" [0.55135898, 0.02933044, 0.29142465],\n",
32+
" [0.02745073, 0.9058125 , 0.33332778],\n",
33+
" [0.77050918, 0.22592456, 0.79742113],\n",
34+
" [0.32355707, 0.39808319, 0.51058496],\n",
35+
" [0.54893633, 0.46920048, 0.42631236],\n",
36+
" [0.06625477, 0.01840404, 0.12293609],\n",
37+
" [0.18495803, 0.18841901, 0.44726363],\n",
38+
" [0.930496 , 0.25575929, 0.99384862],\n",
39+
" [0.18861802, 0.63469948, 0.59330767],\n",
40+
" [0.93784481, 0.85932239, 0.34059446],\n",
41+
" [0.67945637, 0.80951088, 0.25275286],\n",
42+
" [0.40479666, 0.96152374, 0.15785926],\n",
43+
" [0.87539872, 0.06414777, 0.39982498],\n",
44+
" [0.65101227, 0.65018467, 0.529103 ],\n",
45+
" [0.09670817, 0.56969742, 0.8748495 ],\n",
46+
" [0.26659514, 0.19276489, 0.63348146],\n",
47+
" [0.8400831 , 0.32396245, 0.85072164],\n",
48+
" [0.72074906, 0.66361744, 0.55696167],\n",
49+
" [0.60781852, 0.00821706, 0.9049929 ],\n",
50+
" [0.39145011, 0.16247534, 0.69021987],\n",
51+
" [0.77561671, 0.44300145, 0.58768359],\n",
52+
" [0.37390132, 0.12720886, 0.31267122],\n",
53+
" [0.85189291, 0.02612322, 0.0743608 ],\n",
54+
" [0.99605337, 0.61541593, 0.23039751],\n",
55+
" [0.97521937, 0.23951825, 0.44752608],\n",
56+
" [0.31241785, 0.83913499, 0.421719 ]])"
5757
]
5858
},
5959
"execution_count": 2,
@@ -74,8 +74,8 @@
7474
{
7575
"data": {
7676
"text/plain": [
77-
"array([[0.02019233, 0.27361025, 0.3371139 ],\n",
78-
" [0.30887799, 0.29100802, 0.67885865]])"
77+
"array([[0.85189291, 0.02612322, 0.0743608 ],\n",
78+
" [0.97521937, 0.23951825, 0.44752608]])"
7979
]
8080
},
8181
"execution_count": 3,
@@ -107,14 +107,14 @@
107107
{
108108
"data": {
109109
"text/plain": [
110-
"(array([1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
111-
" 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32),\n",
112-
" array([0.32218654, 0.40944404, 0. , 0.43565661, 0.71440968,\n",
113-
" 0.78821104, 0. , 0.47087023, 0.41919574, 0.68421461,\n",
114-
" 0.65860765, 0.27837669, 0.70333989, 0.34407788, 0.6239503 ,\n",
115-
" 0.16952531, 0.84603657, 0.32872864, 0.53176266, 0.69235268,\n",
116-
" 0.6563794 , 0.36967716, 0.78507106, 0.77832868, 0.3245677 ,\n",
117-
" 0.87615251, 0.11621495, 0.81949606, 0.7222352 , 0.6565174 ]))"
110+
"(array([1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
111+
" 1, 1, 1, 0, 0, 1, 1, 1]),\n",
112+
" array([0.47947198, 0.84748773, 0.88556269, 0.53158014, 0.37073932,\n",
113+
" 1.16415406, 0.40560768, 0.67363418, 0.48468668, 0.78717623,\n",
114+
" 0.78115209, 0.54839061, 0.8922792 , 0.63007016, 0.67104692,\n",
115+
" 0.96466619, 0.20735083, 0.52953937, 1.03121555, 0.73410734,\n",
116+
" 0.43354272, 0.50654853, 0.63068178, 0.63688495, 0.31763306,\n",
117+
" 0.54358635, 0. , 0.43460089, 0. , 0.8941544 ]))"
118118
]
119119
},
120120
"execution_count": 5,
@@ -123,31 +123,137 @@
123123
}
124124
],
125125
"source": [
126-
"vq(data, clust_centers)"
126+
"clusters = vq(data, clust_centers)\n",
127+
"clusters"
127128
]
128129
},
129130
{
130131
"cell_type": "code",
131132
"execution_count": 6,
132133
"metadata": {},
134+
"outputs": [
135+
{
136+
"data": {
137+
"text/plain": [
138+
"array([1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
139+
" 1, 1, 1, 0, 0, 1, 1, 1])"
140+
]
141+
},
142+
"execution_count": 6,
143+
"metadata": {},
144+
"output_type": "execute_result"
145+
}
146+
],
147+
"source": [
148+
"labels = clusters[0]\n",
149+
"labels"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 7,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"import plotly.plotly as py\n",
159+
"import plotly.graph_objs as go\n",
160+
"import plotly.offline as ply"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"x = []\n",
170+
"y = []\n",
171+
"z = []\n",
172+
"x2 = []\n",
173+
"y2 = []\n",
174+
"z2 = []\n",
175+
"\n",
176+
"for i in range(0, len(labels)):\n",
177+
" if(labels[i] == 0):\n",
178+
" x.append(data[i,0])\n",
179+
" y.append(data[i,1])\n",
180+
" z.append(data[i,2])\n",
181+
" \n",
182+
" else:\n",
183+
" x2.append(data[i,0])\n",
184+
" y2.append(data[i,1])\n",
185+
" z2.append(data[i,2])\n",
186+
"\n",
187+
"cluster1 = go.Scatter3d(\n",
188+
" x=x,\n",
189+
" y=y,\n",
190+
" z=z,\n",
191+
" mode='markers',\n",
192+
" marker=dict(\n",
193+
" size=12,\n",
194+
" line=dict(\n",
195+
" color='rgba(217, 217, 217, 0.14)',\n",
196+
" width=0.5\n",
197+
" ),\n",
198+
" opacity=0.9\n",
199+
" ),\n",
200+
" name=\"Cluster 0\"\n",
201+
")\n",
202+
"\n",
203+
"\n",
204+
"cluster2 = go.Scatter3d(\n",
205+
" x=x2,\n",
206+
" y=y2,\n",
207+
" z=z2,\n",
208+
" mode='markers',\n",
209+
" marker=dict(\n",
210+
" color='rgb(127, 127, 127)',\n",
211+
" size=12,\n",
212+
" symbol='circle',\n",
213+
" line=dict(\n",
214+
" color='rgb(204, 204, 204)',\n",
215+
" width=1\n",
216+
" ),\n",
217+
" opacity=0.9\n",
218+
" ),\n",
219+
" name=\"Cluster 1\"\n",
220+
")\n",
221+
"data2 = [cluster1, cluster2]\n",
222+
"layout = go.Layout(\n",
223+
" margin=dict(\n",
224+
" l=0,\n",
225+
" r=0,\n",
226+
" b=0,\n",
227+
" t=30\n",
228+
" )\n",
229+
")\n",
230+
"\n",
231+
"fig = go.Figure(data=data2, layout=layout)\n",
232+
"ply.plot(fig, filename='Clusters')\n"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 8,
238+
"metadata": {},
133239
"outputs": [],
134240
"source": [
135241
"from scipy.cluster.vq import kmeans"
136242
]
137243
},
138244
{
139245
"cell_type": "code",
140-
"execution_count": 7,
246+
"execution_count": 9,
141247
"metadata": {},
142248
"outputs": [
143249
{
144250
"data": {
145251
"text/plain": [
146-
"(array([[0.19487654, 0.51759619, 0.55633953],\n",
147-
" [0.75214332, 0.54668191, 0.44252866]]), 0.3838735276464874)"
252+
"(array([[0.55149309, 0.14895199, 0.50973511],\n",
253+
" [0.59158003, 0.69692438, 0.50948822]]), 0.38784900251440213)"
148254
]
149255
},
150-
"execution_count": 7,
256+
"execution_count": 9,
151257
"metadata": {},
152258
"output_type": "execute_result"
153259
}
@@ -158,17 +264,17 @@
158264
},
159265
{
160266
"cell_type": "code",
161-
"execution_count": 8,
267+
"execution_count": 10,
162268
"metadata": {},
163269
"outputs": [
164270
{
165271
"data": {
166272
"text/plain": [
167-
"(array([[0.76842027, 0.534974 , 0.42119024],\n",
168-
" [0.21520987, 0.52826022, 0.56596239]]), 0.3836206433353544)"
273+
"(array([[0.55511363, 0.20456483, 0.5587382 ],\n",
274+
" [0.59603233, 0.75078951, 0.45343179]]), 0.3860151283499985)"
169275
]
170276
},
171-
"execution_count": 8,
277+
"execution_count": 10,
172278
"metadata": {},
173279
"output_type": "execute_result"
174280
}
@@ -201,7 +307,7 @@
201307
"name": "python",
202308
"nbconvert_exporter": "python",
203309
"pygments_lexer": "ipython3",
204-
"version": "3.6.4"
310+
"version": "3.6.5"
205311
}
206312
},
207313
"nbformat": 4,

0 commit comments

Comments
 (0)