Kaggle competition: House Prices - Advanced Regression Techniques
train_data['Trim'].value_counts()<drop_column>
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.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1 )<count_values>
n_train = len(train)
 features = pd.concat([train, test], sort=False ).reset_index(drop=True )
train_data['SubModel'].value_counts()<drop_column>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1 )<count_values>
train_data['Color'].value_counts()<count_values>
test_data['Color'].value_counts()<data_type_conversions>
test = features[n_train:].reset_index(drop=True)
 train = features[:n_train]
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )<count_missing_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
print("Number of null values in Color column "+str(train_data['Color'].isnull().sum()))
print("Number of null values in Color column "+str(test_data['Color'].isnull().sum()))<count_values>
categorical_cols = list(train.select_dtypes(include=['object'] ).columns)
 print(categorical_cols)
 train, test = label_encoding(train, test, categorical_cols )
train_data['Transmission'].value_counts()<count_values>
X = train.drop(['Id', 'SalePrice'], axis=1)
 y = np.log1p(train['SalePrice'])
 X_test = test.drop(['Id','SalePrice'], axis=1 )
test_data['Transmission'].value_counts()<filter>
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[train_data['Transmission']=='Manual']<rename_columns>
cv = KFold(n_splits=5, shuffle=True, random_state=0 )
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )<count_values>
y_preds = []
models = []
oof_train = np.zeros(( len(X),))
cv = KFold(n_splits=5, 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()<data_type_conversions>
print(f'CV: {np.sqrt(mean_squared_error(y, oof_train)) }' )
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )<count_values>
y_pred = np.expm1(model.predict(X_test,num_iteration=model.best_iteration))
train_data['WheelTypeID'].value_counts()<drop_column>
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()