Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<count_missing_values>
%matplotlib inline
warnings.filterwarnings("ignore", category=FutureWarning)
 warnings.filterwarnings("ignore", category=DeprecationWarning )
print('MARKET TRAIN:')
 for col in market_train.columns:
print(col+' has '+str(market_train[col].isna().sum())+' NAs' )<categorify>
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )
market_train['returnsClosePrevMktres1'] = market_train.groupby(['assetCode'])['returnsClosePrevMktres1'].transform(lambda x: x.fillna(x.median()))
market_train['returnsOpenPrevMktres1'] = market_train.groupby(['assetCode'])['returnsOpenPrevMktres1'].transform(lambda x: x.fillna(x.median()))
market_train['returnsClosePrevMktres10'] = market_train.groupby(['assetCode'])['returnsClosePrevMktres10'].transform(lambda x: x.fillna(x.median()))
market_train['returnsOpenPrevMktres10'] = market_train.groupby(['assetCode'])['returnsOpenPrevMktres10'].transform(lambda x: x.fillna(x.median()))
market_train = market_train.fillna(market_train.median() )<sort_values>
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )
market_train = market_train.sort_values(['assetCode','time'])
 market_train.reset_index(drop=True,inplace=True)
 market_train.head()<data_type_conversions>
fe_name = list(test)
 df_train = train[fe_name]
df = pd.concat(( df_train,test))
market_train['time'] = pd.to_datetime(market_train['time'].dt.date )<feature_engineering>
from scipy import stats
from scipy.stats import norm
market_train['margin1'] = market_train['open'] / market_train['close']
market_train['TARGET'] = np.sign(market_train['returnsOpenNextMktres10'] )<groupby>
target = train['SalePrice']
total_market_obs_df = [market_train.loc[(market_train['time'].dt.year >= 2016)&(market_train['time'].dt.month >= 9)].groupby('assetCode' ).tail(30 ).drop(['universe','returnsOpenNextMktres10'], axis=1)]<define_variables>
null_df =(df.isna().sum() / len(df)) *100
null_df = null_df.drop(null_df[null_df == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :null_df})
 missing_data.head(20 )
LSTM_COLUMNS_TO_USE = ['time',
'assetCode',
'universe',
'returnsOpenNextMktres10',
'TARGET',
'volume',
'returnsClosePrevRaw1', 'returnsOpenPrevRaw1',
'returnsClosePrevMktres1', 'returnsOpenPrevMktres1',
'returnsClosePrevRaw10', 'returnsOpenPrevRaw10',
'returnsClosePrevMktres10', 'returnsOpenPrevMktres10',
'margin1',
]<drop_column>
df["PoolQC"] = df["PoolQC"].fillna("None" )
market_train = market_train[LSTM_COLUMNS_TO_USE]<set_options>
df["MiscFeature"] = df["MiscFeature"].fillna("None" )
gc.collect()<define_variables>
df["Alley"] = df["Alley"].fillna("None" )
INFORMATION_COLS = ['time','assetCode','universe','returnsOpenNextMktres10','TARGET']
INPUT_COLS = [f for f in market_train.columns if f not in INFORMATION_COLS]<normalization>
df["Fence"] = df["Fence"].fillna("None" )
scaler = StandardScaler()
market_train[INPUT_COLS] = scaler.fit_transform(market_train[INPUT_COLS] )<categorify>
df["FireplaceQu"] = df["FireplaceQu"].fillna("None" )
def series_to_supervised(dataset, n_in=1):
data_X = []
data_time = []
data_assetCode = []
data_universe = []
data_returns = []
data_TARGET = []
for i in range(0, len(dataset)) :
data_time.append(dataset[i][0])
 data_assetCode.append(dataset[i][1])
 data_universe.append(dataset[i][2])
 data_returns.append(dataset[i][3])
 data_TARGET.append(dataset[i][4])
 to_append = np.append(np.zeros(shape=(max(0,n_in - 1 - i), 10)) ,(dataset[max(0, i - n_in+1):i+1, len(INFORMATION_COLS):]), axis=0)
 data_X.append(to_append)
 return data_X, data_time, data_assetCode, data_universe, data_returns, data_TARGET<define_variables>
df["LotFrontage"] = df.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
LOOK_BACK = 15<set_options>
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
df[col] = df[col].fillna('None' )
del market_train
gc.collect()<import_modules>
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
df[col] = df[col].fillna(0 )
from keras import callbacks<compute_train_metric>
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
df[col] = df[col].fillna(0 )
class Metrics(callbacks.Callback):
def on_train_begin(self, logs={}):
self._data = []
def on_epoch_end(self, batch, logs={}):
X_val, y_val = self.validation_data[0], self.validation_data[1]
y_predict =(pd.DataFrame(model.predict(X_val)) * 2)- 1
_sigmascore = sigma_scorelstm(y_val, y_predict)
 print(" — sigmascore: %f" %(_sigmascore))
self._data.append({
'val_sigmascore': _sigmascore
})
 return
def get_data(self):
return self._data
metrics = Metrics()<define_variables>
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
df[col] = df[col].fillna('None' )
train_index = [i for i in range(len(lstm_df_list)) ]<compute_test_metric>
df["MasVnrType"] = df["MasVnrType"].fillna("None")
 df["MasVnrArea"] = df["MasVnrArea"].fillna(0 )
