Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<train_model>
plt.style.use('ggplot')
 %matplotlib inline
plt.rcParams['font.size'] = 12
train_labels = y
data, test = data.align(test, join = 'inner', axis = 1)
 data['TARGET'] = y
print('Training Features shape: ', data.shape)
 print('Testing Features shape: ', test.shape )<feature_engineering>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print('train data shape :',train_df.shape)
 print('test data shape :',test_df.shape )
test['DAYS_EMPLOYED_ANOM'] = test["DAYS_EMPLOYED"] == 365243
test["DAYS_EMPLOYED"].replace({365243: np.nan}, inplace = True)
 print('There are %d anomalies in the test data out of %d entries' %(test["DAYS_EMPLOYED_ANOM"].sum() , len(test)) )<categorify>
y=train_df['Target']
y.value_counts()
poly_features = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH', 'TARGET']]
poly_features_test = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH']]
imputer = SimpleImputer(strategy = 'median')
 poly_target = poly_features['TARGET']
poly_features = poly_features.drop(columns = ['TARGET'])
 poly_features = imputer.fit_transform(poly_features)
 poly_features_test = imputer.transform(poly_features_test)
 poly_transformer = PolynomialFeatures(degree = 3 )<train_model>
train_null = train_df.isnull().sum()
train_null[train_null > 0]
poly_transformer.fit(poly_features)
 poly_features = poly_transformer.transform(poly_features)
 poly_features_test = poly_transformer.transform(poly_features_test)
 print('Polynomial Features shape: ', poly_features.shape )<feature_engineering>
test_null = test_df.isnull().sum()
test_null[test_null > 0]
poly_transformer.get_feature_names(input_features = ['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH'])[:15]<prepare_output>
train_grphh = train_df.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 err_train = train_grphh[train_grphh !=True]
print('Number of households with incorrect poverty level :',len(err_train))
poly_features = pd.DataFrame(poly_features,
columns = poly_transformer.get_feature_names(['EXT_SOURCE_1', 'EXT_SOURCE_2',
'EXT_SOURCE_3', 'DAYS_BIRTH']))
poly_features['TARGET'] = poly_target
poly_corrs = poly_features.corr() ['TARGET'].sort_values()
print(poly_corrs.head(20))
print(poly_corrs.tail(20))<merge>
for household in err_train.index:
target = int(train_df[(train_df['idhogar']==household)&(train_df['parentesco1']==1.0)]['Target'])
 train_df.loc[train_df['idhogar']==household,'Target'] = target
poly_features_test = pd.DataFrame(poly_features_test,
columns = poly_transformer.get_feature_names(['EXT_SOURCE_1', 'EXT_SOURCE_2',
'EXT_SOURCE_3', 'DAYS_BIRTH']))
poly_features['SK_ID_CURR'] = data['SK_ID_CURR']
app_train_poly = data.merge(poly_features, on = 'SK_ID_CURR', how = 'left')
 poly_features_test['SK_ID_CURR'] = test['SK_ID_CURR']
app_test_poly = test.merge(poly_features_test, on = 'SK_ID_CURR', how = 'left')
 app_train_poly, app_test_poly = app_train_poly.align(app_test_poly, join = 'inner', axis = 1)
 print('Training data with polynomial features shape: ', app_train_poly.shape)
 print('Testing data with polynomial features shape: ', app_test_poly.shape )<drop_column>
train_df,test_df = train_df.align(test_df,join='inner',axis=1)
 print('Training Features shape: ', train_df.shape)
 print('Testing Features shape: ', test_df.shape )
app_train_poly.head()
app_train_poly=app_train_poly.rename(columns={'EXT_SOURCE_1_x':'EXT_SOURCE_1','EXT_SOURCE_2_x':'EXT_SOURCE_2','EXT_SOURCE_3_x':'EXT_SOURCE_3','DAYS_BIRTH_x':'DAYS_BIRTH'})
 app_test_poly=app_test_poly.rename(columns={'EXT_SOURCE_1_x':'EXT_SOURCE_1','EXT_SOURCE_2_x':'EXT_SOURCE_2','EXT_SOURCE_3_x':'EXT_SOURCE_3','DAYS_BIRTH_x':'DAYS_BIRTH'} )<drop_column>
data = pd.concat([train_df,test_df],axis=0)
 data.head()
app_test_poly.drop('1',axis=1,inplace=True)
 app_train_poly.drop('1',axis=1,inplace=True )<drop_column>
