Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<save_to_csv>
X_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 X_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 X_train.dropna(axis=0, subset=['SalePrice'], inplace=True)
 y_train = X_train.SalePrice
X_train.drop(['SalePrice'], axis=1, inplace=True)
 categorical_cols = [cname for cname in X_train.columns if
X_train[cname].nunique() < 20 and
X_train[cname].dtype == "object"]
numerical_cols = [cname for cname in X_train.columns if
X_train[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_train[my_cols].copy()
X_test= X_test[my_cols].copy()
df_mask.to_csv("submission.csv", index=False )<data_type_conversions>
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ] )
if 1:
df_mask['Class'] = df_mask['ImageId_ClassId'].str[-1].astype(np.int32)
 df_mask['Label'] =(df_mask['EncodedPixels']!='' ).astype(np.int32)
 pos1 =(( df_mask['Class']==1)&(df_mask['Label']==1)).sum()
pos2 =(( df_mask['Class']==2)&(df_mask['Label']==1)).sum()
pos3 =(( df_mask['Class']==3)&(df_mask['Label']==1)).sum()
pos4 =(( df_mask['Class']==4)&(df_mask['Label']==1)).sum()
num_image = len(df_mask)//4
num = len(df_mask)
 pos =(df_mask['Label']==1 ).sum()
neg = num-pos
print('')
 print('\t\tnum_image = %5d(1801)'%num_image)
 print('\t\tnum = %5d(7204)'%num)
 print('\t\tneg = %5d(6172)%0.3f'%(neg,neg/num))
print('\t\tpos = %5d(1032)%0.3f'%(pos,pos/num))
print('\t\tpos1 = %5d(128)%0.3f %0.3f'%(pos1,pos1/num_image,pos1/pos))
print('\t\tpos2 = %5d(43)%0.3f %0.3f'%(pos2,pos2/num_image,pos2/pos))
print('\t\tpos3 = %5d(741)%0.3f %0.3f'%(pos3,pos3/num_image,pos3/pos))
print('\t\tpos4 = %5d(120)%0.3f %0.3f'%(pos4,pos4/num_image,pos4/pos)) 
<set_options>
def get_score(n_estimators):
my_pipeline = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', XGBRegressor(n_estimators=n_estimators, objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 10)) ])
 scores = -1 * cross_val_score(my_pipeline, X_train, y_train,
cv=3,
scoring='neg_mean_absolute_error')
 return scores.mean()
results = {}
n_estimators=[50,100,250,300,350,400]
for i in n_estimators:
results[i]=get_score(i)
 %matplotlib inline
plt.plot(list(results.keys()), list(results.values()))
plt.show()
<define_variables><EOS> 
clf = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 10, n_estimators = 250)) ])
 clf.fit(X_train, y_train)
 preds_test = clf.predict(X_test)
 output = pd.DataFrame({'Id': X_test.index,
'SalePrice': preds_test})
 output.to_csv('submission.csv', index=False )