Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
%matplotlib inline
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df.shape<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.head()
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
print(pd.isnull(train ).sum() )
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes
category_df.head()<categorify>
print("Percentage of females who survived:", train["Survived"][train["Sex"] == 'female'].value_counts(normalize = True)[1]*100)
 print("Percentage of males who survived:", train["Survived"][train["Sex"] == 'male'].value_counts(normalize = True)[1]*100)
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Handmade'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Handmade']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1)
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
print("Number of people embarking in Southampton(S):")
 southampton = train[train["Embarked"] == "S"].shape[0]
print(southampton)
 print("Number of people embarking in Cherbourg(C):")
 cherbourg = train[train["Embarked"] == "C"].shape[0]
print(cherbourg)
 print("Number of people embarking in Queenstown(Q):")
 queenstown = train[train["Embarked"] == "Q"].shape[0]
print(queenstown)
 train = train.fillna({"Embarked": "S"} )
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df.shape<string_transform>
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()
category_df = train_test_df[train_test_df.split_category_0 == 'Sports & Outdoors']
category_df.head()<feature_engineering>
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()
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
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]]
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes
category_df.head()<categorify>
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()
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Sports & Outdoors'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Sports & Outdoors']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1)
 train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1)
 sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
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 )
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df.shape<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 )
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
ids = test['PassengerId']
predictions = svc.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )