Kaggle competition: Titanic - Machine Learning from Disaster
test['nota_mat'] = rf.predict(test[feats].fillna(-1))<save_to_csv>
train_data = pd.read_csv(".. /input/titanic/train.csv")
 train_data.head()
test[['codigo_mun','nota_mat']].to_csv('randomforest.csv', index=False )<load_from_csv>
test_data = pd.read_csv(".. /input/titanic/test.csv")
 test_data.head()
df = pd.read_csv('.. /input/train.csv')
 df.shape<load_from_csv>
train_data["relatives"] = train_data["SibSp"] + train_data["Parch"]
test_data["relatives"] = test_data["SibSp"] + test_data["Parch"]
df_test = pd.read_csv('.. /input/test.csv')
 df_test.shape<concatenate>
train_data["Sex_cat"] = train_data["Sex"].astype("category" )
df = df.append(df_test, ignore_index=True, sort=False)
 df.shape<data_type_conversions>
test_data["Sex_cat"] = test_data["Sex"].astype("category" )
df['populacao'] = df['populacao'].str.replace('.', '')
 df['populacao'] = df['populacao'].str.replace('\(\)', '')
 df['populacao'] = df['populacao'].str.replace('\(1\)', '')
 df['populacao'] = df['populacao'].str.replace('\(2\)', '')
 df['populacao'] = df['populacao'].str.replace(',', '' ).astype('float')
 df['densidade_dem'] = df['densidade_dem'].str.replace(',', '' ).astype('float')
 df['area'] = df['area'].str.replace(',', '' ).astype('float')
 df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('%', '' ).astype('float' )<data_type_conversions>
train_data["Embarked_filled"] = train_data["Embarked_filled"].astype("category")
 test_data["Embarked_filled"] = test_data["Embarked_filled"].astype("category" )
df['in_na_densidade_dem'] = df['densidade_dem'].isna().astype('int')
 df['densidade_dem'].fillna(df['densidade_dem'].mean() , inplace=True)
 df['in_na_participacao_transf_receita'] = df['participacao_transf_receita'].isna().astype('int')
 df['participacao_transf_receita'].fillna(-1, inplace=True)
 df['in_na_servidores'] = df['servidores'].isna().astype('int')
 df['servidores'].fillna(-1, inplace=True)
 df['in_na_perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'].isna().astype('int')
 df['perc_pop_econ_ativa'].fillna(-1, inplace=True)
 df['in_na_gasto_pc_saude'] = df['gasto_pc_saude'].isna().astype('int')
 df['gasto_pc_saude'].fillna(-1, inplace=True)
 df['in_na_hab_p_medico'] = df['hab_p_medico'].isna().astype('int')
 df['hab_p_medico'].fillna(-1, inplace=True)
 df['in_na_exp_vida'] = df['exp_vida'].isna().astype('int')
 df['exp_vida'].fillna(df['exp_vida'].mean() , inplace=True)
 df['in_na_gasto_pc_educacao'] = df['gasto_pc_educacao'].isna().astype('int')
 df['gasto_pc_educacao'].fillna(-1, inplace=True)
 df['in_na_exp_anos_estudo'] = df['exp_anos_estudo'].isna().astype('int')
 df['exp_anos_estudo'].fillna(-1, inplace=True )<feature_engineering>
train_data["Fare_filled"] = train_data["Fare"].fillna(0)
 test_data["Fare_filled"] = test_data["Fare"].fillna(0 )
ordem_porte = ['Pequeno porte 1', 'Pequeno porte 2', 'Médio porte', 'Grande porte']
df['porte'] = pd.Categorical(df['porte'], categories=ordem_porte ).codes
df['div_gasto'] = df['gasto_pc_saude'] / df['pib']
df['exp_pib'] = df['pib'] / df['exp_vida']
df['exp_pib_estudos'] = df['pib'] / df['exp_anos_estudo']<data_type_conversions>
train_data["Fare_filled"] = train_data["Fare_filled"].astype(int)
 test_data["Fare_filled"] = test_data["Fare_filled"].astype(int )
for col in df.columns:
if df[col].dtype == 'object':
df[col] = df[col].astype('category' ).cat.codes<split>
train_data["Cabin_filled"] = train_data["Cabin"].fillna("U0")
 deck = []
for data in train_data["Cabin_filled"]:
if data[0][0] == "U":
deck.append("U")
 elif data[0][0] == "A":
deck.append("A")
 elif data[0][0] == "B":
deck.append("B")
 elif data[0][0] == "C":
deck.append("C")
 elif data[0][0] == "D":
deck.append("D")
 elif data[0][0] == "E":
deck.append("E")
 elif data[0][0] == "F":
deck.append("F")
 elif data[0][0] == "G":
deck.append("G")
 deck.append("U")
 train_data["Deck"] = deck
