This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Offspring production | |
| def mutate_offspring(offspring): | |
| for q in range(int(n_cities*mutation_rate)): | |
| a = np.random.randint(0,n_cities) | |
| b = np.random.randint(0,n_cities) | |
| offspring[a], offspring[b] = offspring[b], offspring[a] | |
| return offspring | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Pairs crossover | |
| def mate_progenitors(prog_a, prog_b): | |
| offspring = prog_a[0:5] | |
| for city in prog_b: | |
| if not city in offspring: | |
| offspring = np.concatenate((offspring,[city])) | |
| return offspring |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 3. Selecting the progenitors | |
| def progenitor_selection(population_set,fitnes_list): | |
| total_fit = fitnes_list.sum() | |
| prob_list = fitnes_list/total_fit | |
| #Notice there is the chance that a progenitor. mates with oneself | |
| progenitor_list_a = np.random.choice(list(range(len(population_set))), len(population_set),p=prob_list, replace=True) | |
| progenitor_list_b = np.random.choice(list(range(len(population_set))), len(population_set),p=prob_list, replace=True) | |
| progenitor_list_a = population_set[progenitor_list_a] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 2. Evaluation of the fitness | |
| #individual solution | |
| def fitness_eval(city_list, cities_dict): | |
| total = 0 | |
| for i in range(n_cities-1): | |
| a = city_list[i] | |
| b = city_list[i+1] | |
| total += compute_city_distance_names(a,b, cities_dict) | |
| return total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # First step: Create the first population set | |
| def genesis(city_list, n_population): | |
| population_set = [] | |
| for i in range(n_population): | |
| #Randomly generating a new solution | |
| sol_i = city_list[np.random.choice(list(range(n_cities)), n_cities, replace=False)] | |
| population_set.append(sol_i) | |
| return np.array(population_set) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| checkpoint = ModelCheckpoint(weights_path, monitor="auc_roc", verbose=1, save_best_only=True, mode='max', save_weights_only = True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics=['acc',auc_roc]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import rasterio | |
| from rasterio import features | |
| import shapely | |
| from shapely.geometry import Point, Polygon | |
| def mask_to_polygons_layer(mask): | |
| all_polygons = [] | |
| for shape, value in features.shapes(mask.astype(np.int16), mask=(mask >0), transform=rasterio.Affine(1.0, 0, 0, 0, 1.0, 0)): | |
| return shapely.geometry.shape(shape) | |
| all_polygons.append(shapely.geometry.shape(shape)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.linear_model import LogisticRegression as LR | |
| y_train = train_results[class_labels].values | |
| pred_train = train_results[pred_labels].values | |
| pred_calibrated = np.zeros_like(pred) | |
| for i in range(len(class_labels)): | |
| lr = LR(solver='liblinear', max_iter=10000) | |
| lr.fit(pred_train[:, i].reshape(-1, 1), y_train[:, i]) | |
| pred_calibrated[:, i] = lr.predict_proba(pred[:, i].reshape(-1, 1))[:,1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.calibration import calibration_curve | |
| def plot_calibration_curve(y, pred): | |
| plt.figure(figsize=(20, 20)) | |
| for i in range(len(class_labels)): | |
| plt.subplot(4, 4, i + 1) | |
| fraction_of_positives, mean_predicted_value = calibration_curve(y[:,i], pred[:,i], n_bins=20) | |
| plt.plot([0, 1], [0, 1], linestyle='--') | |
| plt.plot(mean_predicted_value, fraction_of_positives, marker='.') | |
| plt.xlabel("Predicted Value") | |
| plt.ylabel("Fraction of Positives") |