Kaggle competition: Costa Rican Household Poverty Level Prediction
X_train = train[['Pclass', 'Sex', 'Parch', 'Embarked', 'CabinBool', 'Title', 'AgeBin', 'FareBin', 'FamilySurvival']]
y_train = train['Survived']<choose_model_class>
KMeansSMOTE,SMOTE,SVMSMOTE)
 EditedNearestNeighbours,
RepeatedEditedNearestNeighbours,
NeighbourhoodCleaningRule,AllKNN,TomekLinks)
model = CatBoostClassifier(verbose=False )<prepare_x_and_y>
df = pd.read_csv("/kaggle/input/costa-rican-household-poverty-prediction/train.csv" )
main_features = ['Sex', 'FamilySurvival', 'FareBin', 'Pclass', 'Title']
X_test = test[main_features]
X_train = train[main_features]<compute_train_metric>
g = df.columns.to_series().groupby(df.dtypes ).groups
cross_val_score(estimator=model, X=X_train, y=y_train, cv=5 ).mean()<find_best_model_class>
def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['r4t3_to_tamhog'] = df['r4t3']/df['tamhog']
df['r4t3_to_rooms'] = df['r4t3']/df['rooms']
df['v2a1_to_r4t3'] = df['v2a1']/df['r4t3']
df['v2a1_to_r4t3'] = df['v2a1']/(df['r4t3'] - df['r4t1'])
 df['hhsize_to_rooms'] = df['hhsize']/df['rooms']
df['rent_to_hhsize'] = df['v2a1']/df['hhsize']
df['rent_to_over_18'] = df['v2a1']/df['num_over_18']
df.loc[df.num_over_18 == 0, "rent_to_over_18"] = df[df.num_over_18 == 0].v2a1
return df
ensemble = [CatBoostClassifier(verbose=False), RandomForestClassifier() , svm.NuSVC(probability=True), neighbors.KNeighborsClassifier() ]
classifiers_with_names = []
_ = [classifiers_with_names.append(( clf.__class__.__name__, clf)) for clf in ensemble]
voting = VotingClassifier(classifiers_with_names, voting='hard')
 cv_results = cross_validate(voting, X_train, y_train, cv=5)
 print(cv_results['test_score'].mean())
 voting.fit(X_train, y_train)
 predictions = voting.predict(X_test)
<save_to_csv>
def trata_cat(df):
df['dependency'] = np.sqrt(df['SQBdependency'])
 df.loc[(df.v14a == 1)&(df.sanitario1 == 1)&(df.abastaguano == 0), "v14a"] = 0
df.loc[(df.v14a == 1)&(df.sanitario1 == 1)&(df.abastaguano == 0), "sanitario1"] = 0
df['num_over_18'] = 0
df['num_over_18'] = df[df.age >= 18].groupby('idhogar' ).transform("count")
 df['num_over_18'] = df.groupby("idhogar")["num_over_18"].transform("max")
 df['num_over_18'] = df['num_over_18'].fillna(0)
 cols = ['edjefe', 'edjefa']
df[cols] = df[cols].replace({'yes':1, 'no':0})
 df["dependency"] = df["dependency"].astype(float)
 df["edjefe"] = df["edjefe"].astype(int)
 df["edjefa"] = df["edjefa"].astype(int)
 df['edjef'] = np.max(df[['edjefa','edjefe']], axis=1)
 df = extract_features(df)
 return df
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions.astype(int)})
 output.to_csv('submission_new_session.csv', index=False )<set_options>
def dropa_trata(df):
df_limpo = df.drop(["Id"],axis=1 ).copy()
df_tratado = trata_cat(df_limpo)
 return df_tratado.drop(["idhogar"],axis=1 )
warnings.filterwarnings("ignore")
 %matplotlib inline<load_from_csv>
def treino(df,model,train_submit=False):
if train_submit:
X, y = df.drop("Target",axis=1),df["Target"]
model.fit(X,y)
 return model
if "Target" in df.columns:
X, y = df.drop("Target",axis=1),df["Target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,stratify=y, random_state=42)
 model.fit(X_train,y_train)
 predicted = model.predict(X_test)
 print('Classifcation report:
', classification_report(y_test, predicted))
print('Confusion matrix:
', confusion_matrix(y_test, predicted))
return model
else:
return model.predict(df )
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 submission = pd.read_csv(".. /input/titanic/gender_submission.csv" )<count_missing_values>
def treino_sampling(df,model,train_submit=False):
if train_submit:
X, y = df.drop("Target",axis=1),df["Target"]
model.fit(X,y)
 return model
if "Target" in df.columns:
resampling = RandomOverSampler()
pipeline = Pipeline([('Resampling', resampling),('XGBClassifier', model)])
 X, y = df.drop("Target",axis=1),df["Target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,stratify=y, random_state=42)
 pipeline.fit(X_train, y_train)
 predicted = model.predict(X_test)
 print('Classifcation report:
', classification_report(y_test, predicted))
print('Confusion matrix:
', confusion_matrix(y_test, predicted))
return model
else:
return model.predict(df )
train.isnull().sum()<count_missing_values>
def sampling_predict(df,func):
resampling = func()
model = XGBClassifier()
pipeline = Pipeline([('Resampling', resampling),('XGBClassifier', model)])
 X=df.drop("Target",axis=1)
 y=df.Target
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=0)
 pipeline.fit(X_train, y_train)
 predicted = pipeline.predict(X_test)
 print('Classifcation report:
', classification_report(y_test, predicted))
print('Confusion matrix:
', confusion_matrix(y_test, predicted))
train.isnull().sum()<count_values>
xgb=XGBClassifier()
modelo_final_sem_sampler = treino(df=dropa_trata(df),model=xgb )
train.groupby('Pclass' ).Survived.value_counts()<feature_engineering>
under = ["RandomUnderSampler","EditedNearestNeighbours",
"RepeatedEditedNearestNeighbours",
"NeighbourhoodCleaningRule","AllKNN","TomekLinks"]
over =("RandomOverSampler","ADASYN","BorderlineSMOTE","SMOTE","SVMSMOTE")
 df_d1 = df.fillna(0 )
train['Name'] = train.Name.str.extract('([A-Za-z]+)\.',expand = False )<count_values>
for model in under:
print("
"+model+"
")
 sampling_predict(dropa_trata(df_d1),eval(model))
top6 = train['Name'].value_counts() [:6].index.to_list()
top6<feature_engineering>
for model in over:
print("
"+model+"
")
 sampling_predict(dropa_trata(df_d1),eval(model))
train['Name'] = train['Name'].apply(lambda x: x if x in top6 else 'Other' )<count_values>
modelo_final = treino_sampling(dropa_trata(df),model=xgb )
<feature_engineering><EOS> 
df_test = pd.read_csv("/kaggle/input/costa-rican-household-poverty-prediction/test.csv")
 preds = treino(dropa_trata(df_test),modelo_final_sem_sampler)
 z = pd.Series(preds,name="Target")
 df_entrega = pd.concat([df_test.Id,z], axis=1)
 df_entrega.to_csv("/kaggle/working/submission.csv",index=False )