<jupyter_start><jupyter_text>Tensor flow development<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.pyplot import rcParams
import os
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

%matplotlib inline
rcParams['figure.figsize'] = 10,8
sns.set(style='whitegrid', palette='muted',
        rc={'figure.figsize': (12,8)})


# Load data as Pandas dataframe. We concat the data to clean the data in both train and set
train = pd.read_csv('/data/train.csv', )
test = pd.read_csv('/data/test.csv')
df = pd.concat([train, test], axis=0, sort=True)
df.head()
def display_all(df):
    with pd.option_context("display.max_rows", 1000, "display.max_columns", 1000): 
        display(df)

        
display_all(df.describe(include='all').T)
df['Survived'].value_counts()
# create new Title column in order to ocalculate more appropriately the missing ages
df['Title'] = df['Name'].str.extract('([A-Za-z]+)\.', expand=True)
df.head()
df['Title'].value_counts()
# replace rare titles with more common ones
mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr',
           'Don': 'Mr', 'Mme': 'Mrs', 'Jonkheer': 'Mr', 'Lady': 'Mrs',
           'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
df.replace({'Title': mapping}, inplace=True)
# confirm that we are left with just six values
df['Title'].value_counts()
# impute missing Age values using median of Title groups
title_ages = dict(df.groupby('Title')['Age'].median())

# create a column of the average ages
df['age_med'] = df['Title'].apply(lambda x: title_ages[x])

# replace all missing ages with the value in this column
df['Age'].fillna(df['age_med'], inplace=True, )
del df['age_med']
sns.barplot(x='Title', y='Age', data=df, estimator=np.median, ci=None, palette='Blues_d')
plt.xticks(rotation=45)
plt.show()
sns.countplot(x='Title', data=df, palette='hls', hue='Survived')
plt.xticks(rotation=45)
plt.show()
sns.swarmplot(x='Sex', y='Fare', hue='Survived', data=df)
plt.show()
# impute missing Fare values using median of Pclass groups
class_fares = dict(df.groupby('Pclass')['Fare'].median())

# create a column of the average fares
df['fare_med'] = df['Pclass'].apply(lambda x: class_fares[x])

# replace all missing fares with the value in this column
df['Fare'].fillna(df['fare_med'], inplace=True, )
del df['fare_med']
    
sns.catplot(x='Embarked', y='Survived', data=df,
            kind='bar', palette='muted', ci=None)
plt.show()
# Need to know what is Backfill!!!

df['Embarked'].fillna(method='backfill', inplace=True)
df['Family_Size'] = df['Parch'] + df['SibSp']
display_all(df.describe(include='all').T)
train = df[pd.notnull(df['Survived'])]
test = df[pd.isnull(df['Survived'])]
train.to_csv('/data/train_clean.csv', index=False)
test.to_csv('/data/test_clean.csv', index=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup