Kaggle competition: Titanic - Machine Learning from Disaster
train_stack = np.vstack([oof_lgb, oof_xgb, oof_cat, oof_lgb_1, oof_lgb_2] ).transpose()
train_stack = pd.DataFrame(train_stack, columns=['lgb', 'xgb', 'cat', 'lgb_1', 'lgb_2'])
 test_stack = np.vstack([prediction_lgb, prediction_xgb, prediction_cat, prediction_lgb_1, prediction_lgb_2] ).transpose()
test_stack = pd.DataFrame(test_stack, columns=['lgb', 'xgb', 'cat', 'lgb_1', 'lgb_2'] )<train_on_grid>
%matplotlib inline
warnings.filterwarnings('ignore' )
model = linear_model.RidgeCV(alphas=(0.01, 0.1, 1.0, 10.0, 100.0), scoring='neg_mean_squared_error', cv=folds)
 oof_rcv_stack, prediction_rcv_stack = train_model(train_stack.values, test_stack.values, y, params=None, model_type='sklearn', model=model )<save_to_csv>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
 train.describe(include="all" )
sub = pd.read_csv('.. /input/tmdb-box-office-prediction/sample_submission.csv')
 sub['revenue'] = np.expm1(prediction_lgb)
 sub.to_csv("lgb.csv", index=False)
 sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb)/ 2)
 sub.to_csv("blend.csv", index=False)
 sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb + prediction_cat)/ 3)
 sub.to_csv("blend1.csv", index=False)
 sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb + prediction_cat + prediction_lgb_1)/ 4)
 sub.to_csv("blend2.csv", index=False)
 sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb + prediction_cat + prediction_lgb_1 + prediction_lgb_2)/ 5)
 sub.to_csv("blend3.csv", index=False)
 sub['revenue'] = prediction_lgb_stack
sub.to_csv("stack_lgb.csv", index=False)
 sub['revenue'] = prediction_rcv_stack
sub.to_csv("stack_rcv.csv", index=False)
<set_options>
print(pd.isnull(train ).sum() )
warnings.simplefilter(action='ignore', category=FutureWarning)
 %matplotlib inline
