<jupyter_start><jupyter_text>RMS Titanic Survival Prediction - Machine Learning from Disaster Importing Necessary Modules<jupyter_code>%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import tree, preprocessing
import sklearn.ensemble as ske
#Reading training data
df = pd.read_csv('titanic.csv', index_col=None,na_values=['NA'])
df.head()
#Checking Survival mean
df['Survived'].mean()
#Checking mean for each class
df.groupby('Pclass').mean()
#Checking mean for grouped data of class and sex
class_gender_group = df.groupby(['Pclass', 'Sex']).mean()
class_gender_group
#Plotting grouped survival for class and sex
class_gender_group['Survived'].plot.bar(color='Red')
#Plotting age by grouping it 
group_by_age = pd.cut(df['Age'], np.arange(0,90,10))
age_group = df.groupby(group_by_age).mean()
age_group['Survived'].plot.bar(color='Red')
#Checking for number of values present in given data
df.count()
#Dropping column Cabin
df = df.drop(['Cabin'], axis=1)
df.head()
#Dropping null values
df = df.dropna()
df.count()
#Using Label Encoder for transforming Sex and Embarked
#Dropping Name and Ticket columns
def preprocess_df(df):
    processed_df = df.copy()
    le = preprocessing.LabelEncoder()
    processed_df.Sex = le.fit_transform(processed_df.Sex)
    processed_df.Embarked = le.fit_transform(processed_df.Embarked)
    processed_df = processed_df.drop(['Name', 'Ticket'],axis=1)
    return processed_df
processed_df = preprocess_df(df)
#Taking Survived values in 'y'
X = processed_df.drop(['Survived'], axis=1).values
y = processed_df['Survived'].values
#Splitting data into train and test data
from sklearn import cross_validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)
#Using Decision Tree Classifier
clf = tree.DecisionTreeClassifier(max_depth=10)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
#Using Random Forest Classifier
ranfor = ske.RandomForestClassifier(n_estimators=50)
ranfor.fit(X_train, y_train)
ranfor.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Starting analysis on test data<jupyter_code>test_df = pd.read_csv('test.csv', index_col=None, na_values=['NA'])
pr_test_df = preprocess_df(test_df)
pr_test_df.count()
pr_test_df.drop(['Cabin'], axis=1)
pr_test_df['Age'] = pr_test_df['Age'].fillna(pr_test_df['Age'].mean())
pr_test_df.head()
#Adding Survived column to test data
pr_test_df['Survived'] = pd.Series(dtype=int, index=pr_test_df.index)
pr_test_df.head()
pr_test_df = pr_test_df.drop(['Cabin'], axis=1)
pr_test_df.head()
pr_test_df['Survived'] = pr_test_df['Survived'].fillna(0)
pr_test_df.isnull().values.any()
pr_test_df.isnull().sum()
pr_test_df['Fare'] = pr_test_df['Fare'].fillna(pr_test_df['Fare'].mean())
test_features = pr_test_df[['PassengerId', 'Pclass', 'Sex', 'Age', 'SibSp', 'Fare', 'Embarked', 'Survived']].values
pred = ranfor.predict(test_features)
#Using Tree Classifier
ids = pr_test_df['PassengerId']
predictions = clf.predict(pr_test_df.drop('PassengerId', axis=1))


output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
output.to_csv('titanic-clf.csv', index = False)
# Using Random Forests
ids = pr_test_df['PassengerId']
#predictions = clf.predict(pr_test_df.drop('PassengerId', axis=1))
pred = ranfor.predict(test_features)

output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': pred })
output.to_csv('titanic-ranfor.csv', index = False)
output.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14040114613180515, 'fraction_numerical': 0.0054441260744985676, 'mean_word_length': 7.412048192771084, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '346273', 'license': 'Apache-2.0', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1274, 'n_words': 266, 'path': 'Titanic_kaggle_first.ipynb', 'repo_name': 'Byte7/Titanic-Survival-Kaggle', 'size': '3490'}