Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<count_missing_values>
%matplotlib inline
train.isnull().sum()<count_missing_values>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train.describe(include="all" )
train.isnull().sum()<count_missing_values>
pd.isnull(train ).sum()
features=[i for i in train.columns]
k=0
for feature in features:
if train[feature].isnull().sum() ==0:
k=k+1
else:
print('{0} has {1} null values'.format(feature,train[feature].isnull().sum()))
if(k==train.shape[1]):
print('no nan's in the train dataset,so proceed')
<count_missing_values>
train.drop(['Cabin'], axis = 1, inplace = True)
 test.drop(['Cabin'], axis = 1, inplace = True)
 train.drop(['Ticket'], axis = 1, inplace = True)
 test.drop(['Ticket'], axis = 1, inplace = True )
features=[i for i in test.columns]
k=0
for feature in features:
if test[feature].isnull().sum() ==0:
k=k+1
else:
print('{0} has {1} null values'.format(feature,test[feature].isnull().sum()))
if(k==test.shape[1]):
print('no nan's in the test dataset,so proceed' )<prepare_x_and_y>
train.fillna({'Embarked':'S'}, inplace = True )
train_y=train['label']
train_x=train.drop('label',axis=1 )<data_type_conversions>
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', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace(['Mlle', 'Ms'], 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
def image_printer(i,train_x):
idx=i
grid_data=train_x.iloc[idx].to_numpy().reshape(28,28 ).astype('uint8')
 plt.imshow(grid_data)
<count_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()
label.value_counts()<normalization>
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 i in range(len(train["AgeGroup"])) :
if train["AgeGroup"][i] == "Unknown":
train["AgeGroup"][i] = age_title_mapping[train["Title"][i]]
for i in range(len(test["AgeGroup"])) :
if test["AgeGroup"][i] == "Unknown":
test["AgeGroup"][i] = age_title_mapping[test["Title"][i]]
std_data=StandardScaler().fit_transform(data)
 print(np.mean(std_data))
print(np.std(std_data))<train_model>
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()
temp_data=std_data
covar_matrix=np.matmul(temp_data.T,temp_data)
 print(f'shape of covar matrix is {covar_matrix.shape}')
 print(f'shape of my data is {temp_data.shape}' )<compute_test_metric>
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )
eigh_values,eigh_vectors=eigh(covar_matrix,eigvals=(782,783))
print(f'shape of eigen vectors {eigh_vectors.shape}' )<train_model>
train.drop(['Name'], axis = 1, inplace = True)
 test.drop(['Name'], axis = 1, inplace = True )
pca_points=np.matmul(temp_data,eigh_vectors)
 print(f'shape of my pca points is {pca_points.shape}' )<create_dataframe>
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
pca_data=np.vstack(( pca_points.T,label)).T
print(f'shape of my pca data is {pca_data.shape}')
 pca_dataframe=pd.DataFrame(data=pca_data,columns=('1st Principal comp','2nd Principal comp','label'))
print(pca_dataframe.head(10))<normalization>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()
pca=decomposition.PCA()<import_modules>
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 )
from sklearn.manifold import TSNE<prepare_x_and_y>
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)
 acc_gaussian
temp_data1=std_data
pca.n_components=450
pca_data=pca.fit_transform(temp_data1)
 pca_data_new=np.vstack(( pca_data1.T,label)).T
new_data=pca_data<import_modules>
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)
 acc_logreg
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import cross_val_score,cross_val_predict
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
from scipy.stats import uniform,truncnorm,randint<compute_train_metric>
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)
 acc_svc
clf=LogisticRegression(multi_class='multinomial',n_jobs=-1)
 cv_scores=cross_val_score(clf,new_data,label,cv=10)
 print(f'we can expect accuracy between {cv_scores.min() } and {cv_scores.max() } with avg accuracy of {cv_scores.mean() }' )<compute_train_metric>
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)
 acc_linear_svc
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accLR=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accLR}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))<compute_train_metric>
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)
 acc_perceptron
for kernel in('poly','rbf'):
clf=svm.SVC(kernel=kernel)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of {kernel} kernel varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
<find_best_params>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(x_train, y_train)
 y_pred = decision_tree.predict(x_val)
 acc_decision_tree = round(accuracy_score(y_pred, y_val)*100 ,2)
 acc_decision_tree
for C in(1,10,100):
clf=svm.SVC(kernel='rbf',C=C)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C={C} varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
<find_best_params>
random_forest = RandomForestClassifier()
random_forest.fit(x_train, y_train)
 y_pred = random_forest.predict(x_val)
 acc_random_forest = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_random_forest
for gamma in(0.001,0.01):
clf=svm.SVC(kernel='rbf',C=10,gamma=gamma)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C=10 and gamma={gamma} varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
<compute_train_metric>
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)
 acc_knn
clf=svm.SVC(kernel='rbf',C=10,gamma=0.001)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C=10 and gamma= 0.001 varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }' )<compute_train_metric>
gbc = GradientBoostingClassifier()
gbc.fit(x_train, y_train)
 y_pred = gbc.predict(x_val)
 acc_gbc = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_gbc
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accSVM=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accSVM}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))<compute_train_metric>
ada = AdaBoostClassifier()
ada.fit(x_train, y_train)
 y_pred = ada.predict(x_val)
 acc_ada = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_ada
clf=KNeighborsClassifier(n_neighbors=5,n_jobs=-1)
 cv_score=cross_val_score(clf,new_data,label,cv=10)
 print(f' we can expect accuracy bwetween {cv_score.min() } and {cv_score.max() } with a mean of {cv_score.mean() }')
<compute_train_metric>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Gradient Boosting Classifier', 'AdaBoost Classifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_random_forest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decision_tree, acc_gbc, acc_ada]})
 models.sort_values(by='Score', ascending=False )
<compute_train_metric><EOS> 
ids = test['PassengerId']
predictions = ada.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )