Example #1
0
data_train = np.genfromtxt('data_lab2/votes.dat', delimiter=',')
#with open('data_lab2/animalnames.txt', 'r') as file:
#    names = file.read().split()


x_train = data_train.reshape((349, 31))

num_nodes = 100

means = np.random.uniform(size=(num_nodes, x_train.shape[1]))
vars = np.ones(means.shape[0]) * 1.
weights = np.ones((means.shape[0], 1))

lr = 0.1
first_rbf = learning_functions.GaussianRBF(means, vars, weights, lr)

for i in range(20):
    num_neigh = int(6 - 6/19 * i)
    first_rbf.som_2d_neighbor_algorithm(x_train, num_neigh)

output = first_rbf.winning_index(x_train)

x_cord = np.mod(output, 10).astype(int)
y_cord = (output / 10).astype(int)



district = np.genfromtxt('data_lab2/mpdistrict.dat', delimiter='\n')
district_label = str(district)
party = np.genfromtxt('data_lab2/mpparty.dat', delimiter='\n')[2:]
Example #2
0
y_train_sin = np.sin(2 * x_train)
y_train_sin += np.random.normal(0.0, 0.1, y_train_sin.shape[0])[:, np.newaxis]

x_test = np.arange(0.05, 2 * np.pi, 0.1)[:, np.newaxis]
y_test_sin = np.sin(2 * x_test)
y_test_sin += np.random.normal(0.0, 0.1, y_test_sin.shape[0])[:, np.newaxis]

mean_distance = 0.4
var_value = 0.2

# SOM: winner takes it all
means = np.arange(0, 2 * np.pi, mean_distance)[:, np.newaxis]
vars = np.ones(means.shape[0]) * var_value
weights = np.zeros(means.shape[0])[:, np.newaxis]

first_rbf = learning_functions.GaussianRBF(means, vars, weights)

for i in range(20):
    first_rbf.som_algorithm(x_train)

first_rbf.least_squares(x_train, y_train_sin)
output_som = first_rbf.forward_pass(x_test)

# without SOM
means = np.arange(0, 2 * np.pi, mean_distance)[:, np.newaxis]
vars = np.ones(means.shape[0]) * var_value
weights = np.zeros(means.shape[0])[:, np.newaxis]

second_rbf = learning_functions.GaussianRBF(means, vars, weights)

for i in range(20):