Kaggle competition: Predict the Income - <DECODE> WITH BOARD
<SOS> metric: meanfscore Kaggle data source: predict-the-income-<decode>-with-board<drop_column>
import pandas as pd
import numpy as np
from sklearn import tree
import graphviz
from sklearn.model_selection import cross_val_score
df_all =df_all.drop(['YrSold','MoSold','MiscVal','PoolQC','ScreenPorch','3SsnPorch','EnclosedPorch',
'KitchenAbvGr','CentralAir','Alley','LotConfig','LotShape','LandSlope','RoofStyle',
'BsmtFinSF2','BsmtUnfSF','HeatingQC','Electrical','BsmtFinType2','BsmtFinType1',
'GarageFinish','MiscFeature','FireplaceQu','Fence','Alley','Street','MasVnrType'], axis = 1 )<normalization>
df = pd.read_csv('/kaggle/input/predict-the-income-bi-hack/train.csv')
 dft = pd.read_csv('/kaggle/input/predict-the-income-bi-hack/test.csv')
 print('train dataset length',len(df),'
test dataset length',len(dft))
Scaler = StandardScaler()
all_scaled = pd.DataFrame(Scaler.fit_transform(df_all))
train_scaled = pd.DataFrame(all_scaled[:1460])
 test_scaled = pd.DataFrame(all_scaled[1460:2920] )<split>
ids=dft['ID']
df.drop('ID', axis=1,inplace =True)
 dft.drop('ID', axis=1,inplace =True )
X_train = train_scaled
y_train = y['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(
 X_train, y_train, test_size=0.15, random_state=0 )<define_search_space>
df = pd.get_dummies(df, columns=['Work', 'Education', 'Marital_Status', 'Occupation', 'Relationship', 'Race', 'Gender','Nationality'])
 dft = pd.get_dummies(dft, columns=['Work', 'Education', 'Marital_Status', 'Occupation', 'Relationship', 'Race', 'Gender','Nationality'] )
XGBoost
param_xgb = {"max_depth": [1,3,5,10,100],
"learning_rate" : [0.0001,0.001,0.01],
"min_child_weight" : [1, 3, 5, 10],
"n_estimators": [1, 10, 100, 1000],
"subsample": [0.5,0.75,0.9],
"gamma":[0,0.1,0.2],
"eta": [0.3,0.15,0.10]}
gs_xgb = GridSearchCV(XGBRegressor() ,
param_xgb,
cv=4,
verbose=True,
n_jobs=-1)
 gs_xgb.fit(X_train, y_train)
 print(gs_xgb.best_estimator_ )<choose_model_class>
df.drop('Nationality_Holand-Netherlands',axis=1, inplace=True )
LightGBM
param_lgb = {"max_depth": [1, 3, 5, 10, 25, 50, 75],
"learning_rate" : [0.001,0.01,0.05,0.1],
"num_leaves": [1, 10, 100,1000],
"n_estimators": [1, 3, 10, 100, 1000]}
gs_lgb = GridSearchCV(LGBMRegressor() ,
param_lgb,
cv=4,
verbose=True,
n_jobs=-1)
 gs_lgb.fit(X_train, y_train)
 print(gs_lgb.best_estimator_ )<choose_model_class>
df['Income'].value_counts()
XGB = XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, eta=0.3, gamma=0,
gpu_id=-1, importance_type='gain', interaction_constraints='',
learning_rate=0.01, max_delta_step=0, max_depth=5,
min_child_weight=1,monotone_constraints='() ',n_estimators=1000,
n_jobs=0, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=0.5,
tree_method='exact', validate_parameters=1, verbosity=None)
 XGB.fit(X_train,y_train )<train_model>
df["Income"] = df["Income"].replace({'<=50K':1,'>50K':0} )
model_lgb = LGBMRegressor(learning_rate=0.05, max_depth=3, n_estimators=1000, num_leaves=10)
 model_lgb.fit(X_train , y_train )<train_model>
d_train = df.copy()
d_test = dft.copy()
df1 = df
votingC = VotingRegressor(estimators=[('xgb_r', XGB),('lgb_r', model_lgb)],n_jobs=4)
 votingC = votingC.fit(X_train, y_train )<compute_test_metric>
X = d_train.drop('Income', axis=1)
 y = d_train['Income']
print("Training score:",XGB.score(X_train,y_train),"Test Score:",XGB.score(X_test,y_test))
print("Training score:",model_lgb.score(X_train,y_train),"Test Score:",model_lgb.score(X_test,y_test))
print("Training score:",votingC.score(X_train,y_train),"Test Score:",votingC.score(X_test,y_test))<predict_on_test>
from sklearn.model_selection import train_test_split
y_pred_voting = pd.DataFrame(votingC.predict(test_scaled))<create_dataframe>
from sklearn.model_selection import train_test_split
y_pred=pd.DataFrame()
y_pred['SalePrice'] = y_pred_voting[0]
y_pred['Id'] = df_test['Id']<save_to_csv>
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.30,random_state=42 )
y_pred.to_csv('submission.csv',index=False )<set_options>
t = tree.DecisionTreeClassifier(criterion='entropy', max_depth=7 )
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 10000)
 pd.set_option('display.max_rows', 10000 )<load_from_csv>
t = t.fit(X_train,y_train )
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<prepare_x_and_y>
t.score(X_test,y_test )
y = df_train[['Id','SalePrice']]
df_train = df_train.drop('SalePrice',axis=1 )<concatenate>
sol=t.predict(dft)
 print(sol )
concat_df = [df_train,df_test]
df_all = pd.concat(concat_df ).reset_index(drop=True )<categorify>
with open('income-predicted.csv','w')as fw:
fw.write('ID,Income
')
 ct=24001
for i in sol:
s=""
if i==1:
s="<=50K"
else:
s=">50K"
fw.write(str(ct)+','+str(s)+'
')
 ct+=1
df_dummy = pd.get_dummies(df_all['MSZoning'],prefix='MSZoning',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['MSZoning']<data_type_conversions>
sdf = pd.read_csv('income-predicted.csv', sep=',')
 sdf.head()
<categorify><EOS> 
sdf.Income.value_counts()