<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Describe the Data
Step1:  Average age of Titanic survivors
Step2:  Average age of non-surviving first class passenger
Step3:  Average age of male survivors over 30 from anywhere but Queenstown
Step4:  Difference between mean and median for all passengers, filling in NaN with mean
Step5:  Difference between mean and median ages for survivors
Step6:  Difference between mean and median ages for non surviving first class passengers
Step7:  Difference between mean and median ages for survivor men over 30 not from Queenstown
Step8:  Most common passenger class
Step9:  Mode of Port of Embarkation
Step10:  Mode of siblings/spouses aboard
Step11:  The median ticket price is 0-1 stds from the mean. It is below the mean
Step12:  The cost difference between the 90th and 5th percent tickets and their classes
Step13:  The port with the most expensive average ticket price is Cherbourg
Step14:  The port with the most similar passenger class is Queenstown because it has the smallest std
Step15:  The average surviving passenger with family members was ~6.88 years younger than the average non surviving passenger with no family
Step16:  Display the relationship (i.e. make a plot) between survival rate and the quantile of the ticket price for 20 integer quantiles. Make sure you label your axes.
Step17:  Surviving men who paid less than median fare price
<ASSISTANT_TASK:>
Python Code:
avg_all = np.mean(titanic_dataframe['Age'])
print(avg_all)
Explanation: Describe the Data:
We have 891 rows in the train.csv data.
Columns in the data:
PassengerId - a seemingly random, unique number given to each row in the data.
Survived - a binary column. 1 means the person survived and 0 means they died.
Pclass - an indicator of socio-economic status.
Name - the name of a passenger on the Titanic.
Sex - the gender of the passenger
Age - the age of a given passenger
SibSp - is the number of siblings and spouses aboard.
Parch - number of parents/children
Ticket - Ticket number of a given passenger
Fare - The cost of the ticket
Cabin - Where they stayed on the ship
Embarked - Port of Embarkation.
Average age of all Titanic passengers:
End of explanation
avg_survive = np.mean(titanic_dataframe[titanic_dataframe.Survived==1].Age)
print(avg_survive)
Explanation: Average age of Titanic survivors:
End of explanation
avg_nonsurvivor_firstclass = np.mean(titanic_dataframe[(titanic_dataframe.Survived==0) & (titanic_dataframe.Pclass==1)].Age)
print(avg_nonsurvivor_firstclass)
Explanation: Average age of non-surviving first class passenger:
End of explanation
avg_male_survived_30_noq = np.mean(titanic_dataframe[(titanic_dataframe.Survived==1) & (titanic_dataframe.Sex=='male') &
                                  (titanic_dataframe.Age > 30) & (titanic_dataframe.Embarked!='Q')].Age)
print(avg_male_survived_30_noq)
Explanation: Average age of male survivors over 30 from anywhere but Queenstown:
End of explanation
median_forall = np.median(titanic_dataframe[titanic_dataframe.Age > 0].Age)
print(median_forall - avg_all)
Explanation: Difference between mean and median for all passengers, filling in NaN with mean
End of explanation
median_survivor = np.median(titanic_dataframe[(titanic_dataframe.Age >0) & (titanic_dataframe.Survived==1)].Age)
print(median_survivor - avg_survive)
Explanation: Difference between mean and median ages for survivors:
End of explanation
median_first_dead = np.median(titanic_dataframe[(titanic_dataframe.Age >0) & (titanic_dataframe.Survived==0) &
                                              (titanic_dataframe.Pclass==1) ].Age)
print(median_first_dead - avg_nonsurvivor_firstclass)
Explanation: Difference between mean and median ages for non surviving first class passengers:
End of explanation
median_men_noq = np.median(titanic_dataframe[(titanic_dataframe.Survived==1) & (titanic_dataframe.Sex=='male') &
                                  (titanic_dataframe.Age > 30) & (titanic_dataframe.Embarked!='Q')].Age)
