Kaggle competition: Zillow Prize: Zillow’s Home Value Prediction (Zestimate)
<SOS> metric: ZillowMAE Kaggle data source: zillow-prize:-zillow’s-home-value-prediction-(zestimate)<train_model>
import numpy as np
import pandas as pd
import xgboost as xgb
from sklearn.preprocessing import LabelEncoder
import lightgbm as lgb
import gc
from sklearn.linear_model import LinearRegression
import random
import datetime as dt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout, BatchNormalization
from keras.layers.advanced_activations import PReLU
from keras.layers.noise import GaussianDropout
from keras.optimizers import Adam
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import Imputer
bgc = BaggingClassifier(DecisionTreeClassifier() , max_samples= 0.5,max_features=1.0,n_estimators=28)
 bgc.fit(x_data,y_data.values.ravel())
 pred = bgc.predict(x_data)
 accuracy_score(y_data,pred)*100<save_to_csv>
prox=pd.read_csv('.. /input/properties_2016.csv')
predic1 = bgc.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic1})
 output.to_csv('my_submission31.csv', index=False)
 print("Your submission was successfully saved!" )<compute_train_metric>
for c in prox.columns:
if(c=='airconditioningtypeid' or c=='taxdelinquencyflag' or c=='poolcnt' or c=='regionidcity' or c=='fireplacecnt' or c=='propertyzoningdesc' or c=='propertycountylandusecode'):
prox[c]=prox[c].fillna(-1)
 else:
prox[c]=prox[c].fillna(prox[c].median())
 prox.head()
rto1 = RandomForestClassifier(n_estimators=10)
 rto1.fit(x_data,y_data.values.ravel())
 y_pred = rto1.predict(x_data)
 accuracy_score(y_data,y_pred)*100<save_to_csv>
lbl = LabelEncoder()
for c in prox.columns:
prox[c]=prox[c].fillna(-1)
 if prox[c].dtype == 'object':
lbl.fit(list(prox[c].values))
prox[c] = lbl.transform(list(prox[c].values))
prox.head()
predic = rto.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic})
 output.to_csv('my_submission32.csv', index=False)
 print("Your submission was successfully saved!" )<predict_on_test>
prox['N-life'] = 2018 - prox['yearbuilt']
prox['N-LivingAreaError'] = prox['calculatedfinishedsquarefeet']/prox['finishedsquarefeet12']
prox['N-LivingAreaProp'] = prox['calculatedfinishedsquarefeet']/prox['lotsizesquarefeet']
prox['N-LivingAreaProp2'] = prox['finishedsquarefeet12']/prox['finishedsquarefeet15']
prox['N-ExtraSpace'] = prox['lotsizesquarefeet'] - prox['calculatedfinishedsquarefeet']
prox['N-ExtraSpace-2'] = prox['finishedsquarefeet15'] - prox['finishedsquarefeet12']
prox['N-TotalRooms'] = prox['bathroomcnt']*prox['bedroomcnt']
prox['N-AvRoomSize'] = prox['calculatedfinishedsquarefeet']/prox['roomcnt']
prox['N-ExtraRooms'] = prox['roomcnt'] - prox['N-TotalRooms']
prox['N-ValueProp'] = prox['structuretaxvaluedollarcnt']/prox['landtaxvaluedollarcnt']
prox['N-GarPoolAC'] =(( prox['garagecarcnt']>0)&(prox['pooltypeid10']>0)&(prox['airconditioningtypeid']!=5)) *1
prox["N-location"] = prox["latitude"] + prox["longitude"]
prox["N-location-2"] = prox["latitude"]*prox["longitude"]
prox["N-location-2round"] = prox["N-location-2"].round(-4)
 prox["N-latitude-round"] = prox["latitude"].round(-4)
 prox["N-longitude-round"] = prox["longitude"].round(-4)
 prox.head()
xgb = xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5,alpha=1,n_estimators=28)
 xgb.fit(x_data,y_data.values.ravel())
 y_pred = xgb.predict(x_data)
 accuracy_score(y_data,y_pred)*100<save_to_csv>