cat_cols = data.nunique() ==2
cat_cols = list(cat_cols[cat_cols].keys() )
colsToRemove = []
for col in app_train_poly.columns:
if col != 'SK_ID_CURR' and col != 'TARGET':
if app_train_poly[col].std() == 0:
colsToRemove.append(col)
 app_train_poly.drop(colsToRemove, axis=1, inplace=True)
 app_test_poly.drop(colsToRemove, axis=1, inplace=True)
 print("Removed `{}` Constant Columns
".format(len(colsToRemove)))
 print(colsToRemove )<load_from_csv>
cols = ['edjefe', 'edjefa','dependency']
data[cols] = data[cols].replace({'no': 0, 'yes':1} ).astype(float)
 data['hogar_mid'] = data['hogar_adul'] - data['hogar_mayor']
data['bedroom%'] = data['bedrooms']/data['rooms']
data['person/rooms'] = data['rooms']/data['hhsize']
data['male_ratio'] = data['r4h3']/data['r4t3']
data['female_ratio'] = data['r4m3']/data['r4t3']
data['female_per_room'] = data['r4m3']/data['rooms']
data['female_per_bedroom'] = data['r4m3']/data['bedrooms']
data['hogarmid_per_bedroom'] = data['hogar_mid']/data['bedrooms']
<rename_columns>
data.fillna(-1,inplace=True )
app_train_poly=app_train_poly.rename(columns={'AMT_CREDIT_x':'AMT_CREDIT','AMT_GOODS_PRICE_x':'AMT_GOODS_PRICE'})
 app_test_poly=app_test_poly.rename(columns={'AMT_CREDIT_x':'AMT_CREDIT','AMT_GOODS_PRICE_x':'AMT_GOODS_PRICE'} )<feature_engineering>
train_df = data[:len(train_df)]
test_df = data[len(train_df):]
print('Training data shape: ', train_df.shape)
 print('Testing data shape: ', test_df.shape )
app_train_domain = app_train_poly.copy()
app_test_domain = app_test_poly.copy()
app_train_domain['CREDIT_INCOME_PERCENT'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['ANNUITY_INCOME_PERCENT'] = app_train_domain['AMT_ANNUITY_x'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['CREDIT_TERM'] = app_train_domain['AMT_ANNUITY_x'] / app_train_domain['AMT_CREDIT']
app_train_domain['DAYS_EMPLOYED_PERCENT'] = app_train_domain['DAYS_EMPLOYED'] / app_train_domain['DAYS_BIRTH']
app_train_domain['NEW_CREDIT_TO_ANNUITY_RATIO'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_ANNUITY_x']
app_train_domain['NEW_CREDIT_TO_GOODS_RATIO'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_GOODS_PRICE']
app_train_domain['NEW_EXT_SOURCES_MEAN'] = app_train_domain[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 app_train_domain['NEW_EMPLOY_TO_BIRTH_RATIO'] = app_train_domain['DAYS_EMPLOYED'] / app_train_domain['DAYS_BIRTH'] 
<feature_engineering>
scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro' )
app_test_domain['CREDIT_INCOME_PERCENT'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['ANNUITY_INCOME_PERCENT'] = app_test_domain['AMT_ANNUITY_x'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['CREDIT_TERM'] = app_test_domain['AMT_ANNUITY_x'] / app_test_domain['AMT_CREDIT']
app_test_domain['DAYS_EMPLOYED_PERCENT'] = app_test_domain['DAYS_EMPLOYED'] / app_test_domain['DAYS_BIRTH']
app_test_domain['NEW_CREDIT_TO_ANNUITY_RATIO'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_ANNUITY_x']
app_test_domain['NEW_CREDIT_TO_GOODS_RATIO'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_GOODS_PRICE']
app_test_domain['NEW_EXT_SOURCES_MEAN'] = app_test_domain[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 app_test_domain['NEW_EMPLOY_TO_BIRTH_RATIO'] = app_test_domain['DAYS_EMPLOYED'] / app_test_domain['DAYS_BIRTH']<train_model>
X = train_df
logR = LogisticRegression(class_weight='balanced',C=0.0005)
 cv_score = cross_val_score(logR, X, y, cv = 10, scoring = scorer)
 print(f'10 Fold Cross Validation F1 Score = {round(cv_score.mean() , 4)} with std = {round(cv_score.std() , 4)}')
<train_model><EOS> 
logR.fit(X,y)
 preds_log = logR.predict(test_df)
 sub_log = pd.DataFrame({'Id':test_df.index, 'Target':preds_log})
 sub_log.to_csv('sub_log1.csv', index=False)