Kaggle competition: Titanic - Machine Learning from Disaster
sub_file.to_csv('sample_submission.csv', index=False )<set_options>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
pd.set_option('display.float_format', lambda x: '%.3f' % x)
 RSEED = 100
%matplotlib inline
plt.style.use('fivethirtyeight')
 plt.rcParams['font.size'] = 18
palette = sns.color_palette('Paired', 10 )<load_from_csv>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
data = pd.read_csv('.. /input/train.csv', nrows = 5_000_000,
parse_dates = ['pickup_datetime'] ).drop(columns = 'key')
 data = data.dropna()
data.head()<filter>
all_data['Embarked'].fillna(all_data['Embarked'].mode() [0], inplace = True)
 all_data['Fare'].fillna(all_data['Fare'].median() , inplace = True )
print(f"There are {len(data[data['fare_amount'] < 0])} negative fares.")
 print(f"There are {len(data[data['fare_amount'] == 0])} $0 fares.")
 print(f"There are {len(data[data['fare_amount'] > 100])} fares greater than $100." )<filter>
all_data['Title'] = all_data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 all_data['Title'].value_counts()
frequent_titles = all_data['Title'].value_counts() [:5].index.tolist()
frequent_titles
all_data['Title'] = all_data['Title'].apply(lambda x: x if x in frequent_titles else 'Other')
 all_data['Title']
data = data[data['fare_amount'].between(left = 2.5, right = 100)]<compute_test_metric>
median_ages = {}
for title in frequent_titles:
median_ages[title] = all_data.loc[all_data['Title'] == title]['Age'].median()
median_ages['Other'] = all_data['Age'].median()
all_data.loc[all_data['Age'].isnull() , 'Age'] = all_data[all_data['Age'].isnull() ]['Title'].map(median_ages)
 all_data['Age']
def ecdf(x):

 x = np.sort(x)
 n = len(x)
 y = np.arange(1, n + 1, 1)/ n
return x, y<filter>
Cat_Features = ['Sex', 'Embarked', 'Title']
for feature in Cat_Features:
label = LabelEncoder()
all_data[feature] = label.fit_transform(all_data[feature])
 all_data[Cat_Features]
data = data.loc[data['passenger_count'] < 6]<train_model>
Cont_Features = ['Age', 'Fare']
num_bins = 5
for feature in Cont_Features:
bin_feature = feature + 'Bin'
all_data[bin_feature] = pd.qcut(all_data[feature], num_bins)
 label = LabelEncoder()
all_data[bin_feature] = label.fit_transform(all_data[bin_feature])
 all_data.head(10 )
print(f'Initial Observations: {data.shape[0]}' )<define_variables>
all_data['Surname'] = all_data.Name.str.extract(r'([A-Za-z]+),', expand=False)
 all_data['TicketPrefix'] = all_data.Ticket.str.extract(r' (.*\d)', expand=False)
 all_data['Surname_Ticket'] = all_data['Surname'] + all_data['TicketPrefix']
all_data['IsFamily'] = all_data.Surname_Ticket.duplicated(keep=False ).astype(int)
 all_data['Child'] = all_data.Age.map(lambda x: 1 if x <=16 else 0)
 FamilyWithChild = all_data[(all_data.IsFamily==1)&(all_data.Child==1)]['Surname_Ticket'].unique()
all_data['FamilyId'] = 0
for ind, identifier in enumerate(FamilyWithChild):
all_data.loc[all_data.Surname_Ticket==identifier, ['FamilyId']] = ind + 1
all_data['FamilySurvival'] = 1
Survived_by_FamilyId = all_data.groupby('FamilyId' ).Survived.sum()
for i in range(1, len(FamilyWithChild)+1):
if Survived_by_FamilyId[i] >= 1:
all_data.loc[all_data.FamilyId==i, ['FamilySurvival']] = 2
elif Survived_by_FamilyId[i] == 0:
all_data.loc[all_data.FamilyId==i, ['FamilySurvival']] = 0
all_data.head(10 )
data = data.loc[data['pickup_latitude'].between(40, 42)]
data = data.loc[data['pickup_longitude'].between(-75, -72)]
data = data.loc[data['dropoff_latitude'].between(40, 42)]
data = data.loc[data['dropoff_longitude'].between(-75, -72)]
print(f'New number of observations: {data.shape[0]}' )<define_search_space>
warnings.filterwarnings('ignore')
 train["CabinBool"] =(train["Cabin"].notnull().astype('int'))
test["CabinBool"] =(test["Cabin"].notnull().astype('int'))
BB_zoom =(-74.1, -73.7, 40.6, 40.85)
 nyc_map_zoom = plt.imread('https://github.com/WillKoehrsen/Machine-Learning-Projects/blob/master/images/nyc_-74.1_-73.7_40.6_40.85.PNG?raw=true' )<define_variables>
main_features = ['Sex', 'FamilySurvival', 'FareBin', 'Pclass', 'Title']
X_test = test[main_features]
X_train = train[main_features]
y_train = train['Survived']
color_mapping = {fare_bin: palette[i] for i, fare_bin in enumerate(data['fare-bin'].unique())}
color_mapping<feature_engineering>
ensemble = [RandomForestClassifier() , svm.NuSVC(probability=True), neighbors.KNeighborsClassifier() ]
classifiers_with_names = []
_ = [classifiers_with_names.append(( clf.__class__.__name__, clf)) for clf in ensemble]
voting = VotingClassifier(classifiers_with_names, voting='hard')
 cv_results = cross_validate(voting, X_train, y_train, cv=5)
 print(cv_results['test_score'].mean())
 voting.fit(X_train, y_train)
 predictions = voting.predict(X_test)
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions.astype(int)})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
data['abs_lat_diff'] =(data['dropoff_latitude'] - data['pickup_latitude'] ).abs()
data['abs_lon_diff'] =(data['dropoff_longitude'] - data['pickup_longitude'] ).abs()<filter>