Kaggle competition: Two Sigma Connect: Rental Listing Inquiries
<SOS> metric: MulticlassLoss Kaggle data source: two-sigma-connect:-rental-listing-inquiries<choose_model_class>
import os
import sys
import operator
import numpy as np
import pandas as pd
from scipy import sparse
import xgboost as xgb
from sklearn import model_selection, preprocessing, ensemble
from sklearn.metrics import log_loss
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
def build_model(width, height, depth, classes):
inputShape =(height, width, depth)
 chanDim = -1
model = Sequential([
Conv2D(16,(3, 3), padding="same", input_shape=inputShape),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(32,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(32,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Flatten() ,
Dense(256),
Activation("relu"),
BatchNormalization() ,
Dropout(0.5),
Dense(classes),
Activation("softmax")
 ])
 return model<compute_train_metric>
def runXGB(train_X, train_y, test_X, test_y=None, feature_names=None, seed_val=0, num_rounds=1000):
param = {}
param['objective'] = 'multi:softprob'
param['eta'] = 0.1
param['max_depth'] = 6
param['silent'] = 1
param['num_class'] = 3
param['eval_metric'] = "mlogloss"
param['min_child_weight'] = 0.7
param['subsample'] = 0.8
param['colsample_bytree'] = 0.7
param['seed'] = seed_val
num_rounds = num_rounds
plst = list(param.items())
 xgtrain = xgb.DMatrix(train_X, label=train_y)
 if test_y is not None:
xgtest = xgb.DMatrix(test_X, label=test_y)
 watchlist = [(xgtrain,'train'),(xgtest, 'test')]
model = xgb.train(plst, xgtrain, num_rounds, watchlist, early_stopping_rounds=20)
 else:
xgtest = xgb.DMatrix(test_X)
 model = xgb.train(plst, xgtrain, num_rounds)
 pred_test_y = model.predict(xgtest)
 return pred_test_y, model
def step(X, y):
with tf.GradientTape() as tape:
pred = model(X)
 loss = categorical_crossentropy(y, pred)
 grads = tape.gradient(loss, model.trainable_variables)
 opt.apply_gradients(zip(grads, model.trainable_variables))<define_variables>
data_path = ".. /input/"
train_file = data_path + "train.json"
test_file = data_path + "test.json"
train_df = pd.read_json(train_file)
 test_df = pd.read_json(test_file)
 print(train_df.shape)
 print(test_df.shape )
EPOCHS = 50
BS = 32
INIT_LR = 1e-3
model = build_model(28, 28, 1, 10)
 opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
 numUpdates = int(X_train.shape[0] / BS)
 for epoch in range(0, EPOCHS):
print("[INFO] starting epoch {}/{}...".format(epoch + 1, EPOCHS), end="")
 sys.stdout.flush()
epochStart = time.time()
for i in range(0, numUpdates):
start = i * BS
end = start + BS
step(X_train[start:end], y_train[start:end])
 epochEnd = time.time()
elapsed =(epochEnd - epochStart)/ 60.0
print("took {:.4} minutes".format(elapsed))<save_to_csv>
features_to_use = ["bathrooms", "bedrooms", "latitude", "longitude", "price"]
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image("model.png" )<train_model>
train_df["num_photos"] = train_df["photos"].apply(len)
 test_df["num_photos"] = test_df["photos"].apply(len)
 train_df["num_features"] = train_df["features"].apply(len)
 test_df["num_features"] = test_df["features"].apply(len)
 train_df["num_description_words"] = train_df["description"].apply(lambda x: len(x.split(" ")))
 test_df["num_description_words"] = test_df["description"].apply(lambda x: len(x.split(" ")))
 train_df["created"] = pd.to_datetime(train_df["created"])
 test_df["created"] = pd.to_datetime(test_df["created"])
 train_df["created_year"] = train_df["created"].dt.year
test_df["created_year"] = test_df["created"].dt.year
train_df["created_month"] = train_df["created"].dt.month
test_df["created_month"] = test_df["created"].dt.month
train_df["created_day"] = train_df["created"].dt.day
test_df["created_day"] = test_df["created"].dt.day
train_df["created_hour"] = train_df["created"].dt.hour
test_df["created_hour"] = test_df["created"].dt.hour
train_df['total_room'] = train_df['bedrooms'] + train_df['bathrooms']
train_df['price_per_room'] = train_df['price'] / train_df['total_room']
test_df['total_room'] = test_df['bedrooms'] + test_df['bathrooms']
test_df['price_per_room'] = test_df['price'] / test_df['total_room']
features_to_use.extend(["num_photos", "num_features", "num_description_words","created_year", "created_month", "created_day", "listing_id", "created_hour", 'price_per_room'] )
model.compile(optimizer=opt, loss=categorical_crossentropy,metrics=["acc"])
<save_to_csv>
categorical = ["display_address", "manager_id", "building_id", "street_address"]
for f in categorical:
if train_df[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(train_df[f].values)+ list(test_df[f].values))
train_df[f] = lbl.transform(list(train_df[f].values))
test_df[f] = lbl.transform(list(test_df[f].values))
features_to_use.append(f )
y_pred = model.predict(X_test)
 y_pred = np.argmax(y_pred,axis=1)
 my_submission = pd.DataFrame({'ImageId': list(range(1, len(y_pred)+1)) , 'Label': y_pred})
 my_submission.to_csv('submission.csv', index=False )<train_model>
target_num_map = {'high':0, 'medium':1, 'low':2}
train_df['label'] = train_df['interest_level'].apply(lambda x: target_num_map[x])
 manager_score = train_df['label'].groupby(train_df['manager_id'] ).mean()
manager_score = pd.DataFrame(manager_score)
 manager_score.columns = { 'manager_score' }
manager_score['manager_id'] = manager_score.index
manager_score.head()
hist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=1000,
epochs=1000,
verbose=2,
validation_data=(X_train[:400,:], Y_train[:400,:]),
callbacks=[annealer])
 final_loss, final_acc = model.evaluate(X_train, Y_train, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc)) 
<save_to_csv>
train_df = pd.merge(train_df, manager_score, on='manager_id')
 train_df.head()
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("MNIST_ver21.csv",index=False )<load_from_csv>
test_df = pd.merge(test_df, manager_score, on='manager_id', how='left')
 test_df.head()
test_df.shape
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<prepare_x_and_y>
features_to_use.append('manager_score' )
X = train.drop("label", axis = 1)
 y = train["label"]<split>
train_df['features'] = train_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
test_df['features'] = test_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
print(train_df["features"].head())
 tfidf = CountVectorizer(stop_words='english', max_features=200)
 tr_sparse = tfidf.fit_transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"] )
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size = 0.25 )<feature_engineering>
train_X = sparse.hstack([train_df[features_to_use], tr_sparse] ).tocsr()
test_X = sparse.hstack([test_df[features_to_use], te_sparse] ).tocsr()
target_num_map = {'high':0, 'medium':1, 'low':2}
train_y = np.array(train_df['interest_level'].apply(lambda x: target_num_map[x]))
print(train_X.shape, test_X.shape )
X_train = X_train / 255
X_test = X_test / 255
test = test / 255<categorify>
cv_scores = []
kf = model_selection.KFold(n_splits=10, shuffle=True, random_state=2016)
 for dev_index, val_index in kf.split(range(train_X.shape[0])) :
dev_X, val_X = train_X[dev_index,:], train_X[val_index,:]
dev_y, val_y = train_y[dev_index], train_y[val_index]
preds, model = runXGB(dev_X, dev_y, val_X, val_y)
 cv_scores.append(log_loss(val_y, preds))
print(cv_scores)
 break
y_train = to_categorical(y_train)
 y_test = to_categorical(y_test )<prepare_x_and_y>
preds, model = runXGB(train_X, train_y, test_X, num_rounds=1000)
 out_df = pd.DataFrame(preds)
 out_df.columns = ["high", "medium", "low"]
out_df["listing_id"] = test_df.listing_id.values
out_df.to_csv("xgb3.csv", index=False )