Kaggle competition: House Prices - Advanced Regression Techniques
import os
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage.draw import draw
from glob import glob
from sklearn.preprocessing import LabelEncoder
from keras.utils.np_utils import to_categorical
from multiprocessing.dummy import Pool
from keras.models import load_model
import time
import ast
import keras
import random
import glob
import math<define_variables>
model2 = linear_model.Lasso(alpha=.00075)
 score = rmsle_cv(model2)
 print("model score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))
ALL_FILES = glob.glob('.. /input/shuffle-csvs*/*.csv.gz')
 VALIDATION_FILE = '.. /input/shuffle-csvs-75000-100000/train_k0.csv.gz'
ALL_FILES.remove(VALIDATION_FILE)
 INPUT_DIR = '.. /input/quickdraw-doodle-recognition/'
BASE_SIZE = 256
NCATS = 340
np.random.seed(seed=1987)
 def apk(actual, predicted, k=3):

 if len(predicted)> k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i, p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits /(i + 1.0)
 if not actual:
return 0.0
return score / min(len(actual), k)
 def mapk(actual, predicted, k=3):

 return np.mean([apk(a, p, k)for a, p in zip(actual, predicted)])
 def preds2catids(predictions):
return pd.DataFrame(np.argsort(-predictions, axis=1)[:, :3], columns=['a', 'b', 'c'])
 def f2cat(filename: str)-> str:
return filename.split('.')[0]
def list_all_categories() :
files = os.listdir(os.path.join(INPUT_DIR, 'train_simplified'))
return sorted([f2cat(f)for f in files], key=str.lower)
 def plot_batch(x):
cols = 4
rows = 6
fig, axs = plt.subplots(nrows=rows, ncols=cols, sharex=True, sharey=True, figsize=(18, 18))
for i in range(rows):
for k in range(0,3):
ax = axs[i, k]
ax.imshow(x[i, :, :, k], cmap=plt.cm.gray)
 ax.axis('off')
 ax = axs[i, 3]
ax.imshow(x[i, :, :],)
 ax.axis('off')
 fig.tight_layout()
plt.show() ;<define_variables>
model3 = RandomForestRegressor(bootstrap=True, ccp_alpha=0.000001, criterion='mse',
max_depth=None, max_features=0.2, max_leaf_nodes=None,
max_samples=None, min_impurity_decrease=0.0,
min_impurity_split=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
n_estimators=1000, n_jobs=-1, oob_score=False,
random_state=None, verbose=0, warm_start=False)
 score = rmsle_cv(model3)
 print("model score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))
AUGMENTATION = True
STEPS = 500
BATCH_SIZE = 448
EPOCHS = 0
LEARNING_RATE = 0.002
IMG_SHAPE =(128,128,3)
 IMG_SIZE = IMG_SHAPE[0]<feature_engineering>
model4 = GradientBoostingRegressor(n_estimators=500,
learning_rate=0.027,
max_features=0.7,
criterion='mse',
min_samples_leaf=4,
min_samples_split=4,
loss='huber',
random_state =42)
 score = rmsle_cv(model4)
 print("gbr score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))
def image_generator(size, batchsize, lw=6, augmentation = False):
while True:
for filename in ALL_FILES:
for df in pd.read_csv(filename, chunksize=batchsize):
df['drawing'] = df['drawing'].apply(eval)
 x = np.zeros(( len(df), size, size,3))
for i, raw_strokes in enumerate(df.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw, augmentation = augmentation)
 x = x / 255.
 x = x.reshape(( len(df), size, size, 3)).astype(np.float32)
 y = keras.utils.to_categorical(df.y, num_classes=NCATS)
 yield x, y
def valid_generator(valid_df, size, batchsize, lw=6):
while(True):
for i in range(0,len(valid_df),batchsize):
chunk = valid_df[i:i+batchsize]
x = np.zeros(( len(chunk), size, size,3))
for i, raw_strokes in enumerate(chunk.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw)
 x = x / 255.
 x = x.reshape(( len(chunk), size, size,3)).astype(np.float32)
 y = keras.utils.to_categorical(chunk.y, num_classes=NCATS)
 yield x,y
def test_generator(test_df, size, batchsize, lw=6):
for i in range(0,len(test_df),batchsize):
chunk = test_df[i:i+batchsize]
x = np.zeros(( len(chunk), size, size,3))
for i, raw_strokes in enumerate(chunk.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw)
 x = x / 255.
 x = x.reshape(( len(chunk), size, size, 3)).astype(np.float32)
 yield x
train_datagen = image_generator(size=IMG_SIZE, batchsize=BATCH_SIZE, augmentation = AUGMENTATION)
 valid_df = pd.read_csv(VALIDATION_FILE)
 valid_df['drawing'] = valid_df['drawing'].apply(eval)
 validation_steps = len(valid_df)//BATCH_SIZE
valid_datagen = valid_generator(valid_df, size=IMG_SIZE, batchsize=BATCH_SIZE )<choose_model_class>
model5 = xgb.XGBRegressor(learning_rate=0.04,n_estimators=1000,
max_depth=2, min_child_weight=0,
gamma=0, subsample=0.75,
colsample_bytree=0.6,
objective='reg:linear', nthread=-1,
scale_pos_weight=1.2, seed=27,
reg_alpha=0.0006 )
def top_3_accuracy(y_true, y_pred):
return keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)
 reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10)
 checkpointer = ModelCheckpoint(filepath='mobileNet-best.hdf5', verbose=0, save_best_only=True)
 model = load_model('.. /input/mobilenet-126x126x3-100k-per-class/mobileNet.hdf5', custom_objects = {'top_3_accuracy':top_3_accuracy})
 opt = Adam(lr = LEARNING_RATE)
 model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy', top_3_accuracy])
 model.summary()<train_model>
score = rmsle_cv(model5)
 print("xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
history = model.fit_generator(train_datagen, steps_per_epoch=STEPS, epochs=EPOCHS , validation_data=valid_datagen, validation_steps = validation_steps, callbacks= [checkpointer, reducer])
 model.save('mobileNet.hdf5')
<load_from_csv>
model1.fit(X, y)
 model2.fit(X, y)
 model3.fit(X, y)
 model4.fit(X, y)
 model5.fit(X, y)
 predictions1 = model1.predict(X_target)
 predictions2 = model2.predict(X_target)
 predictions3 = model3.predict(X_target)
 predictions4 = model4.predict(X_target)
 predictions5 = model5.predict(X_target)
 predictions = 0.2*predictions1+0.3*predictions2+0.1*predictions3+0.2*predictions4+0.2*predictions5
log_df = pd.read_csv('.. /input/whole-training-log/log.csv' )<save_to_csv>
predictions = np.expm1(predictions )
log = pd.DataFrame.from_dict(history.history)
 log.to_csv('train_log.csv', index=False )<predict_on_test>
submission = pd.DataFrame({'Id': data.loc['test'].index, 'SalePrice': predictions} )
gen = test_generator(valid_df, size=IMG_SIZE, batchsize=BATCH_SIZE)
 valid_predictions = model.predict_generator(gen, steps = validation_steps, verbose=1)
 map3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions ).values)
 print('Map3: {:.3f}'.format(map3))<save_to_csv>
submission.to_csv("submission.csv", index=False )
<install_modules><EOS> 
submission.to_csv("submission.csv", index=False )