Exemplo n.º 1
0
def makeContourPlot(scores, average, HEIGHT, WIDTH, outputId, maskId, plt_title, outputdir, barcodeId=-1, vmaxVal=100):
    pylab.bone()
    #majorFormatter = FormatStrFormatter('%.f %%')
    #ax = pylab.gca()
    #ax.xaxis.set_major_formatter(majorFormatter)
    
    pylab.figure()
    ax = pylab.gca()
    ax.set_xlabel(str(WIDTH) + ' wells')
    ax.set_ylabel(str(HEIGHT) + ' wells')
    ax.autoscale_view()
    pylab.jet()
    
    pylab.imshow(scores,vmin=0, vmax=vmaxVal, origin='lower')
    pylab.vmin = 0.0
    pylab.vmax = 100.0
    ticksVal = getTicksForMaxVal(vmaxVal)
    pylab.colorbar(format='%.0f %%',ticks=ticksVal)
    print "'%s'" % average
    if(barcodeId!=-1):
        if(barcodeId==0): maskId = "No Barcode Match,"
        else:             maskId = "Barcode Id %d," % barcodeId
    if plt_title != '': maskId = '%s\n%s' % (plt_title,maskId)
    print "Checkpoint A"
    pylab.title('%s Loading Density (Avg ~ %0.f%%)' % (maskId, average))
    pylab.axis('scaled')
    print "Checkpoint B"
    pngFn = outputdir+'/'+outputId+'_density_contour.png'
    print "Try save to", pngFn;
    pylab.savefig(pngFn, bbox_inches='tight')
    print "Plot saved to", pngFn;
Exemplo n.º 2
0
 def training(self, status):
     
     self.figure.clear()
     self.queue.get() ## 두번째
     
     ## reading the dataset in the csv format  
     self.data = genfromtxt('iris6.csv', delimiter=',',dtype = float)
     self.data = numpy.nan_to_num(self.data)
     ## data normalization
     self.data = apply_along_axis(lambda x: x/linalg.norm(x),1,self.data) 
     
     self.som.random_weights_init(self.data)
     
     print("Training...")
     
     self.som.train_random(self.data,100) # random training
     
     bone()
     ## plotting the distance map as background
     pcolor(self.som.distance_map().T)
     colorbar()
     
     ## loadingthe labels
     target = genfromtxt('iris4_2.csv', delimiter=',', usecols=(0), dtype=int)
     self.t = zeros(len(target),dtype=int)
     
     print("...ready to plot...")
     
     for i in range(len(target)):
         self.t[target == i] = i
     
     self.som.win_map(self.data)
     
     self.queue.task_done() ## 세번째
Exemplo n.º 3
0
def som_plot_mapping(distance_map):
    bone()
    pcolor(distance_map.T) # plotting the distance map as background
    colorbar()
    #axis([0,som.weights.shape[0],0,som.weights.shape[1]])
    ion()
    show() # show the figure
Exemplo n.º 4
0
def testSOMs():
    from sklearn import datasets
    from minisom import MiniSom

    d = datasets.load_iris()
    data = np.apply_along_axis(lambda x: x/np.linalg.norm(x), 1, d['data']) # data normalization

    som = MiniSom(7, 7, 4, sigma=1.0, learning_rate=0.5)

    som.random_weights_init(data)
    print("Training...")
    som.train_random(data, 1000) # random training
    print("\n...ready!")

    ### Plotting the response for each pattern in the iris dataset ###
    from pylab import plot,axis,show,pcolor,colorbar,bone
    bone()
    pcolor(som.distance_map().T) # plotting the distance map as background
    colorbar()
    t = d['target']
    # use different colors and markers for each label
    markers = ['o','s','D']
    colors = ['r','g','b']
    for cnt,xx in enumerate(data):
     w = som.winner(xx) # getting the winner
     # palce a marker on the winning position for the sample xx
     plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None',
        markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2)
    axis([0,som.weights.shape[0],0,som.weights.shape[1]])
    show() # show the figure
Exemplo n.º 5
0
Arquivo: gui.py Projeto: sriveravi/som
    def Draw_figure(self):
  	self.axes.cla()   # Clear axis
	cols = self.columns[self.combobox.get_active()]
	data = self.data[:, 0:len(cols)]


	#ion()       # Turn on interactive mode.
	#hold(True) # Clear the plot before adding new data.


	#print som.distance_map().T
	#exit()
	bone()

	background = self.axes.pcolor(self.som.distance_map().T) # plotting the distance map as background
	#f.colorbar(a)
	t = np.zeros(len(self.target),dtype=int)
	t[self.target == 'A'] = 0
	t[self.target == 'B'] = 1
	t[self.target == 'C'] = 2
	t[self.target == 'D'] = 3

	# use different colors and markers for each label
	markers = ['o','s','D', '+']
	colors = ['r','g','b', 'y']
	for cnt,xx in enumerate(data):
	  w = self.som.winner(xx) # getting the winner
	  # place a marker on the winning position for the sample xx
	  tmp = self.axes.plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None',
	      markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2)
	self.axes.axis([0,self.som.weights.shape[0],0,self.som.weights.shape[1]])
Exemplo n.º 6
0
def makeContourPlot(score, scores, average, HEIGHT, WIDTH, outputId, maskId, outputdir, barcodeId=-1, vmaxVal=100):
    pylab.bone()
    #majorFormatter = FormatStrFormatter('%.f %%')
    #ax = pylab.gca()
    #ax.xaxis.set_major_formatter(majorFormatter)
    
    pylab.figure()
    ax = pylab.gca()
    ax.set_xlabel('<--- Width = '+str(WIDTH)+' wells --->')
    ax.set_ylabel('<--- Height = '+str(HEIGHT)+' wells --->')
    ax.set_yticks([0,HEIGHT/INCREMENT])
    ax.set_xticks([0,WIDTH/INCREMENT])
    ax.autoscale_view()
    pylab.jet()
    #pylab.contourf(scores, 40,origin="lower")
    
    if vmaxVal=='auto':
        vmaxVal = autoGetVmaxFromAverage(average)
    
    pylab.imshow(scores,vmin=0, vmax=vmaxVal, origin='lower')
    pylab.vmin = 0.0
    pylab.vmax = 100.0
    ticksVal = getTicksForMaxVal(vmaxVal)
    pylab.colorbar(format='%.0f %%',ticks=ticksVal)
    
    string_value1 = getFormatForVal(average) % average
    if(barcodeId!=-1):
        if(barcodeId==0): maskId = "No Barcode Match,"
        else:             maskId = "Barcode Id %d," % barcodeId
    pylab.title(maskId+' Loading Density (Avg ~ '+string_value1+'%)')
    pylab.axis('scaled')
    pylab.axis([0,WIDTH/INCREMENT-1,0,HEIGHT/INCREMENT-1])
    pngFn = outputdir+'/'+outputId+'_density_contour.png'
    pylab.savefig(pngFn)
    print "Plot saved to", pngFn;
