Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
print('*'*50)
 print('Pandas Version : ', pd.__version__)
 print('Numpy Version : ', np.__version__)
 print('Matplotlib Version: ', mpl.__version__)
 print('Seaborn Version : ', sns.__version__)
 print('SKLearn Version : ', sk.__version__)
 print('*'*50 )
ts['nota_mat'] = rf.predict(ts[feats] )<save_to_csv>
sns.set_style('whitegrid')
 pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
seed = 42
v_size = 0.33
num_folds = 10
scoring = 'accuracy'
np.random.seed(seed )
ts[['codigo_mun','nota_mat']].to_csv('lucio.csv',index=False )<load_from_csv>
path_train = '.. /input/train.csv'
path_test = '.. /input/test.csv'
df_train = pd.read_csv(path_train)
 df_test = pd.read_csv(path_test )
myfile = pd.read_csv(".. /input/train.csv")
 test_X = pd.read_csv(".. /input/test.csv" )<drop_column>
print('Datasets shapes: ')
 print("Training shape: ", df_train.shape)
 print("Test shape : ", df_test.shape )
test_X = test_X[['exp_vida','anos_estudo_empreendedor','idhm', 'perc_pop_econ_ativa']]<data_type_conversions>
missingData(df_train )
test_X = test_X.fillna(test_X.mean() )<feature_engineering>
missingData(df_test )
a = myfile[['densidade_dem']]
b = myfile[['area']]
c = myfile[['codigo_mun']]
d = myfile[['ranking_igm']]
e = myfile[['comissionados_por_servidor']]
myfile['densidade_dem'] = a.apply(lambda x: x.str.replace(',',''))
myfile['area'] = b.apply(lambda x: x.str.replace(',',''))
myfile['codigo_mun'] = c.apply(lambda x: x.str.replace('ID_',''))
myfile['ranking_igm'] = d.apply(lambda x: x.str.replace('º',''))
myfile['comissionados_por_servidor'] = e.apply(lambda x: x.str.replace('%',''))
e = myfile[['comissionados_por_servidor']]
myfile['comissionados_por_servidor'] = e.apply(lambda x: x.str.replace('
myfile['densidade_dem'] = pd.to_numeric(myfile['densidade_dem'])
 myfile['area'] = pd.to_numeric(myfile['area'])
 myfile['codigo_mun'] = pd.to_numeric(myfile['codigo_mun'])
 myfile['ranking_igm'] = pd.to_numeric(myfile['ranking_igm'])
 myfile['comissionados_por_servidor'] = pd.to_numeric(myfile['comissionados_por_servidor'] )<categorify>
df_train.drop("Cabin", axis=1, inplace = True)
 df_test.drop("Cabin", axis=1, inplace = True )
a = myfile['regiao']
regioes = pd.get_dummies(a)
 myfile = pd.concat([myfile, regioes], axis=1 )<categorify>
df_train["Age"].fillna(df_train["Age"].median() , inplace = True)
 df_test["Age"].fillna(df_test["Age"].median() , inplace = True )
a = myfile['estado']
estado = pd.get_dummies(a)
 myfile = pd.concat([myfile, estado], axis=1 )<categorify>
df_train['Embarked'] = df_train['Embarked'].fillna(df_train['Embarked'].mode() [0] )
a = myfile['porte']
porte = pd.get_dummies(a)
 myfile = pd.concat([myfile, porte], axis=1 )<count_missing_values>
def displayNanValues() :
print("Check the NaN value in train data")
 print(df_train.isnull().sum())
 print("---"*30)
 print("Check the NaN value in test data")
 print(df_test.isnull().sum())
 print("---"*30 )
myfile.isnull().sum()<drop_column>
all_data = [df_train, df_test]
train_df = myfile[['nota_mat','exp_vida',
'anos_estudo_empreendedor','idhm', 'perc_pop_econ_ativa']]
train_df.dropna(inplace=True)
 train_X = train_df.drop(columns=['nota_mat'])
 train_X.head()<prepare_x_and_y>
def featureExtraction(all_data):
for dataset in all_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for dataset in all_data:
dataset['Age_bin'] = pd.cut(dataset['Age'], bins=[0,18,60,120], labels=['Children','Adult','Elder'])
 for dataset in all_data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 for dataset in all_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs' )
train_y = train_df[['nota_mat']]
train_y.head()<choose_model_class>
featureExtraction(all_data )
model = Sequential()
n_cols = train_X.shape[1]
model.add(Dense(10, activation='relu', input_shape=(n_cols,)))
 model.add(Dense(10, activation='relu'))
model.add(Dense(1))<choose_model_class>
dfTrain = df_train.copy()
dfTest = df_test.copy()
model.compile(optimizer='adam', loss='mean_squared_error' )<train_model>
traindf = pd.get_dummies(dfTrain, columns = ["Pclass","Title",'FamilySize',"Sex","Age_bin","Embarked"],
prefix=["Pclass","Title",'FamilySize',"Sex","Age_type","Em_type"])
 testdf = pd.get_dummies(dfTest, columns = ["Pclass","Title",'FamilySize',"Sex","Age_bin","Embarked"],
prefix=["Pclass","Title",'FamilySize',"Sex","Age_type","Em_type"] )
early_stopping_monitor = EarlyStopping(patience=3)
 model.fit(train_X, train_y, validation_split=0.1, epochs=30, callbacks=[early_stopping_monitor] )<predict_on_test>
allData = [traindf, testdf]
test_y_predictions = model.predict(test_X )<create_dataframe>
for dataset in allData:
drop_column = ["Age","Fare","Name","Ticket","SibSp","Parch"]
dataset.drop(drop_column, axis=1, inplace = True)
 traindf.drop(["PassengerId"], axis=1, inplace = True )
test_y_predictions = pd.DataFrame(test_y_predictions )<load_from_csv>
df_data_shuffled = traindf.iloc[np.random.permutation(len(traindf)) ]
test_X = pd.read_csv(".. /input/test.csv")
 b = test_X[['codigo_mun']]
b['codigo_mun'] = b.apply(lambda x: x.str.replace('ID_ID_',''))
b['codigo_mun'] =(b['codigo_mun'] ).astype(int)
 a = pd.concat([b, test_y_predictions], axis=1)
 a.columns = ['codigo_mun', 'nota_mat']<save_to_csv>
array = df_data_shuffled.values
features = array[:,1:].astype(float)
 targeted = array[:,0].astype(float )
a.to_csv('Maria_de_Fatima.csv', index = False )<load_from_csv>
X_train,X_test,y_train,y_test = train_test_split(features,targeted,test_size=v_size,random_state=seed)
 print('Data shapes: ')
 print("X_train shape: ", X_train.shape)
 print("X_test shape : ", X_test.shape)
 print("y_train shape: ", y_train.shape)
 print("y_test shape : ", y_test.shape )
df = pd.read_csv(".. /input/train.csv")
 ts = pd.read_csv(".. /input/test.csv" )<import_modules>
lr_clf = LogisticRegression()
lr_param_grid = {'solver' : ['liblinear', 'lbfgs'],'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000] }
best_lr = algoGridTune(lr_clf, lr_param_grid, X_train, y_train,num_folds, scoring, seed )
import math<feature_engineering>
knn_clf = KNeighborsClassifier()
knn_param_grid = {'n_neighbors':[3,5,7,9,11],
'leaf_size':[1,2,3,5],
'weights':['uniform', 'distance'],
'algorithm':['auto', 'ball_tree','kd_tree','brute']
}
best_knn = algoGridTune(knn_clf, knn_param_grid, X_train, y_train, num_folds, scoring, seed )
math.log(1000)- math.log(1100), math.log(10)- math.log(11 )<split>
dt_clf = DecisionTreeClassifier()
dt_param_grid = {'max_depth' : [3,4,5,6,7,8,9,10],
'max_features': ['sqrt', 'log2'],
'min_samples_split': [3,5,7,9,11],
'min_samples_leaf':[1,3,5,7,9,11]
}
best_dt = algoGridTune(dt_clf, dt_param_grid, X_train, y_train,num_folds, scoring, seed )
test = df[df['nota_mat'].isnull() ]
df = df[~df['nota_mat'].isnull() ]<data_type_conversions>
svc_clf = SVC()
svc_param_grid = [{"kernel": ["rbf"],
"gamma": [10 ,1, 0.1, 1e-2, 1e-3],
"C": [0.1,1,10],
"random_state" : [seed]},
{"kernel": ["linear"], "C": [0.1,1,10,100]}
]
best_SVC = algoGridTune(svc_clf, svc_param_grid, X_train, y_train, num_folds, scoring, seed )
for c in df.columns:
if df[c].dtype == 'object':
df[c] = df[c].astype('category' ).cat.codes<import_modules>
DTC = DecisionTreeClassifier()
ABC_clf = AdaBoostClassifier(DTC, random_state=seed)
 ABC_param_grid = {"base_estimator__criterion" : ["gini"],
"base_estimator__splitter" : ["best"],
"algorithm" : ["SAMME"],
"n_estimators" :[30, 100],
"learning_rate": [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3,1.5]}
best_ABC = algoGridTune(ABC_clf, ABC_param_grid, X_train, y_train, num_folds, scoring, seed )
from sklearn.model_selection import train_test_split<import_modules>
ETC_clf = ExtraTreesClassifier()
ETC_param_grid = {"max_depth": [30],
"max_features": ['sqrt'],
"min_samples_split": [2, 3, 5],
"min_samples_leaf": [1, 3, 5],
"bootstrap": [True],
"n_estimators" :[300],
"criterion": ["gini"]}
best_ETC = algoGridTune(ETC_clf, ETC_param_grid, X_train, y_train, num_folds, scoring, seed )
from sklearn.model_selection import train_test_split<split>
GBC_clf = GradientBoostingClassifier()
GBC_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100, 200, 300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [3, 5, 7],
'min_samples_leaf': [1, 5, 9],
'min_samples_split': [2, 6, 10],
'max_features': ['sqrt', 'log2']
}
best_GBC = algoGridTune(GBC_clf, GBC_param_grid, X_train, y_train, num_folds, scoring, seed )
df, valid = train_test_split(df, random_state=42 )<define_variables>
RFC_clf = RandomForestClassifier()
RFC_param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [True, False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
best_RFC = algoGridTune(RFC_clf, RFC_param_grid, X_train, y_train, num_folds, scoring, seed )
removed_cols = ['municipio','codigo_mun'] 
<import_modules>
kfold = KFold(n_splits=num_folds, random_state=seed )
from sklearn.tree import DecisionTreeRegressor<choose_model_class>
datacols = list(traindf.drop("Survived", axis=1))
dt = DecisionTreeRegressor(random_state=42, max_depth=2 )<define_variables>
passengerIds = testdf["PassengerId"].copy()
testdf.drop(["PassengerId"], axis=1, inplace = True )
lista= ['estado','anos_estudo_empreendedor','nota_mat', 'municipio','codigo_mun','area','capital','codigo_mun','comissionados','comissionados_por_servidor','densidade_dem','estado','exp_anos_estudo','exp_vida','gasto_pc_educacao','gasto_pc_saude','hab_p_medico','idhm','indice_governanca','jornada_trabalho','municipio','nota_mat','participacao_transf_receita', 'perc_pop_econ_ativa','pib','pib_pc','populacao','porte','ranking_igm','regiao','servidores','taxa_empreendedorismo']<split>
test = testdf.values
train,valid = train_test_split(df,random_state=42)
 train.shape,valid.shape<train_model>
test_Survived_lr = pd.Series(best_lr.predict(test), name="LR")
 test_Survived_knn = pd.Series(best_knn.predict(test), name="KNN")
 test_Survived_dt = pd.Series(best_dt.predict(test), name="DT")
 test_Survived_SVC = pd.Series(best_SVC.predict(test), name="SVC")
 test_Survived_ABC = pd.Series(best_ABC.predict(test), name="ABC")
 test_Survived_RFC = pd.Series(best_RFC.predict(test), name="RFC")
 test_Survived_ETC = pd.Series(best_ETC.predict(test), name="ETC")
 test_Survived_GBC = pd.Series(best_GBC.predict(test), name="GBC")
 ensemble_results = pd.concat([test_Survived_lr, test_Survived_knn,
test_Survived_dt, test_Survived_SVC,
test_Survived_ABC, test_Survived_RFC,
test_Survived_ETC, test_Survived_GBC],axis=1)
 correlationHeatmap(ensemble_results, 'Correlation beetween models results' )
rf = RandomForestRegressor(random_state=42,n_estimators=100)
 feats = [c for c in df[lista].columns if c not in ['nota_mat']]
rf.fit(train[feats], train['nota_mat'] )<prepare_output>
votingC = VotingClassifier(estimators=[('lr', best_lr), 
('knn', best_knn), 
('dt', best_dt), 
('svc', best_SVC), 
('abc', best_ABC), 
('rfc', best_RFC), 
('etc', best_ETC), 
('gbc', best_GBC)],
voting='hard', n_jobs=-1)
 votingC = votingC.fit(X_train, y_train )
math.exp(2.78), math.exp(5.27 )<import_modules>
predictions = votingC.predict(test)
 test_Survived = pd.Series(votingC.predict(test), name="Survived")
 test_Survived = test_Survived.apply(int)
 results = pd.concat([passengerIds,test_Survived],axis=1)
 results.to_csv('submission.csv', columns=['PassengerId', 'Survived'], index=False )