Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<count_missing_values>
warnings.filterwarnings('ignore')
 %matplotlib inline
train.isnull().sum()<count_missing_values>
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 combine = [train_df, test_df]
test.isnull().sum()<feature_engineering>
train_df[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()
train['reviews_per_month'] = train['reviews_per_month'].fillna(0)
 test['reviews_per_month'] = test['reviews_per_month'].fillna(0 )<feature_engineering>
target_count = train_df['Survived'].value_counts()
target_count
train['last_review'] = train['last_review'].fillna('2019-12-31')
 test['last_review'] = test['last_review'].fillna('2019-12-31' )<feature_engineering>
print(train_df.isnull().sum())
 print()
print(test_df.isnull().sum())
train['last_review'] = pd.to_datetime(train['last_review'])
 test['last_review'] = pd.to_datetime(test['last_review'])
 train['year'] = train['last_review'].dt.year-2010
test['year'] = test['last_review'].dt.year-2010
train['month'] = train['last_review'].dt.month-1
test['month'] = test['last_review'].dt.month-1<feature_engineering>
for dataset in combine:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.',\
expand=False )
train['latitude'] = train['latitude']-40
train['longitude'] = train['longitude']+75
test['latitude'] = test['latitude']-40
test['longitude'] = test['longitude']+75
train['waru'] = train['latitude'] / train['longitude']
train['kakeru'] = train['latitude'] * train['longitude']
test['waru'] = test['latitude'] / test['longitude']
test['kakeru'] = test['latitude'] * test['longitude']<feature_engineering>
for dataset in combine:
dataset['Title'] = dataset['Title'].\
replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr',\
'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs' )
train['minimum_nights_log'] = np.log1p(train['minimum_nights'])
 test['minimum_nights_log'] = np.log1p(test['minimum_nights'])
 train['calculated_host_listings_count_log'] = np.log1p(train['calculated_host_listings_count'])
 test['calculated_host_listings_count_log'] = np.log1p(test['calculated_host_listings_count'])
 train['availability_365_log'] = np.log1p(train['availability_365'])
 test['availability_365_log'] = np.log1p(test['availability_365'])
 train['number_of_reviews_log'] = np.log1p(train['number_of_reviews'])
 test['number_of_reviews_log'] = np.log1p(test['number_of_reviews'])
 train['reviews_per_month_log'] = np.log1p(train['reviews_per_month'])
 test['reviews_per_month_log'] = np.log1p(test['reviews_per_month'] )<define_variables>
train_df[['Title', 'Survived']].groupby(['Title'] ).mean()
target = 'price'
list_cols = ['neighbourhood_group', 'neighbourhood', 'room_type',]
target_columns = ['neighbourhood_group', 'neighbourhood', 'latitude', 'longitude', 'room_type',
'minimum_nights', 'number_of_reviews', 'reviews_per_month', 'calculated_host_listings_count', 'availability_365', 'waru', 'kakeru', 'minimum_nights_log',
'calculated_host_listings_count_log', 'availability_365_log',
'number_of_reviews_log', 'reviews_per_month_log', 'year', 'month']
ce_ohe = ce.OneHotEncoder(cols=list_cols)
 train_onehot = ce_ohe.fit_transform(train[target_columns] )<count_missing_values>
for dataset in combine:
binary_encoded = pd.get_dummies(dataset.Title)
 newcols = binary_encoded.columns
dataset[newcols] = binary_encoded
train_df.head()
train_onehot.isnull().sum()<categorify>
train_df = train_df.drop(['Name', 'Title'], axis=1)
 test_df = test_df.drop(['Name', 'Title'], axis=1)
 combine = [train_df, test_df]
test_onehot = ce_ohe.transform(test[target_columns] )<categorify>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].\
map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()
pca2 = PCA(n_components=5)
 pca2_results = pca2.fit_transform(train_onehot)
 train_onehot['pca0']=pca2_results[:,0]
train_onehot['pca1']=pca2_results[:,1]
train_onehot['pca2']=pca2_results[:,2]
train_onehot['pca3']=pca2_results[:,3]
train_onehot['pca4']=pca2_results[:,4]
pca2_results = pca2.transform(test_onehot)
 test_onehot['pca0']=pca2_results[:,0]
test_onehot['pca1']=pca2_results[:,1]
test_onehot['pca2']=pca2_results[:,2]
test_onehot['pca3']=pca2_results[:,3]
test_onehot['pca4']=pca2_results[:,4]<prepare_x_and_y>
guess_ages = np.zeros(( 2,3),dtype=int)
 guess_ages
