Kaggle competition: Don't Overfit! II
<SOS> metric: AUC Kaggle data source: dont-overfit!-ii<feature_engineering>
invlogit = lambda x: 1/(1 + tt.exp(-x))
SEED = [42]
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
oof_, predictions_ = run_k_fold(NFOLDS, seed)
 oof += oof_ / len(SEED)
 predictions += predictions_ / len(SEED)
 train[target_cols] = oof
test[target_cols] = predictions<merge>
df = pd.read_csv('.. /input/train.csv')
 y = np.asarray(df.target)
 X = np.array(df.ix[:, 2:302])
 df2 = pd.read_csv('.. /input/test.csv')
 df2.head()
X2 = np.array(df2.ix[:, 1:301] )
valid_results = train_targets_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("CV log_loss: ", score )<save_to_csv>
def get_model(y, X):
model = pm.Model()
with model:
xi = pm.Bernoulli('xi',.05, shape=X.shape[1])
 alpha = pm.Normal('alpha', mu = 0, sd = 5)
 beta = pm.Normal('beta', mu = 0, sd =.75 , shape=X.shape[1])
 p = pm.math.dot(X, xi * beta)
 y_obs = pm.Bernoulli('y_obs', invlogit(p + alpha), observed=y)
 return model
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )<import_modules>
model1 = get_model(y, X )
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization
import h5py<load_from_csv>
results = pd.DataFrame({'var': np.arange(300),
'inclusion_probability':np.apply_along_axis(np.mean, 0, trace['xi']),
'beta':np.apply_along_axis(np.mean, 0, trace['beta']),
'beta_given_inclusion': np.apply_along_axis(np.sum, 0, trace['xi']*trace['beta'])
 /np.apply_along_axis(np.sum, 0, trace['xi'])
 } )
traindf=pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 testdf=pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' )<choose_model_class>
results.sort_values('inclusion_probability', ascending = False ).head(20 )
num_classes = 120
resnet_weights_path = '.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(512))
my_new_model.add(Activation('relu'))
my_new_model.add(Dropout(0.5))
my_new_model.add(Dense(num_classes, activation='softmax'))
my_new_model.layers[0].trainable = False<choose_model_class>
test_beta = trace['beta'][0]
test_inc = trace['xi'][0]
test_score = expit(trace['alpha'][0] + np.dot(X2, test_inc * test_beta))
test_score
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'] )<init_hyperparams>
estimate = trace['beta'] * trace['xi']
y_hat = np.apply_along_axis(np.mean, 1, expit(trace['alpha'] + np.dot(X2, np.transpose(estimate))))
image_size = 224
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rescale=1./255.,
horizontal_flip=True,
width_shift_range = 0.2,
height_shift_range = 0.2,
validation_split=0.25 )<create_dataframe>
np.mean(y_hat), np.sum(results.inclusion_probability/300 )
train_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="training",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))<create_dataframe>
submission = pd.DataFrame({'id':df2.id, 'target':y_hat})
 submission.to_csv('submission.csv', index = False )