def sigma_scorelstm(y_true, y_pred):
x_t_i = y_pred * pd.DataFrame([the_returns[i] for i in train_index])* pd.DataFrame([the_universe[i] for i in train_index])
 data = pd.concat([pd.DataFrame([the_time[i] for i in train_index]), x_t_i], axis=1)
 data.columns = ['day','x_t_i']
x_t = data.groupby('day' ).sum().values.flatten()
mean = np.mean(x_t)
 std = np.std(x_t)
 score_valid = mean / std
return score_valid<prepare_x_and_y>
df['MSZoning'] = df['MSZoning'].fillna(df['MSZoning'].mode() [0] )
trainX = np.array([lstm_df_list[i] for i in train_index])
 trainY =(np.array([the_TARGET[i] for i in train_index])+ 1)/ 2<train_on_grid>
df = df.drop(['Utilities'], axis=1 )
model = Sequential()
model.add(GRU(50, return_sequences=True, input_shape=(LOOK_BACK, trainX.shape[2])))
 model.add(SpatialDropout1D(0.5))
model.add(GlobalMaxPool1D())
 model.add(Dropout(0.50))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
 history = model.fit(trainX, trainY, epochs=2, batch_size=1028, validation_data=(trainX, trainY), verbose=2, shuffle=True, callbacks=[metrics] )<set_options>
df["Functional"] = df["Functional"].fillna("Typ" )
del trainX, trainY
gc.collect()<define_variables>
df['Electrical'] = df['Electrical'].fillna(df['Electrical'].mode() [0] )
days = env.get_prediction_days()<define_variables>
df['KitchenQual'] = df['KitchenQual'].fillna(df['KitchenQual'].mode() [0] )
LSTM_COLUMNS_TO_USE = [col for col in LSTM_COLUMNS_TO_USE if(col!='universe' and col!='returnsOpenNextMktres10' and col!='TARGET')]<drop_column>
df['Exterior1st'] = df['Exterior1st'].fillna(df['Exterior1st'].mode() [0])
 df['Exterior2nd'] = df['Exterior2nd'].fillna(df['Exterior2nd'].mode() [0] )
total_market_obs_df[0].drop('TARGET', axis=1, inplace=True )<categorify>
df['SaleType'] = df['SaleType'].fillna(df['SaleType'].mode() [0] )
def series_to_supervised(dataset, curdate, n_in=1):
data_X = []
for i in range(0, len(dataset)) :
if dataset[i][0]==curdate:
to_append = np.append(np.zeros(shape=(max(0,n_in - 1 - i), 10)) ,(dataset[max(0, i - n_in+1):i+1, 2:]), axis=0)
 data_X.append(to_append)
 return data_X 
<data_type_conversions>
df['MSSubClass'] = df['MSSubClass'].fillna("None" )
for(market_obs_df, _, predictions_template_df)in days:
market_obs_df = market_obs_df.fillna(market_obs_df.median())
 market_obs_df = market_obs_df.sort_values('assetCode')
 market_obs_df['time'] = pd.to_datetime(market_obs_df['time'].dt.date)
 market_obs_df['margin1'] = market_obs_df['open'] / market_obs_df['close']
total_market_obs_df.append(market_obs_df)
 history_df = pd.concat(total_market_obs_df[-(np.max(30)+1):])
 tmp = history_df[LSTM_COLUMNS_TO_USE]
tmp = tmp.fillna(tmp.median())
 tmp[INPUT_COLS] = scaler.fit_transform(tmp[INPUT_COLS])
 lstm_df_list = np.empty(shape=(predictions_template_df.shape[0],LOOK_BACK,10))
row_at = 0
for asset in market_obs_df['assetCode'].unique() :
res = series_to_supervised(tmp.loc[tmp['assetCode']==asset].values, curdate=tmp['time'].max() , n_in=LOOK_BACK)
 lstm_df_list[row_at] = np.array(res)
 row_at = row_at + 1
trainX = np.array([lstm_df_list[i] for i in range(len(lstm_df_list)) ])
 yhat_lstm = model.predict(trainX)
 yhat_lstm = yhat_lstm.flatten()
yhat_lstm = pd.DataFrame(yhat_lstm)
 preds =(yhat_lstm * 2)- 1
predictions_template_df['confidenceValue'] = preds
env.predict(predictions_template_df )<save_to_csv>
null_df =(df.isna().sum() / len(df)) *100
null_df = null_df.drop(null_df[null_df == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :null_df})
 missing_data.head(20 )
env.write_submission_file()<train_model>
df['MSSubClass'] = df['MSSubClass'].apply(str)
 df['OverallCond'] = df['OverallCond'].astype(str)
 df['YrSold'] = df['YrSold'].astype(str)
 df['MoSold'] = df['MoSold'].astype(str )
env = twosigmanews.make_env() 
(market_train, news_train)= env.get_training_data()<filter>
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:
lb = LabelEncoder()
lb.fit(list(df[c].values))
df[c] = lb.transform(list(df[c].values))
market_train = market_train[market_train.returnsOpenNextMktres10<1][market_train.returnsOpenNextMktres10>-1]<feature_engineering>
df['TotalSF'] =(df['TotalBsmtSF']
+ df['1stFlrSF']
+ df['2ndFlrSF'])
 df['YrBltAndRemod'] = df['YearBuilt'] + df['YearRemodAdd']
df['Total_sqr_footage'] =(df['BsmtFinSF1']
+ df['BsmtFinSF2']
+ df['1stFlrSF']
+ df['2ndFlrSF'] 
)
 df['Total_Bathrooms'] =(df['FullBath']
+(0.5 * df['HalfBath'])
 + df['BsmtFullBath']
+(0.5 * df['BsmtHalfBath'])
)
 df['Total_porch_sf'] =(df['OpenPorchSF']
+ df['3SsnPorch']
+ df['EnclosedPorch']
+ df['ScreenPorch']
+ df['WoodDeckSF']
)
def calcRsi(series, period = 14):

 try:
delta = series.diff().dropna()
u = delta * 0
d = u.copy()
u[delta > 0] = delta[delta > 0]
d[delta < 0] = -delta[delta < 0]
u[u.index[period-1]] = np.mean(u[:period])
 u = u.drop(u.index[:(period-1)])
 d[d.index[period-1]] = np.mean(d[:period])
 d = d.drop(d.index[:(period-1)])
 rs = u.ewm(com=period-1, adjust=False ).mean() \
/ d.ewm(com=period-1, adjust=False ).mean()
rsi = 100 - 100 /(1 + rs)
 except IndexError:
rsi = 0
return rsi
def addBollinger(df, period=20, col='close'):

 bbmid_series = df[col].rolling(window=period ).mean()
series_stdev = df[col].rolling(window=period ).std()
df['BBUpperBand'] = bbmid_series + 2*series_stdev
df['BBLowerBand'] = bbmid_series - 2*series_stdev
df['BBBandwidth'] = df['BBUpperBand'] - df['BBLowerBand']
df['BBMiddleBand'] = bbmid_series
return df
def addMACD(df):
ema_fast = df['close'].ewm(span=12 ).mean()
ema_slow = df['close'].ewm(span=26 ).mean()
signal_line = df['close'].ewm(span=9 ).mean()
df['macd'] = ema_fast - ema_slow
df['macd_signal'] = df.macd.ewm(span=9, adjust=False ).mean()
df['macdh'] = df['macd'] - df['macd_signal']
return df
def allindicator(market_train):
market_train['RSI'] = calcRsi(market_train['close'], 14)
 market_train = addBollinger(market_train)
 market_train = addMACD(market_train)
 return market_train
def EMA(df):
df = df.groupby('time')
 EMA_7 = df['close'].ewm(span=7 ).mean()
return df
def groupAsset(market_train):
all_df = []
df_codes = market_train.groupby('assetCode')
 df_codes = [df_code[1] for df_code in df_codes]
pool = Pool(4)
 all_df = pool.map(allindicator, df_codes)
 new_df = pd.concat(all_df)
 pool.close()
del all_df, df_codes, market_train
return new_df<feature_engineering>
df['haspool'] = df['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df['has2ndfloor'] = df['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hasgarage'] = df['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 df['hasbsmt'] = df['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hasfireplace'] = df['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
def data_prep(market_train,news_train):
market_train['formatdate']=market_train['time'].dt.date
news_train['formatdate'] = news_train.firstCreated.dt.date
for col_cat in ['headlineTag', 'provider', 'sourceId']:
news_train[col_cat], uniques = pd.factorize(news_train[col_cat])
 del uniques
news_train_sim = news_train.drop(columns=['time','sourceTimestamp','firstCreated','headline','subjects','audiences','assetCodes'])
 news_train_1 = news_train_sim.groupby(by=['assetName','formatdate'],as_index=False,observed=True ).mean().add_suffix('_mean')
 news_train_1.rename(columns={'assetName_mean': 'assetName', 'formatdate_mean': 'formatdate'}, inplace=True)
 merge_train = pd.merge(market_train, news_train_1, how='left', left_on=['formatdate', 'assetName'],
right_on=['formatdate', 'assetName'])
 del market_train, news_train_sim, news_train_1
return merge_train<merge>
stats.pearsonr(train[li_num[11]],target )
merge_train = data_prep(market_train,news_train)
 df_main = merge_train[['time','assetCode','close']]
df_main = groupAsset(df_main)
 merge_train = pd.merge(merge_train, df_main, how='left',on=['time','assetCode','close'])
 df_main = df_main[['time','assetCode','close']]
test123 = merge_train[['time','close','volume']]
test123['cap']=test123['close']*test123['volume']
testabc = test123.groupby('time' ).sum()
testabc['wholemarket_index']=testabc['cap']/testabc['volume']
testabc.reset_index(inplace=True)
 for i in range(len(testabc)-1):
testabc.loc[i+1,'changeWMI'] =(testabc.loc[i+1,'wholemarket_index']-testabc.loc[i,'wholemarket_index'])/(testabc.loc[i,'wholemarket_index'])
 testabc = testabc[['time','changeWMI']]
merge_train = pd.merge(merge_train, testabc, how='left',on=['time'])
 test123 = merge_train[['time','close','volume']]
merge_train.fillna(0,inplace=True)
 up = merge_train.returnsOpenNextMktres10 >= 0
up = up.values
fcol = [c for c in merge_train.columns if c not in ['assetCode','assetName','time',
'returnsOpenNextMktres10', 'formatdate', 'universe','close','open']]
X = merge_train[fcol].values
r = merge_train.returnsOpenNextMktres10.values
mins = np.min(X, axis=0)
 maxs = np.max(X, axis=0)
 rng = maxs - mins
X = 1 -(( maxs - X)/ rng )<split>
strong_num = ['OverallQual','YearBuilt','YearRemodAdd','TotalBsmtSF','1stFlrSF',
'FullBath','TotRmsAbvGrd','GarageYrBlt','GarageCars','GrLivArea']
X_train, X_test, up_train, up_test, r_train, r_test = model_selection.train_test_split(X, up, r, test_size=0.1, random_state=99)
<train_model>
strong_obj = [ 'MSZoning', 'Neighborhood', 'Condition2', 'MasVnrType', 'ExterQual',
'BsmtQual','CentralAir', 'Electrical', 'KitchenQual', 'SaleType']
params = {'learning_rate': 0.15, 'max_depth': 20, 'num_leaves':512, 'boosting': 'gbdt', 'objective': 'binary', 'metric': 'binary_logloss', 'is_training_metric': True, 'seed': 42}
model = lgb.train(params, train_set=lgb.Dataset(X_train, label=up_train), num_boost_round=100,
valid_sets=[lgb.Dataset(X_train, label=up_train), lgb.Dataset(X_test, label=up_test)],
verbose_eval=50, early_stopping_rounds=30 )<merge>
numeric_features = df.dtypes[df.dtypes != "object"].index
df_main = df_main[df_main.time>'2016-12-01']
n_days = 0
for(market_obs_df, news_obs_df, predictions_template_df)in env.get_prediction_days() :
n_days +=1
if n_days % 2 == 0:
print(n_days,end=' ')
 market_obs_df = data_prep(market_obs_df, news_obs_df)
 df_days = market_obs_df[['time','assetCode','close']]
df_main = df_main.append(df_days)
 df_main.sort_values(by=['time'],inplace=True)
 df_main.reset_index(inplace=True,drop=True)
 df_main = groupAsset(df_main)
 market_obs_df = pd.merge(market_obs_df, df_main, how='left',on=['time','assetCode','close'])
 df_main = df_main[['time','assetCode','close']]
test456 = market_obs_df[['time','close','volume']]
test123 = test123.append(test456)
 test123['cap']=test123['close']*test123['volume']
testabc = test123.groupby('time' ).sum()
testabc['wholemarket_index']=testabc['cap']/testabc['volume']
testabc.reset_index(inplace=True)
 for i in range(len(testabc)-1):
testabc.loc[i+1,'changeWMI'] =(testabc.loc[i+1,'wholemarket_index']-testabc.loc[i,'wholemarket_index'])/(testabc.loc[i,'wholemarket_index'])
 testabc = testabc[['time','changeWMI']]
market_obs_df = pd.merge(market_obs_df, testabc, how='left',on=['time'])
 test123 = test123[['time','close','volume']]
market_obs_df.fillna(0,inplace=True)
 X_live = market_obs_df[fcol].values
X_live = 1 -(( maxs - X_live)/ rng)
 lp = model.predict(X_live)
 confidence = 2 * lp -1
preds = pd.DataFrame({'assetCode':market_obs_df['assetCode'],'confidence':confidence})
 predictions_template_df = predictions_template_df.merge(preds,how='left' ).drop('confidenceValue',axis=1 ).fillna(0 ).rename(columns={'confidence':'confidenceValue'})
 env.predict(predictions_template_df)
<save_to_csv>
skewness = df[numeric_features].apply(lambda x: skew(x.dropna())).sort_values(ascending=False )
env.write_submission_file()<set_options>
high_skewness = skewness[abs(skewness)> 0.5]
skew_feat = high_skewness.index
env = twosigmanews.make_env()
print('Done!' )<prepare_x_and_y>
df[['MiscVal', 'PoolArea', 'haspool', 'LotArea', 'LowQualFinSF',
'3SsnPorch', 'LandSlope', 'KitchenAbvGr', 'BsmtFinSF2', 'EnclosedPorch',
'ScreenPorch', 'BsmtHalfBath', 'MasVnrArea', 'OpenPorchSF',
'WoodDeckSF', 'Total_porch_sf', '1stFlrSF', 'Total_sqr_footage',
'LotFrontage', 'GrLivArea', 'TotalSF', 'BsmtFinSF1', 'BsmtUnfSF',
'2ndFlrSF', 'TotRmsAbvGrd', 'Fireplaces', 'HalfBath', 'TotalBsmtSF',
'BsmtFullBath', 'OverallCond', 'YearBuilt', 'GarageFinish', 'LotShape',
'MoSold', 'Alley', 'BsmtExposure', 'KitchenQual', 'ExterQual', 'Fence',
'ExterCond', 'BsmtCond', 'PavedDrive', 'BsmtFinType2', 'GarageQual',
'CentralAir', 'GarageCond', 'GarageYrBlt', 'hasgarage', 'Functional',
'hasbsmt', 'Street', 'PoolQC']].head(3 )
( marketdf, newsdf)= env.get_training_data()<drop_column>
lam = 0.15
for feat in skew_feat:
df[feat] = boxcox1p(df[feat], lam )
def preprocess(marketdf, newsdf,encode=None):
news=newsdf.drop(['sourceTimestamp', 'firstCreated', 'sourceId', 'headline','takeSequence','provider', 'subjects', 'audiences', 'headlineTag'],axis=1)
 news['sentiment_v']=(abs(news['sentimentNegative']-news['sentimentPositive'])) *news['sentimentWordCount']/news['wordCount']
news_1=news[['time', 'urgency','companyCount','assetName','relevance', 'sentimentClass','sentiment_v','noveltyCount12H', 'noveltyCount24H',
'noveltyCount3D', 'noveltyCount5D', 'noveltyCount7D', 'volumeCounts12H',
'volumeCounts24H', 'volumeCounts3D', 'volumeCounts5D', 'volumeCounts7D']]
news_1['time'] =(news_1['time'] - np.timedelta64(22,'h')).dt.ceil('1D')
 marketdf['time'] = marketdf['time'].dt.floor('1D')
 com=pd.merge(marketdf, news_1, how='left', on=['time', 'assetName'])
 com_1=com.groupby(['time','assetCode'], sort=False ).aggregate(np.mean ).reset_index()
com_1=com_1.fillna(0)
 if encode==None:
encode={name:id_asset for id_asset,name in enumerate(com_1.assetCode.astype(str ).unique())}
com_2=com_1.assetCode.apply(lambda x :get_encoder(name=x,encode=encode))
com_1.assetCode=com_2
return com_1,encode
def get_encoder(name,encode):
if name in encode.keys() :
return encode[name]
else:
return 5
com_1,encode = preprocess(marketdf, newsdf)
<prepare_x_and_y>
df[['MiscVal', 'PoolArea', 'haspool', 'LotArea', 'LowQualFinSF',
'3SsnPorch', 'LandSlope', 'KitchenAbvGr', 'BsmtFinSF2', 'EnclosedPorch',
'ScreenPorch', 'BsmtHalfBath', 'MasVnrArea', 'OpenPorchSF',
'WoodDeckSF', 'Total_porch_sf', '1stFlrSF', 'Total_sqr_footage',
'LotFrontage', 'GrLivArea', 'TotalSF', 'BsmtFinSF1', 'BsmtUnfSF',
'2ndFlrSF', 'TotRmsAbvGrd', 'Fireplaces', 'HalfBath', 'TotalBsmtSF',
'BsmtFullBath', 'OverallCond', 'YearBuilt', 'GarageFinish', 'LotShape',
'MoSold', 'Alley', 'BsmtExposure', 'KitchenQual', 'ExterQual', 'Fence',
'ExterCond', 'BsmtCond', 'PavedDrive', 'BsmtFinType2', 'GarageQual',
'CentralAir', 'GarageCond', 'GarageYrBlt', 'hasgarage', 'Functional',
'hasbsmt', 'Street', 'PoolQC']].head(3 )
col=[i for i in com_1.columns if i not in ['returnsOpenNextMktres10','universe']]
target='returnsOpenNextMktres10'
train_r=com_1[target]
train_data=com_1[col]
train_r=np.clip(train_r,a_min=-1,a_max=1)
 def get_train_x(train_data):
x=dict()
x['Code']=train_data['assetCode']
x['num']=train_data.iloc[:,2:].values
return x
X=get_train_x(train_data)
<categorify>
df = pd.get_dummies(df)
 print(df.shape )
def create_multi(r_train):
y=copy.deepcopy(r_train)
 y1=pd.Series(copy.deepcopy(y))
z=pd.Series([0]*len(y))
z[(y<y1.quantile(0.1)).tolist() ]=1
z[(( y1.quantile(0.1)<=y)&(y<y1.quantile(0.2)) ).tolist() ]=2
z[(( y1.quantile(0.2)<=y)&(y<y1.quantile(0.3)) ).tolist() ]=3
z[(( y1.quantile(0.3)<=y)&(y<y1.quantile(0.4)) ).tolist() ]=4
z[(( y1.quantile(0.4)<=y)&(y<y1.quantile(0.5)) ).tolist() ]=5
z[(( y1.quantile(0.5)<=y)&(y<y1.quantile(0.6)) ).tolist() ]=6
z[(( y1.quantile(0.6)<=y)&(y<y1.quantile(0.7)) ).tolist() ]=7
z[(( y1.quantile(0.7)<=y)&(y<y1.quantile(0.8)) ).tolist() ]=8
z[(( y1.quantile(0.8)<=y)&(y<y1.quantile(0.9)) ).tolist() ]=9
z[(y1.quantile(0.9)<=y ).tolist() ]=10
return z
cata_train_r=create_multi(train_r )<categorify>
new_train = df[:train.shape[0]]
new_test = df[train.shape[0]:]
encoded_z = to_categorical(cata_train_r)
 encoded_z=encoded_z[:,1:] 
<choose_model_class>
new_train = pd.concat([new_train,target], axis=1, sort=False )
cata_input=Input(shape=[1], name='Code')
 embed=Embedding(input_dim=len(encode.keys()), output_dim=30, input_length=1 )(cata_input)
 categorical_logits = Flatten()(embed)
 categorical_logits = Dense(32,activation='relu' )(categorical_logits)
 numerical_inputs = Input(shape=(26,), name='num')
 numerical_logits = numerical_inputs
numerical_logits = BatchNormalization()(numerical_logits)
 numerical_logits = Dense(128,activation='relu' )(numerical_logits)
 numerical_logits = Dense(64,activation='relu' )(numerical_logits)
 numerical_logits=Dropout(0.5 )(numerical_logits)
 numerical_logits = Dense(32,activation='relu' )(numerical_logits)
 logits = Concatenate()([numerical_logits,categorical_logits])
 logits = Dense(64,activation='relu' )(logits)
 logits=Dropout(0.5 )(logits)
 logits = Dense(32,activation='relu' )(logits)
 logits=Dropout(0.5 )(logits)
 logits = Dense(16,activation='relu' )(logits)
 out = Dense(10,activation='sigmoid' )(logits)
 model = Model(inputs = [cata_input] + [numerical_inputs], outputs=out)
 model.compile(optimizer='adam',loss=binary_crossentropy )<train_model>
col_corr_dict = corr_new_train['SalePrice'].sort_values(ascending=False ).to_dict()
check_point = ModelCheckpoint('model.hdf_4p',verbose=True, save_best_only=True)
 early_stop = EarlyStopping(patience=5,verbose=True)
 model.fit(X,encoded_z,
epochs=2,
verbose=True,
callbacks=[early_stop,check_point] )<categorify>
best_columns=[]
for key,value in col_corr_dict.items() :
if(( value>=0.33)&(value<0.9)) |(value<=-0.325):
best_columns.append(key)
 print(len(best_columns))
y=pd.Series(copy.deepcopy(train_r))
a=y.quantile(np.arange(0.1,1,0.1 ).tolist())
 def decode_pre(d1):
decode_z_valid_pre=[np.argmax(d1[i,:])+1 for i in range(d1.shape[0])]
q_list=[]
for i in range(len(a)+1):
if i==0:
tmp=(a.iloc[0]-a.iloc[1])/2+a.iloc[0]
elif i==(len(a)) :
tmp=(a.iloc[-1]-a.iloc[-2])/2+a.iloc[-1]
else:
tmp=(a.iloc[i-1]+a.iloc[i])/2
q_list.append(tmp)
 pre=pd.Series(decode_z_valid_pre)
 pre=pre.replace([i+1 for i in range(len(a)+1)],q_list)
 return pre<define_variables>
new_train = new_train.drop(['SalePrice'], axis=1)
 new_train = new_train.drop(['Id'], axis=1)
 new_test = new_test.drop(['Id'], axis=1 )
days = env.get_prediction_days()<predict_on_test>
from sklearn.model_selection import KFold, cross_val_score, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.linear_model import ElasticNet, Lasso, LinearRegression
for(market_obs_df, news_obs_df, predictions_template_df)in days:
com,_=preprocess(market_obs_df, news_obs_df,encode=encode)
 X=get_train_x(com[col])
 y=model.predict(X)
 pre=decode_pre(y)
 predictions_template_df.confidenceValue=pre
env.predict(predictions_template_df)
<save_to_csv>
from sklearn.preprocessing import RobustScaler
env.write_submission_file()<find_best_params>
rbst_scaler=RobustScaler()
X_rbst=rbst_scaler.fit_transform(new_train)
 test_rbst=rbst_scaler.transform(new_test )
env = twosigmanews.make_env()
days = env.get_prediction_days()<feature_engineering>
import statsmodels.api as sm
all_asset_close = dict()
WINDOW_LONG = 30
WINDOW_SHORT = 5
for dayCount,(market_obs_df, news_obs_df, template_df)in enumerate(days):
for index, row in market_obs_df.iterrows() :
asset = row['assetCode']
if asset not in all_asset_close:
all_asset_close[asset] = []
else:
all_asset_close[asset].append(row['close'])
 if len(all_asset_close[asset])< WINDOW_LONG:
template_df.loc[index, 'confidenceValue'] = 0.0
else:
sma_short = sum(all_asset_close[asset][-1*WINDOW_SHORT:])/ float(WINDOW_SHORT)
 sma_long = sum(all_asset_close[asset][-1*WINDOW_LONG:])/ float(WINDOW_LONG)
 template_df.loc[index, 'confidenceValue'] = 1.0 if sma_short > sma_long else -1.0
env.predict(template_df)
 if dayCount % 50 == 0:
print('day ', dayCount+1)
<save_to_csv>
model = sm.OLS(target.values, new_train )
env.write_submission_file()<set_options>
re = model.fit()
sns.set_style('whitegrid')
<load_from_csv>
vif = pd.DataFrame()
vif['Features'] = new_train.columns
vif['vif'] = [variance_inflation_factor(
 new_train.values, i)for i in range(new_train.shape[1])]
train_df = pd.read_csv('.. /input/train.csv')
 print('Rows: ',train_df.shape[0],'Columns: ',train_df.shape[1])
 train_df.info()<count_values>
vif.sort_values(by='vif',ascending=False)[165:190]
train_df['target'].value_counts()<load_from_csv>
rbst_scaler=RobustScaler()
X_rbst=rbst_scaler.fit_transform(new_train)
 test_rbst=rbst_scaler.transform(new_test )
test_df = pd.read_csv('.. /input/test.csv' )<drop_column>
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold
X_test = test_df.drop('ID_code',axis=1 )<prepare_x_and_y>
kfold = KFold(n_splits=4 )
X = train_df.drop(['ID_code','target'],axis=1)
 y = train_df['target']<choose_model_class>
random_state = 1
reg = []
reg.append(Lasso(random_state = random_state))
reg.append(ElasticNet(random_state = random_state))
reg.append(RandomForestRegressor(random_state=random_state))
reg.append(GradientBoostingRegressor(random_state=random_state))
reg.append(XGBRegressor(silent=True,random_state=random_state))
reg.append(LGBMRegressor(verbose_eval=False,random_state = random_state))
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )<init_hyperparams>
reg_results = []
for regre in reg :
reg_results.append(np.mean(np.sqrt(-cross_val_score(regre, X_rbst, y = target,scoring = 'neg_mean_squared_error',
cv = kfold, n_jobs=-4))))
params = {'num_leaves': 8,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4}<train_model>
reg_re = pd.DataFrame({"CrossValMeans":reg_means,"CrossValerrors": reg_std})
 reg_re
prediction = np.zeros(len(X_test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/5<train_model>
GBC = GradientBoostingRegressor()
gb_param_grid = {'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold, scoring="neg_mean_squared_error", n_jobs= 4, verbose = 1)
 gsGBC.fit(X_rbst,target)
 GBC_best = gsGBC.best_estimator_
gsGBC.best_score_
train_pool = Pool(X,y)
 m = CatBoostClassifier(iterations=300,eval_metric="AUC", boosting_type = 'Ordered')
 m.fit(X,y,silent=True)
 y_pred1 = m.predict(X_test)
 m.best_score_<save_to_csv>
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )<save_to_csv>
XGB = XGBRegressor()
xgb_param_grid = {'learning_rate': [1,0.1,0.01,0.001],
'n_estimators': [50, 100, 200, 500, 1000],
'max_depth' : [1,3,5,10,50]}
gsXGB = GridSearchCV(XGB,param_grid = xgb_param_grid, cv=kfold, scoring="neg_mean_squared_error", n_jobs= 4, verbose = 1)
 gsXGB.fit(X_rbst,target)
 XGB_best = gsXGB.best_estimator_
gsXGB.best_score_
sub1 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub1["target"] = y_pred1
sub1.to_csv("submission1.csv", index=False )<save_to_csv>
LGB = LGBMRegressor()
lgb_param_grid = {
'num_leaves' : [1,5,10],
'learning_rate': [1,0.1,0.01,0.001],
'n_estimators': [50, 100, 200, 500, 1000,5000],
'max_depth': [15,20,25],
'num_leaves': [50, 100, 200],
'min_split_gain': [0.3, 0.4],
}
gsLGB = GridSearchCV(LGB,param_grid = lgb_param_grid, cv=kfold, scoring="neg_mean_squared_error", n_jobs= 4, verbose = 1)
 gsLGB.fit(X_rbst,target)
 LGB_best = gsLGB.best_estimator_
gsLGB.best_score_
sub2 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub2["target"] =(prediction + y_pred1)/2
sub2.to_csv("submission2.csv", index=False )<set_options>
ensemble = np.expm1(0.1*test_Survived_GBC + 0.8*test_Survived_XGB + 0.1*test_Survived_LGB)
 submission = pd.DataFrame({
"Id" :test['Id'],
"SalePrice": ensemble
})
 submission.head()
warnings.filterwarnings('ignore' )<load_from_csv>
from sklearn.ensemble import VotingRegressor
%%time
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )<count_missing_values>
votingC = VotingRegressor(estimators=[('XGB', XGB_best),('LGB', LGB_best),('GBC',GBC_best)], n_jobs=4)
 votingC = votingC.fit(X_rbst, target )
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))<count_missing_values>
test_SalePrice = pd.Series(votingC.predict(test_rbst), name="SalePrice" )
%%time
missing_data(train_df )<count_missing_values>
submission = pd.DataFrame({
"Id" :test['Id'],
"SalePrice": np.expm1(test_SalePrice)
 })
 submission.head()
%%time
missing_data(test_df )<count_values>
from mlxtend.regressor import StackingRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.utils.testing import ignore_warnings
print("There are {}% target values with 1".format(100 * train_df["target"].value_counts() [1]/train_df.shape[0]))<sort_values>
params = {'meta_regressor__C': [0.1, 1.0, 10.0, 100.0],
'use_features_in_secondary' : [True, False]}
%%time
correlations = train_df[features].corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.head(10 )<count_unique_values>
clf1 = XGB_best
clf2 = LGB_best
clf3 = GBC_best
lr = LogisticRegression()
st_re= StackingRegressor(regressors=[clf1, clf2, clf3], meta_regressor=RandomForestRegressor())
 st_mod = st_re.fit(X_rbst, target)
 st_pred = st_mod.predict(test_rbst )
%%time
features = train_df.columns.values[2:202]
unique_max_train = []
unique_max_test = []
for feature in features:
values = train_df[feature].value_counts()
unique_max_train.append([feature, values.max() , values.idxmax() ])
 values = test_df[feature].value_counts()
unique_max_test.append([feature, values.max() , values.idxmax() ] )<feature_engineering>
submission = pd.DataFrame({
"Id" :test['Id'],
"SalePrice": np.expm1(st_pred)
 })
 submission.head()
%%time
idx = features = train_df.columns.values[2:202]
for df in [test_df, train_df]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurtosis(axis=1)
 df['med'] = df[idx].median(axis=1 )<train_model>
kfolds = KFold(n_splits=10, shuffle=True, random_state=42 )
print('Train and test columns: {} {}'.format(len(train_df.columns), len(test_df.columns)) )<define_variables>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def cv_rmse(model, X=new_train):
rmse = np.sqrt(-cross_val_score(model, X_rbst, target,
scoring="neg_mean_squared_error",
cv=kfolds))
return(rmse )
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<init_hyperparams>
alphas_ridge = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas_lasso = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
alphas_enect = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
enect_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<train_model>
from sklearn.linear_model import ElasticNetCV, LassoCV, RidgeCV
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVR
from mlxtend.regressor import StackingCVRegressor
folds = StratifiedKFold(n_splits=10, shuffle=False, random_state=44000)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 1000000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3000)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )<save_to_csv>
ridge = make_pipeline(RobustScaler() ,
RidgeCV(alphas=alphas_ridge, cv=kfolds))
sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = predictions
sub_df.to_csv("submission.csv", index=False )<load_from_csv>
lasso = make_pipeline(RobustScaler() ,
LassoCV(max_iter=1e7, alphas=alphas_lasso,
random_state=2, cv=kfolds))
train=pd.read_csv(".. /input/train.csv",index_col="ID_code")
 test=pd.read_csv(".. /input/test.csv",index_col="ID_code")
 target=train.target
train=train.drop("target",axis=1 )<import_modules>
enet = make_pipeline(RobustScaler() ,
ElasticNetCV(max_iter=1e7, alphas=alphas_enect,
cv=kfolds, l1_ratio=enect_l1ratio))
from sklearn.ensemble import AdaBoostClassifier
from sklearn import metrics
from sklearn.model_selection import KFold
import time<import_modules>
svr = make_pipeline(RobustScaler() ,
SVR(C= 20, epsilon= 0.008, gamma=0.0003,))
import lightgbm as lgb<init_hyperparams>
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, enet,
GBC_best, XGB_best, LGB_best),
meta_regressor=XGB_best,
use_features_in_secondary=True )
<train_model>
score = cv_rmse(ridge)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() ,score.std()))
score = cv_rmse(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(enet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(svr)
 print("SVR score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(GBC_best)
 print("Lightgbm score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(XGB_best)
 print("GradientBoosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(LGB_best)
 print("Xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
def train_model(params,train=train,target=target,test=test,samples=-1):
kford=KFold(n_splits=5,random_state=2,shuffle=True)
 start_time=time.time()
aucs=[]
test_preds=[]
if samples<=-1:
samples=train.shape[0]
else:
samples=min(train.shape[0],samples)
 print("
print("
print("fit on {} samples".format(samples))
for ford,(train_idx,val_idx)in enumerate(kford.split(train[:samples],target[:samples])) :
print("
print("
sample_x=train.iloc[train_idx].values
sample_y=target.iloc[train_idx].values
sample_val_x=train.iloc[val_idx].values
sample_val_y=target.iloc[val_idx].values
train_dataset=lgb.Dataset(data=sample_x,label=sample_y)
 val_dataset=lgb.Dataset(data=sample_val_x,label=sample_val_y)
 ford_time=time.time()
clf=lgb.train(params,train_dataset,valid_sets=[train_dataset,val_dataset],verbose_eval=50)
 print("epoch cost time {:1}s".format(time.time() -ford_time))
y_pred_prob=clf.predict(sample_x,clf.best_iteration)
 y_val_pred_prob=clf.predict(sample_val_x,clf.best_iteration)
 train_auc=metrics.roc_auc_score(sample_y,y_pred_prob)
 val_auc=metrics.roc_auc_score(sample_val_y,y_val_pred_prob)
 print("train auc:{:4},val auc:{:4}".format(train_auc,val_auc))
aucs.append([train_auc,val_auc])
 test_preds.append(clf.predict(test,clf.best_iteration))
end_time=time.time()
val_aucs=[auc[1] for auc in aucs]
print("using {} samples,total time:{:1}s,mean val auc:{:4}".format(samples,end_time-start_time,np.mean(val_aucs)))
 test_preds=pd.DataFrame(test_preds ).T
test_preds.index=test.index
return test_preds<train_model>
stack_gen_model = stack_gen.fit(np.array(X_rbst), np.array(target))
elastic_model_full_data = enet.fit(X_rbst, target)
 lasso_model_full_data = lasso.fit(X_rbst, target)
 ridge_model_full_data = ridge.fit(X_rbst, target)
 svr_model_full_data = svr.fit(X_rbst, target)
 gbr_model_full_data = GBC_best.fit(X_rbst, target)
 xgb_model_full_data = XGB_best.fit(X_rbst, target)
 lgb_model_full_data = LGB_best.fit(X_rbst, target )
params={
"metric":"auc",
"num_threads":8,
"object":"binary",
"boosting":"gbdt",
"num_iterations":10000,
"learning_rate":0.03,
"num_leaves":32,
"max_depth":2,
"early_stopping_round":100,
"lambda_l2":1,
"min_data_in_leaf":2000,
"bagging_fraction":0.9,
"feature_fraction":0.9
}
test_preds=train_model(params,samples=-1 )<save_to_csv>
def blend_models_predict(X):
return(( 0.1 * elastic_model_full_data.predict(X)) + \ 
(0.1 * lasso_model_full_data.predict(X)) + \ 
(0.1 * ridge_model_full_data.predict(X)) + \ 
(0.1 * svr_model_full_data.predict(X)) + \ 
(0.1 * gbr_model_full_data.predict(X)) + \ 
(0.15 * xgb_model_full_data.predict(X)) + \ 
(0.1 * lgb_model_full_data.predict(X)) + \ 
(0.25 * stack_gen_model.predict(np.array(X))))
submission=pd.DataFrame(test_preds.mean(axis=1),columns=["target"])
 submission.head()
submission.to_csv("submission.csv" )<load_from_csv>
pred = np.floor(np.expm1(blend_models_predict(test_rbst)))
 submission = pd.DataFrame({
"Id" :test['Id'],
"SalePrice": pred
})
 submission.head()
<count_missing_values><EOS> 
submission.to_csv('final_submission.csv', index=False )