Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
from sklearn.ensemble import ExtraTreesClassifier
import seaborn as sns
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import GridSearchCV
le = LabelEncoder()
def FunLabelEncoder(df):
for c in df.columns:
if df.dtypes[c] == object:
le.fit(df[c].astype(str))
df[c] = le.transform(df[c].astype(str))
return df<categorify>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 union = [train, test]
passenger_id = []
train = FunLabelEncoder(train)
 train.info()
train.iloc[235:300,:]<categorify>
for i, df in enumerate(union):
name = df['Name'].str.split('.', n=1, expand = True)
 name = name[1].str.split(expand = True)[0]
name.replace(['(\() ','(\)) '],'',regex=True, inplace = True)
 df['Name'] = name
del name
mean_age = df[['Name','Age']].groupby(['Name'] ).mean()
df = df.merge(mean_age, on='Name')
 df['Age'] = df['Age_x'].fillna(df['Age_y'])
 df = df.drop(['Age_x', 'Age_y', 'Name'], axis=1)
 df['Age'] = df['Age'].fillna(df['Age'].mean())
 age_buckets= [0,2,10,18,60,200]
age_labels = [0,1,2,3,4]
df['AgeGroup'] = pd.cut(df['Age'], bins=age_buckets, labels=age_labels, right=False)
 parch_buckets= [0,1,200]
parch_labels = [0,1]
df['Parch'] = pd.cut(df['Parch'], bins=parch_buckets, labels=parch_labels, right=False)
 sibsp_buckets= [0,1,2,200]
sibsp_labels = [0,1,2]
df['SibSp'] = pd.cut(df['SibSp'], bins=sibsp_buckets, labels=sibsp_labels, right=False ).astype(np.int8)
 df['Fare'] = df['Fare'].fillna(df['Fare'].mean())
 df['Pclass'] = df['Pclass'].astype(np.int8)
 df['Ability'] = df['Fare'] / df['Pclass']
df['Family'] = df['SibSp'].astype(np.int8)+ df['Parch'].astype(np.int8)+ 1
fare_buckets= [0,23,10000]
fare_labels = [0,1]
df['Fare'] = pd.cut(df['Fare'], bins=fare_buckets, labels=fare_labels, right=False)
 ab_buckets= [0,4,9,15,20,59,70,10000]
ab_labels = [0,1,2,3,4,5,6]
df['Ability'] = pd.cut(df['Ability'], bins=ab_buckets, labels=ab_labels, right=False)
 df = df.sort_values(by=['PassengerId'])
 passenger_id.append(df["PassengerId"])
 df['Sex'] = pd.get_dummies(df['Sex'])
 df['Fare'] = pd.get_dummies(df['Fare'])
 df['SibSp'] = pd.get_dummies(df['SibSp'])
 df['Parch'] = pd.get_dummies(df['Parch'])
 df = df.drop(['Embarked', 'PassengerId', 'Ticket', 'Age', 'Cabin'], axis=1)
 union[i] = df
test = FunLabelEncoder(test)
 test.info()
test.iloc[235:300,:]<define_variables>
x_train = union[0].drop("Survived", axis=1)
 y_train = union[0]["Survived"]
x_test = union[1]
x_train.shape, y_train.shape, x_test.shape
features=['Date','Province_State','Country_Region']
target = 'ConfirmedCases'<train_model>
ex = ExtraTreesClassifier(random_state = 6, bootstrap=True, oob_score=True)
 ex.fit(x_train, y_train)
 y_pred = ex.predict(x_test)
 ex.score(x_train, y_train)
 score = round(ex.score(x_train, y_train)* 100, 2)
 print('Extremely Randomized Trees', score )
dtcla = DecisionTreeClassifier(random_state=1000)
 dtcla.fit(train[features],train[target] )<predict_on_test>
for i,j in enumerate(x_train.head(1)) :
print('%s: %s' %(j, int(ex.feature_importances_[i]*100)) + '%' )
<define_variables><EOS> 
submission = pd.DataFrame({
"PassengerId": passenger_id[1],
"Survived": y_pred
})
 submission.to_csv('/kaggle/working/submission.csv', index=False)