<jupyter_start><jupyter_text>Get targets<jupyter_code>df = pd.read_csv("data/2018_02_23-all_objects.csv")
df = df[df.selected]
df.head()



targets = df.drop_duplicates("HSC_id") \
            .set_index("HSC_id")
    


targets = (targets.log_mass > 8) & (targets.log_mass < 9) & (targets.photo_z < .15)
print(targets.mean())
print(targets.sum())<jupyter_output><empty_output><jupyter_text>Split training and validation sets<jupyter_code>batch_size = 64
np.random.seed(seed=0)

randomized_indices = np.arange(X.shape[0])
np.random.shuffle(randomized_indices)

testing_fraction = 0.2
# make sure testing set size is an even multiple of 64
num_testing = (int(testing_fraction*X.shape[0]) // batch_size) * batch_size

testing_set_indices = randomized_indices[:int(num_testing)]
training_set_indices = np.array(list(set([*randomized_indices]) - set([*testing_set_indices])))

testing_set_indices.size
training_set_indices.size<jupyter_output><empty_output><jupyter_text>Setup standard augmentation<jupyter_code>from keras.preprocessing.image import ImageDataGenerator
print('Using real-time _simple_ data augmentation.')

h_before, w_before = X[0,0].shape
print("image shape before: ({},{})".format(h_before, w_before))

# this will do preprocessing and realtime data augmentation
datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    horizontal_flip=True, # randomly apply a reflection (in x)
    vertical_flip=True, # randomly apply a reflection (in y)
    rotation_range=0, # randomly apply a rotation of angle randomly between 0 and `rotation_range`
    zoom_range=0.0,
    shear_range=0.0,
    channel_shift_range=0.0,
    rescale=0,
    width_shift_range=0.002,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.002,  # randomly shift images vertically (fraction of total height)
)
datagen.fit(X[training_set_indices])<jupyter_output><empty_output><jupyter_text>Setup Classifier<jupyter_code>from classifier import Classifier

input_shape = X.shape[1:]

classifier_model = Classifier(input_shape)
classifier_model.configure_optimizer(lr=0.001)
classifier_model.build_model()
classifier_model.configure_early_stopping()

Y = targets[HSC_ids].values
data_iterator = datagen.flow(X[training_set_indices],
                             Y[training_set_indices],
                             batch_size=classifier_model.batch_size,
                            )<jupyter_output><empty_output><jupyter_text>Run Basic Classifier<jupyter_code>history = classifier_model.fit_model(X, Y, 
                                     training_set_indices,
                                     testing_set_indices,
                                     data_iterator,
                                    )<jupyter_output><empty_output><jupyter_text>Check Classifier Performance<jupyter_code>from sklearn.metrics import log_loss
p = Y[training_set_indices].mean()
prior_loss = log_loss(Y[testing_set_indices], 
                      [p]*testing_set_indices.size)
print("performance (prior): {:.3f}".format(prior_loss))
print("performance (best):  {:.3f}".format(min(history.history["val_loss"])))
from matplotlib.ticker import MaxNLocator

with mpl.rc_context(rc={"figure.figsize": (10,6)}):

    plt.plot(history.history["val_loss"], label="Validation")
    plt.plot(history.history["loss"], label="Training")
    
    plt.axhline(prior_loss, label="Prior", 
                linestyle="dashed", color="black")

    plt.legend(loc="best")
    
    plt.xlabel("Epoch")
    plt.ylabel("Loss\n(mean binary cross-entropy)")
    
    plt.ylim(.4, .7)
    
    # Force only integer labels, not fractional labels
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
class_probs = classifier_model.model \
                              .predict_proba(X[testing_set_indices]) \
                              .flatten()
class_probs
with mpl.rc_context(rc={"figure.figsize": (10,6)}):
    sns.distplot(class_probs[Y[testing_set_indices]==True], color="g", label="true dwarfs")
    sns.distplot(class_probs[Y[testing_set_indices]==False], color="b", label="true non-dwarfs")

    plt.xlabel("p(dwarf | image)")
    plt.ylabel("density (galaxies)")

    plt.xlim(0, .7)
    plt.axvline(Y[training_set_indices].mean(), linestyle="dashed", color="black", label="prior\n(from training set)")
    plt.axvline(.5, linestyle="dotted", color="black", label="50/50")

    plt.legend(
        loc="upper left",
        bbox_to_anchor=(1, 1),
    )

from sklearn import metrics
from sklearn.metrics import roc_auc_score

with mpl.rc_context(rc={"figure.figsize": (10,6)}):
    fpr, tpr, _ = metrics.roc_curve(Y[testing_set_indices], class_probs)
    roc_auc = roc_auc_score(Y[testing_set_indices], class_probs)

    plt.plot(fpr, tpr, label="DNN (AUC = {:.2})".format(roc_auc))
    plt.plot([0,1], [0,1], linestyle="dashed", color="black", label="random guessing")

    plt.xlim(0,1)
    plt.ylim(0,1)

    plt.xlabel("False Positive Rate")
    plt.ylabel("True Positive Rate")

    plt.title("ROC Curve")

    plt.legend(loc="best")

from sklearn import metrics
from sklearn.metrics import average_precision_score
with mpl.rc_context(rc={"figure.figsize": (10,6)}):
    precision, recall, _ = metrics.precision_recall_curve(Y[testing_set_indices], class_probs)
    pr_auc = average_precision_score(Y[testing_set_indices], class_probs)

    plt.plot(recall, precision, label="AUC = {:.2}".format(pr_auc))
    plt.plot([0,1], [Y[testing_set_indices].mean()]*2, linestyle="dashed", color="black")

    plt.xlim(0,1)
    plt.ylim(0,1)

    plt.xlabel("Recall")
    plt.ylabel("Precision")

    plt.title("PR Curve")

    plt.legend(loc="best")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1289891395154553, 'fraction_numerical': 0.014870509607351713, 'mean_word_length': 4.147033533963887, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '198733', 'license': 'Apache-2.0', 'n_tokens_mistral': 2110, 'n_tokens_neox': 1984, 'n_words': 345, 'path': 'simple classifier.ipynb', 'repo_name': 'egentry/galaxyCGAN', 'size': '5985'}