Skip to content

Instantly share code, notes, and snippets.

@rocreguant
rocreguant / tf_auc.py
Last active January 12, 2021 20:52
This gist is about how to create a AUC metric for tensorflow/Keras
def auc_roc(y_true, y_pred):
    # can be any tensorflow metric
    value, update_op = tf.contrib.metrics.streaming_auc(y_pred, y_true)
    # find all variables created for this metric
metric_vars = [i for i in tf.local_variables() if 'auc_roc' in i.name.split('/')[1]]
    # Add metric variables to GLOBAL_VARIABLES collection.
# They will be initialized for new session.
for v in metric_vars:
    tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, v)
@rocreguant
rocreguant / Calibration-curve-plot-in-python.py
Last active June 14, 2020 17:22
Calibration curve plot in python using sklearn
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")
@rocreguant
rocreguant / platt-model-calibration.py
Created June 14, 2020 17:24
Deep learning model calibration using Platt scaling approach via sklearn
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]
@rocreguant
rocreguant / threshold_to_polygon.py
Created August 7, 2020 15:21
Function to transform cv2 thesholdiag (black and white / binary image) to a group of polygons
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))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics=['acc',auc_roc])
checkpoint = ModelCheckpoint(weights_path, monitor="auc_roc", verbose=1, save_best_only=True, mode='max', save_weights_only = True)
# 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)
# 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
# 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]
# 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