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)
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) # training
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)