def execute(self): # Pre processing x_data = util.normalizeData(self.x_data) x_data = util.insertBias(x_data) y_data = self.y_data for i in range(self.realizations): self.initWeigths() x_data_aux, y_data_aux = util.shuffleData(x_data, y_data) x_train, x_test, y_train, y_test = util.splitData( x_data_aux, y_data_aux, self.train_size) self.train(x_train, y_train) acc, tpr, spc, ppv = self.test(x_test, y_test) self.hit_rate.append(acc) self.tpr.append(tpr) self.spc.append(spc) self.ppv.append(ppv) #util.plotColorMap(x_train, x_test, y_train, self.predict) self.acc = np.mean(self.hit_rate) self.std = np.std(self.hit_rate) self.tpr = np.mean(self.tpr) self.spc = np.mean(self.spc) self.ppv = np.mean(self.ppv) print('Hit rate: {}'.format(self.hit_rate)) print('Accuracy: {:.2f}'.format(self.acc * 100)) print('Minimum: {:.2f}'.format(np.amin(self.hit_rate) * 100)) print('Maximum: {:.2f}'.format(np.amax(self.hit_rate) * 100)) print('Standard Deviation: {:.2f}'.format(self.std)) print('Sensitivity: {:.2f}'.format(self.tpr * 100)) print('Specificity: {:.2f}'.format(self.spc * 100)) print('Precision: {:.2f}'.format(self.ppv * 100))
def cancer(): dataset = pd.read_csv('Datasets/wdbc.data', sep=',') classe = dataset['Classe'] x = dataset.drop(['Classe'], axis=1) y = util.transform(classe) return np.array(x), y
def train(self, x_train, y_train): error_old = 0 cont_epochs = 0 mse_vector = [] while True: self.updateEta(cont_epochs) x_train, y_train = util.shuffleData(x_train, y_train) (m, _) = x_train.shape error_epoch = 0 for i in range(m): xi = x_train[i] y = self.predict(xi) d = y_train[i] error = d - y error_epoch += error**2 # update weights self.w += self.eta * (error * xi) mse = error_epoch / m mse_vector.append(mse) if abs(error_epoch - error_old) <= self.precision: #print('Stop Precision: {}'.format(abs(error_epoch - error_old))) break if cont_epochs >= self.epochs: #print('Stop Epochs: {}'.format(cont_epochs)) break error_old = error_epoch cont_epochs += 1
def train(self, x_train, y_train): stop_error = 1 cont_epochs = 0 vector_error = [] while (stop_error and cont_epochs < self.epochs): self.updateEta(cont_epochs) stop_error = 0 x_train, y_train = util.shuffleData(x_train, y_train) (m, _) = x_train.shape aux = 0 for i in range(m): xi = x_train[i] y = self.predict(xi) d = y_train[i] error = d - y aux += abs(int(error)) # check if this error if not np.array_equal(error, [0]): stop_error = 1 # update weights self.w += self.eta * (error * xi) vector_error.append(aux) cont_epochs += 1
def dermatology(): dataset = pd.read_csv('Datasets/dermatology.dat', sep=',') classe = dataset['Classe'] x = dataset.drop(['Classe'], axis=1) y = util.transform(classe) return np.array(x), y
def test(self, x_test, y_test, params, n_centers, width): y_true = [] y_pred = [] (p, _) = x_test.shape for i in range(p): d = y_test[i] y = self.predict(x_test[i], params, n_centers, width) # Confusion Matrix y_true.append(list(d)) y_pred.append(list(y)) a = util.inverse_transform(y_true, self.n_classes) b = util.inverse_transform(y_pred, self.n_classes) return acc(a, b), tpr(a, b, average='macro'), 0, ppv(a, b, average='weighted')
def irisFlower(): dataset = pd.read_csv('Datasets/iris.data', sep=',') classe = dataset['Classe'] x = dataset.drop(['Classe'], axis=1) y = util.transform(classe) # Remove column #x = x.drop(['Petal_Length'], axis=1) #x = x.drop(['Petal_Width'], axis=1) return np.array(x), y
def train(self, x_train, y_train, hidden_layer): error_old = 0 cont_epochs = 0 mse_vector = [] params = self.initWeigths(hidden_layer) w = params['w'] m = params['m'] while True: self.updateEta(cont_epochs) x_train, y_train = util.shuffleData(x_train, y_train) (p, _) = x_train.shape error_epoch = 0 for k in range(p): x_k = x_train[k] H = np.dot(x_k, w) H = self.function(H) H_ = self.derivate(H) H = np.concatenate(([-1], H), axis=None) Y = np.dot(H, m) Y = self.function(Y) Y_ = self.derivate(Y) # Quadratic Error Calculation d = y_train[k] error = d - Y error_epoch += 0.5 * np.sum(error**2) # Output layer delta_output = (error * Y_).reshape(-1, 1) aux_output = (self.eta * delta_output) m += np.dot(H.reshape(-1, 1), aux_output.T) # Hidden layer delta_hidden = np.sum(np.dot(m, delta_output)) * H_ aux_hidden = (self.eta * delta_hidden).reshape(-1, 1) w += np.dot(x_k.reshape(-1, 1), aux_hidden.T) mse = error_epoch / p mse_vector.append(mse) if abs(error_epoch - error_old) <= self.precision: #print('Stop Precision: {} (Epochs {})'.format(abs(error_epoch - error_old), cont_epochs)) break if cont_epochs >= self.epochs: #print('Stop Epochs: {}'.format(cont_epochs)) break error_old = error_epoch cont_epochs += 1 #util.plotErrors(mse_vector) params['w'] = w params['m'] = m return params
def test(self, x_test, y_test): (m, _) = x_test.shape y_actu = [] y_pred = [] for i in range(m): xi = x_test[i] y, _ = self.predict(xi) d = y_test[i] # Confusion Matrix y_actu.append(list(d)) y_pred.append(list(y)) a = util.inverse_transform(y_actu) b = util.inverse_transform(y_pred) cm = ConfusionMatrix(a, b) #cm.print_stats() #util.plotConfusionMatrix(cm) return cm.ACC, cm.TPR, cm.SPC, cm.PPV
def execute(self): x_data = util.insertBias(self.x_data) y_data = self.y_data for i in range(self.realizations): x_data_aux, y_data_aux = util.shuffleData(x_data, y_data) x_train, x_test, y_train, y_test = util.splitData(x_data_aux, y_data_aux, self.train_size) best_hidden_layer = self.hidden_layer params = self.train(x_train, y_train, best_hidden_layer) mse, rmse = self.test(x_test, y_test, params) self.mse.append(mse) self.rmse.append(rmse) print('{} Realizations'.format(self.realizations)) print('MSE: {}'.format(np.mean(self.mse))) print('Std MSE: {}'.format(np.std(self.mse))) print('RMSE: {}'.format(np.mean(self.rmse))) print('Std RMSE: {}'.format(np.std(self.rmse)))
def execute(self): x_data = util.normalizeData(self.x_data) x_data = util.insertBias(x_data) y_data = self.y_data for i in range(self.realizations): x_data_aux, y_data_aux = util.shuffleData(x_data, y_data) x_train, x_test, y_train, y_test = util.splitData( x_data_aux, y_data_aux, self.train_size) if self.g_search: best_n_centers, best_width = self.grid_search(x_train, y_train) print('Best N Centers: ', best_n_centers) print('Best Width: ', best_width) else: best_n_centers = self.n_centers best_width = self.width params = self.train(x_train, y_train, best_n_centers, best_width) acc, tpr, spc, ppv = self.test(x_test, y_test, params, best_n_centers, best_width) self.hit_rate.append(acc) self.tpr.append(tpr) self.spc.append(spc) self.ppv.append(ppv) self.acc = np.mean(self.hit_rate) self.std = np.std(self.hit_rate) self.tpr = np.mean(self.tpr) self.spc = np.mean(self.spc) self.ppv = np.mean(self.ppv) print('Hit rate: {}'.format(self.hit_rate)) print('Accuracy: {:.2f}'.format(self.acc * 100)) print('Minimum: {:.2f}'.format(np.amin(self.hit_rate) * 100)) print('Maximum: {:.2f}'.format(np.amax(self.hit_rate) * 100)) print('Standard Deviation: {:.2f}'.format(self.std)) print('Sensitivity: {:.2f}'.format(self.tpr * 100)) print('Specificity: {:.2f}'.format(self.spc * 100)) print('Precision: {:.2f}'.format(self.ppv * 100))
def abalone(): dataset = pd.read_csv('Datasets/abalone.csv', sep=',') Y = dataset['Rings'] Y = Y.real.reshape(1, -1).T X = dataset.drop(['Rings'], axis=1) sex = dataset['Sex'] X = X.drop(['Sex'], axis=1) sex = util.transform_number(sex) X = np.concatenate((sex, X), axis=1) return np.array(X), Y
def adaline(self): # Pre processing x_data = util.normalizeData(self.x_data) x_data = util.insertBias(x_data) y_data = self.y_data for i in range(self.realizations): self.initWeigths() x_data_aux, y_data_aux = util.shuffleData(x_data, y_data) x_train, x_test, y_train, y_test = util.splitData( x_data_aux, y_data_aux, self.train_size) self.train(x_train, y_train) mse, rmse = self.test(x_test, y_test) self.mse.append(mse) self.rmse.append(rmse) print('{} Realizations'.format(self.realizations)) print('MSE: {}'.format(np.mean(self.mse))) print('Std MSE: {}'.format(np.std(self.mse))) print('RMSE: {}'.format(np.mean(self.rmse))) print('Std RMSE: {}'.format(np.std(self.rmse)))
def vertebralColumn(): dataset = pd.read_csv('Datasets/column_3C.dat', sep=' ') classe = dataset['Classe'] x = dataset.drop(['Classe'], axis=1) y = util.transform(classe) # Remove column #x = x.drop(['lumbar_lordosis_angle'], axis=1) #x = x.drop(['sacral_slope'], axis=1) #x = x.drop(['pelvic_radius'], axis=1) #x = x.drop(['grade_of_spondylolisthesis'], axis=1) return np.array(x), y
def train(self, x_train, y_train, n_centers, width): params = self.initWeigths(n_centers) c = params['c'] x_train, y_train = util.shuffleData(x_train, y_train) (p, _) = x_train.shape h = np.zeros((p, n_centers)) for i in range(p): for j in range(n_centers): h[i, j] = self.saidas_centro(x_train[i], c[j], width) bias = -1 * np.ones((p, 1)) h = np.concatenate((bias, h), axis=1) w = np.dot(np.linalg.pinv(h), y_train) params['w'] = w return params
def train(self, x_train, y_train): stop_error = 1 cont_epochs = 0 vector_error = [] while (stop_error and cont_epochs < self.epochs): self.updateEta(cont_epochs) stop_error = 0 x_train, y_train = util.shuffleData(x_train, y_train) (m, _) = x_train.shape for i in range(m): xi = x_train[i] y, y_ = self.predict(xi) d = y_train[i] error = d - y # check if this error if not np.array_equal(error, [0, 0, 0]): stop_error = 1 # update weights aux = (y_ * error).reshape(-1, 1) self.w += self.eta * (aux * xi) cont_epochs += 1
def artificialCircles(): X, y = make_circles(n_samples=300, factor=.5, noise=.05) return X, util.transform(y)
def artificialMoons(): X, y = make_moons(n_samples=300, noise=0.05,random_state=10) return X, util.transform(y)