def SOM(data,leninput,lentarget,alpha_som,omega_som):
    som = MiniSom(16,16,leninput,sigma=omega_som,learning_rate=alpha_som)
    som.random_weights_init(data)
    print("Training...")
    som.train_batch(data,20000) # training with 10000 iterations
    print("\n...ready!")
    
    numpy.save('weight_som',som.weights)
   
    bone()
    pcolor(som.distance_map().T) # distance map as background
    colorbar()
    
    t = zeros(lentarget,dtype=int)
    
    # use different colors and markers for each label
    markers = ['o','s','D']
    colors = ['r','g','b']
    outfile = open('cluster-result.csv','w')
    for cnt,xx in enumerate(data):
        w = som.winner(xx) # getting the winner
        
        
        for z in xx:
            outfile.write("%s " % str(z))
        outfile.write("%s-%s \n" % (str(w[0]),str(w[1])))
        
        
    outfile.close()
def SOM(data,leninput,lentarget):
    som = MiniSom(5,5,leninput,sigma=1.0,learning_rate=0.5)
    som.random_weights_init(data)
    print("Training...")
    som.train_batch(data,10000) # training with 10000 iterations
    print("\n...ready!")
    
    numpy.save('weight_som.txt',som.weights)
   
    bone()
    pcolor(som.distance_map().T) # distance map as background
    colorbar()
    
    t = zeros(lentarget,dtype=int)
    
    # use different colors and markers for each label
    markers = ['o','s','D']
    colors = ['r','g','b']
    outfile = open('cluster-result.csv','w')
    for cnt,xx in enumerate(data):
        w = som.winner(xx) # getting the winner
        #print cnt
        #print xx
        #print w
        
        for z in xx:
            outfile.write("%s " % str(z))
        outfile.write("%s-%s \n" % (str(w[0]),str(w[1])))
        
        #outfile.write("%s %s\n" % str(xx),str(w))
        # palce a marker on the winning position for the sample xx
        #plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None',
        #     markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2)
    outfile.close()
Exemplo n.º 9
0
 def show( self, maxIdx=None, indices=None):
     print( 'Exemplar projection')
     som = self.som
     if maxIdx == None:
         maxIdx = len(self.data)            
     if indices ==None:            
         data= self.data[0:maxIdx]
         target = self.target
     else:
         data= self.data[indices]
         target= self.target[indices]
         
     bone()
     pcolor(som.distance_map().T) # plotting the distance map as background
     colorbar()
     t = zeros(len(target),dtype=int)
     t[target == 'A'] = 0
     t[target == 'B'] = 1
     # use different colors and markers for each label
     markers = ['o','s','D']
     colors = ['r','g','b']
     for cnt,xx in enumerate(data):
         w = som.winner(xx) # getting the winner
         # palce a marker on the winning position for the sample xx
         plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None',
             markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2)
     axis([0,som.weights.shape[0],0,som.weights.shape[1]])
     show() # show the figure
def vector_flow(output_file, i_factor=1):
	# DPI resolution of the image to be saved
	dpi = 200

	file_path_x = "example/vector_field_x_0.csv"
	file_path_y = "example/vector_field_y_0.csv"

	vector_field_x = np.loadtxt(file_path_x, delimiter=",")
	vector_field_y = np.loadtxt(file_path_y, delimiter=",")

	x_steps, y_steps = vector_field_x.shape
	
	# Interpolation factor. For 1 no interpolation occurs.
	if i_factor > 1:
		vector_field_x = scipy.ndimage.zoom(vector_field_x, i_factor)
		vector_field_y = scipy.ndimage.zoom(vector_field_y, i_factor)

		x_steps *= i_factor
		y_steps *= i_factor


	# Putting data in the expected format
	vectors = np.zeros((x_steps, y_steps, 2), dtype=np.float32)

	vectors[...,0] += vector_field_y
	vectors[...,1] += vector_field_x
	
	texture = np.random.rand(x_steps,y_steps).astype(np.float32)

	kernellen=20
	kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)
	kernel = kernel.astype(np.float32)

	image = lic_internal.line_integral_convolution(vectors, texture, kernel)
	mag = np.hypot(vector_field_x, vector_field_y)

	plt.jet()
	plt.figure()
	plt.axis('off')
	plt.imshow(texture, interpolation='nearest')
	plt.savefig(output_file+"-texture.png",dpi=dpi)

	
	plt.figure()
	fig = plt.quiver(vector_field_y, vector_field_x, mag)
	plt.colorbar()
	plt.savefig(output_file+".png",dpi=dpi)

	plt.bone()
	fig = plt.imshow(image, interpolation='nearest')
	# plt.colorbar()
	plt.savefig(output_file+"-flow.png",dpi=dpi)
Exemplo n.º 11
0
    def Draw_figure(self):
	# this function draws the exemplars on the best matching units

	self.axes.cla()   # Clear axis
	cols = self.columns[self.combobox.get_active()]
	data = self.data[:, 0:len(cols)]
	test_data = self.test_data[:, 0:len(cols)]

	#ion()       # Turn on interactive mode.
	#hold(True) # Clear the plot before adding new data.


	#print som.distance_map().T
	#exit()
	bone()

	background = self.axes.pcolor(self.som.distance_map().T) # plotting the distance map as background
	#f.colorbar(a)
	t = np.zeros(len(self.target),dtype=int)
	t[self.target == 'A'] = 0
	t[self.target == 'B'] = 1
	#t[self.target == 'C'] = 2
	#t[self.target == 'D'] = 3

	tTest = np.zeros(len(self.test_target),dtype=int)
	tTest[self.test_target == 'A'] = 2 #0
	tTest[self.test_target == 'B'] = 3 #1


	# use different colors and markers for each label
	markers = ['o','s','*', '+']
	colors = ['r','g','b', 'y']
	for cnt,xx in enumerate(data):  # training data ( noisy simulation)
	  w = self.som.winner(xx) # getting the winner
	  # place a marker on the winning position for the sample xx
	  tmp = self.axes.plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None',
	      markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2)

	# plot the test data (ideal input)
	for cnt,xx in enumerate(test_data): # test data ( ideal )
	  w = self.som.winner(xx) # getting the winner
	  # place a marker on the winning position for the sample xx
	  tmp = self.axes.plot(w[0]+.5,w[1]+.5,markers[tTest[cnt]],markerfacecolor='None',
	      markeredgecolor=colors[tTest[cnt]],markersize=12,markeredgewidth=2)


	self.axes.axis([0,self.som.weights.shape[0],0,self.som.weights.shape[1]])
