Kaggle competition: Costa Rican Household Poverty Level Prediction
for col in train.columns:
print(col, str(round(100* train[col].isnull().sum() / len(train), 2)) + '%' )<feature_engineering>
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape
train['LastName'] = train['Name'].str.split(',', expand=True)[0]
test['LastName'] = test['Name'].str.split(',', expand=True)[0]
ds = pd.concat([train, test])
 sur = list()
died = list()
for index, row in ds.iterrows() :
s = ds[(ds['LastName']==row['LastName'])&(ds['Survived']==1)]
d = ds[(ds['LastName']==row['LastName'])&(ds['Survived']==0)]
s=len(s)
 if row['Survived'] == 1:
s-=1
d=len(d)
 if row['Survived'] == 0:
d-=1
sur.append(s)
 died.append(d)
 ds['FamilySurvived'] = sur
ds['FamilyDied'] = died
ds['FamilySize'] = ds['SibSp'] + ds['Parch'] + 1
ds['IsAlone'] = 0
ds.loc[ds['FamilySize'] == 1, 'IsAlone'] = 1
ds['Fare'] = ds['Fare'].fillna(train['Fare'].median())
 ds['Embarked'] = ds['Embarked'].fillna('Q')
 train = ds[ds['Survived'].notnull() ]
test = ds[ds['Survived'].isnull() ]
test = test.drop(['Survived'], axis=1)
 train['rich_woman'] = 0
test['rich_woman'] = 0
train['men_3'] = 0
test['men_3'] = 0
train.loc[(train['Pclass']<=2)&(train['Sex']=='female'), 'rich_woman'] = 1
test.loc[(test['Pclass']<=2)&(test['Sex']=='female'), 'rich_woman'] = 1
train.loc[(train['Pclass']==3)&(train['Sex']=='male'), 'men_3'] = 1
test.loc[(test['Pclass']==3)&(test['Sex']=='male'), 'men_3'] = 1
train['rich_woman'] = train['rich_woman'].astype(np.int8)
 test['rich_woman'] = test['rich_woman'].astype(np.int8)
 train["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in train['Cabin']])
 test['Cabin'] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in test['Cabin']])
 for cat in ['Pclass', 'Sex', 'Embarked', 'Cabin']:
train = pd.concat([train, pd.get_dummies(train[cat], prefix=cat)], axis=1)
 train = train.drop([cat], axis=1)
 test = pd.concat([test, pd.get_dummies(test[cat], prefix=cat)], axis=1)
 test = test.drop([cat], axis=1)
 train = train.drop(['PassengerId', 'Ticket', 'LastName', 'SibSp', 'Parch', 'Sex_male', 'Name'], axis=1)
 test = test.drop(['PassengerId', 'Ticket', 'LastName', 'SibSp', 'Parch', 'Sex_male', 'Name'], axis=1)
 train = train.fillna(-1)
 test = test.fillna(-1)
 train.head()<split>
x_train = df['Id']
x_test = test['Id']
y_train = df['Target']
df_all = df.append(test)
 df_all.shape
y = train['Survived']
X = train.drop(['Survived', 'Cabin_T'], axis=1)
 X_test = test.copy()
X, X_val, y, y_val = train_test_split(X, y, random_state=0, test_size=0.2, shuffle=False )<train_model>
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split,cross_val_score, GridSearchCV
from sklearn.metrics import classification_report,multilabel_confusion_matrix
class Optimizer:
def __init__(self, metric, trials=30):
self.metric = metric
self.trials = trials
self.sampler = TPESampler(seed=666)
 def objective(self, trial):
model = create_model(trial)
 model.fit(X, y)
 preds = model.predict(X_val)
 if self.metric == 'acc':
return accuracy_score(y_val, preds)
 else:
return f1_score(y_val, preds)
 def optimize(self):
study = optuna.create_study(direction="maximize", sampler=self.sampler)
 study.optimize(self.objective, n_trials=self.trials)
 return study.best_params<choose_model_class>
titulo = 'Parentesco'
variaveis = 'parentesco1','parentesco2','parentesco3','parentesco4','parentesco5','parentesco6','parentesco7','parentesco8','parentesco9','parentesco10','parentesco11','parentesco12'
eixoX = ['Chefe de família','Cônjugue','Filho','Divorciado','Genro/Nora','Neto','Pai','Sogro','Irmão','Cunhada','Outro Familiar','Outro Não Familiar']
grafico_barras(variaveis,eixoX,titulo )
rf = RandomForestClassifier(random_state=666)
 rf.fit(X, y)
 preds = rf.predict(X_val)
 print('Random Forest accuracy: ', accuracy_score(y_val, preds))
print('Random Forest f1-score: ', f1_score(y_val, preds))
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 2, 150)
 min_samples_leaf = trial.suggest_int("min_samples_leaf", 1, 10)
 model = RandomForestClassifier(
 min_samples_leaf=min_samples_leaf,
n_estimators=n_estimators,
max_depth=max_depth,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 rf_f1_params = optimizer.optimize()
rf_f1_params['random_state'] = 666
rf_f1 = RandomForestClassifier(**rf_f1_params)
 rf_f1.fit(X, y)
 preds = rf_f1.predict(X_val)
 print('Optimized on F1 score')
 print('Optimized Random Forest: ', accuracy_score(y_val, preds))
print('Optimized Random Forest f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 rf_acc_params = optimizer.optimize()
rf_acc_params['random_state'] = 666
rf_acc = RandomForestClassifier(**rf_acc_params)
 rf_acc.fit(X, y)
 preds = rf_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized Random Forest: ', accuracy_score(y_val, preds))
print('Optimized Random Forest f1-score: ', f1_score(y_val, preds))<choose_model_class>
varNumericas = ['v2a1','rooms','v18q','v18q1','r4h1','r4h2','r4h3','r4m1','r4m2','r4m3','r4t1','r4t2','r4t3','tamhog','tamviv','escolari','rez_esc','hhsize','hogar_nin','hogar_adul','hogar_mayor','hogar_total','dependency','edjefe','edjefa','meaneduc','bedrooms','overcrowding','qmobilephone','age','SQBescolari','SQBage','SQBhogar_total','SQBedjefe','SQBhogar_nin','SQBovercrowding','SQBdependency','SQBmeaned','agesq']
xgb = XGBClassifier(random_state=666)
 xgb.fit(X, y)
 preds = xgb.predict(X_val)
 print('XGBoost accuracy: ', accuracy_score(y_val, preds))
print('XGBoost f1-score: ', f1_score(y_val, preds))
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 1, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0000001, 1)
 gamma = trial.suggest_uniform('gamma', 0.0000001, 1)
 model = XGBClassifier(
 learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
gamma=gamma,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 xgb_f1_params = optimizer.optimize()
xgb_f1_params['random_state'] = 666
xgb_f1 = XGBClassifier(**xgb_f1_params)
 xgb_f1.fit(X, y)
 preds = xgb_f1.predict(X_val)
 print('Optimized on F1 score')
 print('Optimized XGBoost accuracy: ', accuracy_score(y_val, preds))
print('Optimized XGBoost f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 xgb_acc_params = optimizer.optimize()
xgb_acc_params['random_state'] = 666
xgb_acc = XGBClassifier(**xgb_acc_params)
 xgb_acc.fit(X, y)
 preds = xgb_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized XGBoost accuracy: ', accuracy_score(y_val, preds))
print('Optimized XGBoost f1-score: ', f1_score(y_val, preds))<choose_model_class>
naoUsar = ['idhogar','Id','Target']
naoUsarNumericas = ['tamhog','hogar_total','agesq','hhsize']
lgb = LGBMClassifier(random_state=666)
 lgb.fit(X, y)
 preds = lgb.predict(X_val)
 print('LightGBM accuracy: ', accuracy_score(y_val, preds))
print('LightGBM f1-score: ', f1_score(y_val, preds))
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 1, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0000001, 1)
 num_leaves = trial.suggest_int("num_leaves", 2, 3000)
 min_child_samples = trial.suggest_int('min_child_samples', 3, 200)
 model = LGBMClassifier(
 learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
num_leaves=num_leaves,
min_child_samples=min_child_samples,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 lgb_f1_params = optimizer.optimize()
lgb_f1_params['random_state'] = 666
lgb_f1 = LGBMClassifier(**lgb_f1_params)
 lgb_f1.fit(X, y)
 preds = lgb_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized LightGBM accuracy: ', accuracy_score(y_val, preds))
print('Optimized LightGBM f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 lgb_acc_params = optimizer.optimize()
lgb_acc_params['random_state'] = 666
lgb_acc = LGBMClassifier(**lgb_acc_params)
 lgb_acc.fit(X, y)
 preds = lgb_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized LightGBM accuracy: ', accuracy_score(y_val, preds))
print('Optimized LightGBM f1-score: ', f1_score(y_val, preds))<compute_train_metric>
df['Target'].value_counts().sort_values()
lr = LogisticRegression(random_state=666)
 lr.fit(X, y)
 preds = lr.predict(X_val)
 print('Logistic Regression: ', accuracy_score(y_val, preds))
print('Logistic Regression f1-score: ', f1_score(y_val, preds))<choose_model_class>
df_all['edjefa'].value_counts()
dt = DecisionTreeClassifier(random_state=666)
 dt.fit(X, y)
 preds = dt.predict(X_val)
 print('Decision Tree accuracy: ', accuracy_score(y_val, preds))
print('Decision Tree f1-score: ', f1_score(y_val, preds))
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 min_samples_split = trial.suggest_int('min_samples_split', 2, 16)
 min_weight_fraction_leaf = trial.suggest_uniform('min_weight_fraction_leaf', 0.0, 0.5)
 min_samples_leaf = trial.suggest_int('min_samples_leaf', 1, 10)
 model = DecisionTreeClassifier(
 min_samples_split=min_samples_split,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_depth=max_depth,
min_samples_leaf=min_samples_leaf,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 dt_f1_params = optimizer.optimize()
dt_f1_params['random_state'] = 666
dt_f1 = DecisionTreeClassifier(**dt_f1_params)
 dt_f1.fit(X, y)
 preds = dt_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized Decision Tree accuracy: ', accuracy_score(y_val, preds))
print('Optimized Decision Tree f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 dt_acc_params = optimizer.optimize()
dt_acc_params['random_state'] = 666
dt_acc = DecisionTreeClassifier(**dt_acc_params)
 dt_acc.fit(X, y)
 preds = dt_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized Decision Tree accuracy: ', accuracy_score(y_val, preds))
print('Optimized Decision Tree f1-score: ', f1_score(y_val, preds))<choose_model_class>
df_all['edjefe'].value_counts()
bc = BaggingClassifier(random_state=666)
 bc.fit(X, y)
 preds = bc.predict(X_val)
 print('Bagging Classifier accuracy: ', accuracy_score(y_val, preds))
print('Bagging Classifier f1-score: ', f1_score(y_val, preds))
def create_model(trial):
n_estimators = trial.suggest_int('n_estimators', 2, 200)
 max_samples = trial.suggest_int('max_samples', 1, 100)
 model = BaggingClassifier(
 n_estimators=n_estimators,
max_samples=max_samples,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 bc_f1_params = optimizer.optimize()
bc_f1_params['random_state'] = 666
bc_f1 = BaggingClassifier(**bc_f1_params)
 bc_f1.fit(X, y)
 preds = bc_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized Bagging Classifier accuracy: ', accuracy_score(y_val, preds))
print('Optimized Bagging Classifier f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 bc_acc_params = optimizer.optimize()
bc_acc_params['random_state'] = 666
bc_acc = BaggingClassifier(**bc_acc_params)
 bc_acc.fit(X, y)
 preds = bc_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized Bagging Classifier accuracy: ', accuracy_score(y_val, preds))
print('Optimized Bagging Classifier f1-score: ', f1_score(y_val, preds))<choose_model_class>
df_all['dependency'].value_counts()
knn = KNeighborsClassifier()
knn.fit(X, y)
 preds = knn.predict(X_val)
 print('KNN accuracy: ', accuracy_score(y_val, preds))
print('KNN f1-score: ', f1_score(y_val, preds))
sampler = TPESampler(seed=0)
 def create_model(trial):
n_neighbors = trial.suggest_int("n_neighbors", 2, 25)
 model = KNeighborsClassifier(n_neighbors=n_neighbors)
 return model
optimizer = Optimizer('f1')
 knn_f1_params = optimizer.optimize()
knn_f1 = KNeighborsClassifier(**knn_f1_params)
 knn_f1.fit(X, y)
 preds = knn_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized KNN accuracy: ', accuracy_score(y_val, preds))
print('Optimized KNN f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 knn_acc_params = optimizer.optimize()
knn_acc = KNeighborsClassifier(**knn_acc_params)
 knn_acc.fit(X, y)
 preds = knn_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized KNN accuracy: ', accuracy_score(y_val, preds))
print('Optimized KNN f1-score: ', f1_score(y_val, preds))<choose_model_class>
mapeamento = {'yes': 1, 'no': 0}
df_all['edjefa'] = df_all['edjefa'].replace(mapeamento ).astype(int)
 df_all['edjefe'] = df_all['edjefe'].replace(mapeamento ).astype(int)
 df_all['dependency'] = df_all['dependency'].replace(mapeamento ).astype(float )
abc = AdaBoostClassifier(random_state=666)
 abc.fit(X, y)
 preds = abc.predict(X_val)
 print('AdaBoost accuracy: ', accuracy_score(y_val, preds))
print('AdaBoost f1-score: ', f1_score(y_val, preds))
def create_model(trial):
n_estimators = trial.suggest_int("n_estimators", 2, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0005, 1.0)
 model = AdaBoostClassifier(
 n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=666 
)
 return model
optimizer = Optimizer('f1')
 abc_f1_params = optimizer.optimize()
abc_f1_params['random_state'] = 666
abc_f1 = AdaBoostClassifier(**abc_f1_params)
 abc_f1.fit(X, y)
 preds = abc_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized AdaBoost accuracy: ', accuracy_score(y_val, preds))
print('Optimized AdaBoost f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 abc_acc_params = optimizer.optimize()
abc_acc_params['random_state'] = 666
abc_acc = AdaBoostClassifier(**abc_acc_params)
 abc_acc.fit(X, y)
 preds = abc_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized AdaBoost accuracy: ', accuracy_score(y_val, preds))
print('Optimized AdaBoost f1-score: ', f1_score(y_val, preds))<choose_model_class>
df_all.isnull().sum()
et = ExtraTreesClassifier(random_state=666)
 et.fit(X, y)
 preds = et.predict(X_val)
 print('ExtraTreesClassifier accuracy: ', accuracy_score(y_val, preds))
print('ExtraTreesClassifier f1-score: ', f1_score(y_val, preds))
def create_model(trial):
n_estimators = trial.suggest_int("n_estimators", 2, 150)
 max_depth = trial.suggest_int("max_depth", 2, 6)
 model = ExtraTreesClassifier(
 n_estimators=n_estimators,
max_depth=max_depth,
random_state=0 
)
 return model
optimizer = Optimizer('f1')
 et_f1_params = optimizer.optimize()
et_f1_params['random_state'] = 666
et_f1 = ExtraTreesClassifier(**et_f1_params)
 et_f1.fit(X, y)
 preds = et_f1.predict(X_val)
 print('Optimized on F1-score')
 print('Optimized ExtraTreesClassifier accuracy: ', accuracy_score(y_val, preds))
print('Optimized ExtraTreesClassifier f1-score: ', f1_score(y_val, preds))
optimizer = Optimizer('acc')
 et_acc_params = optimizer.optimize()
et_acc_params['random_state'] = 666
et_acc = ExtraTreesClassifier(**et_acc_params)
 et_acc.fit(X, y)
 preds = et_acc.predict(X_val)
 print('Optimized on accuracy')
 print('Optimized ExtraTreesClassifier accuracy: ', accuracy_score(y_val, preds))
print('Optimized ExtraTreesClassifier f1-score: ', f1_score(y_val, preds))<train_on_grid>
df_all['v2a1'].fillna(-1, inplace=True)
 df_all['v18q1'].fillna(0, inplace=True)
 df_all['SQBmeaned'].fillna(-1, inplace=True)
 df_all['meaneduc'].fillna(-1, inplace=True)
 df_all['rez_esc'].fillna(-1, inplace=True )
model = SuperLearner(
 folds=5,
random_state=666 
)
 model.add(
 [
bc,
lgb,
xgb,
rf,
dt,
knn
] 
)
 model.add_meta(
 LogisticRegression() 
)
 model.fit(X, y)
 preds = model.predict(X_val)
 print('SuperLearner accuracy: ', accuracy_score(y_val, preds))
print('SuperLearner f1-score: ', f1_score(y_val, preds))<choose_model_class>
df_all.isnull().sum()
mdict = {
'RF': RandomForestClassifier(random_state=666),
'XGB': XGBClassifier(random_state=666),
'LGBM': LGBMClassifier(random_state=666),
'DT': DecisionTreeClassifier(random_state=666),
'KNN': KNeighborsClassifier() ,
'BC': BaggingClassifier(random_state=666),
'OARF': RandomForestClassifier(**rf_acc_params),
'OFRF': RandomForestClassifier(**rf_f1_params),
'OAXGB': XGBClassifier(**xgb_acc_params),
'OFXGB': XGBClassifier(**xgb_f1_params),
'OALGBM': LGBMClassifier(**lgb_acc_params),
'OFLGBM': LGBMClassifier(**lgb_f1_params),
'OADT': DecisionTreeClassifier(**dt_acc_params),
'OFDT': DecisionTreeClassifier(**dt_f1_params),
'OAKNN': KNeighborsClassifier(**knn_acc_params),
'OFKNN': KNeighborsClassifier(**knn_f1_params),
'OABC': BaggingClassifier(**bc_acc_params),
'OFBC': BaggingClassifier(**bc_f1_params),
'OAABC': AdaBoostClassifier(**abc_acc_params),
'OFABC': AdaBoostClassifier(**abc_f1_params),
'OAET': ExtraTreesClassifier(**et_acc_params),
'OFET': ExtraTreesClassifier(**et_f1_params),
'LR': LogisticRegression(random_state=666),
'ABC': AdaBoostClassifier(random_state=666),
'SGD': SGDClassifier(random_state=666),
'ET': ExtraTreesClassifier(random_state=666),
'MLP': MLPClassifier(random_state=666),
'GB': GradientBoostingClassifier(random_state=666),
'RDG': RidgeClassifier(random_state=666),
'PCP': Perceptron(random_state=666),
'PAC': PassiveAggressiveClassifier(random_state=666)
 }<choose_model_class>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target']]
def create_model(trial):
model_names = list()
models_list = [
'RF', 'XGB', 'LGBM', 'DT',
'KNN', 'BC', 'OARF', 'OFRF',
'OAXGB', 'OFXGB', 'OALGBM',
'OFLGBM', 'OADT', 'OFDT',
'OAKNN', 'OFKNN', 'OABC',
'OFBC', 'OAABC', 'OFABC',
'OAET', 'OFET', 'LR',
'ABC', 'SGD', 'ET',
'MLP', 'GB', 'RDG',
'PCP', 'PAC'
]
head_list = [
'RF', 'XGB', 'LGBM', 'DT',
'KNN', 'BC', 'LR', 'ABC',
'SGD', 'ET', 'MLP', 'GB',
'RDG', 'PCP', 'PAC'
]
n_models = trial.suggest_int("n_models", 2, 6)
 for i in range(n_models):
model_item = trial.suggest_categorical('model_{}'.format(i), models_list)
 if model_item not in model_names:
model_names.append(model_item)
 folds = trial.suggest_int("folds", 2, 6)
 model = SuperLearner(
 folds=folds,
random_state=666 
)
 models = [
mdict[item] for item in model_names
]
model.add(models)
 head = trial.suggest_categorical('head', head_list)
 model.add_meta(
 mdict[head] 
)
 return model
def objective(trial):
model = create_model(trial)
 model.fit(X, y)
 preds = model.predict(X_val)
 score = accuracy_score(y_val, preds)
 return score
study = optuna.create_study(
 direction="maximize",
sampler=sampler 
)
 study.optimize(
 objective,
n_trials=50
)<train_model>
rf = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42 )
model = SuperLearner(
 folds=folds,
random_state=666 
)
 models = [
mdict[item] for item in result
]
model.add(models)
 model.add_meta(mdict[head])
 model.fit(X, y)
 preds = model.predict(X_val)
 print('Optimized SuperLearner accuracy: ', accuracy_score(y_val, preds))
print('Optimized SuperLearner f1-score: ', f1_score(y_val, preds))<predict_on_test>
rf.fit(train[feats], train['Target'] )
preds = model.predict(X_test)
 preds = preds.astype(np.int16 )<save_to_csv>
test['Target'] = rf.predict(test[feats] ).astype(int )
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = preds
submission.to_csv('submission.csv', index=False )<load_from_csv>
test['Target'].value_counts(normalize=True )
sns.set()
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )<count_missing_values>
test[['Id', 'Target']].to_csv('submission_1.csv', index=False )
print('Out of 891 entries, cabin is missing 687 so I'm droping it, also droping ticket and embarked because it isnt relevant')
 train.isna().sum()<drop_column>
varNaoUtilizadas = ['Id', 'idhogar', 'Target']
varNaoUtilizadasCat = ['female','area2']
varNaoUtilizadasNum = ['tamhog','hogar_total','hhsize']
varNaoUtilizadasSQ = ['SQBescolari','SQBage','SQBhogar_total','SQBedjefe','SQBhogar_nin','SQBovercrowding','SQBdependency','SQBmeaned','agesq']
varNaoUtilizadas = varNaoUtilizadas + varNaoUtilizadasCat + varNaoUtilizadasNum + varNaoUtilizadasSQ
varNaoUtilizadas
train = train.drop(['Cabin', 'Ticket', 'Embarked'], axis=1)
 test = test.drop(['Cabin', 'Ticket', 'Embarked'], axis=1 )<count_missing_values>
feats = [c for c in df_all.columns if c not in varNaoUtilizadas]
print('filling age with age mean')
 train.isna().sum()<feature_engineering>
rf.fit(train[feats], train['Target'] )
train['Age'] = train['Age'].fillna(np.mean(train['Age']))<count_missing_values>
test['Target'] = rf.predict(test[feats] ).astype(int )
print('age and fare filling with mean')
 test.isna().sum()<feature_engineering>
test['Target'].value_counts(normalize=True )
test['Age'] = test['Age'].fillna(np.mean(test['Age']))
test['Fare'] = test['Fare'].fillna(np.mean(test['Fare']))<define_variables>
test[['Id', 'Target']].to_csv('submission.csv', index=False)
selected_features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare']<categorify>
varNaoUtilizadas = ['Id', 'idhogar', 'Target']
varNaoUtilizadasCat = ['female','area2']
varNaoUtilizadasNum = ['tamhog','hogar_total','hhsize']
varNaoUtilizadasSQ = ['SQBescolari','SQBage','SQBhogar_total','SQBedjefe','SQBhogar_nin','SQBovercrowding','SQBdependency','SQBmeaned','agesq']
varPoucosRegistros = ['techoentrepiso','techocane','techootro','abastaguafuera','abastaguano','sanitario1','sanitario2', 'sanitario5','sanitario6','energcocinar1', 'energcocinar3','energcocinar4','elimbasu2','elimbasu3','elimbasu4','elimbasu6','tipovivi2','tipovivi3','tipovivi4','tipovivi5']
varNaoUtilizadas = varNaoUtilizadas + varNaoUtilizadasCat + varNaoUtilizadasNum + varNaoUtilizadasSQ + varPoucosRegistros
varNaoUtilizadas
X_train = train[selected_features]
X_test = test[selected_features]
X_train = pd.get_dummies(X_train)
 X_test = pd.get_dummies(X_test)
 y_train = train['Survived']<train_model>
feats = [c for c in df_all.columns if c not in varNaoUtilizadas]
model = svm.LinearSVC()
model.fit(X_train, y_train )<train_model>
rf.fit(train[feats], train['Target'] )
model = svm.SVC()
model.fit(X_train, y_train )<train_model>
test['Target'] = rf.predict(test[feats] ).astype(int )
model = svm.NuSVC()
model.fit(X_train, y_train )<train_model>
test['Target'].value_counts(normalize=True )
model = neighbors.KNeighborsClassifier()
model.fit(X_train, y_train )<train_model>
test[['Id', 'Target']].to_csv('submission.csv', index=False)
model = GradientBoostingClassifier(n_estimators=130, learning_rate=0.05)
 model.fit(X_train, y_train )<predict_on_test>
rf = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced' )
predictions = model.predict(X_test )<save_to_csv>
feats = [c for c in df_all.columns if c not in varNaoUtilizadas]
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")
 output<set_options>
rf.fit(train[feats], train['Target'] )
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
test['Target'] = rf.predict(test[feats] ).astype(int )
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train.describe(include="all" )<define_variables>
test['Target'].value_counts(normalize=True )
ids = test['PassengerId']<count_missing_values>
test[['Id', 'Target']].to_csv('submission.csv', index=False)
print(pd.isnull(train ).sum() )<data_type_conversions>
param_grid = {'max_depth': [None,5,10],
'max_leaf_nodes': [None,2,6],
'min_impurity_decrease' : [1,1e-3],
'n_jobs': [-1],
'min_samples_leaf': [2,4],
'n_estimators': [100,300,700],
'class_weight' : [None,'balanced']}
grid = GridSearchCV(rf,param_grid=param_grid,cv=4,scoring='f1_macro' )
all_data['Embarked'].fillna(all_data['Embarked'].mode() [0], inplace = True)
 all_data['Fare'].fillna(all_data['Fare'].median() , inplace = True)
<count_values>
grid.fit(train[feats], train['Target'] )
all_data['Title'] = all_data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 all_data['Title'].value_counts()<count_values>
grid_df = pd.DataFrame(grid.cv_results_)
 grid_df
frequent_titles = all_data['Title'].value_counts() [:5].index.tolist()
frequent_titles<feature_engineering>
grid_df.sort_values('rank_test_score',ascending=True ).iloc[0,:]
all_data['Title'] = all_data['Title'].apply(lambda x: x if x in frequent_titles else 'Other')
<feature_engineering>
grid.best_params_
median_ages = {}
for title in frequent_titles:
median_ages[title] = all_data.loc[all_data['Title'] == title]['Age'].median()
median_ages['Other'] = all_data['Age'].median()
all_data.loc[all_data['Age'].isnull() , 'Age'] = all_data[all_data['Age'].isnull() ]['Title'].map(median_ages )<categorify>
rf = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=100,max_leaf_nodes=None,
min_impurity_decrease=0.001, min_samples_leaf=4,
verbose=0, class_weight='balanced' )
Cat_Features = ['Sex', 'Embarked', 'Title']
for feature in Cat_Features:
label = LabelEncoder()
all_data[feature] = label.fit_transform(all_data[feature] )<categorify>
feats = [c for c in df_all.columns if c not in varNaoUtilizadas]
Cont_Features = ['Age', 'Fare']
num_bins = 5
for feature in Cont_Features:
bin_feature = feature + 'Bin'
all_data[bin_feature] = pd.qcut(all_data[feature], num_bins)
 label = LabelEncoder()
all_data[bin_feature] = label.fit_transform(all_data[bin_feature] )<data_type_conversions>
rf.fit(train[feats], train['Target'] )
all_data['Surname'] = all_data.Name.str.extract(r'([A-Za-z]+),', expand=False)
 all_data['TicketPrefix'] = all_data.Ticket.str.extract(r' (.*\d)', expand=False)
 all_data['Surname_Ticket'] = all_data['Surname'] + all_data['TicketPrefix']
all_data['IsFamily'] = all_data.Surname_Ticket.duplicated(keep=False ).astype(int )<feature_engineering>
test['Target'] = rf.predict(test[feats] ).astype(int )
all_data['Child'] = all_data.Age.map(lambda x: 1 if x <=16 else 0)
 FamilyWithChild = all_data[(all_data.IsFamily==1)&(all_data.Child==1)]['Surname_Ticket'].unique()
len(FamilyWithChild )<feature_engineering>
test['Target'].value_counts(normalize=True )
all_data['FamilyId'] = 0
for ind, identifier in enumerate(FamilyWithChild):
all_data.loc[all_data.Surname_Ticket==identifier, ['FamilyId']] = ind + 1<prepare_x_and_y>
test[['Id', 'Target']].to_csv('submission.csv', index=False)