Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<train_model>
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )
model_3.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )<save_to_csv>
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats])
 all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean())
 X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice
y_pred_3 = model_3.predict(X_test)
 submission_3 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_3.reshape(-1)})
 submission_3.to_csv('submission_3.csv', index=False )<save_to_csv>
model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y )
y_pred_avg =(y_pred_0 + y_pred_1 + y_pred_2 + y_pred_3)/ 4
submission_avg = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_avg.reshape(-1)})
 submission_avg.to_csv('submission_avg.csv', index=False )<import_modules>
rmse_cv(model_lasso ).mean()
print(check_output(["ls", ".. /input"] ).decode("utf8"))
BATCH_SIZE = 16
EPOCHS = 100
RANDOM_STATE = 11
CLASS = {
'Black-grass': 0,
'Charlock': 1,
'Cleavers': 2,
'Common Chickweed': 3,
'Common wheat': 4,
'Fat Hen': 5,
'Loose Silky-bent': 6,
'Maize': 7,
'Scentless Mayweed': 8,
'Shepherds Purse': 9,
'Small-flowered Cranesbill': 10,
'Sugar beet': 11
}
INV_CLASS = {
0: 'Black-grass',
1: 'Charlock',
2: 'Cleavers',
3: 'Common Chickweed',
4: 'Common wheat',
5: 'Fat Hen',
6: 'Loose Silky-bent',
7: 'Maize',
8: 'Scentless Mayweed',
9: 'Shepherds Purse',
10: 'Small-flowered Cranesbill',
11: 'Sugar beet'
}<choose_model_class>
alphas = [10, 1, 0.1, 0.01, 0.001, 0.0005, 0.0003]
lassos = [LassoCV(alphas = [a] ).fit(X_train, y)for a in alphas]
def dense_set(inp_layer, n, activation, drop_rate=0.) :
dp = Dropout(drop_rate )(inp_layer)
 dns = Dense(n )(dp)
 bn = BatchNormalization(axis=-1 )(dns)
 act = Activation(activation=activation )(bn)
 return act<choose_model_class>
nonzeros = [sum(c != 0)for c in coef]
def conv_layer(feature_batch, feature_map, kernel_size=(3, 3),strides=(1,1), zp_flag=False):
if zp_flag:
zp = ZeroPadding2D(( 1,1))(feature_batch)
 else:
zp = feature_batch
conv = Conv2D(filters=feature_map, kernel_size=kernel_size, strides=strides )(zp)
 bn = BatchNormalization(axis=3 )(conv)
 act = LeakyReLU(1/10 )(bn)
 return act<choose_model_class>
model_ridge = Ridge(alpha=10 ).fit(X_train, y)
 model_lasso = LassoCV(alphas=[0.0003] ).fit(X_train, y)
 ridge1_preds = np.expm1(model_ridge.predict(X_test))
lasso1_preds = np.expm1(model_lasso.predict(X_test))
y_lasso = model_lasso.predict(X_train)
 y_ridge = model_ridge.predict(X_train)
 y_lasso_test = model_lasso.predict(X_test)
 y_ridge_test = model_ridge.predict(X_test )
def get_model() :
inp_img = Input(shape=(51, 51, 3))
conv1 = conv_layer(inp_img, 64, zp_flag=False)
 conv2 = conv_layer(conv1, 64, zp_flag=False)
 mp1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv2)
 conv3 = conv_layer(mp1, 128, zp_flag=False)
 conv4 = conv_layer(conv3, 128, zp_flag=False)
 mp2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv4)
 conv7 = conv_layer(mp2, 256, zp_flag=False)
 conv8 = conv_layer(conv7, 256, zp_flag=False)
 conv9 = conv_layer(conv8, 256, zp_flag=False)
 mp3 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv9)
 flt = Flatten()(mp3)
 ds1 = dense_set(flt, 128, activation='tanh')
 out = dense_set(ds1, 12, activation='softmax')
 model = Model(inputs=inp_img, outputs=out)
 mypotim = Adam(lr=0.5 * 1e-3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy',
optimizer=mypotim,
metrics=['accuracy'])
 model.summary()
return model<split>
X_train['y_lasso'] = y_lasso
X_train['y_ridge'] = y_ridge
X_test['y_lasso'] = y_lasso_test
X_test['y_ridge'] = y_ridge_test
def train_model(img, target):
callbacks = get_callbacks(filepath='.. /input/plant-weight/model_weight_SGD.hdf5', patience=5)
 gmodel = get_model()
gmodel.load_weights(filepath='.. /input/plant-weight/model_weight_Adam.hdf5')
 x_train, x_valid, y_train, y_valid = train_test_split(
 img,
target,
shuffle=True,
train_size=0.8,
random_state=RANDOM_STATE 
)
 gen = ImageDataGenerator(
 rotation_range=360.,
width_shift_range=0.3,
height_shift_range=0.3,
zoom_range=0.6,
horizontal_flip=True,
vertical_flip=True 
)
 gmodel.fit_generator(gen.flow(x_train, y_train,batch_size=BATCH_SIZE),
steps_per_epoch=10*len(x_train)/BATCH_SIZE,
epochs=EPOCHS,
verbose=1,
shuffle=True,
validation_data=(x_valid, y_valid),
callbacks=callbacks)
<save_to_csv>
stacked_ridge = Ridge(alpha=10 ).fit(X_train, y )
def test_model(img, label):
gmodel = get_model()
gmodel.load_weights(filepath='.. /input/plant-weight/model_weight_SGD.hdf5')
 prob = gmodel.predict(img, verbose=1)
 pred = prob.argmax(axis=-1)
 sub = pd.DataFrame({"file": label,
"species": [INV_CLASS[p] for p in pred]})
 sub.to_csv("sub.csv", index=False, header=True )<normalization>
preds = np.expm1(stacked_ridge.predict(X_test))
def img_reshape(img):
img = imresize(img,(51, 51, 3))
return img<define_variables>
sub = pd.DataFrame({"Id": test.Id, "SalePrice": lasso1_preds} )
def img_label(path):
return str(str(path.split('/')[-1]))
def img_class(path):
return str(path.split('/')[-2] )<categorify>
sub.to_csv('submission.csv', index=False )
<prepare_x_and_y><EOS> 
sub.to_csv('submission.csv', index=False )