def ga_run_std(x_train, y_train, x_test, y_test, x_verif, y_verif, k): # Run GA to find best weights. N_init_pop = 50 N_crossover = 50 N_selection = 20 improv_thresh = 1e-3 _, nFeats = np.shape(x_train) weight_ga = GeneticAlgorithm(nFeats, N_init_pop, mu=0.1) weight_pop = weight_ga.get_population() metric_array = np.empty(N_init_pop) # Create the initial population. for i in range(len(weight_pop)): # Scale input data scaled_x_train = np.multiply(x_train, weight_pop[i]) # Scale verificaion data scaled_x_verif = np.multiply(x_verif, weight_pop[i]) # Classifier. clf = KNNClassifier(scaled_x_train, y_train, k) neighbors = clf.find_all_neighbors(scaled_x_verif) nbh_ent = clf.find_neighborhood_std(neighbors) metric_array[i] = nbh_ent # Update fitness in GA object. weight_ga.set_fitness(metric_array) weight_ga.selection(N_selection) new_best_metric = 2.5 # while (best_metric - new_best_metric) > improv_thresh: count = 0 while (count < 20): count += 1 best_metric = new_best_metric # Crossover. weight_ga.crossover(N_crossover) # Get new population. weight_pop = weight_ga.get_population() metric_array = np.empty(N_crossover) # Evaluate and set fitness. for i in range(len(weight_pop)): # Scale input data scaled_x_train = np.multiply(x_train, weight_pop[i]) # Scale verificaion data scaled_x_verif = np.multiply(x_verif, weight_pop[i]) # Classifier. clf = KNNClassifier(scaled_x_train, y_train, k) neighbors = clf.find_all_neighbors(scaled_x_verif) nbh_ent = clf.find_neighborhood_std(neighbors) metric_array[i] = nbh_ent # Update fitness in GA object weight_ga.set_fitness(metric_array) # get_best_sol best_weights, new_best_metric = weight_ga.best_sol() #print("Metric of this iteration are: ", new_best_metric) weight_ga.selection(N_selection) # print("Best weights = ", best_weights, "\tBest metric = ", new_best_metric) # Test with scaling after GA # Concatenate training and verification sets. x_train = np.concatenate((x_train, x_verif), axis=0) y_train = np.concatenate([y_train, y_verif]) # Print the results of KNN. clf = KNNClassifier(np.multiply(x_train, best_weights), y_train, k) y_pred = clf.predict(np.multiply(x_test, best_weights)) acc = accuracy(y_test, y_pred) print("ga-std,knn,", k, ",", acc) # Print the results of KNN. clf = DwKNNClassifier(np.multiply(x_train, best_weights), y_train, k) y_pred = clf.predict(np.multiply(x_test, best_weights)) mse_iter = accuracy(y_test, y_pred) print("ga-std,dknn,", k, ",", mse_iter)
for i in range(0, 10): x_train, x_test, y_train, y_test = train_test_split(X, y,\ test_size=0.2) """ Try non-optimized methods. """ # Vanilla KNN. for k in K_VALS: reg = KNNClassifier(x_train, y_train, k) y_pred = reg.predict(x_test) mse_iter = accuracy(y_test, y_pred) print("xx,knn,", k, ",", mse_iter) # Distance-weighted KNN. for k in K_VALS: reg = DwKNNClassifier(x_train, y_train, k) y_pred = reg.predict(x_test) mse_iter = accuracy(y_test, y_pred) print("xx,dknn,", k, ",", mse_iter) """ PCA with KNN. """ pca = PCA(n_components=4) pca.fit(x_train.copy()) x_train_pca = pca.transform(x_train.copy()) x_test_pca = pca.transform(x_test.copy()) # PCA + Vanilla KNN. for k in K_VALS: reg = KNNClassifier(x_train_pca, y_train, k) y_pred = reg.predict(x_test_pca)
def lbest_pso_run_ent(x_train, y_train, x_test, y_test, x_verif, y_verif, k): #Run PSO to find best weights N_init_pop = 50 _, nFeats = np.shape(x_train) weight_pso = LBestPSO(nFeats, N_init_pop) pos = weight_pso.get_positions() pbest = weight_pso.get_pbest() pbest_metric_array = np.empty(N_init_pop) pos_metric_array = np.empty(N_init_pop) #Set pbest metrics for i in range(len(pbest)): #Scale input data scaled_x_train = np.multiply(x_train, pbest[i]) #Scale verificaion data scaled_x_verif = np.multiply(x_verif, pbest[i]) #Method 1 clf = KNNClassifier(scaled_x_train, y_train, k) neighbors = clf.find_all_neighbors(scaled_x_verif) nbh_ent = clf.find_neighborhood_entropy(neighbors) pbest_metric_array[i] = nbh_ent weight_pso.set_pbest_fitness(pbest_metric_array) #Set pos metrics for i in range(len(pbest)): #Scale input data scaled_x_train = np.multiply(x_train, pos[i]) #Scale verificaion data scaled_x_verif = np.multiply(x_verif, pos[i]) #Method 1 clf = KNNClassifier(scaled_x_train, y_train, k) neighbors = clf.find_all_neighbors(scaled_x_verif) nbh_ent = clf.find_neighborhood_entropy(neighbors) pos_metric_array[i] = nbh_ent weight_pso.set_p_fitness(pos_metric_array) #Set initial gbest. weight_pso.set_init_best(pos_metric_array) count = 0 while (count < 50): count += 1 weight_pso.optimize() #get_population weight_pop = weight_pso.get_positions() metric_array = np.empty(N_init_pop) #evaluate and set fitness for i in range(len(weight_pop)): #Scale input data scaled_x_train = np.multiply(x_train, weight_pop[i]) #Scale verificaion data scaled_x_verif = np.multiply(x_verif, weight_pop[i]) #Method 1 clf = KNNClassifier(scaled_x_train, y_train, k) neighbors = clf.find_all_neighbors(scaled_x_verif) nbh_ent = clf.find_neighborhood_entropy(neighbors) metric_array[i] = nbh_ent weight_pso.set_p_fitness(metric_array) weight_pso.set_best(metric_array) #get_best_sol best_metric = weight_pso.get_gbest_fit() best_weights = weight_pso.get_gbest() # Concatenate training and verification sets. x_train = np.concatenate((x_train, x_verif), axis = 0) y_train = np.concatenate([y_train, y_verif]) # Print the results of KNN. clf = KNNClassifier(np.multiply(x_train, best_weights), y_train, k) y_pred = clf.predict(np.multiply(x_test, best_weights)) mse_iter = accuracy(y_test, y_pred) print("lbest-pso-ent,knn,", k, ",", mse_iter) # Print the results of KNN. clf = DwKNNClassifier(np.multiply(x_train, best_weights), y_train, k) y_pred = clf.predict(np.multiply(x_test, best_weights)) mse_iter = accuracy(y_test, y_pred) print("lbest-pso-ent,dknn,", k, ",", mse_iter)