def plot_figures(som): #BMU map vhts = BmuHitsView(12, 12, "Hits Map", text_size=12) #U matrix u = sompy.umatrix.UMatrixView(50, 50, 'umatrix', show_axis=True, text_size=8, show_text=True) UMAT = u.build_u_matrix(sm, distance=1, row_normalized=False) #Cluster map sm.cluster(6) hits = HitMapView(10, 10, "Clustering", text_size=12) #Show factor influence view2D = View2D(15, 15, "time-series", text_size=10, names=names) #Show plots view2D.show(sm, col_sz=4, which_dim="all", denormalize=True) vhts.show(sm, anotate=True, onlyzeros=False, labelsize=12, cmap="Greys", logaritmic=False) UMAT = u.show(sm, distance2=1, row_normalized=False, show_data=True, contooor=True, blob=False) a = hits.show(sm) plt.show()
- random :param name: name used to identify the som :param training: Training mode (seq, batch) """ from sompy.visualization.mapview import View2DPacked view2D = View2DPacked(10,10,"", text_size=7) view2D.show(sm, col_sz=5, what = 'codebook',)#which_dim="all", denormalize=True) plt.show() #These maps' scale is between -1 and 1 because we did the normalization # Interpretation at the end should be the same # SOM: less sensitive to outliers from sompy.visualization.mapview import View2D view2D = View2D(10,10,"", text_size=7) view2D.show(sm, col_sz=5, what = 'codebook',)#which_dim="all", denormalize=True) plt.show() # Number of people in each neuron from sompy.visualization.bmuhits import BmuHitsView vhts = BmuHitsView(12,12,"Hits Map",text_size=7) vhts.show(sm, anotate=True, onlyzeros=False, labelsize=10, cmap="autumn", logaritmic=False) # Apply k- means over SOM # K-Means Clustering from sompy.visualization.hitmap import HitMapView sm.cluster(3)# <n_clusters> hits = HitMapView(10,10,"Clustering",text_size=7) a=hits.show(sm, labelsize=12)
verbose='info', train_rough_len=35, train_finetune_len=80) # 'final_clusters_consump' is a dataframe similar to df but including a column 'Labels' which indicates the closest neuron to each obs final_clusters_consump = pd.DataFrame(sm_consump._data, columns=Consumption.columns).set_index(Consumption.index) my_labels_c = pd.DataFrame(sm_consump._bmu[0], columns=['Labels']).set_index(Consumption.index) final_clusters_consump = pd.concat([final_clusters_consump, my_labels_c], axis=1) # Plot the number of observations associated to each neuron vhts_c = BmuHitsView(12, 12, "Hits Map", text_size=7) vhts_c.show(sm_consump, anotate=True, onlyzeros=False, labelsize=10, cmap="summer", logaritmic=False) plt.show() # Visualization of the value of the grid neurons in each variable view2D_c = View2D(9, 9, "", text_size=7) view2D_c.show(sm_consump, col_sz=5, what='codebook') # 14.1.1. Hierarchical Clustering on top of SOM # Create a dataframe that specifies the coordinates of the neurons grid_consump = final_clusters_consump.groupby(by='Labels').mean() labels_som_cons = grid_consump.index # Plot a dendrogram to choose the number of clusters Z = linkage(grid_consump, method='ward') # method='single', 'complete', 'ward' dendrogram(Z, p=30) # Aplly hierarchical clustering with k clusters defined and best method (ward) k = 3 Hclustering = AgglomerativeClustering(n_clusters=k, affinity='euclidean', linkage='ward') my_HC_consump = Hclustering.fit(grid_consump)
print(Z) print('\n\n') sm = SOMFactory().build(Z, normalization='var', initialization='random', component_names=gunn) sm.train(n_job=1, verbose=False, train_rough_len=2, train_finetune_len=5) topographic_error = sm.calculate_topographic_error() quantization_error = np.mean(sm._bmu[1]) print("Topographic error = %s; Quantization error = %s" % (topographic_error, quantization_error)) vhts = BmuHitsView(10, 10, 'Hits Map', text_size=7) vhts.show(sm, anotate=True, onlyzeros=False, labelsize=12, cmap='Greys', logaritmic=False) pl.show() view2D = View2D(10, 10, 'rand data', text_size=10) view2D.show(sm, col_sz=4, which_dim='all', denormalize=True) pl.show()
sm = sompy.SOMFactory().build(colors, [20, 30], normalization='var', initialization='random', neighborhood='gaussian', component_names=['R', 'G', 'B']) sm.train(n_job=4, verbose=False, train_len_factor=0.5, train_rough_len=2, train_finetune_len=100) # In[113]: from sompy.visualization.mapview import View2D view2D = View2D(50, 50, "rand data", text_size=10) view2D.show(sm, col_sz=4, which_dim="all", denormalize=True) # In[114]: codebook = sm._normalizer.denormalize_by(sm.data_raw, sm.codebook.matrix) mp = codebook.reshape(sm.codebook.mapsize[0], sm.codebook.mapsize[1], codebook.shape[1]) mp.shape # Definiendo un SOM con una cuadrícula de 20x40 neuronas de salida y 400 iteraciones. # In[115]: import matplotlib.pyplot as plt #som = SimpleSOMMapper((20, 30), 400, learning_rate=0.05)
sm = SOMFactory().build(data.values, normalization='var', initialization='pca', component_names=names) sm.train(n_job=1, verbose=False, train_rough_len=2, train_finetune_len=5) # The quantization error: average distance between each data vector and its BMU. # The topographic error: the proportion of all data vectors for which first and second BMUs are not adjacent units. topographic_error = sm.calculate_topographic_error() quantization_error = np.mean(sm._bmu[1]) print("Topographic error = %s; Quantization error = %s" % (topographic_error, quantization_error)) # component planes view from sompy.visualization.mapview import View2D view2D = View2D(10, 10, "rand data", text_size=12) view2D.show(sm, col_sz=4, which_dim="all", desnormalize=True) # U-matrix plot from sompy.visualization.umatrix import UMatrixView umat = UMatrixView(width=10, height=10, title='U-matrix') umat.show(sm) # do the K-means clustering on the SOM grid, sweep across k = 2 to 20 from sompy.visualization.hitmap import HitMapView K = 20 # stop at this k for SSE sweep K_opt = 18 # optimal K already found [labels, km, norm_data] = sm.cluster(K, K_opt) hits = HitMapView(20, 20, "Clustering", text_size=12) a = hits.show(sm)
mapSize = [20, 20] sm = SOMFactory().build(training_split_0.values, mapSize, normalization="var", lattice="rect", initialization="random", component_names=training_split_0.columns) sm.train( n_job=1, verbose=None, train_rough_len=2, train_finetune_len=100) # I left some of the codes as the example provided # plot the results, components map from sompy.visualization.mapview import View2D view2D = View2D(20, 20, "", text_size=12) view2D.show(sm, col_sz=3, which_dim="all", denormalize=False) # Hit maps from sompy.visualization.bmuhits import BmuHitsView vhts = BmuHitsView(15, 10, "Hits Map", text_size=12) vhts.show(sm, anotate=False, onlyzeros=False, labelsize=12, cmap="jet", logaritmic=False) # U martix from sompy.visualization.umatrix import UMatrixView
dif_hiper_features=list((dif_SA_hiper,dif_dyn_hiper,dif_fj_hiper,dif_granfuzzy_hiper)) features_dif_oclusao=prepara_features_p_som(dif_oclusao_features) features_dif_hiper=prepara_features_p_som(dif_hiper_features) ## SELF ORGANIZED MAP mapsize=[40,40] som_oclusao = sompy.SOMFactory.build(features_dif_oclusao, mapsize, mask=None, mapshape='planar', lattice='rect', initialization='pca', neighborhood='gaussian', training='batch') som_oclusao.train(n_job=1,train_rough_len=3, train_finetune_len=2,verbose='info') som_recov = sompy.SOMFactory.build(features_dif_hiper, mapsize, mask=None, mapshape='planar', lattice='rect', initialization='pca', neighborhood='gaussian', training='batch') som_recov.train(n_job=1,train_rough_len=3, train_finetune_len=2,verbose='info') sa_som_oclusao=extrai_plano_componente(som,0,40) sa_som_recov=extrai_plano_componente(som_recov,0,40) dyn_som_oclusao=extrai_plano_componente(som,1,40) dyn_som_recov=extrai_plano_componente(som_recov,1,40) from sompy.visualization.mapview import View2D,View2DPacked from matplotlib import pyplot as plt v1=View2D(100,100,"Data Map",text_size=14) v1.show(som,col_sz=4,desnormalize=True) v2=View2D(10,10,'SOM',text_size=10) v2.show(som_recov,what='codebook',which_dim='all',cmap='jet',col_sz=4,desnormalize=True)
##Investigating df.info() som = None # create the SOM network and train it. You can experiment with different normalizations and initializations som = SOMFactory().build(df.values, mapsize=[50,50], normalization = 'var', initialization='pca', component_names=names) som.train(n_job=1, verbose=False, train_rough_len=2, train_finetune_len=5) # The quantization error: average distance between each data vector and its BMU. # The topographic error: the proportion of all data vectors for which first and second BMUs are not adjacent units. topographic_error = som.calculate_topographic_error() quantization_error = np.mean(som._bmu[1]) print "Topographic error = %s; Quantization error = %s" % (topographic_error, quantization_error) from sompy.visualization.mapview import View2D view2D = View2D(4,4,"rand data",text_size=16) view2D.show(som, col_sz=2, which_dim="all", desnormalize=True) # U-matrix plot from sompy.visualization.umatrix import UMatrixView umat = UMatrixView(width=10,height=10,title='U-matrix') umat.show(som) from sompy.visualization.hitmap import HitMapView K=10 Kluster = som.cluster(K) hits = HitMapView(20,20,"K-Means Clustering",text_size=16) a=hits.show(som)
def draw_input_weights(self): from sompy.visualization.mapview import View2D view2D = View2D(10, 10, "rand data", text_size=10) view2D.show(self.sm, col_sz=4, which_dim="all", desnormalize=True) plt.show()
def extrai_plano_componente(objeto_som, indice_variavel, N_som): plano = np.reshape(objeto_som.codebook.matrix[:, indice_variavel], (N_som, N_som)) return plano from sompy.visualization.hitmap import HitMapView som.cluster(3) hits = HitMapView(10, 10, "Clustering", text_size=12) a = hits.show(som) from sompy.visualization.mapview import View2D, View2DPacked v = View2D(10, 10, 'SOM', text_size=10) v.show(som, what='codebook', which_dim='all', cmap='jet', col_sz=4, desnormalize=True) from sompy.visualization.bmuhits import BmuHitsView vhts = BmuHitsView(10, 10, "Hits Map", text_size=5) vhts.show(som, anotate=True, onlyzeros=False, labelsize=12, cmap="Greys",
def prototype_visualization(sm): view2D = View2D(4, 4, "", text_size=7) view2D.show(sm, col_sz=5, which_dim="all", denormalize=True) plt.show()
def som_component_planes(som): # component planes view view2D = View2D(15, 15, "rand data", text_size=12) view2D.show(som, col_sz=4, which_dim="all", desnormalize=True) plt.show()