Kaggle competition: Titanic - Machine Learning from Disaster
<import_modules>
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from numpy import savetxt
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from IPython.display import display
from sklearn import metrics
import lightgbm as lgb
from sklearn.metrics import accuracy_score<split>
base_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 base_train = base_train[['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked', 'Survived']]
del base_train['Name']
del base_train['Ticket']
del base_train['Cabin']
base_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 del base_test['Name']
del base_test['Ticket']
del base_test['Cabin']
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed )<create_dataframe>
imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean')
 imputer = imputer.fit(base_train.iloc[:, 3:4])
 base_train.iloc[:, 3:4] = imputer.transform(base_train.iloc[:, 3:4])
 base_test.iloc[:, 3:4] = imputer.transform(base_test.iloc[:, 3:4] )
d_train = lgb.Dataset(X_train, label=y_train)
 params = {}
params['objective'] = 'regression'
params['metric'] = 'mae'<train_model>
labelencoder = LabelEncoder()
base_train['Embarked'] = labelencoder.fit_transform(base_train['Embarked'].astype(str))
base_test['Embarked'] = labelencoder.fit_transform(base_test['Embarked'].astype(str))
base_train.iloc[:, 2] = labelencoder.fit_transform(base_train.iloc[:, 2])
 base_test.iloc[:, 2] = labelencoder.fit_transform(base_test.iloc[:, 2] )
model = lgb.train(params, d_train )<predict_on_test>
scaler = StandardScaler()
base_train.iloc[:, 1:8] = scaler.fit_transform(base_train.iloc[:, 1:8])
 base_test.iloc[:, 1:8] = scaler.fit_transform(base_test.iloc[:, 1:8] )
y_pred=model.predict(X_test )<compute_test_metric>
base_test[base_test==np.inf]=np.nan
base_test.fillna(base_test.mean() , inplace=True )
mae = mean_absolute_error(y_test, y_pred)
 print("MAE: {}".format(mae))<create_dataframe>
predictors_train = base_train.iloc[:, 0:8].values
class_train = base_train.iloc[:, 8].values
d_train_full = lgb.Dataset(X, label=Y)
 params = {}
params['objective'] = 'regression'
params['metric'] = 'mae'<train_model>
classificador = RandomForestClassifier(n_estimators = 40, criterion = 'entropy', random_state = 0)
 classificador.fit(predictors_train, class_train)
 predictions = classificador.predict(base_test )
model_full = lgb.train(params, d_train_full )<load_from_csv>
output = pd.DataFrame({'PassengerId': base_test.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False )