-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sesion6.py
154 lines (114 loc) · 4.22 KB
/
Sesion6.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 4 10:46:04 2020
@author: isaaclera
"""
from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from scipy import stats
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, centroid
## GENERAMOS DATOS ALEATORIOS PARA DIFERENTES SERIES
x = np.random.randint(low=3,high=100, size=40)
print("X: %s"%x)
y = np.random.randint(low=3,high=100, size=40)
print("Y: %s"%y)
print(x[0])
##
# =============================================================================
# DATOS similares valores y MISMAS UNIDADES
# =============================================================================
# En este caso ambas series X,Y podrían tener similares valores y MISMAS UNIDADES
Data = {'x': x,
'y': y
}
df = DataFrame(Data,columns=['x','y'])
kmeans = KMeans(n_clusters=17).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.show()
# =============================================================================
# ¿PERO REALMENTE 4 grupos son los suficienteS?
# DENDROGRAMA!!!
# =============================================================================
#https://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html
centr = centroid(df)
labelList = range(len(x))
plt.figure(figsize=(10, 7))
dendrogram(centr,
orientation='top',
labels=labelList,
distance_sort='descending',
show_leaf_counts=True)
plt.title('Dendrograma using centroid method')
plt.show()
# =============================================================================
# ALERTA! Hay más técnicas de clustering
# =============================================================================
linked = linkage(df, 'single')
labelList = range(len(x))
plt.figure(figsize=(10, 7))
dendrogram(linked,
orientation='top',
labels=labelList,
distance_sort='descending',
show_leaf_counts=True)
plt.title('Dendrograma using linkage method')
plt.show()
# =============================================================================
# DATOS Con diferentes unidades --- SIN NORMALIZAR
# =============================================================================
# Cambiamos los valores de un eje
y = np.random.uniform(low=0,high=2,size=40)
print("Y: %s"%y)
Data = {'x': x,
'y': y
}
df = DataFrame(Data,columns=['x','y'])
kmeans = KMeans(n_clusters=4).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.title('Clustering without normalization')
plt.show()
# =============================================================================
# DATOS Con diferentes unidades --- NORMALIZANDO POR RANGO
# =============================================================================
xr = (x-x.min())/(float(x.max()-x.min()))
yr = (y-y.min())/(y.max()-y.min())
print("Xr: %s"%xr)
print("Yr: %s"%yr)
Data = {'x': xr,
'y': yr
}
df = DataFrame(Data,columns=['x','y'])
kmeans = KMeans(n_clusters=4).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.title('Clustering using range normalization')
plt.show()
# =============================================================================
# DATOS Con diferentes unidades --- NORMALIZANDO POR Z-SCORE
# =============================================================================
xz = stats.zscore(x)
yz = stats.zscore(y)
print("Xz: %s"%xz)
print("Yz: %s"%yz)
Data = {'x': xz,
'y': yz
}
df = DataFrame(Data,columns=['x','y'])
kmeans = KMeans(n_clusters=4).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.title('Clustering using Z-score normalization')
plt.show()