with open('rabbit_test.csv', newline='') as f: r = csv.reader(f) for row in r: if row: test.append(row) c = list(zip(input, target)) shuffle(c) input = [x[0] for x in c] target = [x[1] for x in c] input = np.array(input, np.float64) target = np.array(target, np.float64) test = np.transpose(np.array(test, np.float64)) print(len(input)) net = RBF(len(input), args.n, args.sigma) net.train(input, target, args.reg) x_list = [x[0] for x in oinput] y_list = [net.predict(x) for x in oinput] yo_list = [x for x in otarget] f = lambda x: 233.846 * (1 - np.exp(-0.00604 * x)) yf_list = [f(x) for x in x_list] error = sum([(y_list[i] - yo_list[i])**2 for i in range(len(y_list))]) / len(y_list) print("Error: ", error) plt.plot(x_list, y_list, label='Predicción', c='b') plt.plot(x_list, yo_list, label='Datos', c='r') plt.plot(x_list, yf_list, label='Modelo proporcionado', c='y') plt.scatter(test[0], test[1], label='Conjunto de prueba', c='g', marker='.') plt.xlabel("Edad") plt.ylabel("Peso") plt.legend(loc='upper left')
import numpy as np import matplotlib.pyplot as plt from rbf import RBF # Gerando 100 dados aleatórios X = np.random.uniform(0., 1., 100) X = np.sort(X, axis=0) # Gerando a saída baseado nos dados aleatórios # Seno de (2 * PI * X) + noise noise = np.random.uniform(-0.1, 0.1, 100) Y = np.sin(2 * np.pi * X) + noise # Criando e treinando a rede learning_rate = 0.01 # Taxa de aprendizado epochs = 100 # Epocas k = 2 # Número de neurônios rbf = RBF(k=k, learning_rate=learning_rate, epochs=epochs) rbf.train(X, Y) # Previsão predictions = rbf.predict(X) # Ploantdo dados de treinamento vs previsão plt.plot(X, Y, '-o', label='train') plt.plot(X, predictions, '-o', label='predict') plt.legend() plt.tight_layout() plt.savefig('train-and-predict.png')
hit_rates = [] no_of_attributes = dataset.shape[1] - 1 no_of_classes = len(dataset[0, no_of_attributes]) # insert bias no_rows = dataset.shape[0] dataset = np.c_[-1 * np.ones(no_rows), dataset] # perceptron = Perceptron(no_of_classes, no_of_attributes, 5, 'logistic') for j in range(0, 20): print("realization %d" % j) train_X, train_y, test_X, test_y = Classifier.train_test_split(dataset) train_X = np.array(train_X, dtype=float) test_X = np.array(test_X, dtype=float) sigma, center = RBF.model_training(no_of_classes, no_of_attributes, train_X, train_y) rbf = RBF(no_of_classes, no_of_attributes, center, sigma) rbf.train(train_X, train_y) predictions = rbf.predict(test_X) hit_rates.append(rbf.evaluate(test_y, predictions)) print(rbf.confusion_matrix(test_y, predictions)) # rbf.plot_decision_boundaries(train_X, train_y, test_X, test_y, rbf, j) print('hit rates: {}'.format(hit_rates)) print('accuracy: {}'.format(np.mean(hit_rates))) print('std: {}'.format(np.std(hit_rates))) # RBF.show_plot_decision_boundaries()
ys = h(xs) noise = np.random.normal(0, 1, ys.shape) ys_noise = ys + noise input = [[x] for x in xs] shuffle(input) input = np.array(input, np.float64) ns = [10, 20, 25, 50, 60, 70, 80] sigmas = [0.1, 0.25, 0.5, 0.75, 1, 100, 200, 300, 400, 500] regs = [0, 1, 0.5, 0.1, 0.01, 0.001] for n in ns: for sigma in sigmas: for reg in regs: net = RBF(len(input), n, sigma) net.train(input, ys_noise, reg) approx = [net.predict(x) for x in xs] plt.plot(xs, ys, label='Función', c='b') plt.plot(xs, ys_noise, label='Función con ruido', c='g') plt.plot(xs, approx, label='Aproximación', c='r') plt.legend(loc='upper left') error = sum([(ys_noise[i] - approx[i])**2 for i in range(len(ys_noise))]) / len(ys_noise) print("Centros: {} | Sigma: {} | Lambda: {} | Error: {}".format( n, sigma, reg, error)) plt.savefig("graphs/centers_" + str(n) + "_sigma_" + str(sigma) + "_lambda_" + str(reg) + ".png", bbox_inches='tight') plt.clf()