Kaggle competition: Categorical Feature Encoding Challenge
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge<prepare_output>
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from scipy import sparse
y_pred_exp=np.exp(y_pred)-1<create_dataframe>
train=pd.read_csv(".. /input/cat-in-the-dat/train.csv")
 train.head()
submission_pred=pd.DataFrame(test_merged['Id'],columns=['Id'] )<feature_engineering>
test=pd.read_csv(".. /input/cat-in-the-dat/test.csv")
 test.head()
submission_pred['Sales']=y_pred_exp<feature_engineering>
train=train.drop(index=train[~train.nom_7.isin(test.nom_7)].index)
 train=train.drop(index=train[~train.nom_8.isin(test.nom_8)].index)
 train=train.drop(index=train[~train.nom_9.isin(test.nom_9)].index )
submission_pred['Id']=np.arange(1,len(submission_pred)+1 )<save_to_csv>
new_test=test.drop(columns=['id'])
 new_test.head()
submission_pred.to_csv('Submission.csv',index=False )<compute_test_metric>
new_train=train.drop(columns=['id','target'])
 new_train.head()
def ToWeight(y):
w = np.zeros(y.shape, dtype=float)
 ind = y != 0
w[ind] = 1./(y[ind]**2)
 return w
def rmspe(y, yhat):
w = ToWeight(y)
 rmspe = np.sqrt(np.mean(w *(y - yhat)**2))
return rmspe
rmse_val=np.sqrt(mean_squared_error(y_val_exp,y_pred_val_exp))
rmspe_val=rmspe(y_val_exp,y_pred_val_exp)
 print(rmse_val,rmspe_val )<train_on_grid>
new_train=new_train.drop(columns=['bin_0'])
 new_test=new_test.drop(columns=['bin_0'] )
params={'max_depth':list(range(5,20)) }
base_model=DecisionTreeRegressor()
cv_model=GridSearchCV(base_model,param_grid=params,return_train_score=True ).fit(X_train,y_train )<create_dataframe>
data = pd.concat([new_train, new_test])
 dummies = pd.get_dummies(data, columns=data.columns, drop_first=True,sparse=True)
 new_train = dummies.iloc[:new_train.shape[0], :]
new_test = dummies.iloc[new_train.shape[0]:, :]
del data
del dummies
new_train = new_train.sparse.to_coo().tocsr()
new_test = new_test.sparse.to_coo().tocsr()
df_cv_results=pd.DataFrame(cv_model.cv_results_ ).sort_values(by='mean_test_score',ascending=False )<create_dataframe>
X_train,X_test,y_train,y_test=train_test_split(new_train,train['target'],test_size=0.001,random_state=0)
 lr = LogisticRegression(C=0.095,solver='lbfgs',class_weight='balanced')
 lr.fit(X_train, y_train)
 proba_test = lr.predict_proba(X_test)[:, 1]
LR_result=pd.DataFrame({'pred':proba_test,'real':y_test})
 LR_result['pred_0_1']=LR_result.pred.apply(lambda x:1 if x>=0.5 else 0)
df_cv_results=pd.DataFrame(cv_model.cv_results_ ).sort_values(by='mean_test_score',ascending=False)[['param_max_depth','mean_test_score','mean_train_score']]
df_cv_results<find_best_model_class>
print('LR_acc: ',sum(LR_result.real==LR_result.pred_0_1)/len(LR_result))
def get_rmspe_score(model,input_values,y_actual):
y_predicted=model.predict(input_values)
 y_actual=np.exp(y_actual)-1
y_predicted=np.exp(y_predicted)-1
score=rmspe(y_actual,y_predicted)
 return score
params={'max_depth':list(range(5,8)) }
base_model=DecisionTreeRegressor()
cv_model=GridSearchCV(base_model,param_grid=params,return_train_score=True,scoring=get_rmspe_score ).fit(X_train,y_train)
 pd.DataFrame(cv_model.cv_results_)[['params','mean_test_score','mean_train_score']]<train_model>
lr.predict_proba(new_test)[:, 1]
model_ada=AdaBoostRegressor(n_estimators=5 ).fit(X_train,y_train )<find_best_params>
X_train=X_train.astype(float)
 X_test=X_test.astype(float)
 lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': {'binary_logloss', 'auc'},
'num_leaves':450,
'max_depth': 25,
'min_data_in_leaf': 150,
'learning_rate': 0.1,
'feature_fraction': 0.95,
'bagging_fraction': 0.95,
'bagging_freq': 10,
'lambda_l1': 0,
'lambda_l2': 0,
'min_gain_to_split': 0.1,
'verbose': 0,
'is_unbalance': True
}
model_ada.estimators_[0]<feature_engineering>
gbm = lgb.train(params,
lgb_train,
num_boost_round=10000,
valid_sets=lgb_eval,
early_stopping_rounds=500 )
def draw_tree(model, columns):
graphviz_path = 'C:\Program Files(x86)\Graphviz2.38/bin/'
os.environ["PATH"] += os.pathsep + graphviz_path
dot_data = StringIO()
tree.export_graphviz(model,
out_file=dot_data,
feature_names=columns)
 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
 return Image(graph.create_png() )<import_modules>
lr.fit(new_train, train['target'])
 LR_TEST=lr.predict_proba(new_test)[:, 1]
new_test=new_test.astype(float)
 LGBM_TEST= gbm.predict(new_test, num_iteration=gbm.best_iteration)
import xgboost as xgb<predict_on_test>
prediction=pd.DataFrame({'id':test.id,'LR_TEST':LR_TEST,'LGBM_TEST':LGBM_TEST})
 submit=pd.DataFrame({'id':test.id,'target':LR_TEST})
 prediction.to_csv('prediction.csv',index=False)
 submit.to_csv('submission.csv',index=False )