Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
sns.set(color_codes=True)
 %matplotlib inline
print(check_output(["ls", ".. /input"] ).decode("utf8"))
dl_df = pd.read_csv('.. /input/aoeul-solution-step-2-deep-learning-model/Deep_Learning_predictions.csv')
 svc_df = pd.read_csv('.. /input/aoeul-solution-step-1-linearsvc/LinearSVC_predictions.csv' )<predict_on_test>
train = pd.read_csv(".. /input/train.csv", dtype={"Age": np.float64},)
 test = pd.read_csv(".. /input/test.csv", dtype={"Age": np.float64}, )
dl_df.head(10 )<predict_on_test>
train.isnull().sum(axis=0 )
svc_df.head(10 )<merge>
test['Sex'].value_counts()
new_df = pd.merge(dl_df, svc_df, on='id')
 new_df.head()<string_transform>
test_id = test['PassengerId']
target = train['Survived']
new_tag = []
i = 0
for _, row in tqdm_notebook(new_df.iterrows()):
x1, x2 = row.tagging_x.split(' ')
 x1 = int(x1)
 x2 = int(x2)
 y = row.tagging_y
if x1 == y:
new_tag.append(row.tagging_x)
 i = i + 1
else:
new_tag.append(str(y)+ ' '+ str(x1))<feature_engineering>
train["Fare"].fillna(train["Fare"].median() , inplace=True)
 test["Fare"].fillna(test["Fare"].median() , inplace=True )
i/len(new_df)*100<save_to_csv>
train["Embarked"].fillna(train['Embarked'].mode() [0], inplace=True)
 test["Embarked"].fillna(test['Embarked'].mode() [0], inplace=True )
new_df['tagging']= np.array(new_tag)
 new_df = new_df[['id', 'tagging']]
new_df.to_csv(f'Ensembled_SVC_DL_predictions.csv', index=None )<load_from_csv>
train["Age"].fillna(train["Age"].median() , inplace=True)
 test["Age"].fillna(test["Age"].median() , inplace=True )
TRAIN_PATH = os.path.join(".. /input/ai-academy-intermediate-class-competition-1", "BBC News Train.csv")
 df = pd.read_csv(TRAIN_PATH )<feature_engineering>
df['category_id'] = df['Category'].factorize() [0]
df['category_id'][0:10]<remove_duplicates>
def substrings_in_string(big_string, substrings):
for substring in substrings:
if big_string.find(substring)!= -1:
return substring
return np.nan
category_id_df = df[['Category', 'category_id']].drop_duplicates().sort_values('category_id' )<define_variables>
def replace_titles(x):
title=x['Title']
if title in ['Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col']:
return 'Mr'
elif title in ['Countess', 'Mme']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
if x['Sex']=='Male':
return 'Mr'
else:
return 'Mrs'
else:
return title
category_to_id = dict(category_id_df.values)
 id_to_category = dict(category_id_df[['category_id', 'Category']].values )<count_values>
title_list=['Mrs', 'Mr', 'Master', 'Miss', 'Major', 'Rev',
'Dr', 'Ms', 'Mlle','Col', 'Capt', 'Mme', 'Countess',
'Don', 'Jonkheer']
df.groupby('Category' ).category_id.count() 
<categorify>
train['Title']=train['Name'].map(lambda x: substrings_in_string(x, title_list))
test['Title']=test['Name'].map(lambda x: substrings_in_string(x, title_list))
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
 features = tfidf.fit_transform(df.Text ).toarray()
labels = df.category_id 
<sort_values>
train['Title']=train.apply(replace_titles, axis=1)
 test['Title']=test.apply(replace_titles, axis=1 )
sorted(category_to_id.items() )<statistical_test>
train['Family_Size']=train['SibSp']+train['Parch']
test['Family_Size']=test['SibSp']+test['Parch']
N = 3
for Category, category_id in sorted(category_to_id.items()):
features_chi2 = chi2(features, labels == category_id)
 indices = np.argsort(features_chi2[0])
 feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
print("
print(".Most correlated unigrams: 
.{}".format(' 
.'.join(unigrams[-N:])))
 print(".Most correlated bigrams: 
.{}".format(' 
.'.join(bigrams[-N:])))
<init_hyperparams>
train['Age*Class']=train['Age']*train['Pclass']
test['Age*Class']=test['Age']*test['Pclass']
SAMPLE_SIZE = int(len(features)* 0.3)
 np.random.seed(0)
 indices = np.random.choice(range(len(features)) , size=SAMPLE_SIZE, replace=False)
 projected_features = TSNE(n_components=2, random_state=0 ).fit_transform(features[indices])
<filter>
train['Fare_Per_Person']=train['Fare']/(train['Family_Size']+1)
 test['Fare_Per_Person']=test['Fare']/(test['Family_Size']+1 )
my_id = 0
projected_features[(labels[indices] == my_id ).values]<choose_model_class>
train["Embarked"].fillna(0, inplace=True)
 test["Embarked"].fillna(0, inplace=True )
models = [
RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
MultinomialNB() ,
LogisticRegression(random_state=0),
] 
<create_dataframe>
lb_make = LabelEncoder()
train["Embarked"] = lb_make.fit_transform(train["Embarked"])
 train["Sex"] = lb_make.fit_transform(train["Sex"])
 train["Title"] = lb_make.fit_transform(train["Title"])
 test["Embarked"] = lb_make.fit_transform(test["Embarked"])
 test["Sex"] = lb_make.fit_transform(test["Sex"])
 test["Title"] = lb_make.fit_transform(test["Title"] )
CV = 5
cv_df = pd.DataFrame(index=range(CV * len(models)))
 entries = []<find_best_model_class>
train = train.drop(['Name', 'PassengerId', 'Survived', 'Ticket', 'Cabin'], axis=1)
 test = test.drop(['Name', 'PassengerId', 'Ticket', 'Cabin'], axis=1 )
for model in models:
model_name = model.__class__.__name__
accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)
 for fold_idx, accuracy in enumerate(accuracies):
entries.append(( model_name, fold_idx, accuracy))<create_dataframe>
import xgboost as xgb
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'] )<groupby>
vote_est = [ 
('ada', AdaBoostClassifier()), 
('bc', BaggingClassifier()), 
('etc',ExtraTreesClassifier()), 
('gbc', GradientBoostingClassifier()), 
('rfc', RandomForestClassifier()), 
('gpc', GaussianProcessClassifier()), 
('lr', LogisticRegressionCV()), 
('bnb', BernoulliNB()), 
('gnb', GaussianNB()), 
('knn', KNeighborsClassifier()), 
('svc', SVC(probability=True)) , 
('xgb', xgb.XGBClassifier())
 ]
model = VotingClassifier(estimators = vote_est , voting = 'soft' )
cv_df.groupby('model_name' ).accuracy.mean()<train_model>
print(cross_val_score(model, train, target, cv=5, scoring=make_scorer(accuracy_score)) )
model = LogisticRegression(random_state=0)
 X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, df.index, test_size=0.33, random_state=0)
 model.fit(X_train, y_train)
 y_pred_proba = model.predict_proba(X_test)
 y_pred = model.predict(X_test )<compute_test_metric>
model.fit(train,target)
 pred = model.predict(test )
for predicted in category_id_df.category_id:
for actual in category_id_df.category_id:
if predicted != actual and conf_mat[actual, predicted] >= 2:
print("'{}' predicted as '{}' : {} examples.".format(id_to_category[actual], id_to_category[predicted], conf_mat[actual, predicted]))
display(df.loc[indices_test[(y_test == actual)&(y_pred == predicted)]]['Text'])
 print('' )<train_model>
submission = pd.DataFrame({
"PassengerId": test_id,
"Survived": pred
})
 submission.to_csv('titanic_result_pandas.csv', index=False )