Kaggle competition: I-RICH ML COMPETITION
train_data[categorical[0]]<categorify>
train = pd.read_csv('.. /input/train.csv',index_col = 'Id')
 test = pd.read_csv('.. /input/test.csv',index_col = 'Id' )
pd.get_dummies(train_data[categorical[0]] )<categorify>
train.price= np.log1p(train["price"] )
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )<feature_engineering>
train = train[~(( train.bathrooms>6)&(train.price<14)) ]
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))<feature_engineering>
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.price.values
all_data = pd.concat(( train, test))
all_data.drop(['price'], axis=1, inplace=True)
 all_data.shape
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
all_data['zipcode'] = all_data['zipcode'].apply(str )
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
all_data['YrSold'] =all_data['date'].apply(lambda s:int(s[:4]))
all_data['MonthSold'] =all_data['date'].apply(lambda s:(s[4:6]))
all_data['DaySold'] =all_data['date'].apply(lambda s:int(s[6:8]))
all_data.drop(['date'], axis=1, inplace=True)
 all_data['rooms'] = all_data['bedrooms']+all_data['bathrooms']
all_data.head()
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<import_modules>
numerical_cols = [cname for cname in all_data.columns if
all_data[cname].dtype in ['int64', 'float64']]
skewness = all_data[numerical_cols].apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.9]
print(str(skewness.shape[0])+ " skewed numerical features to log transform")
 skewed_features = list(skewness.index)
 all_data[skewed_features] = np.log1p(all_data[skewed_features] )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
Dummies_all_data = pd.get_dummies(all_data )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
X_train = Dummies_all_data[:ntrain]
X_test = Dummies_all_data[ntrain:]
from sklearn.neighbors import KNeighborsClassifier<train_model>
n_folds=5
def rmsle_cv(model):
rmsle= np.sqrt(-cross_val_score(model, X_train, y_train, scoring="neg_mean_squared_error", cv = 5))
print("
score rmsle: {:.4f}({:.4f})
 ".format(rmsle.mean() , rmsle.std()))
return(rmsle)
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
model_xgb = XGBRegressor(colsample_bytree=1, gamma=0.0468,
learning_rate=0.053, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1)
 score_xgb = rmsle_cv(model_xgb)
KNN.score(X_test,y_test )<predict_on_test>
model_LGB = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
 score_LGB = rmsle_cv(model_LGB )
predict=KNN.predict(test_data.drop('RefId',axis=1))<prepare_output>
model_GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5)
 score_GBoost = rmsle_cv(model_GBoost )
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()<prepare_output>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
score_ENet = rmsle_cv(ENet )
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )<save_to_csv>
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
score_lasso = rmsle_cv(lasso )
Submission.head()
Submission.to_csv('Submission.csv' )<import_modules>
model_GBoost.fit(X_train,y_train)
 model_xgb.fit(X_train,y_train)
 model_LGB.fit(X_train,y_train)
 lasso.fit(X_train,y_train)
 ENet.fit(X_train,y_train)
 lasso.fit(X_train,y_train)
 preds_test = np.expm1(model_xgb.predict(X_test)*0.50 +(
 model_LGB.predict(X_test)*0.10+ model_GBoost.predict(X_test)*0.30)+(
 lasso.predict(X_test)*0.05+ENet.predict(X_test)*0.05))
output = pd.DataFrame({'Id': X_test.index,
'price': preds_test})
 output.to_csv('submission.csv', index=False )