<jupyter_start><jupyter_text>Data science project.____ Introduction.Titanic Data Set, As the name suggests (no points for guessing), this data set provides the data on all the passengers who were aboard the RMS Titanic when it sank on 15 April 1912 after colliding with an iceberg in the North Atlantic ocean. It is the most commonly used and referred to data set for beginners in data science. With 891 rows and 12 columns, this data set provides a combination of variables based on personal characteristics such as age, class of ticket and sex, and tests one’s classification skills.*** Objective:Predict the survival of the passengers aboard RMS Titanic. Link to the Data Set: https://www.kaggle.com/c/titanic/data Program > ```The whole program is written with the help of python lanugage.```<jupyter_code>#pyplot is matplotlib's plotting framework this imports the module matplotlib
import matplotlib.pyplot as plt

#library for scientific computing,np overcome conflict due to namespaces
import numpy as np

#providing fast, flexible, and expressive data structures designed to make working with structured 
import pandas as pd

#dict of Series objects
from pandas import Series, DataFrame

#seaborn help us see the different stylistic parameters
import seaborn as sns

#To ignore warnings
import warnings
warnings.filterwarnings("ignore")

#its a directiory helps Python to display matplotlib plots
%matplotlib inline<jupyter_output><empty_output><jupyter_text>General Survival rate by Categories<jupyter_code>#function will be evaluated against the column names,returning names where the callable function evaluates to True.
titanic=pd.read_csv('train.csv')

#creating the table of number of people died and number of people survived
titanic_Survived=titanic.groupby(['Survived'])

#describe the table and  Remove missing values
titanic_Survived.describe().dropna()<jupyter_output><empty_output><jupyter_text>Mapping of ratio of survival<jupyter_code>#Let's start by creating a new column for legibility purposes through mapping
titanic['Survivor'] = titanic.Survived.map({0:'no', 1:'yes'})

# Let's just get a quick overall view of survied vs died. 
sns.factorplot('Survivor', data=titanic, palette='plasma', kind='count')<jupyter_output><empty_output><jupyter_text>>Analysis says the rate of survival is less. Analysis based on age.<jupyter_code>#sort it by age and gender
titanic_Survived=titanic.groupby(['Age','Sex'])
#describe a person's age and gender in titanic_survied
titanic_Survived.describe().dropna()<jupyter_output><empty_output><jupyter_text>Plot of graph based on Age vs Survival<jupyter_code>#Age with missing value

#using module seaborn lable as died if survived=0
sns.kdeplot(titanic.loc[(titanic['Survived']==0),'Age'], label = 'Died',cut=0)

#using module seaborn lable as survived if survived=1
sns.kdeplot(titanic.loc[(titanic['Survived']==1),'Age'], label = 'Survived',cut=0)

#plot a graph with x-axis age 
plt.xlabel("AGE")

#plot a graoh with y-axis DENSITY ESTIMATION OF SURVIVAL 
plt.ylabel("DENSITY ESTIMATION OF SURVIVAL")

#and the title of graph as AGE VS SURVIVAL 
plt.title("AGE VS SURVIVAL")
plt.show()<jupyter_output><empty_output><jupyter_text>>Analysis says lesser the age, survival rate is high. Analysis based on gender.<jupyter_code># group the survival in the base of survived and gender
titanic_Survived=titanic.groupby(['Survived','Sex'])
#discribe titatic_survived in the form of table
titanic_Survived.describe().dropna()<jupyter_output><empty_output><jupyter_text>Plot based on sex ratio.<jupyter_code>#using module seaborn ploting graph of sex versus count
sns.factorplot('Sex', data=titanic, kind="count")<jupyter_output><empty_output><jupyter_text>>Analysis says male were more in number. Calculation of mean.<jupyter_code>#Female and male that survived in general
#observing the detail and taking the mean of gender and survived passenger
survived=titanic.groupby('Sex')['Survived']
survived.mean()<jupyter_output><empty_output><jupyter_text>let's use a linear plot on age versus survival<jupyter_code>sns.lmplot('Age','Survived', data=titanic)<jupyter_output><empty_output><jupyter_text>>The plot shows that the older the passenger is, the less chance he/she would survive.  Let's use a linear plot on sex ratio<jupyter_code>generations=[10,20,40,60,80]
sns.lmplot('Age','Survived',hue='Sex', data=titanic, palette='inferno',x_bins=generations)<jupyter_output><empty_output><jupyter_text>We shall do the analysis by considering under 16 age as child. And check the survival rate<jupyter_code># We'll treat anyone as under 16 as a child, and then use the apply technique with a function to create a new column

# First let's make a function to sort through the sex 
def male_female_child(passenger):
    # Take the Age and Sex
    age,sex = passenger
    # Compare the age, otherwise leave the sex
    if age < 16:
        return 'child'
    else:
        return sex


    
titanic['Person']= titanic[['Age','Sex']].apply(male_female_child, axis=1)
generations=[10,20,40,60,80]
sns.lmplot('Age','Survived',hue='Person', data=titanic, palette='inferno',x_bins=generations)<jupyter_output><empty_output><jupyter_text>>  Data Analysis by visiualizatin with respect to age and gender is been analized  _Final data preparation for prediction____ Checking the null values & non-null<jupyter_code>#Re-intializing the files
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")

#Grouping & Droping the null values
train_y = train['Survived']
train_x = train.drop(['Survived','Age',"Sex"],axis=1)


test_x = test.drop(['Age','Sex'],axis=1)

train_y.head()
train_x.head()
test_x.head()
train_x.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 9 columns):
PassengerId    891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
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(1), int64(4), object(4)
memory usage: 62.7+ KB<jupyter_text>_Prediction Algorithm____ Predicting using Random forest regressor Definiton:- Random forests or random decision forests are an ensemble learning method for classification, regression and other tasks, that operate by constructing a multitude of decision trees at training time and outputting the class that is the mode of the classes (classification) or mean prediction (regression) of the individual trees.<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

## Add these lines to turn off the warnings
import warnings
warnings.filterwarnings("ignore")

train = pd.io.parsers.read_csv("train.csv")
test = pd.io.parsers.read_csv('test.csv')

train = train.dropna(axis=0)

train_Y = train.Survived
train_predictor_columns = ['Pclass', 'Sex', 'Age', 'Fare']
train_X = train[train_predictor_columns]
test_X = test[train_predictor_columns]

### label encode the categorical values and convert them to numbers 
le = LabelEncoder()
le.fit(train_X['Sex'].astype(str))
train_X['Sex'] = le.transform(train_X['Sex'].astype(str))
test_X['Sex'] = le.transform(test_X['Sex'].astype(str))

le.fit(train_X['Pclass'].astype(str))
train_X['Pclass'] = le.transform(train_X['Pclass'].astype(str))
test_X['Pclass'] = le.transform(test_X['Pclass'].astype(str))

### train the model 
my_model = RandomForestRegressor()
my_model.fit(train_X, train_Y)


## fill the missing values in test data
for col in train_predictor_columns:
    mean_col = sum(train_X[col]) / len(train_X[col])
    test_X[col] = test_X[col].fillna(mean_col)

predictions = my_model.predict(test_X)
print(predictions)


model_tree= RandomForestRegressor();
model_tree = model_tree.fit(train_X, train_Y)
#predict
a = model_tree.predict(test_X)

test_x["Survived"] = a<jupyter_output><empty_output><jupyter_text>>   _Lets create a new file that stores the predicted data._<jupyter_code>submission_data = pd.DataFrame({'PassengerId':test['PassengerId'],'Survived':test_x['Survived']})

submission_data.to_csv("submission_data.csv",index=0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09910125823846615, 'fraction_numerical': 0.012582384661473937, 'mean_word_length': 6.188630490956072, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 64, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '400724', 'license': 'MIT', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2632, 'n_words': 826, 'path': 'Titanic-DataScience+MacheinLearning/Project.ipynb', 'repo_name': 'Vishnutejk98/DataScience_Project', 'size': '8345'}