Exemplo n.º 12
0
def scalogram_levels(data, fs, filename, wv='sym5' ):
    from pywt import WaveletPacket
    wp = WaveletPacket(data, wavelet=wv , maxlevel=2)
    pylab.bone()
    x = np.arange(len(data))/fs
    pylab.subplot(wp.maxlevel + 1, 1, 1)
    pylab.plot(x,data, 'k')
    cm = plt.get_cmap('PiYG')
    #pylab.xlim(0, len(data) - 1)
    pylab.title("Wavelet packet coefficients")
    #ax = pylab.subplot(wp.maxlevel + 1, 1, 1+1)
    for i in range(1, wp.maxlevel + 1):
        ax = pylab.subplot(wp.maxlevel + 1, 1, i + 1)
        nodes = wp.get_level(i, "freq")
        nodes.reverse()
        labels = [n.path for n in nodes]
        values = -abs(np.array([n.data for n in nodes]))
        pylab.imshow(values, interpolation='nearest', aspect='auto',  origin="lower") #extent=[0,1,2,len(values)])
        pylab.yticks(np.arange(len(labels) - 0.5, -0.5, -1), labels)
        pylab.setp(ax.get_xticklabels(), visible=False)
    pylab.savefig(filename+'_'+wv+'.pdf')
Exemplo n.º 13
0
def plotsom(som, features, tostrfct=lambda i: str(i)):
	py.bone()
	py.pcolor(som.distance_map().T)
	py.colorbar()
	
	nb_samples,_ = features.shape
	
	lastPos = {}
	for i in range(nb_samples):
		w = som.winner(features[i,:])
		if w in lastPos:
			pos = lastPos[w]
			pos[0] += 0.2
			if pos[0] >= 1.0:
				pos[0] = 0
				pos[1] += 0.2
		else:
			pos = [0.0, 0.0];
		
		lastPos[w] = pos
		
		if( pos[1] < 1.0):
			py.text(w[0]+pos[0], w[1]+(0.8-pos[1]),tostrfct(i))
Exemplo n.º 14
0
# reading the iris dataset in the csv format
# (downloaded from http://aima.cs.berkeley.edu/data/iris.csv)

data = genfromtxt('iris.csv', delimiter=',', usecols=(0, 1, 2, 3))
# normalization to unity of each pattern in the data
data = apply_along_axis(lambda x: x / linalg.norm(x), 1, data)

from minisom import MiniSom
### Initialization and training ###
som = MiniSom(7, 7, 4, random_seed=10)
som.random_weights_init(data)
som.train_random(data=data, num_iteration=100)

from pylab import plot, axis, show, pcolor, colorbar, bone

bone()
pcolor(som.distance_map().T)  # distance map as background
colorbar()

# loading the labels
target = genfromtxt('iris.csv', delimiter=',', usecols=(4), dtype=str)
t = zeros(len(target), dtype=int)

t[target == 'setosa'] = 0
t[target == 'versicolor'] = 1
t[target == 'virginica'] = 2
# use different colors and markers for each label
markers = ['o', 's', 'D']
colors = ['r', 'g', 'b']
for cnt, xx in enumerate(data):
    w = som.winner(xx)  # getting the winner
Exemplo n.º 15
0
som = SOM(grid_size[0],
          grid_size[1],
          dim=n_dim,
          num_iterations=num_iterations,
          learning_rate=learning_rate,
          sigma=sigma)
som.fit(X)

# Malla con el MID de cada unidad
distance_matrix = som.distance_map().T

# =============================================================================
# 3. Visualización de resultados
# =============================================================================
from pylab import bone, pcolor, colorbar
bone()  # Inicializo la ventana de visualizacion
pcolor(
    distance_matrix
)  # Para pintar el som. El .T para poner la matriz traspuesta. Lo que pinto es el MID de los nodos
colorbar(
)  # Para tener la leyenda de colores. Veré que los MID van de 0 a 1, porque están escalados

max_value = np.amax(distance_matrix)
min_value = np.amin(distance_matrix)

list_mid = list(np.reshape(distance_matrix, (grid_size[0] * grid_size[1], )))
list_mid.sort()
list_mid = [j for j in list_mid if j > 1.48]
list_idx = [np.where(distance_matrix == j) for j in list_mid]
list_idx = [[idx_max[0][0], idx_max[1][0]] for idx_max in list_idx]
Exemplo n.º 16
0

xs = np.linspace(-1,1,size).astype(np.float32)[None,:]
ys = np.linspace(-1,1,size).astype(np.float32)[:,None]

vectors = np.zeros((size,size,2),dtype=np.float32)
#for (x,y) in vortices:
#    rsq = (xs-x)**2+(ys-y)**2
#    vectors[...,0] +=  (ys-y)/rsq
#    vectors[...,1] += -(xs-x)/rsq

print np.shape(vectors)
asdf()
texture = np.random.rand(size,size).astype(np.float32)

plt.bone()
frame=0

kernellen=100
kernel = np.arange(kernellen) #np.sin(np.arange(kernellen)*np.pi/kernellen)
kernel = kernel.astype(np.float32)

image = lic_internal.line_integral_convolution(vectors, texture, kernel)

plt.clf()
plt.axis('off')
plt.figimage(image)
plt.gcf().set_size_inches((size/float(dpi),size/float(dpi)))
plt.savefig("flow-image.png",dpi=dpi)

Exemplo n.º 17
0
X = sc.fit_transform(X)

# Training the SOM
from minisom import MiniSom
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
"""x and y has the 10*10 grid(As we don't have much inspections so a medium size map).input_len for features in X here which is 15 (ID has been included to distinguish the frauds)
sigma to determine the radius, learning_rate how much the weight of 
the neighbourhood will be updated faster the learning faster will be the convergence
decay_function can be tuned to get improved convergence """
som.random_weights_init(X)  # Randomly intializing the weights.
som.train_random(
    data=X, num_iteration=100)  # Step-4 to Step-9 repeatedly upto 100 times.

