Kaggle competition: Zillow Prize: Zillow’s Home Value Prediction (Zestimate)
<SOS> metric: ZillowMAE Kaggle data source: zillow-prize:-zillow’s-home-value-prediction-(zestimate)<save_to_csv>
%matplotlib inline
submission_c.to_csv('./submission_c.csv')
 submission_c.to_csv('.. \output\kaggle\working\submission_c.csv' )<normalization>
print('Loading Properties...')
 properties2016 = pd.read_csv('.. /input/properties_2016.csv', low_memory = False)
 properties2017 = pd.read_csv('.. /input/properties_2017.csv', low_memory = False)
 print('Loading Train...')
 train2016 = pd.read_csv('.. /input/train_2016_v2.csv', parse_dates=['transactiondate'], low_memory=False)
 train2017 = pd.read_csv('.. /input/train_2017.csv', parse_dates=['transactiondate'], low_memory=False)
 print('Loading Sample...')
 sample_submission = pd.read_csv('.. /input/sample_submission.csv', low_memory=False )
test_rate = 0.05
maxlen = 13
train_date_count = len(set(train_df["Date"]))
X, Y = [],[]
scaler = StandardScaler()
train_df["Fatalities_std"] = scaler.fit_transform(train_df["Fatalities"].values.reshape(len(train_df["Fatalities"].values),1))
fatalities_std_min = train_df["Fatalities_std"].min()
train_df["Fatalities_std"] = train_df["Fatalities_std"] + abs(fatalities_std_min)
 ss = StandardScaler()
train_df["ConfirmedCases_std"] = ss.fit_transform(train_df["ConfirmedCases"].values.reshape(len(train_df["ConfirmedCases"].values),1))
confirmedCase_std_min = train_df["ConfirmedCases_std"].min()
train_df["ConfirmedCases_std"] = train_df["ConfirmedCases_std"] + abs(confirmedCase_std_min)
 for state,country in train_df.groupby(["Province_State","Country_Region"] ).sum().index:
df = train_df[(train_df["Country_Region"] == country)&(train_df["Province_State"] == state)]
if df["Fatalities"].sum() != 0 or df["ConfirmedCases"].sum() != 0:
for i in range(len(df)- maxlen):
X.append(df[['Fatalities_std','ConfirmedCases_std']].iloc[i:(i+maxlen)].values)
 Y.append(df[['Fatalities_std']].iloc[i+maxlen].values)
 X=np.array(X)
 Y=np.array(Y)
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_rate, shuffle = True ,random_state = 0 )<choose_model_class>
def add_date_features(df):
df["transaction_year"] = df["transactiondate"].dt.year
df["transaction_month"] = df["transactiondate"].dt.month
df["transaction_day"] = df["transactiondate"].dt.day
df["transaction_quarter"] = df["transactiondate"].dt.quarter
df.drop(["transactiondate"], inplace=True, axis=1)
 return df
epochs_num = 30
n_hidden = 200
n_in = 2
model = Sequential()
model.add(GRU(n_hidden,
batch_input_shape=(None, maxlen, n_in),
kernel_initializer='random_uniform',
return_sequences=False))
model.add(Dense(1, kernel_initializer='random_uniform'))
model.add(Activation("linear"))
opt = Adagrad(lr=0.01, epsilon=1e-08, decay=1e-4)
 model.compile(loss = huber_loss_mean, optimizer=opt )<train_model>
%%time
train2016 = add_date_features(train2016)
 train2017 = add_date_features(train2017)
 sample_submission['parcelid'] = sample_submission['ParcelId']
print('Merge Train & Test with Properties...')
 train2016 = pd.merge(train2016, properties2016, how='left', on='parcelid')
 train2017 = pd.merge(train2017, properties2017, how='left', on='parcelid')
 test_df = pd.merge(sample_submission, properties2016, how='left', on='parcelid')
 print('Concat Train 2016 & 2017...')
 train_df = pd.concat([train2016, train2017], axis=0)
 del properties2016, properties2017, train2016, train2017
gc.collect() ;
print("Train: ", train_df.shape)
 print("Test: ", test_df.shape )
early_stopping = EarlyStopping(monitor='loss', patience=5, verbose=1)
 hist = model.fit(X_train, Y_train, batch_size=8, epochs=epochs_num,
callbacks=[early_stopping],shuffle=False )<predict_on_test>
missing_perc_thresh = 0.98
exclude_missing = []
num_rows = train_df.shape[0]
for c in train_df.columns:
num_missing = train_df[c].isnull().sum()
if num_missing == 0:
continue
missing_frac = num_missing / float(num_rows)
 if missing_frac > missing_perc_thresh:
print(c, "------","{:.5f}".format(missing_frac))
exclude_missing.append(c)
 print("
We exclude:",exclude_missing)
 print("
",len(exclude_missing))
predicted_std = model.predict(X_test)
 result_std= pd.DataFrame(predicted_std)
 result_std.columns = ['predict']
result_std['actual'] = Y_test<normalization>
exclude_unique = []
for c in train_df.columns:
num_uniques = len(train_df[c].unique())
 if train_df[c].isnull().sum() != 0:
num_uniques -= 1
if num_uniques == 1:
exclude_unique.append(c)
 print("We exclude: %s" % exclude_unique)
 print("
",len(exclude_unique))
predicted = scaler.inverse_transform(predicted_std - abs(fatalities_std_min))
Y_test = scaler.inverse_transform(Y_test - abs(fatalities_std_min))<normalization>
exclude_other = ['parcelid', 'logerror','propertyzoningdesc']
train_features = []
for c in train_df.columns:
if c not in exclude_missing \
and c not in exclude_other and c not in exclude_unique:
train_features.append(c)
 print("We use these for training: %s" % train_features)
 print("
",len(train_features))
X_test_ = scaler.inverse_transform(X_test - abs(fatalities_std_min))<prepare_output>
cat_feature_inds = []
cat_unique_thresh = 1000
for i, c in enumerate(train_features):
num_uniques = len(train_df[c].unique())
 if num_uniques < cat_unique_thresh \
and not 'sqft' in c \
and not 'cnt' in c \
and not 'nbr' in c \
and not 'number' in c:
cat_feature_inds.append(i)
 print("Cat features are: %s" % [train_features[ind] for ind in cat_feature_inds] )
submission_df = submission_c<filter>
print("Replacing NaN values by -999 !!")
 train_df.fillna(-999, inplace=True)
 test_df.fillna(-999, inplace=True )
temp =(datetime.datetime.strptime("2020-03-25", '%Y-%m-%d')- datetime.timedelta(days=maxlen)).strftime('%Y-%m-%d')
 test_df = train_df[train_df["Date"] > temp]<normalization>
X_train, X_test, y_train, y_test = train_test_split(train_df[train_features], train_df.logerror, test_size=0.2, random_state=99)
 print(X_train.shape, y_train.shape)
 print(X_test.shape, y_test.shape)
 all_pool = Pool(train_df[train_features], train_df.logerror, cat_feature_inds)
 train_pool = Pool(X_train, y_train, cat_feature_inds)
 test_pool = Pool(X_test, y_test, cat_feature_inds )
check_df["Fatalities_std"] = scaler.transform(check_df["Fatalities"].values.reshape(len(check_df["Fatalities"].values),1)) + abs(fatalities_std_min)
 check_df<concatenate>
catboost_parameters = {
'iterations': 400,
'learning_rate': 0.035,
'depth': 7,
'verbose': 20,
'task_type': 'GPU',
'loss_function': 'MAE',
'eval_metric': 'MAE',
'random_seed': 0,
}
fatalities_pred = []
for i in range(0,306*maxlen,maxlen):
temp_array = np.array(test_df[["Fatalities_std","ConfirmedCases_std"]][i:i+maxlen])
 for j in range(43):
if j<13:
temp_array = np.append(temp_array,np.append(np.array(check_df["Fatalities_std"])[int(i*13/maxlen)+j],np.array(check_df["ConfirmedCases_std"])[int(i*13/maxlen)+j] ).reshape(1,2),axis=0)
 elif np.array(test_df[["Fatalities","ConfirmedCases"]][i:i+maxlen] ).sum() == 0:
temp_array = np.append(temp_array,np.array(temp_array[-1] ).reshape(1,2),axis=0)
 else:
pred = model.predict(temp_array[-maxlen:].reshape(1,maxlen,2))
pred = np.where(pred<temp_array[-1][0],temp_array[-1][0],pred)
 temp_array = np.append(temp_array,np.append(pred,submission_df["ConfirmedCases_std"][i/maxlen*43+j] ).reshape(1,2),axis=0)
 fatalities_pred.append(temp_array[-43:] )<feature_engineering>
model = CatBoostRegressor(**catboost_parameters)
 model.fit(train_pool, eval_set=test_pool )
submission_df["Fatalities"] = np.abs(scaler.inverse_transform([i[0] - abs(fatalities_std_min)for i in np.array(fatalities_pred ).reshape(306*43,2)]))
submission_df<data_type_conversions>
print_feature_importance(model, train_pool, X_train )
submission_df[["ConfirmedCases","Fatalities"]] = submission_df[["ConfirmedCases","Fatalities"]].round().astype(int)
 submission_df<drop_column>
num_ensembles = 5
models = [None] * num_ensembles
for i in range(num_ensembles):
print("
Training(ensemble): %d..." %(i))
catboost_parameters['random_seed'] = i
models[i] = CatBoostRegressor(**catboost_parameters)
 models[i].fit(train_pool, eval_set=test_pool)
 print('-- Feature Importance --')
 print_feature_importance(models[i], train_pool, X_train )
<drop_column><EOS> 
submission = pd.DataFrame({
'ParcelId': test_df['parcelid'],
})
 test_dates = {
'201610': pd.Timestamp('2016-09-30'),
'201611': pd.Timestamp('2016-10-31'),
'201612': pd.Timestamp('2016-11-30'),
'201710': pd.Timestamp('2017-09-30'),
'201711': pd.Timestamp('2017-10-31'),
'201712': pd.Timestamp('2017-11-30')
 }
for label, test_date in test_dates.items() :
print("Predicting for: %s..." %(label))
test_df['transactiondate'] = test_date
test_df = add_date_features(test_df)
 y_pred = 0.0
for i in range(num_ensembles):
print("Ensemble:", i)
 y_pred += models[i].predict(test_df[train_features])
 y_pred /= num_ensembles
submission[label] = y_pred
submission_major = 2
print("Creating submission: submission_%03d.csv..." %(submission_major))
submission.to_csv(
 'submission_%03d.csv' %(submission_major),
float_format='%.4f',
index=False)
 print("Finished." )