seed = 123<load_from_csv>
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1 )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print('train dataset size:', train.shape)
 print('test dataset size:', test.shape )<define_variables>
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )
columns_to_keep = set()<feature_engineering>
train = train.fillna({"Embarked": "S"})
def clean_dictionary_features(feature_name,train,test):
train[feature_name] = train[feature_name].apply(lambda x:{} if pd.isna(x)else ast.literal_eval(x))
test[feature_name] = test[feature_name].apply(lambda x:{} if pd.isna(x)else ast.literal_eval(x))
train[feature_name+'_number'] = train[feature_name].apply(lambda x:len(x)if x!={} else 0)
 test[feature_name+'_number'] = test[feature_name].apply(lambda x:len(x)if x!={} else 0)
 columns_to_keep.add(feature_name+'_number')
 list_of_values = list(train[feature_name].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 train[feature_name+'_all'] = train[feature_name].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_values = [m[0] for m in Counter([i for j in list_of_values for i in j] ).most_common(10)]
for val in top_values:
train[feature_name +'_'+val] = train[feature_name+'_all'].apply(lambda x: 1 if val in x else 0)
 columns_to_keep.add(feature_name +'_'+val)
 test[feature_name+'_all'] = test[feature_name].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for val in top_values:
test[feature_name +'_'+val] = test[feature_name+'_all'].apply(lambda x: 1 if val in x else 0)
 le = LabelEncoder()
le.fit(list(train[feature_name+'_all'].fillna('')) + list(test[feature_name+'_all'].fillna('')))
 train[feature_name+'_all'] = le.transform(train[feature_name+'_all'].fillna('' ).astype(str))
test[feature_name+'_all'] = le.transform(test[feature_name+'_all'].fillna('' ).astype(str))
columns_to_keep.add(feature_name+'_all')
 return train,test<drop_column>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
train,test = clean_dictionary_features('genres',train,test)
 train,test = clean_dictionary_features('production_companies',train,test)
 train,test = clean_dictionary_features('production_countries',train,test)
 train,test = clean_dictionary_features('spoken_languages',train,test)
 train,test = clean_dictionary_features('Keywords',train,test)
 train,test = clean_dictionary_features('cast',train,test)
 train,test = clean_dictionary_features('crew',train,test)
<count_missing_values>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()
print("-"*40,'
',train.isnull().sum())
 print("-"*40,'
',test.isnull().sum() )<feature_engineering>
mr_age = train[train["Title"] == 1]["AgeGroup"].mode()
miss_age = train[train["Title"] == 2]["AgeGroup"].mode()
mrs_age = train[train["Title"] == 3]["AgeGroup"].mode()
master_age = train[train["Title"] == 4]["AgeGroup"].mode()
royal_age = train[train["Title"] == 5]["AgeGroup"].mode()
rare_age = train[train["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train["AgeGroup"])) :
if train["AgeGroup"][x] == "Unknown":
train["AgeGroup"][x] = age_title_mapping[train["Title"][x]]
for x in range(len(test["AgeGroup"])) :
if test["AgeGroup"][x] == "Unknown":
test["AgeGroup"][x] = age_title_mapping[test["Title"][x]]
train['got_collection'] = train['belongs_to_collection'].apply(lambda x:0 if pd.isnull(x)else 1)
 test['got_collection'] = test['belongs_to_collection'].apply(lambda x:0 if pd.isnull(x)else 1 )<filter>
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )
len(train[train['budget']==0] )<feature_engineering>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )
train['budget_log'] = np.log1p(train.budget.values)
 test['budget_log'] = np.log1p(test.budget.values )<drop_column>
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
columns_to_keep.add('budget_log' )<feature_engineering>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()
train['belongs_to_three_lang'] = train.original_language.apply(lambda x:1 if str(x)in['en','zh','tr'] else 0)
 test['belongs_to_three_lang'] = test.original_language.apply(lambda x:1 if str(x)in['en','zh','tr'] else 0 )<drop_column>
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )
columns_to_keep.add('belongs_to_three_lang' )<categorify>
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gaussian )
le = LabelEncoder()
le.fit(list(train['original_language'].fillna('')) + list(test['original_language'].fillna('')))
 train['original_language_encoded'] = le.transform(train['original_language'].fillna('' ).astype(str))
test['original_language_encoded'] = le.transform(test['original_language'].fillna('' ).astype(str))<rename_columns>
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg )
columns_to_keep.add('original_language_encoded' )<feature_engineering>
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc )
train['original_title_length'] = train.original_title.str.len()
test['original_title_length'] = test.original_title.str.len()<categorify>
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_svc )
def clean_date(date):
year = date.split('/')[2]
if int(year)<= 19:
return date[:-2] + '20' + year
else:
return date[:-2] + '19' + year<feature_engineering>
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_perceptron )
test.loc[test['release_date'].isnull() == True, 'release_date'] = '05/01/00' 
<data_type_conversions>
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_decisiontree )
train['release_date'] = train['release_date'].apply(lambda x:clean_date(x))
test['release_date'] = test['release_date'].apply(lambda x:clean_date(x))
train['release_date'] = pd.to_datetime(train['release_date'])
 test['release_date'] = pd.to_datetime(test['release_date'] )<data_type_conversions>
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest )
def date_features(dataset):
date_sections = ["year", "weekday", "month", 'weekofyear', 'day']
for sec in date_sections:
section_col = 'release_date' + "_" + sec
dataset[section_col] = getattr(dataset['release_date'].dt, sec ).astype(int)
 columns_to_keep.add(section_col)
 return dataset 
<feature_engineering>
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn )
train = date_features(train)
 test = date_features(test )<count_missing_values>
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_sgd )
train.runtime.isnull().sum()<count_missing_values>
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk )
test.runtime.isnull().sum()<feature_engineering>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk]})
 models.sort_values(by='Score', ascending=False )
<count_values><EOS> 
ids = test['PassengerId']
predictions = gbk.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )