def SOM_clustering(): splits_count = 4 selectedCab_index = 1 selectedCab = [] for q in open('sampled.txt', 'r').read().split(): selectedCab.append(int(q)) for split in range(1,splits_count+1): arr = np.array([]) print("Split number : %d" % (split)) for cabID in selectedCab: arrcol = np.array([]) #print(cabID) filename = "..//Split//" + str(cabID) + "_" + str(split) + "_.traj" with open(filename) as f: for line in f: line = line.rstrip() arrcol = np.concatenate((arrcol,np.array([int(line)])),axis = 0) #print(arrcol) if len(arr) == 0: arr = np.concatenate((arr,arrcol),axis = 0) else: arr = np.vstack((arr,arrcol)) testX = arr print(testX) print ("Cluster Start") length = len(arr[0]) print("Dim : %d" %(length)) #Train a 20x30 SOM with 400 iterations som = SOM(20, 30, length, 1) som.train(testX) print ("get_centroids") #Get output grid image_grid = som.get_centroids() #Map colours to their closest neurons mapped = som.map_vects(testX) print ("Plot") array = np.zeros((20,30)) filename_ClusterLabels = "Cabs_ClusterLabels" + str(split) +".csv" target = open(filename_ClusterLabels,'w') for i, m in enumerate(mapped): index = ((m[0]) * 2) + (m[1]+1) target.write("%s\n" % (index)) #print(index) array[m[0], m[1]] += 1 target.close() array = array.astype(int) array = np.reshape(array,600) print(array) #break; return;
formatter.generate_validation_file() # Train SOM, if there is no weights. if Path("weights.txt").is_file() is False: # Use pandas for loading data using dataframes. d = os.path.dirname(os.getcwd()) file_name = "huge_merged_csv_file.csv" data = pd.read_csv(file_name, header=None) # Shuffle the data in place. data = data.sample(frac=1).reset_index(drop=True) # create SOM object som = SOM(7, 1) # Train with 100 neurons. som.train(data) # Get output grid grid = som.get_centroids() # Save the weights in a file. result_file = open("weights.txt", "w") result_file.writelines(str(grid)) result_file.close() # Map data to neurons. mapped_vectors = mapper.map_vects() tester.open_test_file() tester.test_som(mapped_vectors)
def SOM_clustering(): arr = np.array([]) with open("AllTrajecsNopePoints.data") as f: for line in f: strings = line.split(" ") arrcol = np.array([]) for str in strings: arrcol = np.concatenate((arrcol, np.array([float(str)])), axis=0) if len(arr) == 0: arr = np.concatenate((arr, arrcol), axis=0) else: arr = np.vstack((arr, arrcol)) #print (arr) testX = arr print("Cluster Start") #Train a 20x30 SOM with 400 iterations som = SOM(20, 30, 720, 400) som.train(testX) print("get_centroids") #Get output grid image_grid = som.get_centroids() #Write to file target = open("ClusteredCentroids.csv", 'w') count = 0 for i, m in enumerate(image_grid): for column in m: for k, dim in enumerate(column): if (k != 0): target.write(" ") target.write("%s" % (dim)) target.write("\n") # target.write("%s " % (n)) # target.write("\n") target.close() #for i, m in enumerate(image_grid): # print("i m[1] m[0] =", i ,m[1], m[0]) #Map colours to their closest neurons mapped = som.map_vects(testX) print("Plot") array = np.zeros((20, 30)) #Plot #plt.imshow(image_grid) #plt.title('Color SOM') #plt.plot(20, 30, 'b.') target = open("ClusteredCabs_labels.csv", 'w') for i, m in enumerate(mapped): index = ((m[0]) * 30) + (m[1] + 1) target.write("%s\n" % (index)) print(index) array[m[0], m[1]] += 1 target.close() #print("i m[1] m[0] =", i ,m[1], m[0]) #plt.plot(m[1], m[0], 'ro') #plt.text(m[1], m[0], index, ha='center', va='center', # bbox=dict(facecolor='white', alpha=0.5, lw=0)) #plt.show() array = array.astype(int) array = np.reshape(array, 600) print(array) #print (testX) ###Write to file ##target = open("ClusteredCabs.clu",'w') ##for i, m in enumerate(array): ## #if m != 0 : ## target.write("%s %s" % (i, m)) ## target.write("\n") ##target.close() return
for row in reader: audio_features.append(list(row[i] for i in range(7, 17))) track_names.append(list(row[i] for i in range(6, 7))) i += 1 if i == num_tracks: break # Training inputs for tracks audio_features = np.asarray(audio_features, dtype=float) # Train a 20x30 SOM with 10 iterations som = SOM(20, 30, 10, 50) som.train(audio_features) # Get output grid image_grid = som.get_centroids() # Map tracks to their closest neurons mapped = som.map_vects(audio_features) # Plot plt.imshow(image_grid[10]) plt.title('Track SOM') plt.ylim([0, 20]) plt.xlim([0, 30]) for i, m in enumerate(mapped): plt.text(m[1], m[0], track_names[i], ha='center', va='center',
[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]]) colorNames = [ 'black', 'blue', 'darkblue', 'skyblue', 'greyblue', 'lilac', 'green', 'red', 'cyan', 'violet', 'yellow', 'white', 'darkgrey', 'mediumgrey', 'lightgrey' ] #Train a 20x30 SOM with 400 iterations som = SOM(m=20, n=30, dim=3, epochs=400) som.train(colors) #Get output grid imageGrid = som.get_centroids() #Map colours to their closest neurons mapped = som.map_vects(colors) #Plot plt.imshow(imageGrid) plt.title('Color SOM') for i, m in enumerate(mapped): plt.text(m[1], m[0], colorNames[i], ha='center', va='center', bbox=dict(facecolor='white', alpha=0.5, lw=0)) plt.show()