prox['N-ValueRatio'] = prox['taxvaluedollarcnt']/prox['taxamount']
prox['N-TaxScore'] = prox['taxvaluedollarcnt']*prox['taxamount']
prox["N-taxdelinquencyyear-2"] = prox["taxdelinquencyyear"] ** 2
prox["N-taxdelinquencyyear-3"] = prox["taxdelinquencyyear"] ** 3
prox['N-life'] = 2018 - prox['taxdelinquencyyear']
predic3 = xgb.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic3})
 output.to_csv('my_submission33.csv', index=False)
 print("Your submission was successfully saved!" )<choose_model_class>
zip_count = prox['regionidzip'].value_counts().to_dict()
prox['N-zip_count'] = prox['regionidzip'].map(zip_count)
 city_count = prox['regionidcity'].value_counts().to_dict()
prox['N-city_count'] = prox['regionidcity'].map(city_count)
 region_count = prox['regionidcounty'].value_counts().to_dict()
prox['N-county_count'] = prox['regionidcounty'].map(city_count)
estimator3 = []
estimator3.append(( 'svm', SVC(gamma='auto',kernel='linear',C=1)))
 estimator3.append(( 'lg', LogisticRegression(solver='liblinear',multi_class='auto',C=1)))
 estimator3.append(( 'rfc', RandomForestClassifier(n_estimators=28)))
 estimator3.append(( 'xgb', xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha=1.1,n_estimators=28)))
 estimator3.append(( 'bg', BaggingClassifier(DecisionTreeClassifier() , max_samples= 0.5,max_features=1.0,n_estimators=28)))
 estimator3.append(( 'ada2', AdaBoostClassifier(DecisionTreeClassifier() ,n_estimators=5,learning_rate=1)))
 votin = VotingClassifier(estimators=estimator3,voting='hard')
 votin.fit(x_data1,y_data.values.ravel() )<save_to_csv>
