def __init__(self, inputs, outputs, data, name=None, outputFullMap=False): # initiate the KohonenMap KohonenMap.__init__(self, inputs, outputs, name, outputFullMap) # data representing feature space/dimensions self.data = data #keep a list of callbacks self._messageHandlers = []
def kohonen(): som = KohonenMap(2, 10) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') for i in range(25000): # one forward and one backward (training) pass som.activate(random.random(2)) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw()
def categorize(categories, data, words): nnodes = sqrt(categories) # make a kohonen map som = KohonenMap(len(data[1])-1, nnodes) # train the network for i in range(25000): # one forward and one backward (training) pass on a random line som.activate(data[random.randint(1, len(data)-1)][1:]) som.backward() # run on the data if not words: for point in data[1:-1]: #find the category for a point of data print(point[0] + ',' + str(som.activate(point[1:]))) #make wordlist output similar to ICA for use in GAs if words: results = [[] for i in range(categories)] for point in data[1:-1]: # find the cluster for this word and add in the word's data result = som.activate(point[1:]) results[int(result[0]*nnodes + result[1])].append(point) # print out the clusters for i in range(NWORDS): # TODO: This is super inefficient for cluster in results: if len(cluster) == 0: sys.stdout.write('EMPTY') continue for j in range(len(cluster)): for k in range(1, len(cluster[j])): cluster[j][k] = float(cluster[j][k]) tempCluster = sorted(cluster, key=lambda point: -sum(point[1:])) sys.stdout.write(str(tempCluster[i%len(cluster)][0]) + ',') sys.stdout.write('\n')
def aldohonen(size, iterations, training_input, refresh_rate, callback): som = KohonenMap(3, size) #just changing initial neurons weight from 0~1 to 0~255 for i in som.neurons: for j in i: for k in range(len(j)): j[k] *= 255 training_size = len(training_input) - 1 for i in range(iterations): # one forward and one backward (training) pass som.activate(training_input[random.randint(0, training_size)]) som.backward() #just draw som in our Qt View if (i % refresh_rate == 0): image = som2image(som, size) callback(image, i) else: callback(None, i) callback(som2image(som, size), iterations - 1)
patternTestTarget = np.zeros([numPatTest]) for i in range(numPatTrain): patternTrainTarget[i] = patternTrain[i, 0] for i in range(numPatValid): patternValidTarget[i] = patternValid[i, 0] for i in range(numPatTest): patternTestTarget[i] = patternTest[i, 0] counterOut = 0 while(counterOut < 10): neuronas = 10 #Crear y entrenar el mapa autoorganizado som = KohonenMap(numColsTrain-1, neuronas) #Entrenar el mapa for i in range(numPatTrain): som.activate(patternTrainInput[i]) som.backward() #Crear el dataset de entrenamiento de backprop con resultados del mapa input = np.zeros([numPatTrain,neuronas**2]) distMatrix = np.zeros([neuronas, neuronas]) for i in range(numPatTrain): tmp = som.activate(patternTrainInput[i]) distMatrix[tmp[0], tmp[1]] += 1 inputTMP = np.zeros(neuronas**2) inputTMP[(neuronas*tmp[0]) + tmp[1]] = 1.0 input[i] = inputTMP
################################################## # Example for Kohonen Map # # Clusters random 2D coordinates in range [0,1] # with a Kohonen Map of 5x5 neurons. # # Note: you need pylab to show the results ################################################## __author__ = "Thomas Rueckstiess, [email protected]" import pylab from scipy import random from pybrain.structure.modules import KohonenMap som = KohonenMap(2, 5) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), "s") for i in range(25000): # one forward and one backward (training) pass som.activate(random.random(2)) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw()
import numpy as np from lib.features import Features from pybrain.structure.modules import KohonenMap import pickle np.random.seed(42) symbol1 = '0005.HK' yahoo_data1 = YahooHistorical(data_from=date(2000, 1, 1), data_to=date(2015, 12, 31)) yahoo_data1.open(os.path.join(os.path.dirname(__file__), 'data/' + symbol1 + '.csv')) data1 = yahoo_data1.get() dataset1 = np.asarray([n['adj_close'] for n in data1]) p = 17 # 17-day p = 5 # 5-day nodes = 3 som = KohonenMap(p, nodes) # som = pickle.load(open("pattern5.p", "rb")) som.learningrate = 0.01 epochs = 1000 training_dataset = [] result = {} # preparation for i in xrange(p, len(dataset1)): training_input = dataset1[i-p:i] mmax = np.max(training_input) mmin = np.min(training_input) training_input = (training_input - mmin) / (mmax - mmin) if np.isnan(training_input).any(): training_input = np.array([0] * p) training_dataset.append(training_input)
def run(): # init the SquareMap object (which initiates pygame) if imgFlag == 0: # if its a grayscale similarity map use a small grid square size squaremap = SquareMap(gridSize=(somOutputs, somOutputs), gridSquareSize=(5, 5)) else: squaremap = SquareMap(gridSize=(somOutputs, somOutputs)) squaremap.initMap() # Used to manage how fast the screen updates #clock = pygame.time.Clock() fullscreen = False #Loop until the user presses the escape button (or otherwise quits). done = False while done == False: # Limit to 15 frames per second #clock.tick(15) ########################### # Key events for quitting # # & setting fullscreen # ########################### for event in pygame.event.get(): if event.type == QUIT: done = True if squaremap.keyPressed(K_ESCAPE): done = True # toggle fullscreen and cursor on/off elif squaremap.keyPressed(K_f): if not fullscreen: squaremap.fullScreen(True) fullscreen = True else: squaremap.fullScreen(False) fullscreen = False elif squaremap.keyPressed(K_SPACE): # load and place the images squaremap.placeImages() # this is only for testing ############################### # Database and SOM operations # ############################### # get data from DB (all the rows) rows = getData() imgPath = "" # initialize SOM object som = KohonenMap(somInputs, somOutputs, name="SquareMap", outputFullMap=False) # set the learning rate (should this be a command line arg?) som.learningrate = learningRate # Do SOM operations here # do as many iterations as there is DB entries/images #======================================================================= # for i in range(rows): # # 8 feature vectors # data = [rows[i][1], rows[i][2], rows[i][3], rows[i][4], rows[i],[5], rows[i][6], rows[i][7], rows[i][8]] # # one forward and one backward (training) pass # som.activate(data) # feature vectors go here # som.backward() # training #======================================================================= for i in range(100): som.activate(random.random(somInputs)) som.backward() #=================================================================== # # print & display every 10th step # if i % 1 == 0: # print som.neurons # print "======================" #=================================================================== # send data to update the square map # if images we selected at the command line do an image similarity mapping #otherwise do a grayscale 'blobs' similarity map if imgFlag == 0: squaremap.createSimilarityMap(som.neurons) else: #send the current img file path and the coords of the winner neuron along w/SOM's output imgPath = rows[i][10] squaremap.placeImages(som.neurons, imgPath, som.winner)
parser.add_argument('infile', type=argparse.FileType('r'), help='the file of data, separated onto separate lines') parser.add_argument('categories', type=int, help='the number of categories, should be a perfect square') parser.add_argument('--words', '-w', '-words', dest='words', action='store_const', const=True, default=False, help='Prints the words associated with each category at the end of all of the categories') args = parser.parse_args() infile = args.infile categories = args.categories nnodes = sqrt(categories) words = args.words # process the data data = [] for line in infile: data.append(line.split(',')) som = KohonenMap(len(data[1])-1, nnodes) # train the network for i in range(25000): # one forward and one backward (training) pass on a random line som.activate(data[random.randint(1, len(data))][1:]) som.backward() # run on the data #if not words: for point in data[1:]: #find the category for a point of data print(point[0] + ',' + str(som.activate(point[1:])))
################################################## # Example for Kohonen Map # # Clusters random 2D coordinates in range [0,1] # with a Kohonen Map of 5x5 neurons. # # Note: you need pylab to show the results ################################################## __author__ = 'Thomas Rueckstiess, [email protected]' import pylab from scipy import random from pybrain.structure.modules import KohonenMap som = KohonenMap(2, 5) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') for i in range(25000): # one forward and one backward (training) pass som.activate(random.random(2)) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw()
import pylab import numpy as np from scipy import random from pybrain.structure.modules import KohonenMap from random import randint kohonendata = [ l.strip().replace(',', '.').split('^')[2:4] for l in open('data/banks.dat').readlines() ] som = KohonenMap(len(kohonendata[0]), 66) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') l = len(kohonendata) for i in range(250000): # one forward and one backward (training) pass k = randint(0, l - 1) som.activate(kohonendata[k]) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw() pylab.savefig('kohonen.png') # =CONCATENATE("[";C2;",";D2;",";E2;",";F2;",";G2;",";H2;",";I2;",";J2;",";K2;",";"]")
def run(): # init the HexagonMap object (which initiates pygame) hexmap = HexagonMap(gridSize=(somOutputs, somOutputs)) hexmap.initMap() # Used to manage how fast the screen updates #clock = pygame.time.Clock() fullscreen = False #Loop until the user presses the escape button (or otherwise quits). done = False while done == False: # Limit to 15 frames per second #clock.tick(15) ########################### # Key events for quitting # # & setting fullscreen # ########################### for event in pygame.event.get(): # User did something if event.type == QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if hexmap.keyPressed(K_ESCAPE): done = True # toggle fullscreen and cursor on/off elif hexmap.keyPressed(K_f): if not fullscreen: hexmap.fullScreen(True) fullscreen = True else: hexmap.fullScreen(False) fullscreen = False elif hexmap.keyPressed(K_SPACE): hexmap.createSimilarityMap() # this is only for testing ############################### # Database and SOM operations # ############################### # get data from DB (all the rows) rows = getData() # initialize SOM object som = KohonenMap(somInputs, somOutputs, name="HexagonMap", outputFullMap=False) # set the learning rate (should this be a command line arg?) som.learningrate = learningRate # Do SOM operations here # do as many iterations as there is DB entries/images #======================================================================= # for i in range(rows): # # 8 feature vectors # data = [rows[i][1], rows[i][2], rows[i][3], rows[i][4], rows[i],[5], rows[i][6], rows[i][7], rows[i][8]] # # one forward and one backward (training) pass # som.activate(data) # feature vectors go here # som.backward() # training #======================================================================= for i in range(100): som.activate(random.random(somInputs)) som.backward() #=================================================================== # # print & display every 10th step # if i % 10 == 0: # print som.neurons # print "======================" #=================================================================== # send data to update the hex map hexmap.createSimilarityMap(som.neurons)