<jupyter_start><jupyter_text>The attributes have the following meaning:**Survived:** that's the target, 0 means the passenger did not survive, while 1 means he/she survived.**Pclass:** passenger class.**Name, Sex, Age:** self-explanatory**SibSp:** how many siblings & spouses of the passenger aboard the Titanic.**Parch:** how many children & parents of the passenger aboard the Titanic.**Ticket:** ticket id**Fare:** price paid (in pounds)**Cabin:** passenger's cabin number**Embarked:** where the passenger embarked the Titanic<jupyter_code>#data info

train_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId    891 non-null int64
Survived       891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
Sex            891 non-null object
Age            714 non-null float64
SibSp          891 non-null int64
Parch          891 non-null int64
Ticket         891 non-null object
Fare           891 non-null float64
Cabin          204 non-null object
Embarked       889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB<jupyter_text>**Age**, **Cabin** and **Embarked** have missing values.**Age** data is missing about **19%**, **Cabin** data is missing by **77%** ( we will ignore this column for now), **Embarked** is missing only **2 values.** We can impute Embarked by highest occuring value in the data. **Age** can be replaced by median values $(?)$Also , **Name** and **Ticket** Data is **object** type, so we need to check if we get some information out of it in the later part.<jupyter_code>#Embarked

train_data["Embarked"].value_counts()
#Replace NaN in Embarked by category S

train_data["Embarked"].fillna('S', inplace=True)

#Get Median Age and replace NaN with Median Age

age_median = train_data["Age"].median()
age_median

train_data["Age"].fillna(age_median, inplace=True)
#Let's describe the data now.

train_data.describe()<jupyter_output><empty_output><jupyter_text>All Numerical columns are free of **NaN** now. Let's makes some notes of data distribution now.* Mean for survived is 38%, This means only 38% of the passengers survived.* Average Fare is $32, Average Age is 29 years.<jupyter_code>#A quick look at categorical features.

#Pclass

train_data["Pclass"].value_counts()
#Let's see how is the distribution for survived passengers.

train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)<jupyter_output><empty_output><jupyter_text>**Observation:** Higher the class, greater the chance of survival<jupyter_code>#Sex

train_data["Sex"].value_counts()

train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)<jupyter_output><empty_output><jupyter_text>**Observation:** : Females has very high chnaces of survival as compared to males<jupyter_code>#Embarked

train_data["Embarked"].value_counts()
train_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False).mean().sort_values(by='Survived', ascending=False)<jupyter_output><empty_output><jupyter_text>**Observation:**: From initial summary, it looks like even port of embarkment is related to survival chances We will also need an imputer for string categorical values,<jupyter_code>train_data.head() 
#Drop columns Cabin, Name, PassengerId, Ticket

train_data.drop(["PassengerId","Name", "Ticket", "Cabin"], axis =1, inplace=True)
train_data.head()<jupyter_output><empty_output><jupyter_text>* First we will import Label encoder and changed categorical values into numerical values as per the category.* Second, we will call OneHotEncoder and changed required variables into hot encoding.* Third, we will concate the original data set with hot encoding values dataset.* Fouth, we will remove unneccesary columns.<jupyter_code>from sklearn.preprocessing import LabelEncoder

le_pClass = LabelEncoder()
le_sex = LabelEncoder()
le_embarked = LabelEncoder()
train_data['PClass_encoded'] = le_pClass.fit_transform(train_data.Pclass)
train_data['Sex_encoded'] = le_sex.fit_transform(train_data.Sex)
train_data['Embarked_encoded'] = le_embarked.fit_transform(train_data.Embarked)

train_data.head()
#One hot encoding for categorical columns (PClass, Sex, Embarked)

from sklearn.preprocessing import OneHotEncoder

pClass_ohe = OneHotEncoder()
sex_ohe = OneHotEncoder()
embarked_ohe = OneHotEncoder()

Xp =pClass_ohe.fit_transform(train_data.PClass_encoded.values.reshape(-1,1)).toarray()
Xs =sex_ohe.fit_transform(train_data.Sex_encoded.values.reshape(-1,1)).toarray()
Xe =embarked_ohe.fit_transform(train_data.Embarked_encoded.values.reshape(-1,1)).toarray()

#Add back to original dataframe

train_dataOneHot = pd.DataFrame(Xp, columns = ["PClass_"+str(int(i)) for i in range(Xp.shape[1])])
train_data = pd.concat([train_data, train_dataOneHot], axis=1)

train_dataOneHot = pd.DataFrame(Xs, columns = ["Sex_"+str(int(i)) for i in range(Xs.shape[1])])
train_data = pd.concat([train_data, train_dataOneHot], axis=1)

train_dataOneHot = pd.DataFrame(Xe, columns = ["Embarked_"+str(int(i)) for i in range(Xe.shape[1])])
train_data = pd.concat([train_data, train_dataOneHot], axis=1)
train_data.head()
#Drop unneccesary columns

train_data.drop(["Pclass","Sex", "Embarked", "PClass_encoded", "Sex_encoded", "Embarked_encoded"], axis =1, inplace=True)
train_data.head()
train_data.shape<jupyter_output><empty_output><jupyter_text>Split into train and Test Data<jupyter_code>train_data.info()
#Feature Matrix

X = train_data.drop(['Survived'], axis=1)
X.shape
#Target Vector

y = train_data['Survived']
y.shape
#Split into train test data

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state =42)
print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)
#SGDClassifier

from sklearn.linear_model import SGDClassifier

sgd_clf = SGDClassifier(random_state =42)
sgd_clf.fit(X_train, y_train)
#SupportVectorMachine

from sklearn.svm import SVC
svm_clf = SVC()
svm_clf.fit(X_train, y_train)
#RandomForestClassifier