test = df[df['nota_mat'].isnull() ]
df = df[~df['nota_mat'].isnull() ]<filter>
test_data["Cabin_filled"] = test_data["Cabin"].fillna("U0")
 deck_test = []
for data in test_data["Cabin_filled"]:
if data[0][0] == "U":
deck_test.append("U")
 elif data[0][0] == "A":
deck_test.append("A")
 elif data[0][0] == "B":
deck_test.append("B")
 elif data[0][0] == "C":
deck_test.append("C")
 elif data[0][0] == "D":
deck_test.append("D")
 elif data[0][0] == "E":
deck_test.append("E")
 elif data[0][0] == "F":
deck_test.append("F")
 elif data[0][0] == "G":
deck_test.append("G")
 test_data["Deck"] = deck_test
df = df[df['populacao'] > 5000].copy()<split>
data = [train_data, test_data]
for dataset in data:
mean = train_data["Age"].mean()
std = test_data["Age"].std()
is_null = dataset["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = dataset["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
dataset["Age_filled"] = age_slice
dataset["Age_filled"] = dataset["Age_filled"].astype(int)
 test_data["Age_filled"].isnull().sum()
train, valid = train_test_split(df, test_size=0.2, random_state=42 )<define_variables>
data = [train_data, test_data]
for dataset in data:
dataset['Age_filled'] = dataset['Age_filled'].astype(int)
 dataset.loc[ dataset['Age_filled'] <= 11, 'Age_filled'] = 0
dataset.loc[(dataset['Age_filled'] > 11)&(dataset['Age_filled'] <= 18), 'Age_filled'] = 1
dataset.loc[(dataset['Age_filled'] > 18)&(dataset['Age_filled'] <= 22), 'Age_filled'] = 2
dataset.loc[(dataset['Age_filled'] > 22)&(dataset['Age_filled'] <= 27), 'Age_filled'] = 3
dataset.loc[(dataset['Age_filled'] > 27)&(dataset['Age_filled'] <= 33), 'Age_filled'] = 4
dataset.loc[(dataset['Age_filled'] > 33)&(dataset['Age_filled'] <= 40), 'Age_filled'] = 5
dataset.loc[(dataset['Age_filled'] > 40)&(dataset['Age_filled'] <= 66), 'Age_filled'] = 6
dataset.loc[ dataset['Age_filled'] > 66, 'Age_filled'] = 6
test_data['Age_filled'].value_counts()
feats = [c for c in df.columns if c not in ['nota_mat', 'codigo_mun', 'municipio']]<import_modules>
train_data["Deck_cat"] = train_data["Deck"].astype("category")
 test_data["Deck_cat"] = test_data["Deck"].astype("category" )
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=200, min_samples_split=5, max_depth=4, random_state=42 )<train_model>
y = train_data["Survived"]
rf.fit(train[feats], train['nota_mat'] )<predict_on_test>
X = train_data[["Pclass", "Sex_cat", "relatives", "Deck_cat", "Age_filled"]]
X_test = test_data[["Pclass", "Sex_cat", "relatives", "Deck_cat", "Age_filled"]]
preds = rf.predict(valid[feats] )<compute_test_metric>
X["Sex_cat"] = X["Sex_cat"].cat.codes
X_test["Sex_cat"] = X_test["Sex_cat"].cat.codes
accuracy_score(valid['nota_mat'], preds )<count_values>
X["Deck_cat"] = X["Deck_cat"].cat.codes
X_test["Deck_cat"] = X_test["Deck_cat"].cat.codes
valid['nota_mat'].value_counts()<predict_on_test>
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
pred_test = rf.predict(test[feats] )<save_to_csv>
model.fit(X, y )
r1 = pd.DataFrame({'codigo_mun': test['codigo_mun'], 'nota_mat': pred_test})
 r1.to_csv('r1.csv', index=False )<compute_train_metric>
predictions = model.predict(X_test )
def cv(df, test, feats, y_name, k=5):
preds, score, fis = [], [], []
chunk = df.shape[0] // k
for i in range(k):
if i+1 < k:
valid = df.iloc[i*chunk:(i+1)*chunk]
train = df.iloc[:i*chunk].append(df.iloc[(i+1)*chunk:])
 else:
valid = df.iloc[i*chunk:]
train = df.iloc[:i*chunk]
rf = RandomForestClassifier(random_state=42, n_jobs=-1, n_estimators=200)
 rf.fit(train[feats], train[y_name])
 score.append(accuracy_score(valid[y_name], rf.predict(valid[feats])))
 preds.append(rf.predict(test[feats]))
fis.append(rf.feature_importances_)
 print(i, 'OK')
 return score, preds, fis<compute_test_metric>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions} )
score, preds, fis = cv(df, test, feats, 'nota_mat' )<save_to_csv>
output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )