Kaggle competition: Titanic - Machine Learning from Disaster
print('Number of casted persons in films')
 train['crew'].apply(lambda x: len(x)if x != {} else 0 ).value_counts().head(10 )<count_values>
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
list_of_crew_names = list(train['crew'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 Counter([i for j in list_of_crew_names for i in j] ).most_common(15 )<count_values>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 y = train.Survived
passengerid = test.PassengerId
titanic = train.append(test, ignore_index = True )
list_of_crew_jobs = list(train['crew'].apply(lambda x: [i['job'] for i in x] if x != {} else [] ).values)
 Counter([i for j in list_of_crew_jobs for i in j] ).most_common(15 )<count_values>
train_index = len(train)
 test_index = len(titanic)- len(test )
list_of_crew_genders = list(train['crew'].apply(lambda x: [i['gender'] for i in x] if x != {} else [] ).values)
 Counter([i for j in list_of_crew_genders for i in j] ).most_common(15 )<count_values>
titanic['Title'] = titanic.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip() )
list_of_crew_departments = list(train['crew'].apply(lambda x: [i['department'] for i in x] if x != {} else [] ).values)
 Counter([i for j in list_of_crew_departments for i in j] ).most_common(14 )<feature_engineering>
titanic.Title.value_counts()
train['num_crew'] = train['crew'].apply(lambda x: len(x)if x != {} else 0)
 top_crew_names = [m[0] for m in Counter([i for j in list_of_crew_names for i in j] ).most_common(15)]
for g in top_crew_names:
train['crew_name_' + g] = train['crew'].apply(lambda x: 1 if g in str(x)else 0)
 train['genders_0_crew'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
train['genders_1_crew'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
train['genders_2_crew'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
top_cast_characters = [m[0] for m in Counter([i for j in list_of_cast_characters for i in j] ).most_common(15)]
for g in top_cast_characters:
train['crew_character_' + g] = train['crew'].apply(lambda x: 1 if g in str(x)else 0)
 top_crew_jobs = [m[0] for m in Counter([i for j in list_of_crew_jobs for i in j] ).most_common(15)]
for j in top_crew_jobs:
train['jobs_' + j] = train['crew'].apply(lambda x: sum([1 for i in x if i['job'] == j]))
top_crew_departments = [m[0] for m in Counter([i for j in list_of_crew_departments for i in j] ).most_common(15)]
for j in top_crew_departments:
train['departments_' + j] = train['crew'].apply(lambda x: sum([1 for i in x if i['department'] == j]))
test['num_crew'] = test['crew'].apply(lambda x: len(x)if x != {} else 0)
 for g in top_crew_names:
test['crew_name_' + g] = test['crew'].apply(lambda x: 1 if g in str(x)else 0)
 test['genders_0_crew'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
test['genders_1_crew'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
test['genders_2_crew'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
for g in top_cast_characters:
test['crew_character_' + g] = test['crew'].apply(lambda x: 1 if g in str(x)else 0)
 for j in top_crew_jobs:
test['jobs_' + j] = test['crew'].apply(lambda x: sum([1 for i in x if i['job'] == j]))
for j in top_crew_departments:
test['departments_' + j] = test['crew'].apply(lambda x: sum([1 for i in x if i['department'] == j]))
train = train.drop(['crew'], axis=1)
 test = test.drop(['crew'], axis=1 )<feature_engineering>
normalized_title = {
'Mr':"Mr",
'Mrs': "Mrs",
'Ms': "Mrs",
'Mme':"Mrs",
'Mlle':"Miss",
'Miss':"Miss",
'Master':"Master",
'Dr':"Officer",
'Rev':"Officer",
'Col':"Officer",
'Capt':"Officer",
'Major':"Officer",
'Lady':"Royalty",
'Sir':"Royalty",
'the Countess':"Royalty",
'Dona':"Royalty",
'Don':"Royalty",
'Jonkheer':"Royalty"
}
train['log_revenue'] = np.log1p(train['revenue'] )<feature_engineering>
titanic.Title = titanic.Title.map(normalized_title )
train['log_budget'] = np.log1p(train['budget'])
 test['log_budget'] = np.log1p(test['budget'] )<count_values>
print(titanic.Title.value_counts() )
train['homepage'].value_counts().head()<feature_engineering>
titanic.Age = grouped.Age.apply(lambda x: x.fillna(x.median()))
titanic.isnull().sum()
train['has_homepage'] = 0
train.loc[train['homepage'].isnull() == False, 'has_homepage'] = 1
test['has_homepage'] = 0
test.loc[test['homepage'].isnull() == False, 'has_homepage'] = 1<train_model>
titanic.Fare = titanic.Fare.fillna(titanic.Fare.mean())
 titanic.Cabin = titanic.Cabin.fillna('U')
 most_Embarked = titanic.Embarked.value_counts().index[0]
titanic.Embarked = titanic.Embarked.fillna(most_Embarked )
vectorizer = TfidfVectorizer(
 sublinear_tf=True,
analyzer='word',
token_pattern=r'\w{1,}',
ngram_range=(1, 2),
min_df=5)
 overview_text = vectorizer.fit_transform(train['overview'].fillna(''))
linreg = LinearRegression()
linreg.fit(overview_text, train['log_revenue'])
 eli5.show_weights(linreg, vec=vectorizer, top=20, feature_filter=lambda x: x != '<BIAS>' )<feature_engineering>
titanic.isnull().sum()
test.loc[test['release_date'].isnull() == True, 'release_date'] = '01/01/98'<categorify>
titanic['FamilySize'] = titanic['SibSp'] + titanic['Parch'] + 1
def fix_date(x):

 year = x.split('/')[2]
if int(year)<= 19:
return x[:-2] + '20' + year
else:
return x[:-2] + '19' + year<data_type_conversions>
titanic['Cabin'].value_counts()
train['release_date'] = train['release_date'].apply(lambda x: fix_date(x))
test['release_date'] = test['release_date'].apply(lambda x: fix_date(x))
train['release_date'] = pd.to_datetime(train['release_date'])
 test['release_date'] = pd.to_datetime(test['release_date'] )<data_type_conversions>
titanic.Cabin = titanic.Cabin.map(lambda x: x[0])
 titanic.Cabin.head()
def process_date(df):
date_parts = ["year", "weekday", "month", 'weekofyear', 'day', 'quarter']
for part in date_parts:
part_col = 'release_date' + "_" + part
df[part_col] = getattr(df['release_date'].dt, part ).astype(int)
 return df
train = process_date(train)
 test = process_date(test )<count_values>
titanic.Sex = titanic.Sex.map({"male":0,"female":1})
train['status'].value_counts()<count_values>
enc_lst = ['Title','Cabin','Pclass','Embarked']
le = LabelEncoder()
for i in enc_lst:
titanic[i] = le.fit_transform(titanic[i] )
test['status'].value_counts()<drop_column>
titanic.drop(['Name', 'Ticket'],axis = 1,inplace = True )
train = train.drop(['homepage', 'imdb_id', 'poster_path', 'release_date', 'status', 'log_revenue'], axis=1)
 test = test.drop(['homepage', 'imdb_id', 'poster_path', 'release_date', 'status'], axis=1 )<count_unique_values>
train_x = titanic[:train_index]
test_x = titanic[train_index:]
for col in train.columns:
if train[col].nunique() == 1:
print(col)
 train = train.drop([col], axis=1)
 test = test.drop([col], axis=1 )<categorify>
train_x.Survived = train_x.Survived.astype(int )
for col in ['original_language', 'collection_name', 'all_genres']:
le = LabelEncoder()
le.fit(list(train[col].fillna('')) + list(test[col].fillna('')))
 train[col] = le.transform(train[col].fillna('' ).astype(str))
test[col] = le.transform(test[col].fillna('' ).astype(str))<define_variables>
X = train_x.drop('Survived', axis=1 ).values
y = train_x.Survived.values
train_texts = train[['title', 'tagline', 'overview', 'original_title']]
test_texts = test[['title', 'tagline', 'overview', 'original_title']]<feature_engineering>
X_test = test_x.drop('Survived', axis=1 ).values
for col in ['title', 'tagline', 'overview', 'original_title']:
train['len_' + col] = train[col].fillna('' ).apply(lambda x: len(str(x)))
 train['words_' + col] = train[col].fillna('' ).apply(lambda x: len(str(x.split(' '))))
train = train.drop(col, axis=1)
 test['len_' + col] = test[col].fillna('' ).apply(lambda x: len(str(x)))
 test['words_' + col] = test[col].fillna('' ).apply(lambda x: len(str(x.split(' '))))
test = test.drop(col, axis=1 )<feature_engineering>
label = train_x.Survived
train_X , val_X , train_Y , val_Y = train_test_split(train_x , label,test_size = 0.2,shuffle = True )
train.loc[train['id'] == 16,'revenue'] = 192864
train.loc[train['id'] == 90,'budget'] = 30000000
train.loc[train['id'] == 118,'budget'] = 60000000
train.loc[train['id'] == 149,'budget'] = 18000000
train.loc[train['id'] == 313,'revenue'] = 12000000
train.loc[train['id'] == 451,'revenue'] = 12000000
train.loc[train['id'] == 464,'budget'] = 20000000
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 930000
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 819,'budget'] = 90000000
train.loc[train['id'] == 850,'budget'] = 90000000
train.loc[train['id'] == 1112,'budget'] = 7500000
train.loc[train['id'] == 1131,'budget'] = 4300000
train.loc[train['id'] == 1359,'budget'] = 10000000
train.loc[train['id'] == 1542,'budget'] = 1
train.loc[train['id'] == 1570,'budget'] = 15800000
train.loc[train['id'] == 1571,'budget'] = 4000000
train.loc[train['id'] == 1714,'budget'] = 46000000
train.loc[train['id'] == 1721,'budget'] = 17500000
train.loc[train['id'] == 1865,'revenue'] = 25000000
train.loc[train['id'] == 2268,'budget'] = 17500000
train.loc[train['id'] == 2491,'revenue'] = 6800000
train.loc[train['id'] == 2602,'budget'] = 31000000
train.loc[train['id'] == 2612,'budget'] = 15000000
train.loc[train['id'] == 2696,'budget'] = 10000000
train.loc[train['id'] == 2801,'budget'] = 10000000
test.loc[test['id'] == 3889,'budget'] = 15000000
test.loc[test['id'] == 6733,'budget'] = 5000000
test.loc[test['id'] == 3197,'budget'] = 8000000
test.loc[test['id'] == 6683,'budget'] = 50000000
test.loc[test['id'] == 5704,'budget'] = 4300000
test.loc[test['id'] == 6109,'budget'] = 281756
test.loc[test['id'] == 7242,'budget'] = 10000000
test.loc[test['id'] == 7021,'budget'] = 17540562
test.loc[test['id'] == 5591,'budget'] = 4000000
test.loc[test['id'] == 4282,'budget'] = 20000000
power_six = train.id[train.budget > 1000][train.revenue < 100]
for k in power_six :
train.loc[train['id'] == k,'revenue'] = train.loc[train['id'] == k,'revenue'] * 1000000<prepare_x_and_y>
dtrain_X = train_X.drop('Survived',axis = 1 )
X = train.drop(['id', 'revenue'], axis=1)
 y = np.log1p(train['revenue'])
 X_test = test.drop(['id'], axis=1 )<split>
dval_X = val_X.drop('Survived',axis = 1 )
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.1 )<choose_model_class>
estimators = [('log_reg',LogisticRegression(random_state = 2,max_iter = 15)) , 
('svm',make_pipeline(StandardScaler() ,
SVC(gamma = 'auto',random_state = 2))), 
('random_forest',RandomForestClassifier(n_estimators=10,random_state = 2))
]
stacking_clf = StackingClassifier(estimators = estimators,final_estimator=LogisticRegression())
 stacking_clf.fit(dtrain_X,train_Y )
params = {'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 5,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
"verbosity": -1}
model1 = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model1.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=1000, early_stopping_rounds=200 )<load_pretrained>
stacking_clf.score(dval_X,val_Y )
eli5.show_weights(model1, feature_filter=lambda x: x != '<BIAS>' )<choose_model_class>
stack_ypred = stacking_clf.predict(dval_X)
 print('Stacking classifier accuracy score is.... ',accuracy_score(val_Y,stack_ypred))
n_fold = 5
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42 )<split>
prediction_stacking_clf = stacking_clf.predict(X_test )
<train_model><EOS> 
output = pd.DataFrame({"PassengerId":passengerid , "Survived" : prediction_stacking_clf})
 output.to_csv("Submission_stacking_clf.csv",index = False )