from sklearn.ensemble import RandomForestClassifier
fr_clf = RandomForestClassifier(random_state=42)
fr_clf.fit(X_train, y_train)
#LogisticRegression

from sklearn.linear_model import LogisticRegression
lr_clf = LogisticRegression(random_state=42)
lr_clf.fit(X_train, y_train)
#KNearestNeighbors

from sklearn.neighbors import KNeighborsClassifier
knn_clf = KNeighborsClassifier(n_neighbors=3)
knn_clf.fit(X_train,y_train)
#DecisionTree

from sklearn.tree import DecisionTreeClassifier
dt_clf = DecisionTreeClassifier(random_state=42)
dt_clf.fit(X_train, y_train)
#Measuring accuracy Using Cross Validation

from sklearn.model_selection import cross_val_score

#SGDClassifier
sgd_clf_score = cross_val_score(sgd_clf, X_train, y_train, cv=10, scoring="accuracy")
sgd_mean = sgd_clf_score.mean()
sgd_mean
#SVMClassifier
svm_clf_score = cross_val_score(svm_clf, X_train, y_train, cv=10)
svm_mean = svm_clf_score.mean()
svm_mean
#RandomForestClassifier
rf_clf_score = cross_val_score(fr_clf, X_train, y_train, cv=10)
rf_mean = rf_clf_score.mean()
rf_mean
#LogisticRegression
lr_clf_score = cross_val_score(lr_clf, X_train, y_train, cv=10)
lr_mean = lr_clf_score.mean()
lr_mean
#KNearestNeighbors
knn_clf_score = cross_val_score(knn_clf, X_train, y_train, cv=10)
knn_mean = knn_clf_score.mean()
knn_mean
#DecisionTreeClassifier
dt_clf_score = cross_val_score(dt_clf, X_train, y_train, cv=10)
dt_mean = dt_clf_score.mean()
dt_mean
#Model Evaluations

models = pd.DataFrame({'Model': ['SGDClassifier', 'SupportVectorMachine', 'RandomForestClassifier',
                                'LogisticRegression', 'KNearestNeighbors', 'DecisionTreeClassifier'], 
                       'Score':[sgd_mean, svm_mean, rf_mean, lr_mean, knn_mean, dt_mean
                    ]})
models.sort_values(by = 'Score', ascending = False)<jupyter_output><empty_output><jupyter_text>Predictions on test data<jupyter_code>test_data.head()
test_data.info()
#We need to do the same transformation on test data file, so we can predict.

#Replace NaN in Embarked by category S
test_data["Embarked"].fillna('S', inplace=True)


#Get Median Age and replace NaN with Median Age
age_median = test_data["Age"].median()
test_data["Age"].fillna(age_median, inplace=True)

#Drop columns Cabin, Name, Ticket
test_data.drop(["Name", "Ticket", "Cabin"], axis =1, inplace=True)
#OneHotEncoding

le_pClass = LabelEncoder()
le_sex = LabelEncoder()
le_embarked = LabelEncoder()
test_data['PClass_encoded'] = le_pClass.fit_transform(test_data.Pclass)
test_data['Sex_encoded'] = le_sex.fit_transform(test_data.Sex)
test_data['Embarked_encoded'] = le_embarked.fit_transform(test_data.Embarked)

test_data.head()
pClass_ohe = OneHotEncoder()
sex_ohe = OneHotEncoder()
embarked_ohe = OneHotEncoder()

Xp =pClass_ohe.fit_transform(test_data.PClass_encoded.values.reshape(-1,1)).toarray()
Xs =sex_ohe.fit_transform(test_data.Sex_encoded.values.reshape(-1,1)).toarray()
Xe =embarked_ohe.fit_transform(test_data.Embarked_encoded.values.reshape(-1,1)).toarray()

test_data.head()
#Add back to original dataframe

test_dataOneHot = pd.DataFrame(Xp, columns = ["PClass_"+str(int(i)) for i in range(Xp.shape[1])])
test_data = pd.concat([test_data, test_dataOneHot], axis=1)

test_dataOneHot = pd.DataFrame(Xs, columns = ["Sex_"+str(int(i)) for i in range(Xs.shape[1])])
test_data = pd.concat([test_data, test_dataOneHot], axis=1)

test_dataOneHot = pd.DataFrame(Xe, columns = ["Embarked_"+str(int(i)) for i in range(Xe.shape[1])])
test_data = pd.concat([test_data, test_dataOneHot], axis=1)

test_data.head()
#Drop unneccesary columns

test_data.drop(["Pclass","Sex", "Embarked", "PClass_encoded", "Sex_encoded", "Embarked_encoded"], axis =1, inplace=True)
test_data.head()
#Predictions
test_data_pred = test_data.drop(["PassengerId"], axis =1)
test_data_pred.info()
#FARE is missing one entry , so will impute that by a median value.

fare_median = test_data_pred["Fare"].median()
fare_median

test_data_pred["Fare"].fillna(fare_median, inplace=True)
test_data_pred.info()
#Predict using LogisticRegression model

Y_pred = lr_clf.predict(test_data_pred)
#Make submission file version 1

submission = pd.DataFrame({
        "PassengerId": test_data["PassengerId"],
        "Survived": Y_pred
    })

submission.to_csv('Titanic_Prediction_v1.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12122367292480701, 'fraction_numerical': 0.013437529781759269, 'mean_word_length': 6.704845814977974, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '147769', 'license': 'Apache-2.0', 'n_tokens_mistral': 3833, 'n_tokens_neox': 3671, 'n_words': 851, 'path': 'KaggleProjects/Titanic_v1.ipynb', 'repo_name': 'vivekanandjoshi/ds', 'size': '10493'}