Kaggle competition: House Prices - Advanced Regression Techniques
AllSub = pd.DataFrame({ 'RefId': test['RefId'],
'IsBadBuy' : sub_test_pred
})
 AllSub.to_csv("DGK_Pipeline_RF.csv", index = False )<load_from_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")
 sub = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv" )
train_data = pd.read_csv("/kaggle/input/DontGetKicked/training.csv")
 train_data.head()<load_from_csv>
pd.set_option('display.max_columns', None)
 train.head()
test_data = pd.read_csv("/kaggle/input/DontGetKicked/test.csv")
 test_data.head()<count_missing_values>
n_train = len(train)
 all_features = pd.concat([train, test], sort=False ).reset_index(drop=True )
train_data.isnull().sum()<count_missing_values>
all_features['HasWoodDeck'] =(all_features['WoodDeckSF'] == 0)* 1
all_features['HasOpenPorch'] =(all_features['OpenPorchSF'] == 0)* 1
all_features['HasEnclosedPorch'] =(all_features['EnclosedPorch'] == 0)* 1
all_features['Has3SsnPorch'] =(all_features['3SsnPorch'] == 0)* 1
all_features['HasScreenPorch'] =(all_features['ScreenPorch'] == 0)* 1
all_features['YearsSinceRemodel'] = all_features['YrSold'].astype(int)- all_features['YearRemodAdd'].astype(int)
 all_features['YearsSinceBuilt'] = all_features['YrSold'].astype(int)- all_features['YearBuilt'].astype(int)
 all_features['Total_Home_Quality'] = all_features['OverallQual'] + all_features['OverallCond']
all_features = all_features.drop(['Utilities', 'Street', 'PoolQC',], axis=1)
 all_features['TotalSF'] = all_features['TotalBsmtSF'] + all_features['1stFlrSF'] + all_features['2ndFlrSF']
all_features['Total_sqr_footage'] =(all_features['BsmtFinSF1'] + all_features['BsmtFinSF2'] +
all_features['1stFlrSF'] + all_features['2ndFlrSF'])
 all_features['Total_Bathrooms'] =(all_features['FullBath'] +(0.5 * all_features['HalfBath'])+
all_features['BsmtFullBath'] +(0.5 * all_features['BsmtHalfBath']))
all_features['Total_porch_sf'] =(all_features['OpenPorchSF'] + all_features['3SsnPorch'] +
all_features['EnclosedPorch'] + all_features['ScreenPorch'] +
all_features['WoodDeckSF'])
 all_features['TotalBsmtSF'] = all_features['TotalBsmtSF'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 all_features['2ndFlrSF'] = all_features['2ndFlrSF'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 all_features['GarageArea'] = all_features['GarageArea'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 all_features['GarageCars'] = all_features['GarageCars'].apply(lambda x: 0 if x <= 0.0 else x)
 all_features['LotFrontage'] = all_features['LotFrontage'].apply(lambda x: np.exp(4.2)if x <= 0.0 else x)
 all_features['MasVnrArea'] = all_features['MasVnrArea'].apply(lambda x: np.exp(4)if x <= 0.0 else x)
 all_features['BsmtFinSF1'] = all_features['BsmtFinSF1'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 all_features['haspool'] = all_features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 all_features['has2ndfloor'] = all_features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasgarage'] = all_features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasbsmt'] = all_features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasfireplace'] = all_features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
test_data.isnull().sum()<count_values>
test = all_features[n_train:].reset_index(drop=True)
 train = all_features[:n_train]
train_data['IsBadBuy'].value_counts()<count_values>
def label_encoding(train: pd.DataFrame, test: pd.DataFrame, col_definition: list):
n_train = len(train)
 train = pd.concat([train, test], sort=False ).reset_index(drop=True)
 for column in col_definition:
try:
lbl = preprocessing.LabelEncoder()
train[column] = lbl.fit_transform(list(train[column].values))
except:
print(column)
 test = train[n_train:].reset_index(drop=True)
 train = train[:n_train]
return train, test
train_data['Model'].value_counts()
train_data.drop("Model",axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )<drop_column>
categorical_cols = list(train.select_dtypes(include=['object'] ).columns)
 print(categorical_cols)
 train, test = label_encoding(train, test, categorical_cols )
train_data.drop("Trim",axis=1,inplace=True)
 test_data.drop("Trim",axis=1,inplace=True)
 train_data.drop("SubModel",axis=1,inplace=True)
 test_data.drop("SubModel",axis=1,inplace=True )<count_values>
import optuna.integration.lightgbm as lgb
train_data["Color"].value_counts(dropna=False )<count_values>
X = train.drop(['Id', 'SalePrice'], axis=1)
 y = np.log1p(train['SalePrice'])
 X_test = test.drop(['Id','SalePrice',], axis=1 )
test_data["Color"].value_counts(dropna=False )<categorify>
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 X.reset_index(inplace=True, drop=True)
 y = y.drop(y.index[outliers])
 y.reset_index(inplace=True, drop=True )
train_data["Color"].fillna(value="ColorUnknown",inplace=True)
 test_data["Color"].fillna(value="ColorUnknown",inplace=True )<count_values>
y_preds = []
models = []
oof_train = np.zeros(( len(X),))
cv = KFold(n_splits=10, shuffle=True, random_state=0)
 params = {
'num_leaves': 24,
'max_depth': 6,
'objective': 'regression',
'metric': 'rmse',
'learning_rate': 0.05
}
for fold_id,(train_index, valid_index)in enumerate(cv.split(X)) :
X_tr = X.loc[train_index, :]
X_val = X.loc[valid_index, :]
y_tr = y[train_index]
y_val = y[valid_index]
lgb_train = lgb.Dataset(X_tr,
y_tr,
categorical_feature=categorical_cols)
 lgb_eval = lgb.Dataset(X_val,
y_val,
reference=lgb_train,
categorical_feature=categorical_cols)
 model = lgb.train(params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 oof_train[valid_index] = model.predict(X_val,
num_iteration=model.best_iteration)
 y_pred = model.predict(X_test,
num_iteration=model.best_iteration)
 y_preds.append(y_pred)
 models.append(model )
train_data["Transmission"].value_counts(dropna=False )<rename_columns>
print(f'CV: {np.sqrt(mean_squared_error(y, oof_train)) }' )
train_data["Transmission"].replace("Manual","MANUAL",inplace=True )<count_values>
y_sub = sum(y_preds)/ len(y_preds)
 y_sub = np.expm1(y_sub)
 sub['SalePrice'] = y_sub
sub.to_csv('submission.csv', index=False)
 sub.head()