# Visualising the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  # To get the figure window
pcolor(
    som.distance_map().T
)  # som.distance_map() will return the MIDs in a matrix for all the winning nodes and T for transpose to have the correct order for pcolor function.
colorbar()  # To get the Legend
# We know where the potential frauds are bu=y looking at the highest MID values
markers = ['o', 's']
colors = ['r', 'g']
for i, x in enumerate(X):  # Here i is row index and x is ith customer vector
    w = som.winner(x)  # Getting the winner node
    plot(
        w[0] + 0.5,  # To reach the middle x co-ordinate of winner node
        w[1] + 0.5,  # To reach the middle y co-ordinate of winner node
        markers[y[i]],
        markeredgecolor=colors[y[i]],
        markerfacecolor=None,
Exemplo n.º 18
0
from minisom import MiniSom
from pylab import bone, pcolor, colorbar, plot, show
''' Importing the dataset '''
dataset = pd.read_csv('Credit_Card_Applications.csv')
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
''' Feature Scaling '''
sc = MinMaxScaler(feature_range=(0, 1))
x = sc.fit_transform(x)
''' Training the Self Organizing Map '''
#15 no of col  #default val
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
som.random_weights_init(x)  # randomly initializing weights by giving data
som.train_random(data=x, num_iteration=100)
''' Visualizing the results '''
bone()  # initializing the window of graphical representation
pcolor(
    som.distance_map().T
)  # plotting trained mean-neuron distance to visualize and taking transpose as well
colorbar(
)  # showing the colorbar to differentiate lower and higher values color
markers = ['o', 's']
colors = ['r', 'g']
for i, ex in enumerate(
        x):  # i is the index number, ex is the values in each index
    w = som.winner(ex)  # finding the winning node for each customer data
    plot(
        w[0] +
        0.5,  # plotting the values of point x,y (w[0],w[1]), and adding with 0.5 to mark in the center
        w[1] + 0.5,
        markers[y[
Exemplo n.º 19
0
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

#Training SOM
from minisom import MiniSom
som = MiniSom(
    x=10, y=10, input_len=15, sigma=1.0,
    learning_rate=0.5)  #10 X 10 Grid, Sigma is the circle radius parameter
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

#Visualizing the Data
from pylab import bone, pcolor, colorbar, plot, show
bone()  #The window
pcolor(
    som.distance_map().T
)  #Plot the colors according to the mean internueron distances given by the "distance_map" function  [.T means transpose]
colorbar()

# Here if you run the plot...the white one have HIGH interneuron distance and hence are the frauds....
# Now we will mark the customers who were approved and who were not approved for further clarity
markers = ['o', 's']  # o-> circle || s->square
colors = ['r', 'g']  # red and green

for i, x in enumerate(X):  #i is the index...x is the vector of each customer
    W = som.winner(x)  #Winner node for customer X
    plot(
        W[0] +
        0.5,  #W[0] and W[1]  are the coordinates of the corner of the winning node....hence we added the values to find the center  
Exemplo n.º 20
0
dataset = pd.read_csv('Credit_Card_Applications.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

#Feature Scaling
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

#Training the SOM
#Initialize 10*10 grid, input_len = features number
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

#Visualizing the results
bone()  #initialize the window
#distance_map: return all Mean Interneuron Distances in one matrix
pcolor(som.distance_map().T)
colorbar()  #add legend
#From the output we can find the outlier: white block

markers = ['o', 's']  #o: circle, s: square
colors = ['r', 'g']
for i, x in enumerate(X):
    w = som.winner(x)
    plot(
        w[0] + 0.5,  #x coordinates, 0.5 put in center
        w[1] + 0.5,  #y coordinates
        markers[y[i]],  #y[i]: label
        markeredgecolor=colors[y[i]],
        markerfacecolor='None',
Exemplo n.º 21
0
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)
"""##Training the SOM"""

from minisom import MiniSom

som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
#the grid size is 10*10 , input length is the # of features in X, sigma is the
#neighborhood size
som.random_weights_init(X)  #initializing the weights
som.train_random(data=X, num_iteration=100)
"""##Visualizing the results"""

from pylab import bone, pcolor, colorbar, plot, show

bone()  #initializing the window
pcolor(som.distance_map().T)  #returns transpose of the matrix of the distance
#of all the winning nodes
colorbar()  #adding the legend
markers = ['o', 's']  #circles and squares
colors = ['r', 'g']  #red if customer did't get approval and vice versa
for i, x in enumerate(X):  # i is the # of rows, x is the vector of the rows
    w = som.winner(x)  #getting the winning nodes
    plot(
        w[0] + 0.5,  #to put the marker in the center of the square of the SOM
        w[1] + 0.5,
        markers[y[i]],  #y contains the labels if customers got approval or no
        markeredgecolor=colors[y[i]],  #for each customer, adding the color
        #only coloring the edge of the marker
        markerfacecolor='None',  #inside color of the marker
        markersize=10,  #size of the marker
Exemplo n.º 22
0
import numpy as np
import pylab as py

a = np.loadtxt('blur.txt')

py.imshow(a)
py.bone()
py.show()
Exemplo n.º 23
0
X_scaled = scaler.fit_transform(X)

# Entrainement
from minisom import MiniSom

# pour creer la carte, on choisit ici 10x10=100 valeurs (~suffisant pour 690 observations ...)
som = MiniSom(x=10, y=10, input_len=15)  # 15 dimensions

# initialiser aleatoirement les poids
som.random_weights_init(X_scaled)
som.train_random(X_scaled, num_iteration=100)

# Visualisation des résultats
from pylab import plot, colorbar, pcolor, show, bone
bone()  # initialisation graphe
#pcolor(som.distance_map().T)
colorbar(pcolor(som.distance_map().T))  # add a color bar

markers = ['o', 's']
colors = ['r', 'g']

for i, x in enumerate(X_scaled):
    w = som.winner(x)
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[y[i]],
         markeredgecolor=colors[y[i]],
         markerfacecolor="None",
         markersize=10,
         markeredgewidth=2)
Exemplo n.º 24
0
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

# Training the SOM
from minisom import MiniSom  # requires minisom.py file in the same folder
som = MiniSom(
    x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5
)  # SOM will be a 10 by 10 grid (arbitrary choice); 15 features are present in training data X; radius is 1; higher the LR, faster the convergence
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  # initializes the window containing the map
pcolor(som.distance_map().T
       )  # adds the inter-neuron distance for all the winning nodes of the SOM
colorbar()  # add the legend of the colors added above
# adding markers (red circles and green squares) to the winning nodes to check if they got approval or not
markers = ['o', 's']
colors = ['r', 'g']
for i, x in enumerate(X):  # loop over each customer
    w = som.winner(x)  # obtain winning node for the customer
    plot(
        w[0] + 0.5,
        w[1] + 0.5,
        markers[y[i]],
        markeredgecolor=colors[y[i]],
        markerfacecolor='None',
        markersize=10,
Exemplo n.º 25
0
y = df.iloc[:, -1].values
#----------------------------------------------------------------------------------------
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)
#----------------------------------------------------------------------------------------
# Training the SOM
from minisom import MiniSom
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
som.random_weights_init(X)  # to initialize the weights randomly.
som.train_random(data=X, num_iteration=100)  # to train SOM.
#----------------------------------------------------------------------------------------
# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  # this is the window that will contain the map.
pcolor(som.distance_map().T
       )  # all the different colors corresponding to the MID's.
colorbar()  # white colors are the outliers (frauds).
markers = ['o',
           's']  # red circles(r, o) : the customers who didn't get approval.
colors = ['r', 'g']  # green squares(g, s) : the customers who got approval.
for i, j in enumerate(
        X):  # i : indexes, j : all the vectors of customers at i.
    w = som.winner(j)  # winning node.
    plot(
        w[0] + 0.5,  # we want to put the marker at the center of the square.
        w[1] + 0.5,  # we want to put the marker at the center of the square.
        markers[y[i]],
        markeredgecolor=colors[y[i]],
        markerfacecolor='None',
Exemplo n.º 26
0
data = genfromtxt('data5.csv', delimiter=',',dtype = float)
data = numpy.nan_to_num(data)
print (data)
data = apply_along_axis(lambda x: x/linalg.norm(x),1,data) # data normalization

### Initialization and training ###
som = MiniSom(40,40,136,sigma=1.0,learning_rate=0.5)
som.random_weights_init(data)
print("Training...")
som.train_random(data,10000) # random training
print("\n...ready!")

### Plotting the response for each pattern in the iris dataset ###
from pylab import plot,axis,show,pcolor,colorbar,bone

bone()
pcolor(som.distance_map().T) # plotting the distance map as background
colorbar()

target = genfromtxt('class5.csv',delimiter=',',usecols=(0),dtype=int) # loadingthe labels
t = zeros(len(target),dtype=int)
print (target)

t[target == 0] = 0
t[target == 1] = 1
t[target == 2] = 2
t[target == 3] = 3
t[target == 4] = 4
t[target == 5] = 5
t[target == 6] = 6
t[target == 7] = 7
Exemplo n.º 27
0
X = sc.fit_transform(X)

# Training the SOM
### Importing MiniSom function from minisom.py file and training it on X
from minisom import MiniSom

som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)

### Initialising the weights and training the SOM
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show

bone()  # Initialising the diagram screen
pcolor(som.distance_map().T)  # Converting MID to color
colorbar()  # Providing color legend
markers = ['o', 's']  # Defining marker shape
colors = ['r', 'g']  # Defining marker color
for i, x in enumerate(X):  # Looping over X, each row becoming a vector
    w = som.winner(x)  # Getting winning node for each row
    plot(
        w[0] + 0.5,  # Plotting marker at center of each node (x coordinate)
        w[1] + 0.5,  # and y coordinate
        markers[y[
            i]],  # Deciding marker shape based on output (marker[0] or marker[1])
        markeredgecolor=colors[y[
            i]],  # Getting  marker edge color based on output (color[0] or color[1])
        markerfacecolor='None',  # Putting no color for marker center          
        markersize=10,  # Defining size of each marker
Exemplo n.º 28
0
 def plot_distance_map(self):
     '''Plots distance map. Need to call get_mid before calling this.'''
     bone()
     pcolor(self.dm.T)
     colorbar()
     show()
Exemplo n.º 29
0
Y = dataset.iloc[:, -1].values

#Feature Scaling
from sklearn.preprocessing import MinMaxScaler
Mc = MinMaxScaler()
X = Mc.fit_transform(X)

from minisom import MiniSom  #A Library used for traning the SOM
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
som.random_weights_init(X)  #Randomly Inizializing the weights
som.train_random(data=X,
                 num_iteration=100)  #Training the Som with 100 iterations

#Visualize the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  # white window
pcolor(som.distance_map(
).T)  #will return all the mean inter nueron distances for the winning nodes
colorbar()  #the colorbar on the right
markers = [
    'o', 's'
]  #the circle shows normal people and the s is for square which shows fraud
colors = ['r', 'g']  #red and green color
for i, x in enumerate(
        X
):  #i is the index of all the customes and x is the detail of each customer who will be assigned a red or a green color based on the MID
    w = som.winner(x)  #will fetch the winning node for the customer x
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[Y[i]],
         markeredgecolor=colors[Y[i]],
def som_with_data():
    data = np.genfromtxt(
        "breast-cancer-wisconsin.data.txt",
        delimiter=",",
        names=True,
    )

    # Define what columns are considered features for
    # describing the cells
    feature_names = [
        "clump_thickness", 'uniform_cell_size', 'uniform_cell_shape',
        'marginal_adhesion', 'single_epi_cell_size', 'bare_nuclei',
        'bland_chromation', 'normal_nucleoli', 'mitoses'
    ]

    # Gather features into one matrix (this is the "X" matrix in task)
    features = []
    for feature_name in feature_names:
        features.append(data[feature_name])
    features = np.stack(features, axis=1)

    # Do same for class
    classes = data["class"]

    # Remove non-numeric values (appear as NaNs in the data).
    # If any feature in a sample is nan, drop that sample (row).
    cleaned_features = []
    cleaned_classes = []
    for i in range(features.shape[0]):
        if not np.any(np.isnan(features[i])):
            cleaned_features.append(features[i])
            cleaned_classes.append(classes[i])
    cleaned_features = np.stack(cleaned_features, axis=0)
    cleaned_classes = np.array(cleaned_classes)

    # Rename to match the exercises
    X = cleaned_features
    y = cleaned_classes

    # Standardize features with standard scaling ([x - mean] / std)
    X = (X - X.mean(axis=0)) / X.std(axis=0)

    # Transform y into {0,1} label array.
    # Turn 2 into 0 and 4 into 1
    y = (y == 4).astype(np.int64)

    data, target = X, y
    som = MiniSom(x=30,
                  y=20,
                  input_len=data.shape[1],
                  sigma=1,
                  learning_rate=0.5)
    som.random_weights_init(data)

    som.train_random(data, 500)
    bone()
    pcolor(som.distance_map().T)
    colorbar()
    markers = ['o', 's', 'D']
    colors = ['r', 'g', 'b']
    for cnt, xx in enumerate(data):
        w = som.winner(xx)
        plot(w[0] + .5,
             w[1] + .5,
             markers[target[cnt]],
             markerfacecolor='None',
             markeredgecolor=colors[target[cnt]],
             markersize=12,
             markeredgewidth=2)
    axis([0, som._weights.shape[0], 0, som._weights.shape[1]])
    show()
som.train_random(data=X, num_iteration=100000)

som_distance = som.distance_map().T

mappings = som.win_map(X)
print('size of the SOM map', len(mappings))
text_file = open("mappings_individual.txt", "w")
text_file.write("winning map is %s" % mappings)
text_file.close()

# List of fake review user IDs
faker_list = []
faker_id = []
for i in xrange(len(som_distance)):
    for j in xrange(len(som_distance[0])):
        if som_distance[i][j] > 0.8:
            fake_reviewers = mappings[(i, j)]
            for user in fake_reviewers:
                faker_id.append(user[0])
                faker_list.append(mapping[user[0]])
print('list of fake user IDs is\n', faker_list)

# Visualizing the results (SOM in a 2-D plot)
from pylab import bone, pcolor, colorbar, plot, show

bone()  # creates a white window
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's']
colors = ['r', 'g']
show()
Exemplo n.º 32
0
som = MiniSom(
    x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5
)  #object som trained on X//   X & y are dimension of SOM(MORE THE DATA i.e no of CUSTOMER more will be dimension)///  here input_len is the no of feature in training dataset i.e X(14) and +1 for customer id
som.random_weights_init(
    X
)  #sigma is the radious of different neighbourhood i.e default value is 1.0//  learning_rate will decide how much weight updated in each learning rate so  default value is 0.5 so higher will be the learning_rate faster will be convergence, lower the learning_rate, longer the self organising map take time to build.//  decay_function can be use to improve convergence
som.train_random(
    data=X, num_iteration=100)  #num_iteration is no of time it need to repeate
#random_weights_init IS THE method initialize the weight mention by developer i.e by Minisom1.0
#train_random method use to train

# ---------Visualizing the results
#here we will calculate mean interneuron distance(MID) i.e mean of euclian distance between study neuron and neighbourhood so we can detect outlier which will be far from the nieghbour neuron on basis of euclian distance
#larger the mid closer to white in colour
from pylab import bone, pcolor, colorbar, plot, show  #BUILDING self organising map
bone()  #initlizee the figure i.e window contain map
pcolor(
    som.distance_map().T
)  #use different colour for different MID///   distance_map WILL RETURN ALL mid IN MAPS.//  ".T" will take transpose of MID matrics
colorbar()  # that is legend for all colour
markers = ['o', 's']  # 'o', 's' circle and squire as markers
colors = [
    'r', 'g'
]  # red circle if customer did not get approval and green squire if customer got approval
for i, x in enumerate(
        X):  # enumerate(X) all vector of customer in all iteration
    w = som.winner(x)  #  winner get winning nodes of all customer
    plot(
        w[0] + 0.5,  #adding markers and color in each winner nodes
        w[1] + 0.5,  #0.5 to put at centre of marker
        markers[y[
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy
import pylab

from pywt import WaveletPacket

x = numpy.arange(612 - 80, 20, -0.5) / 150.0
data = numpy.sin(20 * pylab.log(x)) * numpy.sign((pylab.log(x)))
from sample_data import ecg as data

wp = WaveletPacket(data, "sym5", maxlevel=4)

pylab.bone()
pylab.subplot(wp.maxlevel + 1, 1, 1)
pylab.plot(data, "k")
pylab.xlim(0, len(data) - 1)
pylab.title("Wavelet packet coefficients")

for i in range(1, wp.maxlevel + 1):
    ax = pylab.subplot(wp.maxlevel + 1, 1, i + 1)
    nodes = wp.get_level(i, "freq")
    nodes.reverse()
    labels = [n.path for n in nodes]
    values = -abs(numpy.array([n.data for n in nodes]))
    pylab.imshow(values, interpolation="nearest", aspect="auto")
    pylab.yticks(numpy.arange(len(labels) - 0.5, -0.5, -1), labels)
    pylab.setp(ax.get_xticklabels(), visible=False)

pylab.show()
 X= dataset.iloc[:,:-1].values
 #only the last collumn
 y= dataset.iloc[:,-1].values

#Feature  scaling 
from sklearn.preprocessing import MinMaxScaler
sc=MinMaxScaler(feature_range=(0,1))
X=sc.fit_transform(X)

from minisom import MiniSom
som=MiniSom(x=10, y=10 , input_len=15,sigma=1.0,learning_rate=0.5 ) 
som.random_weights_init(X)
som.train_random(data=X,num_iteration=100) 

from pylab import bone, pcolor,colorbar,plot,show
bone()#in order to initialize the window
#get the mean neuron distance between the nodes(winning nodes)(neighbourhood of nodes)
#by the distance we can identify the frauds because if there is high
#flactuation then we may have a fraud
#in the graph we can say that it is formed neighbourhoods according to the color
#For example if we have a very different in a neighbourhood which is based 
#on black then we have a possible fraud
pcolor(som.distance_map().T)
colorbar()
markers=['o','s']
colors=['r','g']
#detection of id for frauds
for i,x in enumerate(X):
    #think the winning node as the visual represantation of a customer in a 2D plot
    #get the winning node for current customer
    w=som.winner(x)
Exemplo n.º 35
0
def plot_distances(som_model):
    from pylab import bone, pcolor, colorbar
    bone()
    pcolor(som_model.distance_map().T)
    colorbar()
X = sc.fit_transform(X)

# Training Self Organizing Map

from minisom import MiniSom

som = MiniSom(
    x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.01
)  # x,y for grid dimms ; input_len for x attributes; sigma default; l_r default
som.random_weights_init(X)  # initialize weights with random small number
som.train_random(data=X, num_iteration=100)

# Visualizing the results (higher mid, higher chance for outlayer - fraud)
from pylab import bone, pcolor, colorbar, plot, show
## Map of SOM nodes
bone()  ## window for a map
pcolor(som.distance_map(
).T)  ## matrix of all the node distances/different colors (T for transpose),
colorbar()  ## color legend

## Customers
markers = ['o', 's']  ## circles & squares
colors = ['r', 'g']  ## colors
for i, x in enumerate(
        X):  ## i for row, x for customer attributes in form of vector
    w = som.winner(x)  ## returns winning node for customer x
    plot(
        w[0] +
        0.5,  ## plot the marker into center of the winning node (0.5 to move to the middle)
        w[1] + 0.5,  ## y-cord
        markers[y[i]],  ## did customer got approval class
Exemplo n.º 37
0
x = sc.fit_transform(x)

# Training SOM
from minisom import MiniSom
som = MiniSom(x=20, y=20, input_len=15, learning_rate=0.5, sigma=1)

# Initializing the Input Weights
som.random_weights_init(x)

# Training the Model
som.train_random(x, num_iteration=100)

# Visualising the Data
from pylab import bone, show, pcolor, colorbar, plot

bone()  # Initializes the window containing the figure
pcolor(som.distance_map().T
       )  # Putting the different inter neuron distance on the map
colorbar()
markers = ['o', 's']
colors = ['r', 'g']

for i, x in enumerate(x):
    w = som.winner(x)  # Returns the winning node of the map
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[y[i]],
         markeredgecolor=colors[y[i]],
         markerfacecolor=None,
         markersize=10,
         markeredgewidth=2)
Exemplo n.º 38
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pywt import WaveletPacket
import pylab
import numpy

x = numpy.arange(612 - 80, 20, -0.5) / 150.
data = numpy.sin(20 * pylab.log(x)) * numpy.sign((pylab.log(x)))
from sample_data import ecg as data

wp = WaveletPacket(data, 'sym5', maxlevel=4)

pylab.bone()
pylab.subplot(wp.maxlevel + 1, 1, 1)
pylab.plot(data, 'k')
pylab.xlim(0, len(data) - 1)
pylab.title("Wavelet packet coefficients")

for i in range(1, wp.maxlevel + 1):
    ax = pylab.subplot(wp.maxlevel + 1, 1, i + 1)
    nodes = wp.get_level(i, "freq")
    nodes.reverse()
    labels = [n.path for n in nodes]
    values = -abs(numpy.array([n.data for n in nodes]))
    pylab.imshow(values, interpolation='nearest', aspect='auto')
    pylab.yticks(numpy.arange(len(labels) - 0.5, -0.5, -1), labels)
    pylab.setp(ax.get_xticklabels(), visible=False)

pylab.show()
Exemplo n.º 39
0
def som_train():
    # importing the dataset
    print("导入数据...")
    dataset = pd.read_csv('data_train.csv')
    # get the matrix of features
    X = dataset.iloc[:, :-1].values
    # get the label of each sample
    Y = dataset.iloc[:, -1].values
    # get the labels
    labels = list(set(Y))

    print(labels)
    print(type(labels[0]))
    print(len(X), len(labels))
    # feature scaling
    print("归一化数据...")

    sc = MinMaxScaler(feature_range=(0, 1))
    X = sc.fit_transform(X)

    # training the SOM
    print("训练数据...")
    from minisom import MiniSom
    # this can be changed:
    som = MiniSom(x=7, y=7, input_len=len(X[0]), sigma=3.0, learning_rate=0.5,
                  neighborhood_function='triangle', random_seed=10)
    som.random_weights_init(X)
    som.train_random(data=X, num_iteration=4000)

    # visualizing the result
    print("画图并记录日志")
    if os.path.exists('logs'):
        os.remove('logs')
    else:
        filelogs = open('logs', 'w', encoding="utf-8")

    bone()
    pcolor(som.distance_map().T)
    colorbar()
    markers = ['o', 'D', 'h', 'H', '_', '8', 'p', ',',
               '+', '.', 's', '*', 'd', '3', '0', '1', '2', 'v', '<', '7', '4', '5', '6']
    colors = ['#F0F8FF', '#FAEBD7', '#00FFFF', '#7FFFD4', '#F0FFFF', '#F5F5DC', '#FFE4C4',
              '#000000', '#FFEBCD', '#0000FF', '#8A2BE2', '#A52A2A', '#DEB887', '#5F9EA0',
              '#7FFF00', '#D2691E', '#FF7F50', '#6495ED', '#FFF8DC', '#DC143C', '#00FFFF',
              '#00008B', '#008B8B']

    for i, x in enumerate(X):
        w = som.winner(x)
        filelogs.write("第" + str(i) + "数据对应的优胜神经元为:(" +
                   str(w[0]) + "," + str(1) + "),攻击类型的数据标识为: " +
                   str(Y[i]) + "\n")
        plot(w[0] + 0.5,
             w[1] + 0.5,
             markers[labels.index(Y[i])],
             markeredgecolor=colors[labels.index(Y[i])],
             markerfacecolor='None',
             markersize=10,
             markeredgewidth=3)
    filelogs.close()
    print("日志录入完毕请查看logs文件")
    show()

    print("测试模型...")
    test_cases = pd.read_csv('testcases.csv')
    print(test_cases)
    test_cases_X = test_cases.iloc[:, :-1].values
    print(len(test_cases_X))
    test_cases_Y = test_cases.iloc[:, -1].values
    print(len(test_cases_Y))
    test_cases_X = sc.fit_transform(test_cases_X)
    test_result_file = open('testresult', 'w', encoding="utf-8")
    for i, x in enumerate(test_cases_X):
        w = som.winner(x)
        test_result_file.write("第" + str(i) + "数据对应的优胜神经元为:(" +
                   str(w[0]) + "," + str(1) + "),正确的攻击类型的数据标识为: " +
                   str(test_cases_Y[i]) + "\n")

    print("模型测试完毕,请查看testlog文件")
Exemplo n.º 40
0
    if -extra_factor < x < extra_factor and -extra_factor < y < extra_factor
]


xs = np.linspace(-1, 1, size).astype(np.float32)[None, :]
ys = np.linspace(-1, 1, size).astype(np.float32)[:, None]

vectors = np.zeros((size, size, 2), dtype=np.float32)
for (x, y) in vortices:
    rsq = (xs - x) ** 2 + (ys - y) ** 2
    vectors[..., 0] += (ys - y) / rsq
    vectors[..., 1] += -(xs - x) / rsq

texture = np.random.rand(size, size).astype(np.float32)

plt.bone()
frame = 0

if video:
    kernellen = 31
    for t in np.linspace(0, 1, 16 * 5):
        kernel = np.sin(np.arange(kernellen) * np.pi / kernellen) * (
            1 + np.sin(2 * np.pi * 5 * (np.arange(kernellen) / float(kernellen) + t))
        )

        kernel = kernel.astype(np.float32)

        image = lic_internal.line_integral_convolution(vectors, texture, kernel)

        plt.clf()
        plt.axis("off")
Exemplo n.º 41
0
som.random_weights_init(X)

# train
som.train_random(data=X, num_iteration=1000)  #  reinforcement learning, update the weights after each observation

# Visualizing the result

# We ll see the 2D grid that will contain all final winning nodes and we will get MIDs for the specific nodes
# MID of a node - mean of the distances of all the neurons around the winning node based on sigma (radius of the neighbourhood).
# The higher is MID the further the winning node will be from it s neoigbours. So the more the MID - the more it is an outlier, cauz it s the furthest from general behaviour pattern

# We ll use colors. The larger the MID, the closer to white the color will be

from pylab import bone, pcolor, colorbar, plot, show

bone()  # init the window for the map

# put the different winning nodes on the map. put info about the MIDs
# different colors correspond to the different values of MIDs

pcolor(som.distance_map().T)  # som.dist will return a matrix of distances but we transpose the matrix

# add legend
colorbar()

# white are frauds

# now we ll distiguish between those who got approval
# more interested in the ones who got the approval and are frauds

# red squares - who didnt, green - did
Exemplo n.º 42
0
   to make, since we start with a high dimensional dataset with lots of non-linear relationships and it 
   will be much easier for the model  to be trained if features are scaled.
'''

# Training the SOM
from minisom import MiniSom  # the other python file containing the class
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
'''10x10 grid, 15 features in the dataset, sigma-radius of neighbourhood, 0.5-by how much
   the weights are updated during each iteration, the higher, the faster there will be convergence
   the lower, the slower it will be built '''
som.random_weights_init(X)  # initialise weights
som.train_random(data=X, num_iteration=100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  # the window that will contain the map
pcolor(som.distance_map().T
       )  # all the values of the MID for all the winning nodes of the SOM
# the T takes the transpose of this MID matrix
colorbar()  # leyend of colours
'''The white squares represent where the frauds are. Frauds are identified by outlying winning nodes,
   the ones that are far from the general rules.
   We want to find the customers who are a fraud but got approved (class 1)'''
markers = ['o', 's']  # o - circle, s - square
colors = ['r', 'g']  # red, green
for i, x in enumerate(X):  # 2 looping variables (i and x)
    '''i - the different values of all the indexes of dataset 0 - 689 //rows
       x - the different vectors of customers (first customer, then second customer, etc...) //columns
       enumerate(X) - X is the dataset 
    '''
    w = som.winner(x)  # winning node of customer x
Exemplo n.º 43
0
X = sc.fit_transform(X)

# Training the SOM using MiniSOM
from minisom import MiniSom

map_dims = (10, 10)
num_of_iters = 100

som = MiniSom(*map_dims, X.shape[1])
som.random_weights_init(X)
som.train_random(X, num_of_iters)

# Plot the resulting map
from pylab import bone, pcolor, colorbar, plot, show

bone()  # Makes blank plot window
pcolor(som.distance_map().T)  # Adds distance map
colorbar()  # Adds a color bar to explain what the colors in the map represent

markers = ['o', 's']
colors = ['r', 'g']

for i, x in enumerate(X):
    w = som.winner(x)
    plot(
        w[0] + 0.5,
        w[1] + 0.5,
        markers[Y[i]],
        markeredgecolor=colors[Y[i]],
        markerfacecolor='None',
        markersize=10,
Exemplo n.º 44
0
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

# Training the SOM
from minisom import MiniSom
#minisom is a package for Self Organising Maps, not on pip
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
#x,y are diemensions of som. in input we keep customer id so that we can identify customers later
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  #initialize figure
pcolor(
    som.distance_map().T
)  #pcolor makes a color range,and distance_map will return all mean inter neuron distances and we take transpose.
colorbar()
markers = ['o', 's']
colors = ['r', 'g']
for i, x in enumerate(X):
    w = som.winner(x)  #returns cordinates of neuran with least MID
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[y[i]],
         markeredgecolor=colors[y[i]],
         markerfacecolor='None',
         markersize=10,
         markeredgewidth=2)
Exemplo n.º 45
0
def spline_subtract_counts(file1, file2, outfile, outplot=None, soffset=0.0):
    # given two gr files, do a spline fit to find the best mapping of file2 to file1, and then subtract from file1
    #  the fitted value at each point
    # write the difference to outfile
    # soffset is added to all of the fitted values prior to subtraction
    # This is basically designed for finding differences between log scaled data sets (e.g., ipod vs inp log ratio files)

    print "entering spline_subtract_counts with %s %s %s %s %s" % (
        file1, file2, outfile, outplot, soffset)

    locs1, vals1 = read_grfile(file1)
    locs2, vals2 = read_grfile(file2)

    sortord = numpy.argsort(vals2)

    v1_forfit = vals1[sortord]
    v2_forfit = vals2[sortord]

    # apply a correction to prevent negative rnapol values to correspond to positive expected ipod occupancies
    v1_forfit[v2_forfit < 0.0] = 0.0

    myspl = growthcurves.fit_bspline(v2_forfit, v1_forfit, numknots=5)

    xmin = numpy.min(vals2)
    xmax = numpy.max(vals2)
    xvals_test_plot = numpy.linspace(xmin, xmax)
    print "done with fit"
    predvals = growthcurves.eval_bspline(vals2, myspl)
    predvals_forplot = growthcurves.eval_bspline(xvals_test_plot, myspl)

    print xvals_test_plot
    pylab.figure()
    pylab.bone()
    pylab.hexbin(vals2,
                 vals1,
                 gridsize=50,
                 xscale='linear',
                 yscale='linear',
                 bins='log')
    #pylab.plot(vals2,vals1,'bo')
    pylab.plot(xvals_test_plot, predvals_forplot, 'g-')
    pylab.plot(xvals_test_plot, 1 + predvals_forplot, 'b-')
    pylab.plot(xvals_test_plot, predvals_forplot - 1, 'r-')
    pylab.plot(xvals_test_plot, soffset + predvals_forplot, 'y-')
    pylab.xlabel(file2)
    pylab.ylabel(file1)
    if (outplot is not None):
        pylab.savefig(outplot + "_2dhist.png")

    #pylab.figure()
    #pylab.hist(numpy.log2( v1_forloess / v2_forloess), bins=25)
    #if (outplot is not None):
    #  pylab.savefig(outplot + "_1dhist.png")

    print "A"
    pylab.figure()
    tmpvals = vals1 - (predvals + soffset)
    print "B"
    pylab.hist(tmpvals, bins=25)
    if (outplot is not None):
        pylab.savefig(outplot + "_1dhist_subvals.png")

    print "C"

    print vals1.shape
    print predvals.shape
    predvals.shape = vals1.shape

    newvals = vals1 - (predvals + soffset)
    write_grfile(locs1, newvals, outfile)
Exemplo n.º 46
0
    output_fn_fig = output_fn_base + 'frame%d.png' % (frame)
    print "Saving to file: ", output_fn_dat
    np.savetxt(output_fn_dat, output_arrays[frame])

    print "Plotting frame: ", frame
    fig = pylab.figure()
    ax = fig.add_subplot(111, axisbg=bg_color)
    ax.set_xlabel('$x$')
    ax.set_ylabel('$y$')
    ax.set_title('Spatial activity readout')

#    for gid in xrange(n_cells): # compute color
#     simple colormap
    norm = matplotlib.mpl.colors.Normalize(vmin=0, vmax=z_max)
    cax = ax.pcolor(output_arrays[frame], norm=norm)#, edgecolor='k', linewidths='1')
    pylab.colorbar(cax, cmap=pylab.bone())


    print "Saving figure: ", output_fn_fig
    pylab.savefig(output_fn_fig)


# let's make a movie!
print 'Creating the movie in file:', output_fn_movie
fps = 8     # frames per second
input_fn = output_fn_base + 'frame%d.png'
command = "ffmpeg -f image2 -r %f -i %s -b 72000 %s" % (fps, input_fn, output_fn_movie)
os.system("rm %s" % output_fn_movie) # remove old one
os.system(command)

show = False
Exemplo n.º 47
0
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
X = sc.fit_transform(X)

# Training the SOM
from minisom import MiniSom
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5)
#x,y is dimension of som
#input len is feature here 15 because to catch faruad so use ID also,sigma is radius ,l_r is how much weight iterate for each iteration
#higher l_r fastely get O/P
som.random_weights_init(X)  #ransom weights
som.train_random(data=X, num_iteration=100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()  #get blank window
pcolor(som.distance_map().T)  #transpose of dis vector
colorbar()  #legend highest distance then 1
markers = ['o', 's']
colors = ['r', 'g']  #red circle not approved
for i, x in enumerate(X):  #i is 1,2,3...,x is vector of values
    w = som.winner(x)
    plot(
        w[0] + 0.5,
        w[1] + 0.5,  #centre
        markers[y[i]],  #gives approved or  not
        markeredgecolor=colors[y[i]],
        markerfacecolor='None',  #inside color
        markersize=10,
        markeredgewidth=2)
show()