y_train = np.log1p(train["price"] )<split>
for idx,dataset in enumerate(combine):
if idx==0:
print('Working on Training Data set
')
 else:
print('-'*35)
 print('Working on Test Data set
')
 print('Guess values of age based on sex and pclass of the passenger...')
 for i in range(0, 2):
for j in range(0,3):
guess_df = dataset[(dataset['Sex'] == i)\
&(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess)
 print('Guess_Age table:
',guess_ages)
 print('
Assigning age values to NAN age values in the dataset...')
 for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)\
&(dataset.Pclass == j+1),'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 print()
print('Done!
')
 train_df.head()
kf = KFold(n_splits=5, shuffle=True, random_state=1)
 s = list(kf.split(train_onehot, y_train))<choose_model_class>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']=4
train_df.head()
gbk_models = []
for train_i, val_i in s:
gbk = GradientBoostingRegressor()
gbk.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = gbk.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 gbk_models.append(gbk )<train_model>
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()
mlp_models = []
for train_i, val_i in s:
mlp = MLPRegressor(max_iter=100, hidden_layer_sizes=(100,100),
activation='relu', learning_rate_init=0.01)
 mlp.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = mlp.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 mlp_models.append(mlp )<find_best_model_class>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=True)\ 
.mean().sort_values(by='Survived', ascending=False )
rfc_models = []
for train_i, val_i in s:
rfc = RandomForestRegressor()
rfc.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = rfc.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 rfc_models.append(rfc )<predict_on_test>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=True ).mean()
models = gbk_models + rfc_models
preds = np.array([np.expm1(model.predict(test_onehot)) for model in models])
 preds = preds.mean(axis=0)
 preds = np.where(preds < 0 , 0, preds )<save_to_csv>
for df in combine:
df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1, inplace=True)
 train_df.head()
ans["price"] = preds
ans.to_csv("heroz_nakai.csv", index=False )<set_options>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head()
%matplotlib inline 
<import_modules>
train_df[['Age*Class', 'Survived']].groupby(['Age*Class'], as_index=True ).mean()
import pandas as pd
import numpy as np<import_modules>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=True)\ 
.mean().sort_values(by='Survived', ascending=False)
import pandas as pd
import numpy as np<save_to_csv>
for dataset in combine:
binary_encoded = pd.get_dummies(dataset.Embarked)
 newcols = binary_encoded.columns
dataset[newcols] = binary_encoded
train_df.head()
def create_download_link(df, title = "Download CSV file",count=[0]):
count[0] = count[0]+1
filename = "data"+str(count[0])+".csv"
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html )<load_from_csv>
for dataset in combine:
dataset.drop('Embarked', axis=1, inplace=True )
df = pd.read_csv("/kaggle/input/dmassign1/data.csv")
 df_test = df.iloc[1300:]
df_train = df.iloc[:1300]<drop_column>
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True)
 test_df.head()
class_labels={}
class_id = {}
for i,j in df_train.iterrows() :
class_labels[j["ID"]] = j["Class"]
class_id[i]=j["ID"]
labels = df_train["Class"]
df_train1 = df_train.drop(["ID","Class"],axis=1)
 df_test1 = df_test.drop(["ID","Class"],axis=1 )<concatenate>
for dataset in combine:
dataset['Fare']=pd.qcut(train_df['Fare'],4,labels=np.arange(4))
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df[['Fare','FareBand']].head()
df_full = df_train1.append(df_test1)
 df_full.replace("?",np.nan,inplace=True )<count_unique_values>
train_df = train_df.drop(['FareBand'], axis=1)
 combine = [train_df, test_df]
a = df_full.nunique()
print(a)
 a = dict(a)
 for i,j in a.items() :
a[i] = [j,df_full[i].dtype] 
<count_values>
train_df.head(7 )
for i,j in a.items() :
t,q = j
if q == np.dtype('O'):
if t <100:
print(Counter(df_full[i]))<categorify>
X = train_df.drop("Survived", axis=1)
 Y = train_df["Survived"]
X_submission = test_df.drop("PassengerId", axis=1 ).copy()
print(X.shape, Y.shape )
def myconverter(string):
if string=="me":return "me"
elif string=="ME":return "me"
elif string=="M.E.":return "me"
else:
try:
return string.lower()
except:
return string<categorify>
np.random.seed(1337)
 X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.2)
 print(X_train.shape, Y_train.shape)
 print(X_val.shape, Y_val.shape )
le = preprocessing.LabelEncoder()
scaler = StandardScaler()
ab=[]
for i,j in a.items() :
t,q = j
if q == np.dtype('O'):
if t <100:
df_full[i] = df_full[i].apply(lambda x:myconverter(x))
df_full[i].fillna(df_full[i].mode() [0], inplace=True)
 ab.append(i)
 else:
df_full[i]= df_full[i].astype("float64")
 df_full[i].fillna(df_full[i].mean() ,inplace=True)
 df_full[i] = scaler.fit_transform(df_full[i].values.reshape(-1,1))
else:
df_full[i] = scaler.fit_transform(df_full[i].values.reshape(-1,1))
df_full = pd.get_dummies(df_full, prefix=ab)
<feature_engineering>
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_val)
 acc_logreg = sum(Y_pred == Y_val)/len(Y_val)*100
print('Logistic Regression labeling accuracy:', str(round(acc_logreg,2)) ,'%' )
modified_PCA = PCA(n_components = 0.99,svd_solver="full" ).fit_transform(df_full.values )<create_dataframe>
acc_log_2 = logreg.score(X_val, Y_val)
 print('Logistic Regression using built-in method:', str(round(acc_log_2*100,2)) ,'%' )
PCA_df = pd.DataFrame(data=modified_PCA )<find_best_params>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 acc_knn = knn.score(X_val, Y_val)
 print('K-Nearest Neighbors labeling accuracy:', str(round(acc_knn*100,2)) ,'%' )
n_clusters=144
model = KMeans(n_clusters=n_clusters,random_state=42)
 pred = model.fit_predict(PCA_df.values)
 cntr = Counter(pred)
 mapping={}
res={0:0,1:0,2:0,3:0,4:0,5:0}
score = [defaultdict(int)for i in range(n_clusters)]
for i,j in enumerate(pred[:1300]):
score[j][labels[i]]+=1
for j,i in enumerate(score):
t =[(j,k)for k,j in i.items() ]
t.sort(reverse=True)
 t = [(j,k)for k,j in t]
try:
mapping[j]=int(t[0][0])
 res[int(t[0][0])] +=cntr[j]
except:
random_int = 0
mapping[j]=random_int
res[random_int]+=cntr[j]
print(res )<prepare_output>
svc = SVC()
svc.fit(X_train, Y_train)
 acc_svc = svc.score(X_val, Y_val)
 print('Support Vector Machines labeling accuracy:', str(round(acc_svc*100,2)) ,'%' )
PCA_df["pred"] = pred<categorify>
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 acc_perceptron = perceptron.score(X_val, Y_val)
 print('Perceptron labeling accuracy:', str(round(acc_perceptron*100,2)) ,'%' )
def mapper(x,mapping):
y = mapping[x]
if y==0:return np.random.randint(1,6)
 else:return y<feature_engineering>
gradboost = xgb.XGBClassifier(n_estimators=1000)
 gradboost.fit(X_train, Y_train)
 acc_xgboost = gradboost.score(X_val, Y_val)
 print('XGBoost labeling accuracy:', str(round(acc_xgboost*100,2)) ,'%' )
PCA_df["pred"]=PCA_df["pred"].apply(lambda x:mapper(x,mapping))<count_values>
random_forest = RandomForestClassifier(n_estimators=500)
 random_forest.fit(X_train, Y_train)
 acc_rf = random_forest.score(X_val, Y_val)
 print('K-Nearest Neighbors labeling accuracy:', str(round(acc_rf*100,2)) ,'%' )
Counter(PCA_df["pred"] )<split>
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
df_submission = PCA_df.iloc[1300:]<prepare_output>
model = Sequential()
model.add(Dense(units=300, activation='relu', input_shape=(14,)))
 model.add(Dense(units=100, activation='relu'))
model.add(Dense(units=50, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
predictions = df_submission["pred"]<create_dataframe>
model.compile(loss = 'binary_crossentropy', optimizer = 'adam',\
metrics = ['accuracy'])
 model.fit(X_train.values, Y_train.values, epochs = 50, batch_size= 50 )
submission = pd.DataFrame(data={"ID": df["ID"][1300:], "Class": predictions } )<count_values>
print('Neural Network accuracy:',str(round(model.evaluate(X_val.values, \
Y_val.values, batch_size=50,verbose=False)[1]*100,2)) ,'%' )
Counter(submission["Class"] )<load_pretrained>
base_model = RandomForestClassifier(n_jobs=-1,
random_state=42)
 parameters = {
"max_depth": [None, 1,2,3,4,5,6,7,8,9,10,11,12,13,14],
"n_estimators": [350, 400, 450, 500, 550, 600,650,700,750],
"max_features": ["auto", 1,2,3,4,5,6,7,8,9,10,11,12,13,14],
"criterion": ["gini", "entropy"],
"bootstrap": [False, True],
"class_weight": [None, "balanced"]
}
optimal_model = RandomizedSearchCV(base_model,
param_distributions=parameters,
n_iter=30,
cv=3,
verbose=2,
scoring="accuracy")
 optimal_model.fit(X_train, Y_train)
 optimal_model.best_estimator_
pred = optimal_model.predict(X_val )
create_download_link(submission )<save_to_csv>
Y_submission = optimal_model.predict(X_submission)
 submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_submission
})
 submission.to_csv('titanic.csv', index=False )