Kaggle competition: House Prices - Advanced Regression Techniques
model = Lasso(alpha=5.5)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-5.5.csv', index=False)
 test_predicted.describe() 
<save_to_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')
 frame =(train, test)
 df1 = pd.concat(frame, keys = ['train','test'] )
model = RandomForestRegressor(n_estimators=150)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-random-forest.csv', index=False)
 test_predicted.describe() 
<save_to_csv>
df1.corr().sum().sort_values(ascending=False )
model = Ridge(alpha=330)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-ridge-330.csv', index=False)
 test_predicted.describe() 
<save_to_csv>
df2 = df1.sort_values(by = 'GrLivArea')
 df3 = df2.isnull().sum().sort_values(ascending= False)
 df3 = df2.isnull().sum().sort_values(ascending= False)
 df3.head(40)
model = Lasso(alpha=200000)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-high-alpha.csv', index=False)
 test_predicted.describe() 
<feature_engineering>
for column in ['LotFrontage', 'GarageQual', 'GarageYrBlt', 'GarageFinish',
'GarageCond', 'GarageType', 'BsmtExposure', 'BsmtCond', 'BsmtQual',
'BsmtFinType2', 'BsmtFinType1', 'MasVnrType', 'MasVnrArea', 'MSZoning',
'Utilities', 'Functional', 'BsmtFullBath', 'BsmtHalfBath', 'GarageArea',
'BsmtFinSF2', 'Exterior1st', 'TotalBsmtSF', 'GarageCars', 'BsmtUnfSF',
'Electrical','BsmtFinSF1','KitchenQual','SaleType','Exterior2nd']:
df2[column].fillna(method='ffill',inplace=True )
data['Time_Open'] = round(data['Days_Open'] / 700, 0)
 data = data.drop('Days_Open', axis=1)
 test_data['Time_Open'] = round(test_data['Days_Open'] / 700, 0)
 test_data = test_data.drop('Days_Open', axis=1)
<prepare_x_and_y>
df3.index[0:6]
df2.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu'],axis=1, inplace=True )
X = data.drop(['Id', 'revenue'], axis=1)
 Y = data.revenue 
<save_to_csv>
columns1 = ['LotFrontage','LotArea','TotalBsmtSF','MasVnrArea']
df2[columns1].sort_values(by = columns1,ascending=False)[0:4]
df2.drop(df2[df2['Id'] == 1298].index, inplace = True)
 df2.drop(df2[df2['Id'] == 386].index, inplace = True)
 df2.drop(df2[df2['Id'] == 934].index, inplace = True)
 df2.drop(df2[df2['Id'] == 313].index, inplace = True)
model = Ridge(alpha=330)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission.csv', index=False)
 test_predicted<save_to_csv>
df2_dummy = pd.get_dummies(df2 )
model = Lasso(alpha=200000)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-.csv', index=False)
 test_predicted.describe() 
<load_from_csv>
Id = df2_dummy.loc['test']
dftest = df2_dummy.loc['test']
dftrain = df2_dummy.loc['train']
Id = Id.Id
df_train = pd.read_csv(f'{PATH}train.csv', parse_dates=['Open Date'])
 df_test = pd.read_csv(f'{PATH}test.csv', parse_dates=['Open Date'])
 df_joined = pd.concat([df_train.drop('revenue', axis=1), df_test], axis=0 )<compute_test_metric>
dftrain.dropna(axis = 0, inplace = True)
 dftest.drop(['SalePrice','Id'], axis =1, inplace=True )
def score(model, X_train, y_train, X_valid = [], y_valid = []):
rms = sqrt(mean_squared_error(np.square(np.exp(y_train)) , np.square(np.exp(model.predict(X_train)))))
 score = [rms, model.score(X_train, y_train)]
if len(X_valid)!= 0 and len(y_valid)!= 0:
score.append(sqrt(mean_squared_error(np.square(np.exp(y_valid)) , np.square(np.exp(model.predict(X_valid))))))
if model.oob_score:
score.append(model.oob_score_)
 return score
n_train = df_train.shape[0]
def prcs(df, fe=[]):
add_datepart(df, 'Open Date')
 drop_cols = ['Open Year', 'Open Month', 'Open Week', 'Open Day', 'Open Dayofweek',
'Open Dayofyear', 'Open Is_month_end', 'Open Is_month_start',
'Open Is_quarter_end', 'Open Is_quarter_start', 'Open Is_year_end',
'Open Is_year_start']
df = df.drop(drop_cols, axis=1)
 if 'city' in fe:
df = df.drop('City', axis=1)
 if 'MB' in fe:
df['Type'] = df['Type'].replace('MB', 'DT')
 if 'city_group' in fe:
df = df.drop('City Group', axis=1)
 if 'id' in fe:
df = df.drop('Id', axis=1)
 if 'dummies' in fe:
p_cols = [ f'P{n}' for n in range(1,38)]
df = pd.get_dummies(df, columns=p_cols)
 if 'city_group' not in fe:
df = pd.get_dummies(df, columns=['City Group'], drop_first=True)
 df = pd.get_dummies(df, columns=['Type'])
 if 'outlier' in fe:
df = df.drop(index=16, axis=0)
 train_cats(df)
 X, _, _ = proc_df(df, None)
 if 'scale_open' in fe:
X['Open Elapsed'] =(X['Open Elapsed']/1000 ).apply(np.log)
 X_train = X[:n_train]
X_test = X[n_train:]
return X_train, X_test<prepare_x_and_y>
dftrain_X = dftrain.drop(['SalePrice','Id'], axis = 1)
 dftrain_y = dftrain['SalePrice']
X_train, X_test = prcs(df_joined.copy())
 y_train = df_train['revenue'].copy().apply(np.log )<train_model>
xgboost = xgboost.XGBRegressor(learning_rate=0.05,
colsample_bytree = 0.5,
subsample = 0.8,
n_estimators=1000,
max_depth=5,
gamma=5)
 xgboost.fit(dftrain_X, dftrain_y)
<save_to_csv><EOS> 
predictions= xgboost.predict(dftest)
 solution = pd.DataFrame({"Id": Id, 'SalePrice': predictions})
 solution.to_csv('house_pred.csv', index=False )