Kaggle competition: House Prices - Advanced Regression Techniques
scaler = preprocessing.RobustScaler()
all_data.loc[:,numcols] = scaler.fit_transform(all_data[numcols] )<concatenate>
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.preprocessing import PowerTransformer
mean1 = all_data[all_data['target']==1].mean()
mean0 = all_data[all_data['target']==0].mean()
pd.concat([mean1, mean0, np.abs(mean1-mean0)], axis=1 ).sort_values(by=2, ascending=False)[1:10]<prepare_x_and_y>
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test_x = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 train.columns
X_train = all_data[all_data['target'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['target'].isnull() ].drop(['target'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
X_train.drop(['ID_code'], axis=1, inplace=True)
 X_test_ID = X_test.pop('ID_code')
 Y_train = X_train.pop('target')
 print(X_train.shape, X_test.shape )<train_model>
train_x = train.drop("SalePrice",axis=1)
 train_y = train["SalePrice"]
all_data = pd.concat([train_x,test_x],axis=0,sort=True)
 train_ID = train['Id']
test_ID = test_x['Id']
all_data.drop("Id", axis = 1, inplace = True)
 print("train_x: "+str(train_x.shape))
print("train_y: "+str(train_y.shape))
print("test_x: "+str(test_x.shape))
print("all_data: "+str(all_data.shape))
pca = PCA()
pca.fit(X_train)
 ev_ratio = pca.explained_variance_ratio_
ev_ratio = np.hstack([0,ev_ratio.cumsum() ])
 plt.xlabel('components')
 plt.plot(ev_ratio)
 plt.show()<compute_train_metric>
all_data_na = all_data.isnull().sum() [all_data.isnull().sum() >0].sort_values(ascending=False)
 all_data_na
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 scores = cross_val_score(pca, X_train, Y_train, cv=skf)
 print("PCA, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lda, X_train, Y_train, cv=skf)
 print("LDA, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(gnb, X_train, Y_train, cv=skf)
 print("GNB, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(bnb, X_train, Y_train, cv=skf)
 print("BNB, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lgr, X_train, Y_train, cv=skf)
 print("LGR, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(sgd, X_train, Y_train, cv=skf)
 print("SGD, Accuracy: %0.4f(+/- %0.4f)" %(scores.mean() , scores.std() * 2))<feature_engineering>
na_col_list = all_data.isnull().sum() [all_data.isnull().sum() >0].index.tolist()
all_data[na_col_list].dtypes.sort_values()
for df in [X_train, X_test]:
df['sum'] = df.sum(axis=1)
 df['min'] = df.min(axis=1)
 df['max'] = df.max(axis=1)
 df['mean'] = df.mean(axis=1)
 df['std'] = df.std(axis=1)
 df['skew'] = df.skew(axis=1)
 df['kurt'] = df.kurtosis(axis=1)
 df['med'] = df.median(axis=1)
 df['var'] = df.var(axis=1)
 df['negval'] = df.apply(lambda x:(x < 0 ).astype(int ).sum() , axis=1 )<create_dataframe>
all_data['LotFrontage'] = all_data.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
float_list = all_data[na_col_list].dtypes[all_data[na_col_list].dtypes == "float64"].index.tolist()
obj_list = all_data[na_col_list].dtypes[all_data[na_col_list].dtypes == "object"].index.tolist()
all_data[float_list] = all_data[float_list].fillna(0)
 all_data[obj_list] = all_data[obj_list].fillna("None")
 all_data.isnull().sum() [all_data.isnull().sum() > 0]
n_train = pd.DataFrame()
n_test = pd.DataFrame()
n_train['_lgr'] = X_train_lgr
n_test['_lgr'] = X_test_lgr
n_train['_sgd'] = X_train_sgd
n_test['_sgd'] = X_test_sgd
n_train['_gnb'] = X_train_gnb
n_test['_gnb'] = X_test_gnb 
<categorify>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
_=<concatenate>
skewed_feats_over = skewed_feats[abs(skewed_feats)> 0.5].index
for i in skewed_feats_over:
print(min(all_data[i]))
X_train = pd.concat([X_train, n_train], axis=1)
 X_test = pd.concat([X_test, n_test], axis=1 )<create_dataframe>
all_data["FeetPerRoom"] = all_data["TotalSF"]/all_data["TotRmsAbvGrd"]
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:5] )<choose_model_class>
all_data.dtypes.value_counts()
params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 13,
"learning_rate" : 0.01,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 80,
"min_sum_heassian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : 42,
"verbosity" : 1,
"seed": 42
}
folds = StratifiedKFold(n_splits=10)
 oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 for fold_,(trn_, val_)in enumerate(folds.split(X_train, Y_train)) :
trn_x, trn_y = X_train.iloc[trn_], Y_train[trn_]
val_x, val_y = X_train.iloc[val_], Y_train[val_]
model = lgb.LGBMRegressor(**params, n_estimators=100000)
 model.fit(trn_x, trn_y, eval_set=[(val_x, val_y)], early_stopping_rounds=3000, verbose=1000)
 oof_preds[val_] = model.predict(val_x, num_iteration=model.best_iteration_)
 sub_preds += model.predict(X_test, num_iteration=model.best_iteration_)/ folds.n_splits<train_model>
all_data = pd.get_dummies(all_data,columns=cal_list)
 all_data.shape
_=<prepare_output>
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
import lightgbm as lgb
preds = sub_preds<save_to_csv>
train_x, valid_x, train_y, valid_y = train_test_split(
 train_x,
train_y,
test_size=0.3,
random_state=0 )
submission = pd.DataFrame({
'ID_code': X_test_ID,
'target': preds
})
 submission.to_csv("submission.csv", index=False )<set_options>
dtrain = xgb.DMatrix(train_x, label=train_y)
 dvalid = xgb.DMatrix(valid_x,label=valid_y)
 num_round = 5000
evallist = [(dvalid, 'eval'),(dtrain, 'train')]
evals_result = {}
param = {
'max_depth': 3,
'eta': 0.01,
'objective': 'reg:squarederror',
}
bst = xgb.train(
 param, dtrain,
num_round,
evallist,
evals_result=evals_result,
early_stopping_rounds = 1000 )
warnings.filterwarnings('ignore')
 %matplotlib inline
color = sns.color_palette()
%matplotlib inline
py.init_notebook_mode(connected=True)
 pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999 
<set_options>
dtest = xgb.DMatrix(test_x)
 my_submission = pd.DataFrame()
my_submission["Id"] = test_ID
my_submission["SalePrice"] = np.exp(bst.predict(dtest))
my_submission.to_csv('submission.csv', index=False )
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings('ignore')
 plt.style.use('seaborn')
 sns.set(font_scale=1)
 pd.set_option('display.max_columns', 500 )<load_from_csv>
cols = ["TotalSF","BsmtUnfSF","OverallCond","GrLivArea"]
X = train[cols]
y = train["SalePrice"]
model = LogisticRegression()
model.fit(X,y )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )<define_variables>
cols = ["TotalSF","BsmtUnfSF","OverallCond","GrLivArea"]
X_test=test_x[cols]
print(X_test.dtypes)
 test_predicted = model.predict(X_test )
rows1 = train_df.shape[0]; rows2 = test_df.shape[0];
columns1 = train_df.shape[1]; columns2 = test_df.shape[1]
print("The train dataset contains {0} rows and {1} columns".format(rows1, columns1))
print("The test dataset contains {0} rows and {1} columns".format(rows2, columns2))<init_hyperparams>
sub = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv' )
param = {
'num_leaves': 10,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}<define_variables>
sub['SalePrice'] = list(map(int, test_predicted))
sub.to_csv('submission.csv', index=False )