print(median_men_noq - avg_male_survived_30_noq)
Explanation: Difference between mean and median ages for survivor men over 30 not from Queenstown:
End of explanation
titanic_dataframe['Pclass'].mode().item()
Explanation: Most common passenger class:
End of explanation
titanic_dataframe['Embarked'].mode().item()
Explanation: Mode of Port of Embarkation:
End of explanation
titanic_dataframe['SibSp'].mode().item()
Explanation: Mode of siblings/spouses aboard:
End of explanation
np.median(titanic_dataframe['Fare'])
np.mean(titanic_dataframe['Fare'])
titanic_dataframe['Fare'].mode().item()
np.std(titanic_dataframe['Fare'], ddof=1)
Explanation: The median ticket price is 0-1 stds from the mean. It is below the mean
End of explanation
nintieth_cost = titanic_dataframe['Fare'].quantile(.9)
fifth_cost = titanic_dataframe['Fare'].quantile(.05)
print(nintieth_cost - fifth_cost)
class_level = titanic_dataframe[(titanic_dataframe.Fare ==  titanic_dataframe['Fare'].quantile(.05))].Pclass
print(class_level)
upper_class = titanic_dataframe[(titanic_dataframe.Fare ==  titanic_dataframe['Fare'].quantile(.9))].Pclass
print(upper_class)
Explanation: The cost difference between the 90th and 5th percent tickets and their classes
End of explanation
south = np.mean(titanic_dataframe[(titanic_dataframe.Embarked == 'S')].Fare)
queens =  np.mean(titanic_dataframe[(titanic_dataframe.Embarked == 'Q')].Fare)
cherb = np.mean(titanic_dataframe[(titanic_dataframe.Embarked == 'C')].Fare)
print(south, queens, cherb)
Explanation: The port with the most expensive average ticket price is Cherbourg
End of explanation
southie = np.std(titanic_dataframe[(titanic_dataframe.Embarked=='S')].Pclass)
queenie = np.std(titanic_dataframe[(titanic_dataframe.Embarked=='Q')].Pclass)
cherbie = np.std(titanic_dataframe[(titanic_dataframe.Embarked=='C')].Pclass)
print(southie, queenie, cherbie)
Explanation: The port with the most similar passenger class is Queenstown because it has the smallest std
End of explanation
avg_surv_w_fam = np.mean(titanic_dataframe[(titanic_dataframe.Survived==1) & ((titanic_dataframe.SibSp > 0) | (titanic_dataframe.Parch > 0))].Age)
avg_non_no_fam = np.mean(titanic_dataframe[(titanic_dataframe.Survived==0) & (titanic_dataframe.SibSp==0) & (titanic_dataframe.Parch==0)].Age)
print(avg_surv_w_fam-avg_non_no_fam)
Explanation: The average surviving passenger with family members was ~6.88 years younger than the average non surviving passenger with no family
End of explanation
import matplotlib.pyplot as pl
%matplotlib inline
fare_quantiles = np.percentile(titanic_dataframe['Fare'], np.arange(5, 105, 5.0))
survival_quantiles = []
latest_quantile = 0
for f_q in fare_quantiles:
    folks= titanic_dataframe[(latest_quantile < titanic_dataframe.Fare) & (titanic_dataframe.Fare < f_q)]
    survival_quantiles.append(len(folks[titanic_dataframe.Survived == 1]) / float(len(folks)))
graph = pl.plot(np.arange(5, 105, 5.0), survival_quantiles)
Explanation: Display the relationship (i.e. make a plot) between survival rate and the quantile of the ticket price for 20 integer quantiles. Make sure you label your axes.
End of explanation
median_fare = np.median(titanic_dataframe.Fare)
surviving_first_less_median_fare = len(titanic_dataframe[(titanic_dataframe.Survived==1) 
                                 & (titanic_dataframe.Pclass==1)
                                & (titanic_dataframe.Sex=='male')
                               & (titanic_dataframe.Fare < median_fare)])
surviving_first = len(titanic_dataframe[(titanic_dataframe.Survived==1) 
                                 & (titanic_dataframe.Pclass==1)
                                & (titanic_dataframe.Sex=='male')])
print(surviving_first_less_median_fare)
print(surviving_first)
print(median_fare)
Explanation: Surviving men who paid less than median fare price:
End of explanation
<END_TASK>