prox['N-ACInd'] =(prox['airconditioningtypeid']!=5)*1
prox['N-HeatInd'] =(prox['heatingorsystemtypeid']!=13)*1
prox['N-PropType'] = prox.propertylandusetypeid.replace({31 : "Mixed", 46 : "Other", 47 : "Mixed", 246 : "Mixed", 247 : "Mixed", 248 : "Mixed", 260 : "Home", 261 : "Home", 262 : "Home", 263 : "Home", 264 : "Home", 265 : "Home", 266 : "Home", 267 : "Home", 268 : "Home", 269 : "Not Built", 270 : "Home", 271 : "Home", 273 : "Home", 274 : "Other", 275 : "Home", 276 : "Home", 279 : "Home", 290 : "Not Built", 291 : "Not Built" } )
predic = votin.predict(new_test_data1)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic})
 output.to_csv('my_submission21.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
prox["N-structuretaxvaluedollarcnt-2"] = prox["structuretaxvaluedollarcnt"] ** 2
prox["N-structuretaxvaluedollarcnt-3"] = prox["structuretaxvaluedollarcnt"] ** 3
group = prox.groupby('regionidcity')['structuretaxvaluedollarcnt'].aggregate('mean' ).to_dict()
prox['N-Avg-structuretaxvaluedollarcnt'] = prox['regionidcity'].map(group)
 prox['N-Dev-structuretaxvaluedollarcnt'] = abs(( prox['structuretaxvaluedollarcnt'] - prox['N-Avg-structuretaxvaluedollarcnt'])) /prox['N-Avg-structuretaxvaluedollarcnt']
%matplotlib inline
pd.options.display.max_columns = 100
pd.options.display.max_rows = 100
matplotlib.style.use('ggplot' )<load_from_csv>
print("
Reading data from disk...")
 prop = prox
train = pd.read_csv(".. /input/train_2016_v2.csv" )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train.describe()<drop_column>
print("
Processing data for LightGBM...")
 for c, dtype in zip(prop.columns, prop.dtypes):	
if dtype == np.float64:
prop[c] = prop[c].astype(np.float32)
 df_train = train.merge(prop, how='left', on='parcelid')
 df_train.fillna(df_train.median() ,inplace = True)
 x_train = df_train.drop(['parcelid', 'logerror', 'transactiondate', 'propertyzoningdesc',
'propertycountylandusecode', 'fireplacecnt', 'fireplaceflag'], axis=1)
 y_train = df_train['logerror'].values
print(x_train.shape, y_train.shape)
 train_columns = x_train.columns
for c in x_train.dtypes[x_train.dtypes == object].index.values:
x_train[c] =(x_train[c] == True)
 del df_train; gc.collect()
x_train = x_train.values.astype(np.float32, copy=False)
 d_train = lgb.Dataset(x_train, label=y_train )
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )<feature_engineering>
params = {}
params['max_bin'] = 10
params['learning_rate'] = 0.0021
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'l1'
params['sub_feature'] = 0.345
params['bagging_fraction'] = 0.85
params['bagging_freq'] = 40
params['num_leaves'] = 512
params['min_data'] = 500
params['min_hessian'] = 0.05
params['verbose'] = 0
params['feature_fraction_seed'] = 2
params['bagging_seed'] = 3
np.random.seed(0)
 random.seed(0)
train["SalePrice"] = np.log1p(train["SalePrice"] )<prepare_x_and_y>
print("
Fitting LightGBM model...")
 clf = lgb.train(params, d_train, 430)
 del d_train; gc.collect()
del x_train; gc.collect()
print("
Prepare for LightGBM prediction...")
 print(" Read sample file...")
 sample = pd.read_csv('.. /input/sample_submission.csv')
 print("...")
 sample['parcelid'] = sample['ParcelId']
print(" Merge with property data...")
 df_test = sample.merge(prop, on='parcelid', how='left')
 print("...")
 del sample, prop; gc.collect()
print("...")
 x_test = df_test[train_columns]
print("...")
 del df_test; gc.collect()
print(" Preparing x_test...")
 for c in x_test.dtypes[x_test.dtypes == object].index.values:
x_test[c] =(x_test[c] == True)
 print("...")
 x_test = x_test.values.astype(np.float32, copy=False)
 print("
Start LightGBM prediction...")
 p_test = clf.predict(x_test)
 del x_test; gc.collect()
print("
Unadjusted LightGBM predictions:")
 print(pd.DataFrame(p_test ).head())
ntrain = train.shape[0]
ntest = test.shape[0]
y_train_sale = train.SalePrice.values
combined = pd.concat(( train, test)).reset_index(drop=True)
 combined.drop(['SalePrice'], axis=1, inplace=True )<create_dataframe>
print("
Re-reading properties file...")
 properties = prox
all_data_na =(combined.isnull().sum() / len(combined)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data<data_type_conversions>
print("
Processing data for XGBoost...")
 for c in properties.columns:
properties[c]=properties[c].fillna(-1)
 if properties[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(properties[c].values))
properties[c] = lbl.transform(list(properties[c].values))
train_df = train.merge(properties, how='left', on='parcelid')
 x_train = train_df.drop(['parcelid', 'logerror','transactiondate'], axis=1)
 x_test = properties.drop(['parcelid'], axis=1)
 print('Shape train: {}
Shape test: {}'.format(x_train.shape, x_test.shape))
combined['MasVnrArea'] = combined['MasVnrArea'].fillna(0.0)
 combined["MasVnrType"] = combined["MasVnrType"].fillna("None")
 combined['LotFrontage'] = combined['LotFrontage'].fillna(combined['LotFrontage'].median())
 combined['BsmtFinSF1'] = combined['BsmtFinSF1'].fillna(0.0)
 combined['BsmtFinSF2'] = combined['BsmtFinSF2'].fillna(0.0)
 combined['BsmtUnfSF'] = combined['BsmtUnfSF'].fillna(0.0)
 combined['TotalBsmtSF'] = combined['TotalBsmtSF'].fillna(0.0)
 combined['BsmtFullBath'] = combined['BsmtFullBath'].fillna(0)
 combined['BsmtHalfBath'] = combined['BsmtHalfBath'].fillna(0)
 combined['GarageYrBlt'] = combined['GarageYrBlt'].fillna(0)
 combined['GarageCars'] = combined['GarageCars'].fillna(0)
 combined['GarageArea'] = combined['GarageArea'].fillna(0)
 combined['GarageFinish'] = combined['GarageFinish'].fillna('None')
 combined['MSZoning'] = combined['MSZoning'].fillna(combined['MSZoning'].mode() [0])
 combined = combined.drop(['Utilities'], axis=1)
 combined["Functional"] = combined["Functional"].fillna("Typ")
 combined['Electrical'] = combined['Electrical'].fillna(combined['Electrical'].mode() [0])
 combined['KitchenQual'] = combined['KitchenQual'].fillna(combined['KitchenQual'].mode() [0])
 combined['Exterior1st'] = combined['Exterior1st'].fillna(combined['Exterior1st'].mode() [0])
 combined['Exterior2nd'] = combined['Exterior2nd'].fillna(combined['Exterior2nd'].mode() [0])
 combined['SaleType'] = combined['SaleType'].fillna(combined['SaleType'].mode() [0])
 combined['MSSubClass'] = combined['MSSubClass'].fillna("None")
 combined['PoolQC'] = combined['PoolQC'].fillna('None')
 combined['MiscFeature'] = combined['MiscFeature'].fillna('None')
 combined['Alley'] = combined['Alley'].fillna('None')
 combined['Fence'] = combined['Fence'].fillna('None')
 combined['FireplaceQu'] = combined['FireplaceQu'].fillna('None')
 combined["LotFrontage"] = combined.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
combined[col] = combined[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
combined[col] = combined[col].fillna('None' )<categorify>
print("
Setting up data for XGBoost...")
 xgb_params = {
'eta': 0.037,
'max_depth': 5,
'subsample': 0.80,
'objective': 'reg:linear',
'eval_metric': 'mae',
'lambda': 0.8,
'alpha': 0.4,
'base_score': y_mean,
'silent': 1
}
dtrain = xgb.DMatrix(x_train, y_train)
 dtest = xgb.DMatrix(x_test)
 num_boost_rounds = 250
print("num_boost_rounds="+str(num_boost_rounds))
print("
Training XGBoost...")
 model = xgb.train(dict(xgb_params, silent=1), dtrain, num_boost_round=num_boost_rounds)
 print("
Predicting with XGBoost...")
 xgb_pred1 = model.predict(dtest)
 print("
First XGBoost predictions:")
 print(pd.DataFrame(xgb_pred1 ).head())
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(combined[c].values))
combined[c] = lbl.transform(list(combined[c].values))<feature_engineering>
print("
Setting up data for XGBoost...")
 xgb_params = {
'eta': 0.033,
'max_depth': 6,
'subsample': 0.80,
'objective': 'reg:linear',
'eval_metric': 'mae',
'base_score': y_mean,
'silent': 1
}
num_boost_rounds = 150
print("num_boost_rounds="+str(num_boost_rounds))
print("
Training XGBoost again...")
 model = xgb.train(dict(xgb_params, silent=1), dtrain, num_boost_round=num_boost_rounds)
 print("
Predicting with XGBoost again...")
 xgb_pred2 = model.predict(dtest)
 print("
Second XGBoost predictions:")
 print(pd.DataFrame(xgb_pred2 ).head())
combined['TotalSF'] = combined['TotalBsmtSF'] + combined['1stFlrSF'] + combined['2ndFlrSF']<categorify>
xgb_pred = XGB1_WEIGHT*xgb_pred1 +(1-XGB1_WEIGHT)*xgb_pred2
print("
Combined XGBoost predictions:")
 print(pd.DataFrame(xgb_pred ).head() )
combined = pd.get_dummies(combined )<prepare_x_and_y>
del train_df
del x_train
del x_test
del properties
del dtest
del dtrain
del xgb_pred1
del xgb_pred2
gc.collect()
train = combined[:ntrain]
test = combined[ntrain:]<split>
print("
Processing data for Neural Network...")
 print('
Loading train, prop and sample data...')
 train = pd.read_csv(".. /input/train_2016_v2.csv", parse_dates=["transactiondate"])
 prop = prox
sample = pd.read_csv('.. /input/sample_submission.csv')
 print('Fitting Label Encoder on properties...')
 for c in prop.columns:
prop[c]=prop[c].fillna(-1)
 if prop[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(prop[c].values))
prop[c] = lbl.transform(list(prop[c].values))
x_train, x_test, y_train, y_test = train_test_split(train, y_train_sale, test_size=0.1, random_state=200 )<import_modules>
print('Creating training set...')
 df_train = train.merge(prop, how='left', on='parcelid')
 df_train["transactiondate"] = pd.to_datetime(df_train["transactiondate"])
 df_train["transactiondate_year"] = df_train["transactiondate"].dt.year
df_train["transactiondate_month"] = df_train["transactiondate"].dt.month
df_train['transactiondate_quarter'] = df_train['transactiondate'].dt.quarter
df_train["transactiondate"] = df_train["transactiondate"].dt.day
print('Filling NA/NaN values...')
 df_train.fillna(-1.0)
 print('Creating x_train and y_train from df_train...')
 x_train = df_train.drop(['parcelid', 'logerror', 'transactiondate', 'propertyzoningdesc', 'propertycountylandusecode','fireplacecnt', 'fireplaceflag'], axis=1)
 y_train = df_train["logerror"]
y_mean = np.mean(y_train)
 print(x_train.shape, y_train.shape)
 train_columns = x_train.columns
for c in x_train.dtypes[x_train.dtypes == object].index.values:
x_train[c] =(x_train[c] == True)
from sklearn import ensemble, tree, linear_model
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_squared_error
import lightgbm as lgb<compute_test_metric>
print('Creating df_test...')
 sample['parcelid'] = sample['ParcelId']
print("Merging Sample with property data...")
 df_test = sample.merge(prop, on='parcelid', how='left')
 df_test["transactiondate"] = pd.to_datetime('2016-11-15')
 df_test["transactiondate_year"] = df_test["transactiondate"].dt.year
df_test["transactiondate_month"] = df_test["transactiondate"].dt.month
df_test['transactiondate_quarter'] = df_test['transactiondate'].dt.quarter
df_test["transactiondate"] = df_test["transactiondate"].dt.day
x_test = df_test[train_columns]
print('Shape of x_test:', x_test.shape)
 print("Preparing x_test...")
 for c in x_test.dtypes[x_test.dtypes == object].index.values:
x_test[c] =(x_test[c] == True)
def get_score(prediction, lables):
print('R2: {}'.format(r2_score(prediction, lables)))
 print('RMSE: {}'.format(np.sqrt(mean_squared_error(prediction, lables))))
def train_test(estimator, x_trn, x_tst, y_trn, y_tst):
prediction_train = estimator.predict(x_trn)
 get_score(prediction_train, y_trn)
 prediction_test = estimator.predict(x_tst)
 get_score(prediction_test, y_tst )<train_model>
print("
Preprocessing neural network data...")
 imputer= Imputer()
imputer.fit(x_train.iloc[:, :])
 x_train = imputer.transform(x_train.iloc[:, :])
 imputer.fit(x_test.iloc[:, :])
 x_test = imputer.transform(x_test.iloc[:, :])
 sc = StandardScaler()
x_train = sc.fit_transform(x_train)
 x_test = sc.transform(x_test)
 len_x=int(x_train.shape[1])
 print("len_x is:",len_x)
GBR = ensemble.GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05, max_depth=3, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10, loss='huber' ).fit(x_train, y_train)
 train_test(GBR, x_train, x_test, y_train, y_test )<train_model>
print("
Setting up neural network model...")
 nn = Sequential()
nn.add(Dense(units = 400 , kernel_initializer = 'normal', input_dim = len_x))
nn.add(PReLU())
 nn.add(Dropout (.4))
nn.add(Dense(units = 160 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.6))
nn.add(Dense(units = 64 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.5))
nn.add(Dense(units = 26, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.6))
nn.add(Dense(1, kernel_initializer='normal'))
nn.compile(loss='mae', optimizer=Adam(lr=4e-3, decay=1e-4))
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0007000000000000001, random_state=1)).fit(x_train, y_train)
 train_test(lasso, x_train, x_test, y_train, y_test )<choose_model_class>
print("
Fitting neural network model...")
 nn.fit(np.array(x_train), np.array(y_train), batch_size = 32, epochs = 52, verbose=2)
 print("
Predicting with neural network model...")
 y_pred_ann = nn.predict(x_test)
 print("
Preparing results for write...")
 nn_pred = y_pred_ann.flatten()
print("Type of nn_pred is ", type(nn_pred))
print("Shape of nn_pred is ", nn_pred.shape)
 print("
Neural Network predictions:")
 print(pd.DataFrame(nn_pred ).head() )
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 ).fit(x_train, y_train)
 train_test(model_lgb, x_train, x_test, y_train, y_test )<train_model>
del train
del prop
del sample
del x_train
del x_test
del df_train
del df_test
del y_pred_ann
gc.collect()
GB_model = GBR.fit(train, y_train_sale)
 gbr_labels = np.expm1(GB_model.predict(test))
lasso_model = lasso.fit(train, y_train_sale)
 lasso_labels = np.expm1(lasso_model.predict(test))
lgb_model = model_lgb.fit(train, y_train_sale)
 lgb_labels = np.expm1(lgb_model.predict(test))<define_variables>
np.random.seed(17)
 random.seed(17)
 print("
Processing data for OLS...")
 train = pd.read_csv(".. /input/train_2016_v2.csv", parse_dates=["transactiondate"])
 properties = pd.read_csv('.. /input/properties_2016.csv')
 submission = pd.read_csv(".. /input/sample_submission.csv")
 print(len(train),len(properties),len(submission))
output = lgb_labels*0.50 + lasso_labels*0.25 + gbr_labels*0.25<save_to_csv>
def get_features(df):
df["transactiondate"] = pd.to_datetime(df["transactiondate"])
 df["transactiondate_year"] = df["transactiondate"].dt.year
df["transactiondate_month"] = df["transactiondate"].dt.month
df['transactiondate'] = df['transactiondate'].dt.quarter
df = df.fillna(-1.0)
 return df
def MAE(y, ypred):
return np.sum([abs(y[i]-ypred[i])for i in range(len(y)) ])/ len(y )
pd.DataFrame({'Id': test.Id, 'SalePrice': output} ).to_csv('submission.csv', index =False )<set_options>
train = pd.merge(train, properties, how='left', on='parcelid')
 y = train['logerror'].values
test = pd.merge(submission, properties, how='left', left_on='ParcelId', right_on='parcelid')
 properties = []
exc = [train.columns[c] for c in range(len(train.columns)) if train.dtypes[c] == 'O'] + ['logerror','parcelid']
col = [c for c in train.columns if c not in exc]
train = get_features(train[col])
 test['transactiondate'] = '2016-01-01'
test = get_features(test[col] )
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 %matplotlib inline
sns.set(style="whitegrid")
 warnings.filterwarnings("ignore")
 gc.collect()
SEED = 29082013
os.environ['PYTHONHASHSEED']=str(SEED)
 np.random.seed(SEED)
 rn.seed(SEED)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
print("
Fitting OLS...")
 reg = LinearRegression(n_jobs=-1)
 reg.fit(train, y); print('fit...')
 print(MAE(y, reg.predict(train)))
 train = []; y = []
test_dates = ['2016-10-01','2016-11-01','2016-12-01','2017-10-01','2017-11-01','2017-12-01']
test_columns = ['201610','201611','201612','201710','201711','201712']
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 display(train.head(10))
display(train.tail(10))
test = pd.read_csv('/kaggle/input/titanic/test.csv')
 display(test.head(10))
sub= pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 display(sub.head(3))<count_values>
print("
Combining XGBoost, LightGBM, NN, and baseline predicitons...")
 lgb_weight = 1 - XGB_WEIGHT - BASELINE_WEIGHT - NN_WEIGHT - OLS_WEIGHT
lgb_weight0 = lgb_weight /(1 - OLS_WEIGHT)
 xgb_weight0 = XGB_WEIGHT /(1 - OLS_WEIGHT)
 baseline_weight0 = BASELINE_WEIGHT /(1 - OLS_WEIGHT)
 nn_weight0 = NN_WEIGHT /(1 - OLS_WEIGHT)
 pred0 = 0
pred0 += xgb_weight0*xgb_pred
pred0 += baseline_weight0*BASELINE_PRED
pred0 += lgb_weight0*p_test
pred0 += nn_weight0*nn_pred
train['Survived'].value_counts(normalize=True, dropna=False )<count_missing_values>
print("
Combined XGB/LGB/NN/baseline predictions:")
 print(pd.DataFrame(pred0 ).head())
 print("
Predicting with OLS and combining with XGB/LGB/NN/baseline predicitons:...")
 for i in range(len(test_dates)) :
test['transactiondate'] = test_dates[i]
pred = FUDGE_FACTOR *(OLS_WEIGHT*reg.predict(get_features(test)) +(1-OLS_WEIGHT)*pred0)
 submission[test_columns[i]] = [float(format(x, '.4f')) for x in pred]
print('predict...', i)
 print("
Combined XGB/LGB/NN/baseline/OLS predictions:")
 print(submission.head() )
<count_missing_values><EOS> 
print("
Writing results to disk...")
 submission.to_csv('sub{}.csv'.format(datetime.now().strftime('%Y%m%d_%H%M%S')) , index=False)
 print("
Finished..." )