Kaggle competition: Titanic - Machine Learning from Disaster
test_data = pd.read_csv('.. /input/iris-test.csv', index_col='Id')
 test_data.head()<prepare_x_and_y>
read_train_data = pd.read_csv('.. /input/titanic/train.csv')
 print(read_train_data )
X_train = train_data.drop(['Species'], axis=1)
 y_train = train_data['Species']
print('Forma dos dados de treino:', X_train.shape, y_train.shape )<import_modules>
numeric_train_data = read_train_data
print(numeric_train_data)
 print(type(numeric_train_data.columns))
feature_names = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
print("-------------------feature names-------------------")
 print(feature_names)
 y = numeric_train_data.Survived
X = pd.DataFrame(numeric_train_data[feature_names])
 print(X)
 print(y )
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier<train_model>
col_with_missing_values_categorical_val = [col for col in X.columns
if(X[col].isnull().any() &(X[col].dtype == 'object')) ]
print(col_with_missing_values_categorical_val)
 X[col_with_missing_values_categorical_val] = X[col_with_missing_values_categorical_val].fillna("Unknown")
 print(X )
model = LogisticRegression()
model.fit(X_train, y_train)
 print(model )<predict_on_test>
col_with_categorical_data = [col for col in X.columns
if(X[col].dtype=='object')]
dummies = pd.get_dummies(X[col_with_categorical_data])
 merged = pd.concat([X,dummies], axis=1)
 X = merged.drop(col_with_categorical_data, axis=1)
 print(X.head(62))
y_pred = model.predict(X_test)
 print('Exemplos de previsões:
', y_pred[:10] )<prepare_output>
imputed_X_train, imputed_X_test, y_train, y_test = train_test_split(imputed_X, y, test_size=0.2, random_state=0)
 print(imputed_X_train)
 print(imputed_X_test)
 print(y_train)
 print(y_test )
submission = pd.DataFrame({
'Id': X_test.index,
'Species': y_pred
})
 submission.set_index('Id', inplace=True)
 submission.head(10 )<save_to_csv>
sc_age = StandardScaler()
sc_fare = StandardScaler()
imputed_X_train['Age'] = sc_age.fit_transform(imputed_X_train['Age'].values.reshape(-1,1))
imputed_X_train['Fare'] = sc_fare.fit_transform(imputed_X_train['Fare'].values.reshape(-1,1))
imputed_X_test['Age'] = sc_age.transform(imputed_X_test['Age'].values.reshape(-1,1))
imputed_X_test['Fare'] = sc_fare.transform(imputed_X_test['Fare'].values.reshape(-1,1))
print(imputed_X_train)
 print(imputed_X_test )
submission.to_csv('iris-submission.csv' )<set_options>
model = RandomForestClassifier(bootstrap=True, max_depth=80, max_features=3, min_samples_leaf=3, min_samples_split=10, n_estimators=1000, random_state=0)
 model.fit(imputed_X_train, y_train)
 predicted_y = model.predict(imputed_X_test)
 print(predicted_y)
 calculate_mae = mean_absolute_error(y_test, predicted_y)
 print(calculate_mae )
<import_modules><EOS> 
test_data = pd.read_csv('.. /input/titanic/test.csv')
 print(feature_names)
 X_test = pd.DataFrame(test_data[feature_names])
 col_with_missing_values_categorical_val_test = [col for col in X_test.columns
if(X_test[col].isnull().any() &(X_test[col].dtype == 'object')) ]
print(col_with_missing_values_categorical_val_test)
 col_with_categorical_data_test = [col for col in X_test.columns
if(X_test[col].dtype=='object')]
dummies_test = pd.get_dummies(X_test[col_with_categorical_data_test])
 merged_test = pd.concat([X_test,dummies_test], axis=1)
 X_test = merged_test.drop(col_with_categorical_data_test, axis=1)
 X_test['Embarked_Unknown'] = 0
print(X_test)
 col_with_missing_values_test = [col for col in X_test.columns
if(X_test[col].isnull().any())]
print(col_with_missing_values_test)
 imputed_X_test_data = pd.DataFrame(my_imputer.fit_transform(X_test))
imputed_X_test_data.columns = X_test.columns
print(imputed_X_test)
 imputed_X_test_data['Age'] = sc_age.transform(imputed_X_test_data['Age'].values.reshape(-1,1))
imputed_X_test_data['Fare'] = sc_fare.transform(imputed_X_test_data['Fare'].values.reshape(-1,1))
print(imputed_X_test_data)
 test_preds = model.predict(imputed_X_test_data)
 print(type(test_preds))
print(test_preds)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId,
'Survived': test_preds})
 output.to_csv('./submission.csv', index=False)
 print(pd.read_csv('./submission.csv'))