<jupyter_start><jupyter_text>Features and Label Features    * PassengerId    * Pclass    * Name    * Sex    * Age    * SibSp (denotes that whether a passenger have any Sibling or Spouse in this journey)    * Parch (denotes that whether a passenger have any Parent or Child in this journey)    * Ticket    * Fare    * Cabin    * Embarked (from which station the passenger was start the journey)     Label    * Survived  Data Cleanning and Processing  Working with `PassengerId` Feature:`PassengerId` does not have any special meaning or does not make any effect to our data, so we can simply delete this feature.<jupyter_code>del full_data["PassengerId"]<jupyter_output><empty_output><jupyter_text>Working with `Pclass` Feature: First lets check whether it is contains any `NaN` or `empty/zero` values<jupyter_code>print ("Is Pclass contains any NaN value ? : ", full_data["Pclass"].isna().any())
print ("No. of empty/zero values in Pclass ?: {0}". format(len(full_data.loc[full_data["Pclass"] == 0])))<jupyter_output>Is Pclass contains any NaN value ? :  False
No. of empty/zero values in Pclass ?: 0<jupyter_text>`Pclass` doesn't contain any `NaN` or `empty` values. So now lets explore this feature.<jupyter_code>full_data["Pclass"].head()
full_data.groupby("Pclass").count()<jupyter_output><empty_output><jupyter_text>>`Pclass` already in a decent form, so we can proceed to the next feature.  Working with `Name` Feature: First lets check whether the `Name` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Name contains any NaN value ? : ", full_data["Name"].isna().any())
print ("No. of empty/zero values in Name ?: {0}". format(len(full_data.loc[full_data["Name"] == 0])))<jupyter_output>Is Name contains any NaN value ? :  False
No. of empty/zero values in Name ?: 0<jupyter_text>> `Name` doesn't contains any `empty` or `NaN` value.Name contains `string` values, so we need to somehow convert it to `Numeric` values.We do not need to the whole `name`, we can simply take the `title` from each `Name` which is more common and generic.<jupyter_code># function for finding Title from passenger's name

def find_title(name):
    title = re.search(' ([A-Za-z]+)\.', name)
    if title:
        return title.group(1)
    return ""
# find all Name title and store it into 'titles' List

titles = []
for name in full_data["Name"]:
    titles.append(find_title(name))
# adding 'titles' List into dataframe as a new Column called 'Title'

full_data["Title"] = np.asarray(titles)
full_data.head()
# finding number of unique titles  

full_data["Title"].nunique()
# grouping the whole dataframe(full_data) by Title

full_data.groupby("Title").count()
# We have Title now
# Name doesn't have any special meaning to our data right now !
# so we can safely delete this column from 'full_data'

del full_data["Name"]
full_data.head()
# replace all less frequent and rare Title with 'Others'

full_data = full_data.replace(["Lady", "Countess","Capt", "Col", "Don", "Dr", "Major", "Rev", "Sir", "Jonkheer", "Dona"], "Others")
# now we only have 8 unique titles in our whole dataframe 

full_data["Title"].nunique()
full_data.groupby("Title").count()
# Mlle, Mme are equivalent to Mrs

full_data = full_data.replace(["Mlle","Mme"], "Mrs")
full_data["Title"].nunique()
full_data.groupby("Title").count()
# Ms is equivalent to Miss

full_data = full_data.replace(["Ms"],"Miss")
full_data.groupby("Title").count()
full_data["Title"].nunique()
# mapping Title feature

title_map = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Others": 5}

full_data["Title"] = full_data["Title"].map(title_map)

full_data.head()
full_data.groupby("Title").count()<jupyter_output><empty_output><jupyter_text>Working with `Sex` Feature:First lets check whether the `Sex` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Sex contains any NaN value ? : ", full_data["Sex"].isna().any())
print ("No. of empty/zero values in Sex ?: {0}". format(len(full_data.loc[full_data["Sex"] == 0])))
full_data.groupby("Sex").count()
# Sex feature contain two types of value => Male and Female
# so we can simply map all the values with 0's and 1's

sex_map = {"male":1, "female":0}
full_data["Sex"] = full_data["Sex"].map(sex_map)
full_data.groupby("Sex").count()<jupyter_output><empty_output><jupyter_text>> `466` passengers are female and `843` passengers are male  Working with `Age` Feature:First lets check whether the `Age` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Age contains any NaN value ? : ", full_data["Age"].isna().any())
print ("No. of empty/zero values in Age ?: {0}". format(len(full_data.loc[full_data["Age"] == 0])))
# Age contains NaN value so firstly lets replace the NaN  values with `0`

full_data["Age"] = full_data["Age"].fillna(0)
print ("Is Age contains any NaN value ? : ", full_data["Age"].isna().any())
print ("No. of empty/zero values in Age ?: {0}". format(len(full_data.loc[full_data["Age"] == 0])))
# So firstly we need to replace the missing values with some mean values/substitute values
# replacing missing values with substituted values known as Imputation
# for Imputation a library is already present in sklearn

subs_values = Imputer(missing_values = 0, strategy="mean", axis=0)
full_data["Age"] = subs_values.fit_transform(np.array(full_data["Age"]).reshape((len(full_data["Age"]), 1)))
full_data["Age"] = np.ceil(full_data["Age"])
full_data["Age"] = full_data["Age"].astype(np.int64)
print ("No. of empty/zero values in Age ?: {0}". format(len(full_data.loc[full_data["Age"] == 0])))
# mapping age by range

full_data.loc[full_data["Age"] <= 16, "Age"] = 0
full_data.loc[(full_data["Age"] > 16) & (full_data["Age"] <= 32), "Age"] = 1
full_data.loc[(full_data["Age"] > 32) & (full_data["Age"] <= 48), "Age"] = 2
full_data.loc[(full_data["Age"] > 48) & (full_data["Age"] <= 64), "Age"] = 3
full_data.loc[full_data["Age"] > 64, "Age"] = 4
full_data["Age"].head()
full_data.groupby("Age").count()<jupyter_output><empty_output><jupyter_text>> `Age` looks decent now, so we can step ahead.   Working with `SibSp` and `Parch` Feature:First lets check whether these two feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is SibSp contains any NaN values? : ", full_data["SibSp"].isna().any())
print ("Is Parch contains any NaN values? :", full_data["Parch"].isna().any())
full_data.groupby("SibSp").count()
full_data.groupby("Parch").count()<jupyter_output><empty_output><jupyter_text>> Both `SibSp` and `Parch` are in decent form, so we can proceed further.  Working with `Ticket` Feature:First lets check whether `Ticket` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Ticket conatins any NaN values? : ", full_data["Ticket"].isna().any())
print ("No. of empty/zero values in Ticket ?: {0}". format(len(full_data.loc[full_data["Ticket"] == 0])))
full_data["Ticket"].head()
# according to the above result every passenger have ticket
# so we can simply ignore this feature

del full_data["Ticket"]<jupyter_output><empty_output><jupyter_text>Working with `Fare` Feature:First lets check whether `Fare` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Fare contains any NaN value ? : ", full_data["Fare"].isna().any())
print ("No. of empty/zero values in Fare ?: {0}". format(len(full_data.loc[full_data["Fare"] == 0])))
# Fare contains NaN value so firstly lets replace the NaN  values with `0`

full_data["Fare"] = full_data["Fare"].fillna(0)
print ("Is Fare contains any NaN value ? : ", full_data["Fare"].isna().any())
print ("No. of empty/zero values in Fare ?: {0}". format(len(full_data.loc[full_data["Fare"] == 0])))
# imputing Fare missing values

subs_values = Imputer(missing_values = 0, strategy="mean", axis=0)
full_data["Fare"] = subs_values.fit_transform(np.array(full_data["Fare"]).reshape((len(full_data["Fare"]), 1)))
full_data["Fare"] = np.ceil(full_data["Fare"])
full_data["Fare"] = full_data["Fare"].astype(np.int64)
# mapping `Fare` by range

full_data.loc[full_data["Fare"] <= 7, "Fare"] = 0
full_data.loc[(full_data["Fare"] > 7) & (full_data["Fare"] <= 14), "Fare"] = 1
full_data.loc[(full_data["Fare"] > 14) & (full_data["Fare"] <= 28), "Fare"] = 2
full_data.loc[(full_data["Fare"] > 28) & (full_data["Fare"] <= 42), "Fare"] = 3
full_data.loc[ full_data["Fare"] > 42, "Fare"] = 4
full_data.groupby("Fare").count()<jupyter_output><empty_output><jupyter_text>Working with `Cabin` Feature:First lets check whether `Cabin` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Cabin contains any NaN value ? : ", full_data["Cabin"].isna().any())
print ("No. of empty/zero values in Cabin ?: {0}". format(len(full_data.loc[full_data["Cabin"] == 0])))
# Cabin contains NaN value so firstly lets replace the NaN  values with `0`

full_data["Cabin"] = full_data["Cabin"].fillna(0)
print ("Is Cabin contains any NaN value ? : ", full_data["Cabin"].isna().any())
print ("No. of empty/zero values in Cabin ?: {0}". format(len(full_data.loc[full_data["Cabin"] == 0])))
print ("Total number of passengers who have one or more cabin's: {0} ".format(len(full_data.loc[full_data["Cabin"] !=0 ])))
# replace all the cabin's with 1

full_data.loc[full_data["Cabin"] !=0 , "Cabin"] = 1
full_data["Cabin"].head()<jupyter_output><empty_output><jupyter_text>> `Cabin` looks decent now, so we can step ahead.   Working with `Embarked` Feature:First lets check whether `Embarked` feature contains any `NaN` or `empty/zero` values.<jupyter_code>print ("Is Embarked contains any NaN value ? : ", full_data["Embarked"].isna().any())
print ("No. of empty/zero values in Embarked ?: {0}". format(len(full_data.loc[full_data["Embarked"] == 0])))
# Embarked contains NaN value so firstly lets replace the NaN  values with 'S'
# `S` denotes Southampton, where the jouerney begins

full_data["Embarked"] = full_data["Embarked"].fillna("S")
print ("Is Embarked contains any NaN value ? : ", full_data["Embarked"].isna().any())
print ("No. of empty/zero values in Embarked ?: {0}". format(len(full_data.loc[full_data["Embarked"] == 0])))
full_data.groupby("Embarked").count()<jupyter_output><empty_output><jupyter_text>__Embarked feature denotes that from which station passengers are started their journey__In Embarked feature it has three group `S`, `C`, and `Q``S` denotes `Southampton``C` denotes `Cherbourg``Q` denotes `Queenstown`<jupyter_code># Now we map the Embarked feature

embark_map = {"S" : 0, "C" : 1, "Q": 2}

full_data["Embarked"] = full_data["Embarked"].map(embark_map)
full_data.groupby("Embarked").count()
full_data.head()<jupyter_output><empty_output><jupyter_text>> Every `features` looks good! so now we can start trainning our `Model`.Before that first lets check is their `correlation` present between `features`.<jupyter_code>full_data.corr()<jupyter_output><empty_output><jupyter_text>Visualize the correlation by heatmap:<jupyter_code>sns.set()
fig, ax = plt.subplots(figsize = (15, 10))
ax = sns.heatmap(full_data.corr(), cmap='Blues')<jupyter_output><empty_output><jupyter_text>> accoring to the above result __no__ tow features are correlated. So now we can proceed to train our `Model`.  Model Trainning<jupyter_code># sliceing the process data into train and test

train = full_data.iloc[:891, :]
test = full_data.iloc[891:, :]
train.tail()
test.tail()<jupyter_output><empty_output><jupyter_text>Train Model Using Naive Bayes Classifiers:<jupyter_code># lets specify the features
features_train = train.iloc[: , 1:-1].values
labels_train = train.iloc[: , 0:1].values
features_test = test.iloc[: , 1:-1].values
labels_test = test.iloc[: , 0:1].values

clf = GaussianNB()
clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)
acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = BernoulliNB()
clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = MultinomialNB()
clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = SVC(kernel="rbf")

clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = tree.DecisionTreeClassifier()
clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = SVC(kernel="rbf")

clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
clf = RandomForestClassifier()

clf.fit(features_train, labels_train.ravel())
pred_on_train_data = clf.predict(features_train)

acc_score = accuracy_score(pred_on_train_data, labels_train)

print ("Accuracy score in percentage : " , acc_score, "%")
pred_on_test_data = clf.predict(features_test)
import csv
file_name = "predict4.csv"
with open(file_name, 'w', newline='') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    filewriter.writerow(['PassengerId','Survived'])
    
    for i in range(len(test)):
        filewriter.writerow([test.index[i]+1, pred_on_test_data[i]])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15340991711502108, 'fraction_numerical': 0.00937908971935437, 'mean_word_length': 6.149168399168399, 'pattern_counts': {'":': 8, '<': 75, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '393763', 'license': 'MIT', 'n_tokens_mistral': 4892, 'n_tokens_neox': 4657, 'n_words': 1281, 'path': 'python/Kaggle-Titanic.ipynb', 'repo_name': '78526Nasir/Titanic-Machine-Learning-from-Disaster', 'size': '13754'}