def main(): #Load .xls .xlsx file file_name = raw_input('File name: ') work_book = load_workbook(file_name) #Load sheet print (work_book.get_sheet_names()) sheet_name = raw_input('\nSheet name: ') work_sheet = work_book.get_sheet_by_name(sheet_name) #Select workspace in sheet begin_position = raw_input('Begin position: ') end_position = raw_input('End position: ') workspace = work_sheet[begin_position : end_position] #Workspace(begin, end) #Load data training_set = [] for row in workspace: class_name = row[0].value # first column is class name for item in row[1:]: # skip first column inputs = np.array([item.value]) training_set.append((inputs, class_name)) #Create SOM ANN som_ann = SOM((1, 4, 4)) #Train network print 'Wait, I\'m traning...' som_ann.train([i[0] for i in training_set], 200, -1, 'quadratic') somplot(som_ann, training_set)
def som(self, m, n, dim, iterations): # step 4 trainData = numpy.zeros((len(self.contrastPoints), 1)) for i in range(len(self.contrastPoints)): trainData[i] = [self.contrastPoints[i]] som = SOM(m, n, dim, iterations) som.train(trainData) mapped = som.map_vects(trainData) d0 = 0 d1 = 0 d2 = 0 for d, m in zip(trainData, mapped): mapv = m[0] + m[1] if (d0 == 0 and mapv == 0): d0 = d[0] if d0 < d[0] else d0 if (d1 == 0 and mapv == 1): d1 = d[0] if d1 < d[0] else d1 if (d2 == 0 and mapv == 2): d2 = d[0] if d2 < d[0] else d2 least = 0 if d0 < d1 and d0 < d2 else (1 if d1 < d0 and d1 < d2 else (2 if d2 < d1 and d2 < d0 else 0)) most = 0 if d0 > d1 and d0 > d2 else (1 if d1 > d0 and d1 > d2 else (2 if d2 > d1 and d2 > d0 else 0)) mean = 0 if d0 > d1 and d0 < d2 else (1 if d1 > d0 and d1 < d2 else (2 if d2 > d1 and d2 < d0 else 0)) print(d0, d1, d2) print(least, most, mean) self.mappingDict = {'least': least, 'mean': mean, 'most': most} self.blocksMapping = mapped
def SelfOrganizingMap(Feature_List): som = SOM(SOM_LINES, SOM_COLS, len(Feature_List[0]), SOM_ITERATIONS) som.train(Feature_List) image_grid = som.get_centroids() mapped = som.map_vects(Feature_List) return mapped, image_grid
def SelfOrganizingMap(feature_list): no_features = len(feature_list[0]) print('Clustering', no_features, 'features') som = SOM(SOM_LINES, SOM_COLS, no_features, SOM_ITERATIONS, alpha=ALPHA, sigma=SIGMA) som.train(feature_list) image_grid = som.get_centroids() mapped = som.map_vects(feature_list) return mapped, image_grid
def solve_tsp(filename: str): # Parse problem from txt file problem = parse_tsp_problem(filename) cases = problem[:, 1:] # Initialize self organizing map som = SOM(cases=cases) # Train som.train() # Use Som weights to generate a solution for tsp problem solution = create_tsp_solution(som.cases, som.weights) visualize_tsp(solution, som.weights)
def main(): # Training inputs for RGBcolors credits_data = pd.read_csv("testdata/credit_train.csv", header=None) columns = credits_data.columns train_data = credits_data[columns[:-1]].copy().values label_data = credits_data[columns[-1]].copy().values train_data_scale = preprocessing.scale(train_data) # colors = np.array( # [[0., 0., 0.], # [0., 0., 1.], # [0., 0., 0.5], # [0.125, 0.529, 1.0], # [0.33, 0.4, 0.67], # [0.6, 0.5, 1.0], # [0., 1., 0.], # [1., 0., 0.], # [0., 1., 1.], # [1., 0., 1.], # [1., 1., 0.], # [1., 1., 1.], # [.33, .33, .33], # [.5, .5, .5], # [.66, .66, .66]]) # color_names = \ # ['black', 'blue', 'darkblue', 'skyblue', # 'greyblue', 'lilac', 'green', 'red', # 'cyan', 'violet', 'yellow', 'white', # 'darkgrey', 'mediumgrey', 'lightgrey'] # Train a 20x30 SOM with 400 iterations som = SOM(1, 2, len(columns) - 1, 400) som.train(train_data_scale) # Get output grid image_grid = som.get_centroids() # Map colours to their closest neurons mapped = som.map_vects(train_data_scale) # Plot plt.imshow(image_grid) plt.title('Color SOM') for i, m in enumerate(mapped): plt.text(m[1], m[0], label_data[i], ha='center', va='center', bbox=dict(facecolor='white', alpha=0.5, lw=0)) plt.show()
def SortingSelfOrganizingMap(Feature_List): # Train a SS_LINES x SS_COLS self-organizing map using NUM_ITERATIONS iterations som = SOM(SS_LINES, SS_COLS, len(Feature_List[0]), NUM_ITERATIONS) som.train(Feature_List) mapped = som.map_vects(Feature_List) # Grayscale landscape sorting_landscape = np.zeros((SS_LINES, SS_COLS), np.float16) for i in range(len(mapped)): lin = mapped[i][0] col = mapped[i][1] sorting_landscape[lin][col] = Feature_List[i][0] io.imsave('sorted_landscape.png', sorting_landscape)
def call_som(m, n, dim, input_data, weight_after_insertion=None, alpha=None, sigma=None): # get the shape of input data inorder to calc iternumber in SOM iter_no = input_data.shape[0]*10 som = SOM(m, n, dim, weight_after_insertion, n_iterations= iter_no) trained_weight = som.train(input_data) mapped = som.map_vects(input_data) result = np.array(mapped) return trained_weight, result
def som_plot(img, som_dim): # Method for creating a small Kohonen Self-Organizing Map (SOM) of the image and # plotting the SOM on the screen. RGB values of the colors are printed on the screen. # Select small amount of random pixels from the image. n_pixels = 500 colors = shuffle(img.reshape((img.shape[0] * img.shape[1], 3)))[:n_pixels] print('\n') print('=' * 80) print('Self-Organized Map of {} randomly picked colors:'.format(som_dim * som_dim)) # Train the SOM model with small amount of iterations. som = SOM(som_dim, som_dim, 3, 10) som.train(colors) #Get output grid from the SOM. This is plotted as color palette. image_grid = som.get_centroids() plt.figure(figsize=(5, 5)) plt.imshow(image_grid) plt.title('Color map') plt.show() # Create RGB palette values from the image_grid. grid = np.array(image_grid) grid *= 255 grid = grid.astype(np.int) rgb_df = pd.DataFrame(np.zeros((som_dim, som_dim))) hex_df = pd.DataFrame(np.zeros((som_dim, som_dim))) for i in range(som_dim): for j in range(som_dim): rgb_df.iloc[i, j] = str(grid[i, j]) hex_color = '#{:02x}{:02x}{:02x}'.format(grid[i, j, 0], grid[i, j, 1], grid[i, j, 2]) hex_df.iloc[i, j] = hex_color print('RGB values:') print(rgb_df) print('\n') print('HEX color values:') print(hex_df)
def call_som(m, n, dim, input_data, weight_after_insertion=None, alpha=None, sigma=None): som = SOM(m, n, dim, weight_after_insertion) trained_weight = som.train(input_data) mapped = som.map_vects(input_data) result = np.array(mapped) return trained_weight, result
def tensorSOM(): import numpy as np from som import SOM import tensorflow as tf sess = tf.InteractiveSession() map_size = 30 s = SOM(input_shape=(num_features, ), map_size_n=map_size, num_expected_iterations=epochs, session=sess) sess.run(tf.initialize_all_variables()) #Training inputs for i in range(10): print('Epoch: {0}'.format(i)) rnd_ind = np.random.randint(0, len(X)) s.train(X[rnd_ind, :]) print(np.reshape(s.get_weights(), [map_size, map_size, num_features])) print(np.array(y) * s.get_weights())
def main(): # FIT THE LDA lda = TagsLDA(skip=10) theta, beta = lda.train(ntopics=30, niter=300, seed=42) # TRAIN THE SOM dist_func = lambda p, q: divergence(p, q) som = SOM(grid_shape=(20, 20), ndims=lda.nterms, dist_func=dist_func) # len(dictionary) = len(dictionary.token2id) som.train(lda.beta, nepochs=10) # PLOT labels = [lda.topic_representatives(i, topn=3, show_scores=False) for i in range(lda.ntopics)] areas = np.array([30*2*np.pi*score**2 for score in lda.topic_significances()]) areas_top, top_indices = zip(*sorted([(a, i) for i, a in enumerate(areas)], reverse=True)[:10]) locations = som.get_locations(vecs=lda.beta[top_indices, :], labels=labels) # print(locations) labels, data = zip(*locations.items()) x, y = zip(*data) plt.scatter(x, y, s=areas_top, alpha=0.5) for l, x, y in zip(labels, x, y): plt.annotate(l, xy=(x, y), textcoords='offset points', xytext=(0, 20), horizontalalignment='center') plt.show()
def som3(self, m, n, dim, iterations): # step 4 trainData = numpy.zeros((len(self.contrastPoints), 3)) for i in range(len(self.contrastPoints)): trainData[i] = [self.diagonalContrast[i], self.verticalContrast[i], self.horizontalContrast[i]] som = SOM(m, n, dim, iterations) som.train(trainData) mapped = som.map_vects(trainData) d0 = 0 d1 = 0 d2 = 0 for d, m in zip(trainData, mapped): mapv = m[0] + m[1] if (d0 == 0 and mapv == 0): d0 = d[0] if d0 < d[0] else d0 if (d1 == 0 and mapv == 1): d1 = d[0] if d1 < d[0] else d1 if (d2 == 0 and mapv == 2): d2 = d[0] if d2 < d[0] else d2 arr = numpy.array([d0, d1, d2]) most = numpy.argmax(arr) numpy.delete(arr, most, 0) least = numpy.argmin(arr) numpy.delete(arr, least, 0) mean = 0 if (arr[most] == d1 or arr[most] == d2) and (arr[least] == d1 or arr[least] == d2) else (1 if (arr[most] == d0 or arr[most] == d2) and (arr[least] == d0 or arr[least] == d2) else 2) self.mappingDict = {'least': least, 'mean': mean, 'most': most} self.blocksMapping = mapped self.neuronsMap = som.get_centroids()
def main(): ''' Create and train a SOM with the XOR problem SOM settings ------------ The SOM used in this test works in a 2D space with a map's weights of 2x2 Train settings --------------- The SOM will be trained with the XOR problem, this training will run in 2000 iterations. In the weights adjustment function, a zero neighborhood radius, that mean that only the winner neuron's weights will be updated. ''' som_ann = SOM((2, 2, 2)) training_set = [ np.array([1, 0]), np.array([0, 1]), np.array([1, 1]), np.array([0, 0]), ] #Train network converged, epochs = som_ann.train(training_set, 2000, -1, 'zero') if not converged: print 'SOM didn\'t converge with the minimum learning rate\n' else: print 'SOM converged in {0} epochs\n'.format(epochs) #Mapping values for entry in training_set: class_name = som_ann.map(entry) print 'The object {0} belongs to class: {1}'.format(entry, class_name) #Get the full mapped classes print '' elements = som_ann.get_mapped_classes() print elements
y_list.append(float(data[1])) z_list.append(int(data[2])) input_list = [[a, b] for a, b in zip(x_list, y_list)] output_list = [a for a in z_list] input_list = np.array(input_list) output_list = np.array(output_list) """ Get Central points from SOM network """ som = SOM(2, 2, 2) print('Running SOM network...') som.train(input_list, num_epochs=200, init_learning_rate=0.3) som_result = [np.array(som.output[i]) for i in range(len(som.output))] """ Create RBF objects and run RBF network """ print('RBF with random c, updated by LMS: \n') rbf_1 = RBF(input_list, output_list, 9, 'random', som_c_list=None, update='lms') rbf_1.run() print("-------------------------------------") print('RBF with random c, updated by SGA: \n') rbf_2 = RBF(input_list, output_list, 9, 'random', som_c_list=None, update='sga') rbf_2.run()
filename, varstring = file.split(':') varnames = varstring.split(',') sources.append((filename, varnames)) som = SOM(som_shape, sources) print('SOM nodes shape ', som.nodes.shape) print('SOM std and mean ', som.std.shape, som.mean.shape) radius = (float(max(som.shape)), 0.0) now = time.time() som.train(args.iterations, radius, args.rate) print("\nThat took {:.3f} seconds".format(time.time()-now)) #som.plot('result.png', 0) #plt.figure(figsize=(8,8)) #i = 1 #for index in np.ndindex(som.shape): # print(index, i) # node_full = np.empty(tuple(variables[0].shape[1:])).flatten() # mask = np.ma.getmaskarray(variables[0][0,:].flatten()) # print(node_full.shape, mask.shape)
def SelfOrganizingMap(Feature_List, where): if not os.path.exists(where): os.makedirs(where) som = SOM(SOM_LINES, SOM_COLS, len(Feature_List[0]), NUM_ITERATIONS) som.train(Feature_List) # Get output grid image_grid = som.get_centroids() #print 'Image grid shows weights for all the input features' #print image_grid # Get vector mappings mapped = som.map_vects(Feature_List) #print 'Mapping', mapped # Visualization part # Needs to be refactored to produce output for any number of clusters avg_line = [] for i in range(NUM_CLUST): avg_line.append(0) avg_column = [] for i in range(NUM_CLUST): avg_column.append(0) for i in range(0, len(mapped), NUM_CLUST): for j in range(NUM_CLUST): avg_line[(i + j) % NUM_CLUST] += mapped[i + j][0] avg_column[(i + j) % NUM_CLUST] += mapped[i + j][1] for i in range(len(avg_line)): avg_line[i] = avg_line[i] / (len(mapped) / NUM_CLUST) for i in range(len(avg_column)): avg_column[i] = avg_column[i] / (len(mapped) / NUM_CLUST) 'Average location' print avg_line, avg_column # Grayscale landscape sorting_landscape = [] for i in range(NUM_CLUST): sorting_landscape.append(np.zeros((SOM_LINES, SOM_COLS), np.float16)) for i in range(len(mapped)): lin = mapped[i][0] col = mapped[i][1] sorting_landscape[i % NUM_CLUST][lin][col] += 0.05 if sorting_landscape[i % NUM_CLUST][lin][col] > 1.0: sorting_landscape[i % NUM_CLUST][lin][col] = 1.0 for i in range(NUM_CLUST): sorting_landscape[i][avg_line[i]][avg_column[i]] = 1.0 for i in range(NUM_CLUST): io.imsave(where + str(i) + '_sorting_cluster.png', sorting_landscape[i]) # Colored landscape colored_landscape = np.zeros((SOM_LINES, SOM_COLS, 3), np.float16) white = np.array([1.0, 1.0, 1.0]) # sorting insertion_color = np.array([0.0, 0.0, 1.0]) # blue bubble_color = np.array([0.0, 1.0, 0.0]) # green heap_color = np.array([1.0, 0.0, 0.0]) # red quick_color = np.array([1.0, 0.0, 1.0]) # magenta random_color = np.array([1.0, 1.0, 0.0]) # yellow # non-sorting reverseSorted_color = np.array([0.0, 1.0, 1.0]) # cyan intervalSwapSorted_color = np.array([1.0, 0.5, 0.0]) # orange reverse_color = np.array([0.5, 1.0, 0.0]) # lime-green intervalSwap_color = np.array([1.0, 0.0, 0.5]) # fuchsia colored = [ insertion_color, bubble_color, heap_color, quick_color, random_color ] #add_colored = [reverseSorted_color, intervalSwapSorted_color, reverse_color, intervalSwap_color] #colored.extend(add_colored) grad = 0.005 for i in range(len(mapped)): lin = mapped[i][0] col = mapped[i][1] colored_landscape[lin][col] += grad * colored[i % NUM_CLUST] #if colored_landscape[lin][col].any() > 1.0: # colored_landscape[lin][col] -= 0.05*colored[i%NUM_CLUST] for i in range(NUM_CLUST): colored_landscape[avg_line[i]][ avg_column[i]] = 0.3 * white + 0.7 * colored[i] io.imsave(where + 'all_sorting_clusters.png', colored_landscape)
import numpy as np import pandas as pd from som import SOM from sklearn.datasets import load_boston from sklearn.preprocessing import StandardScaler # read in data boston = load_boston().data # standard scale data ss = StandardScaler() X = ss.fit_transform(boston) # instantiate SOM som = SOM(X, 3, 3, 1, 100, 0.01) # train model tree, weights, winners, distances = som.train(X) # print data # print(weights) print(set(winners)) dist, ind = som.predict(X, tree) print(set([x[0] for x in ind]))
from som import SOM import pandas as pd import numpy as np input_data = pd.read_csv("C:/Users/KHT/Desktop/hw2out.csv") som_net = SOM(10, 10, 2) coor_data = input_data.iloc[np.random.permutation(len(input_data))] trunc_data = coor_data[["x", "y"]] print(trunc_data.values) # print(trunc_data.values.shape[0]) som_net.train(trunc_data.values, num_epochs=1000, init_learning_rate=0.1) """ def predict(df): bmu, bmu_idx = som_net.find_bmu(df.values) df['bmu'] = bmu df['bmu_idx'] = bmu_idx return df clustered_df = trunc_data.apply(predict, axis=1) result_array = clustered_df.to_records().tolist() print("ID number {0}".format(result_array[0][0])) print("X Coordinate on topological map {0}".format(result_array[0][4][0])) print("Y Coordinate on topological map {0}".format(result_array[0][4][1])) """
#For plotting the images from matplotlib import pyplot as plt import numpy as np from som import SOM colors = np.array([[0., 0., 1.], [0., 0., 0.95], [0., 0.05, 1.], [0., 1., 0.], [0., 0.95, 0.], [0., 1, 0.05], [1., 0., 0.], [1., 0.05, 0.], [1., 0., 0.05], [1., 1., 0.]]) som = SOM(4, 4, 3) som.train(colors) plt.imshow(som.centroid_grid) plt.show()
inputFilename = 'myo_raw_data.csv' header = '% qx, qy, qz, qw, ax, ay, az, gx, gy, gz\n' try: f = open(inputFilename, 'r') except Exception as error: print("ERROR: Couldn't read from {}".format(inputFilename)) else: with f: reader = csv.reader(f) for row in reader: if row[0][0] is not '%': myo_data.append(row) print myo_data[0] #Training inputs for Myo data (quat, accel, gyro) som_data = np.array(myo_data) #Train a 20x30 SOM with 400 iterations som = SOM(SOM_X, SOM_Y, MYO_DATA_DIM, SOM_ITER) som.train(som_data) #Get output grid image_grid = som.get_centroids() #Plot plt.imshow(image_grid) plt.title('Myo SOM') plt.show()
[1., 1., 1.], [.33, .33, .33], [.5, .5, .5], [.66, .66, .66]]) colors2 = np.array( [[0., 0., 0.], [0., 0., 1.], [1., 1., 0.], [1., 1., 1.], [1., 0., 0.]]) color_names = \ ['black', 'blue', 'darkblue', 'skyblue', 'greyblue', 'lilac', 'green', 'red', 'cyan', 'violet', 'yellow', 'white', 'darkgrey', 'mediumgrey', 'lightgrey'] s = SOM(colors2, [25, 25], alpha=0.3) # Initial weights plt.imshow(s.w_nodes) plt.show() # Learning to cluster the RGB colors s.train(max_it=30) # Trained weights plt.imshow(s.w_nodes) plt.show()
f.close() f = open('appdata.json', 'r') applist = json.load(f) applist = applist['applist']['apps'] f.close() f = open('tagDATA2.json', 'r') tag = json.load(f) f.close() data2 = [] for i in data0: data2.append(data0[i]['gameplay']) data = np.array(data2) # Train a 20x30 SOM with 400 iterations som = SOM(5, 2, 10, 100) # My parameters som.train(data) cnn0 = load_model('model0.h5') cnn1 = load_model('model1.h5') cnn2 = load_model('model2.h5') cnn3 = load_model('model3.h5') cnn4 = load_model('model4.h5') cnn5 = load_model('model5.h5') cnn6 = load_model('model6.h5') cnn7 = load_model('model7.h5') cnn8 = load_model('model8.h5') cnn9 = load_model('model9.h5') def gameRecommendation(id): id = str(id) url = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=DF0E97F40E0BEE667B0BF01B6626A9DA&steamid=' \
#For plotting the images from matplotlib import pyplot as plt import numpy as np from som import SOM colors = np.array( [[0., 0., 1.], [0., 0., 0.95], [0., 0.05, 1.], [0., 1., 0.], [0., 0.95, 0.], [0., 1, 0.05], [1., 0., 0.], [1., 0.05, 0.], [1., 0., 0.05], [1., 1., 0.]]) som = SOM(4, 4, 3) som.train(colors) plt.imshow(som.centroid_grid) plt.show()
### PREPARE THE BACKGROUND ARRAY ### bg = np.loadtxt('eta_mc_som_background_feb2019.txt') # convert to np array np.random.shuffle(bg) bg = bg[:2000,:] bg2 = bg backupbg = bg2 #bg = bg[:10000,:] # cut the np array to preferred size flag_bg = bg[:,8] # array of zeros for background, flags each event as background bg = bg[:,[0,1,2,3,4,5,6,7]] # cuts the background array to only include variables you want to train on # finish preparing the final dataset (one big numpy array) data = np.concatenate((sig,bg),axis=0) # concatenate the sig and bg numpy arrays into one big array flags = np.concatenate((flag_sig, flag_bg), axis=0) # concatenates the flag arrays into one array, each entry corresponding to the entry of the data array ### TRAINING ### #som = SOM(dimx, dimy, 8, 400) # Train a dimx X dimy SOM with 400 iterations, 8 is the number of variables in the data array som = SOM(dimx, dimy, 8) som.train(data) # trains on the dataset prepared ### THIS WILL TAKE AWHILE ### ### TO STORE THE TRAINING RESULTS INTERACTIVELY IN ipython DO: ### weightages = som._weightages ### %store weightages ### THEN TO RECOVER DO: ### %store -r ### som = SOM(dimx, dimy, 8, 400) ### som._weightages = weightages ### som._trained = True print(str(datetime.datetime.now())) # print the time to observe how long the training will take mapped = np.array(som.map_vects(data)) # map each datapoint to its nearest neuron # post training manipulations to the final dataset (one big numpy array) data = np.append(data,mapped,axis=1) # append the mapped neurons to data data = np.column_stack((data,flags)) # append the flags to the dataset ##test U-matrix
weights = [[5, 0], [-1, 4]] sigma = 1 learning_rate = 1 sigma_decrease_rate = 0 som = SOM(inputs, weights, sigma=sigma, learning_rate=learning_rate, sigma_decrease_rate=sigma_decrease_rate, epochs=10) som.train(ignore_hk=True, display=True) points = [[1, 4], [6, 2], [1, 3], [5, 1], [4, 0], [0, 4], [3, 4]] predictions = som.predict(points, ignore_hk=True, display=True) p, m = som.prediction_line(display=True) def med(points, p, m): y = [] for point in points: y.append(m * (point - p[0]) + p[1]) return y plt.grid()
from som import SOM if __name__ == "__main__": a = SOM(5, 5, 3, 2, False, 0.05) a.train(100, [[[0, 0, 0], [0, 0]], [[1, 0, 0], [0, 1]], [[0, 1, 0], [1, 0]], [[1, 1, 1], [1, 1]]]) print("Prediction 0 and 0 ,", round(a.predict([0, 0, 0])[0, 1]))
data = [] # parse data for row in r: l = [] n = row[0] v = map(float, row[1:]) v = map(lambda x: x / max(v), v) l.append(n) for x in v: l.append(x) data.append(l) # create SOM & train s = SOM() results = s.train(data) # restart if only one node (growth threshold not triggered) while len(results) == 1: print 'Only one node, restarting...' s = SOM() results = s.train(data) print """ Parameters summary: Epochs: {epochs} Cluster Threshold: >={threshold} Radius: {radius} Radius Decay Rate: {radius_decay} Rate: {rate} Rate Decay: {rate_decay}
[0.6, 0.5, 1.0], [0., 1., 0.], [1., 0., 0.], [0., 1., 1.], [1., 0., 1.], [1., 1., 0.], [1., 1., 1.], [.33, .33, .33], [.5, .5, .5], [.66, .66, .66]]) #the color's corresponding names, to label the plot color_names = \ ['black', 'blue', 'darkblue', 'skyblue', 'greyblue', 'lilac', 'green', 'red', 'cyan', 'violet', 'yellow', 'white', 'darkgrey', 'mediumgrey', 'lightgrey'] som = SOM(60, 100, 3) start = time.time() scaling_factor = 1e2 som.train(training_samples, 400, 0.8, scaling_factor, 20, scaling_factor) end = time.time() total_time = end - start print("Elapsed time: {}".format(total_time)) pl.imshow(som.som, origin='lower') #label each weight with its corresponding name for i in range(len(training_samples)): bmu = som.get_bmu(training_samples[i]) pl.text(bmu[1], bmu[0], color_names[i], ha='center', va='center', bbox=dict(facecolor='white', alpha=0.5, lw=0))
def display_digit(num): label = y_train[num].argmax(axis=0) image = x_train[num].reshape([28, 28]) plt.title('Example: %d Label: %d' % (num, label)) plt.imshow(image, cmap=plt.get_cmap('gray_r')) plt.show() display_digit(ran.randint(0, x_train.shape[0])) # Import som class and train into 30 * 30 sized of SOM lattice from som import SOM som = SOM(30, 30, x_train.shape[1], 200) som.train(x_train) # Fit train data into SOM lattice mapped = som.map_vects(x_train) mappedarr = np.array(mapped) x1 = mappedarr[:, 0] y1 = mappedarr[:, 1] index = [np.where(r == 1)[0][0] for r in y_train] index = list(map(str, index)) ## Plots: 1) Train 2) Test+Train ### plt.figure(1, figsize=(12, 6)) plt.subplot(121) # Plot 1 for Training only
samples = [[random.gauss(m, 0.1) for e in range(0, 5)] for m in range(0, 20) for sample in range(0,10)] # training for index in range(0, len(samples)): # learn_rate as a factor of how much the weight vectors are # pulled to the target vector. learn_rate = exponentional_damping(\ start = 1.0, end = 0.1,\ t = float(index), tmax = len(samples)) # sigma influences neighbourhood radius sigma = exponentional_damping(\ start = 1.0, end = 0.1,\ t = float(index), tmax = len(samples)) mh = mexican_hat_with_sigma(sigma) som.train(target = samples[index], rate = learn_rate, distance = euclidean, nf = mh) # query for sample in samples: winner = som.query(target = sample, distance = euclidean) print(winner)
projection = { '_id': False, 'title': True, 'runtime': True, 'metacritic': True, 'tomato.rating': True, 'year': True, 'awards.wins': True } brute_data = conn.find_docs(collec='movieDetails', f=filtro, p=projection) print("...Recuperados datos en bruto...") # Limpia y estandariza la data clean_data = cleaner(brute_data) data = [list(map(standarize, lst)) for lst in clean_data] print("...Creando una SOM...") topo = len(data[0]) red = SOM(100, 100, topo) print("...Entrenando la red...") red.train(clean_data, L0=0.8, lam=1e2, sigma0=10) print("...Construyendo gráfico en 2D...") for p in red.som: build_JSON_coor(p, 'son_map') print("...Calculando el error de cálculo...") print(red.quant_err())