Kaggle competition: Titanic - Machine Learning from Disaster
df_train = df_train.drop_duplicates(subset=['check_id', 'target'] )<count_values>
train_data = pd.read_csv(".. /input/titanic/train.csv")
 train_data.head()
target_probs = df_train.target.value_counts(normalize=True ).sort_index()
target_probs<compute_test_metric>
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column > 0] )
log_loss(df_train.target, [df_train.target.value_counts(normalize=True)]*df_train.shape[0] )<load_from_csv>
features = ['Pclass', 'Sex','SibSp','Age','Parch','Fare','Embarked']
X = pd.get_dummies(train_data[features], drop_first=True)
 y = train_data["Survived"]
df_test = pd.read_csv('.. /input/dmia-sport-2019-fall-competition-1/test.csv' )<define_variables>
my_imputer = SimpleImputer()
imputed_X = my_imputer.fit_transform(X )
columns_names = ['target_{}'.format(i)for i in range(25)]<prepare_output>
forest = RandomForestClassifier(oob_score=True)
 forest.fit(imputed_X,y)
 forest.oob_score_
df_result = pd.DataFrame([target_probs.tolist() ]*len(ids), columns=columns_names)
 df_result.insert(0, 'check_id', ids)
 df_result.head(10 )<save_to_csv>
for name, score in zip(X.columns, forest.feature_importances_):
print(score, name )
df_result.to_csv('const_submission.csv', index=False )<set_options>
smart_forest = RandomForestClassifier(n_estimators = 100, max_depth=5, max_leaf_nodes=12, min_impurity_decrease=.0048,oob_score=True)
 smart_forest.fit(imputed_X,y)
 smart_forest.oob_score_
%matplotlib inline
print(os.listdir(".. /input"))<load_from_csv>
for name, score in zip(X.columns, smart_forest.feature_importances_):
print(score, name )
file_train = '.. /input/train.csv'
file_valid = '.. /input/valid.csv'
file_exemplo = '.. /input/sample_su.csv'
data_train = pd.read_csv(file_train)
 data_valid = pd.read_csv(file_valid)
 print(len(data_train), len(data_valid))<prepare_x_and_y>
bag = BaggingClassifier(
 DecisionTreeClassifier(splitter="random"),
n_estimators = 100, max_samples=1.0, oob_score=True 
)
 bag.fit(imputed_X,y)
 bag.oob_score_
Y_dummies = pd.get_dummies(data_train['is_sarcastic'] ).values
Y = data_train['is_sarcastic']
data_train_format = data_train.drop('is_sarcastic', axis=1)
 data_all = data_train_format.append(data_valid)
 data_all = data_all.drop('article_link', axis=1)
 data_all = data_all.drop('ID', axis=1)
 data_all['headline'] = data_all['headline'].apply(lambda x: x.lower())
 data_all['headline'] = data_all['headline'].apply(( lambda x: re.sub('[^a-zA-z0-9\s]','',x)))
 print(len(data_train['headline']), len(data_valid['headline']))
print(len(data_all))<string_transform>
extra_trees=ExtraTreesClassifier(n_estimators = 50, max_depth=5, max_leaf_nodes=24, bootstrap=True, oob_score=True)
 extra_trees.fit(imputed_X,y)
 extra_trees.oob_score_
max_fatures = 5000
tokenizer = Tokenizer(num_words=max_fatures, split=' ')
 tokenizer.fit_on_texts(data_all['headline'].values)
 X = tokenizer.texts_to_sequences(data_all['headline'].values)
 X = pad_sequences(X )<split>
for name, score in zip(X.columns, extra_trees.feature_importances_):
print(score, name )
data_train_split = X[:len(data_train)]
data_valid_split = X[len(data_train):]
print(len(data_train_split), len(data_valid_split))<choose_model_class>
test_data = pd.read_csv(".. /input/titanic/test.csv")
 X_test=pd.get_dummies(test_data[features], drop_first=True)
 imputed_test=my_imputer.fit_transform(X_test )
embed_dim = 64
lstm_out = 196
model = Sequential()
model.add(Embedding(max_fatures, embed_dim,input_length = data_train_split.shape[1]))
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(2,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])
<train_model>
predictions=smart_forest.predict(imputed_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )