Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore")
 pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None)
 TRAIN_FILE = ".. /input/train.csv"
TEST_FILE = ".. /input/test.csv"
SUBMISSION_FILE = ".. /input/gender_submission.csv"
print('Accuracy: '+ str(metrics.accuracy_score(y_test, y_pred_test2)))
 print('Precision: '+ str(metrics.precision_score(y_test, y_pred_test2, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_test, y_pred_test2, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_test, y_pred_test2, average='macro')) )<compute_test_metric>
train_data = pd.read_csv(TRAIN_FILE)
 train_data.info()
con_matrix_test2= metrics.confusion_matrix(y_test, y_pred_test2)
 cm_logistic_df = pd.DataFrame(con_matrix_test2, index=['ENFJ', 'ENFP', 'ENTJ', 'ENTP', 'ESFJ', 'ESFP',
'ESTJ', 'ESTP', 'INFJ', 'INFP', 'INTJ', 'INTP',
'ISFJ', 'ISFP', 'ISTJ', 'ISTP'],
columns=['predict_ENFJ','predict_ENFP','predict_ENTJ',
'predict_ENTP','predict_ESFJ','predict_ESFP',
'predict_ESTJ','predict_ESTP','predict_INFJ',
'predict_INFP','predict_INTJ','predict_INTP',
'predict_ISFJ','predict_ISFP','predict_ISTJ',
'predict_ISTP'])
<compute_test_metric>
train_data = pd.read_csv(TRAIN_FILE)
 print("
print(train_data[1:6].to_csv(index=False))
print("
train_data.info()
print(metrics.classification_report(y_test, y_pred_test2, target_names=unique_type))<predict_on_test>
test_data = pd.read_csv(TEST_FILE)
 print("
print(test_data[1:6].to_csv(index=False))
print("
test_data.info()
test_preds2 = pipe2.predict(z['clean'] )<categorify>
submission_data = pd.read_csv(SUBMISSION_FILE)
 print("
print(submission_data[1:6].to_csv(index=False))
z['type'] = encoder.inverse_transform(test_preds2)
 z.head()<data_type_conversions>
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelBinarizer
from sklearn import preprocessing
z['E or I'] = z.apply(lambda x: x['type'][0], axis = 1)
 z['N or S'] = z.apply(lambda x: x['type'][1], axis = 1)
 z['T or F'] = z.apply(lambda x: x['type'][2], axis = 1)
 z['J or P'] = z.apply(lambda x: x['type'][3], axis = 1)
 mind = z['E or I'].astype(str ).apply(lambda x: x[0] == 'E' ).astype('int')
 energy = z['N or S'].astype(str ).apply(lambda x: x[0] == 'N' ).astype('int')
 nature = z['T or F'].astype(str ).apply(lambda x: x[0] == 'T' ).astype('int')
 tactics = z['J or P'].astype(str ).apply(lambda x: x[0] == 'J' ).astype('int')
 z.head() 
<prepare_output>
def create_useless_column(data):
data["UselessColumn"] = 0
data_new = data[["UselessColumn"]]
return data_new
def extract_survived(data):
return data["Survived"]
def apply_model(data, data_label):
model = KNeighborsClassifier(n_neighbors=2)
 scores = cross_val_score(model, data, data_label, cv=5, verbose=1, scoring='accuracy')
 print(scores.mean())
 data_label = extract_survived(train_data)
 data = create_useless_column(train_data)
 apply_model(data, data_label )
df_LogReg2 = pd.DataFrame({"id":test_df['id'], "mind":mind, "energy":energy, "nature":nature, 'tactics':tactics})
 df_LogReg2.head()<save_to_csv>
def drop_survived(data):
return data.drop("Survived", axis=1, errors="ignore")
 def drop_passenger_id(data):
return data.drop("PassengerId", axis=1, errors="ignore")
 train_data = pd.read_csv(TRAIN_FILE)
 data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)[["Fare"]]
apply_model(data, data_label )
df_LogReg2.to_csv('EDSA_Team_8_Classification2.csv', index = False )<train_model>
print("Train: {} null(on {})".format(test_data["Pclass"].isnull().sum() , len(test_data)))
 print("Test: {} null(on {})".format(train_data["Pclass"].isnull().sum() , len(train_data)))
 print("")
 print("Correlation:")
 pclass = train_data[["Pclass", "Survived"]]
pclass["Class1"] =(pclass["Pclass"] == 1 ).astype(int)
 pclass["Class2"] =(pclass["Pclass"] == 2 ).astype(int)
 pclass["Class3"] =(pclass["Pclass"] == 3 ).astype(int)
 pclass = pclass.drop("Pclass", axis=1)
 corr = pclass.corr()
print(corr)
 sns.heatmap(corr,
xticklabels=corr.columns.values,
yticklabels=corr.columns.values, center=0 )
random= RandomForestClassifier(min_samples_leaf=2, min_samples_split=3, n_estimators=79,
criterion='entropy', bootstrap='False', n_jobs= -1, random_state=123)
 random_pipe= Pipeline([('vec', Vect),('model', random)] )<train_model>
data = train_data.copy()
data["Pclass"] = data["Pclass"] - 1
data["Pclass"] = preprocessing.maxabs_scale(data["Pclass"])
 print(data["Pclass"].value_counts() )
random_pipe.fit(X_train, y_train )<predict_on_test>
def handle_pclass(data):
new_data = data
new_data["Pclass"] = new_data["Pclass"] -1
new_data["Pclass"] = preprocessing.maxabs_scale(data["Pclass"])
 return new_data
data = train_data.copy()
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)[["Pclass"]]
apply_model(data, data_label )
y_pred_random=random_pipe.predict(X_train )<compute_test_metric>
names = train_data["Name"]
print(names[1:10])
 print("Train: {} null(on {})".format(test_data["Name"].isnull().sum() , len(test_data)))
 print("Test: {} null(on {})".format(train_data["Name"].isnull().sum() , len(train_data)))
 print("" )
print('Accuracy: '+ str(metrics.accuracy_score(y_train, y_pred_random)))
 print('Precision: '+ str(metrics.precision_score(y_train, y_pred_random, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_train, y_pred_random, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_train, y_pred_random, average='macro')) )<compute_test_metric>
data = train_data.copy()
data["Name"] = data["Name"].str.replace(".",";")
 data["Name"] = data["Name"].str.replace(",",";")
 data["Name"] = data["Name"].str.split(';', expand=True)[1]
unique1 = data["Name"].unique()
print(unique1)
 data2 = test_data.copy()
data2["Name"] = data2["Name"].str.replace(".",";")
 data2["Name"] = data2["Name"].str.replace(",",";")
 data2["Name"] = data2["Name"].str.split(';', expand=True)[1]
unique2 = data2["Name"].unique()
print(unique2)
 for i in unique2:
if not unique1.__contains__(i):
print("Missing:" + i)
 corr = data[["Name","Survived"]].corr()
print(corr)
 dumies = pd.get_dummies(data["Name"])
 dumies["Survived"] = data["Survived"]
sns.heatmap(corr,
xticklabels=corr.columns.values,
yticklabels=corr.columns.values, center=0)
 data[["Survived"]].groupby([data["Name"], data["Pclass"], data["Sex"], data["Survived"]] ).count()
print("Classification Report:")
 print(metrics.classification_report(y_train, y_pred_random, target_names=unique_type))<predict_on_test>
data = test_data.copy()
data["Name"] = test_data["Name"].str.replace(".",";")
 data["Name"] = data["Name"].str.replace(",",";")
 data["Name"] = data["Name"].str.split(';', expand=True)[1]
data["Name"] = data["Name"].str.replace("Capt","Mr")
 data["Name"] = data["Name"].str.replace("Col","Mr")
 data["Name"] = data["Name"].str.replace("Don","Mr")
 data["Name"] = data["Name"].str.replace("Dr","Mr")
 data["Name"] = data["Name"].str.replace("Jonkheer","Mr")
 data["Name"] = data["Name"].str.replace("Rev","Mr")
 data["Name"] = data["Name"].str.replace("Sir","Mr")
 data["Name"] = data["Name"].str.replace("Mme","Mrs")
 data["Name"] = data["Name"].str.replace("Mlle","Miss")
 data["Name"] = data["Name"].str.replace("the Countess","Mme")
 data["Name"] = data["Name"].str.replace("Mme","Mrs")
 data["Name"] = data["Name"].str.replace("Ms","Mrs")
 data["Name"] = data["Name"].str.replace("Major","Mr")
 data["Name"] = data["Name"].str.replace("Master","Mr")
 data["Name"] = data["Name"].str.replace("Lady","Miss")
 groups = data.groupby("Name" ).size()
groups.plot.bar()
y_pred_random_test = random_pipe.predict(X_test )<compute_test_metric>
def handle_name(data):
new_data = data
new_data["Name"] = data["Name"]
new_data["Name"] = new_data["Name"].str.replace(".",";")
 new_data["Name"] = new_data["Name"].str.replace(",",";")
 new_data["Name"] = new_data["Name"].str.split(';', expand=True)[1]
new_data["Name"] = new_data["Name"].str.replace("Capt","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Col","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Don","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Dr","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Jonkheer","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Rev","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Sir","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Mme","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Mlle","Miss")
 new_data["Name"] = new_data["Name"].str.replace("the Countess","Mme")
 new_data["Name"] = new_data["Name"].str.replace("Mme","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Ms","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Major","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Master","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Lady","Miss")
 new_data["Miss"] = new_data["Name"].str.contains("Miss" ).astype(int)
 new_data["Mr"] = new_data["Name"].str.contains("Mr" ).astype(int)
 new_data["Mrs"] = new_data["Name"].str.contains("Mrs" ).astype(int)
 new_data = new_data.drop("Name", axis=1)
 return new_data
data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data= handle_name(data)[["Pclass", "Miss", "Mr", "Mrs"]]
apply_model(data, data_label )
print('Accuracy: '+ str(metrics.accuracy_score(y_test, y_pred_random_test)))
 print('Precision: '+ str(metrics.precision_score(y_test, y_pred_random_test, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_test, y_pred_random_test, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_test, y_pred_random_test, average='macro')) )<compute_test_metric>
data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data= handle_name(data)
 data= handle_sex(data)[["Pclass", "Sex", "Miss", "Mr", "Mrs"]]
apply_model(data, data_label )
con_matrix_random= metrics.confusion_matrix(y_test, y_pred_random_test)
 cm_logistic_rf = pd.DataFrame(con_matrix_random, index=['ENFJ', 'ENFP', 'ENTJ', 'ENTP', 'ESFJ', 'ESFP',
'ESTJ', 'ESTP', 'INFJ', 'INFP', 'INTJ', 'INTP',
'ISFJ', 'ISFP', 'ISTJ', 'ISTP'],
columns=['predict_ENFJ','predict_ENFP','predict_ENTJ',
'predict_ENTP','predict_ESFJ','predict_ESFP',
'predict_ESTJ','predict_ESTP','predict_INFJ',
'predict_INFP','predict_INTJ','predict_INTP',
'predict_ISFJ','predict_ISFP','predict_ISTJ',
'predict_ISTP'])
 fig, ax = plt.subplots(figsize=(10,8))
sns.heatmap(cm_logistic_rf, robust=True, annot=True, linewidth=0.3,
fmt='', cmap='BrBG', vmax=303, ax=ax)
 plt.title('Confusion Matrix for Random Forest Classifier ', fontsize=10,
fontweight='bold', y=1.00)
 plt.xticks(fontsize=10)
 plt.yticks(rotation=0, fontsize=10)
 plt.show()<compute_test_metric>
data = train_data.copy()
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data= handle_name(data)
 data= handle_sex(data)
 data= handle_age(data)[["Pclass", "Miss", "Mr", "Mrs", "Sex", "Age"]]
apply_model(data, data_label )
print("Classification Report:")
 print(metrics.classification_report(y_test, y_pred_random_test, target_names=unique_type))<predict_on_test>
def handle_sibsp(data):
new_data = data
new_data["SibSp"] = preprocessing.maxabs_scale(data["SibSp"])
 return new_data
def handle_parch(data):
new_data = data
new_data["Parch"] = preprocessing.maxabs_scale(data["Parch"])
 return new_data
data = train_data.copy()
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = data[["Pclass", "Miss", "Mr", "Mrs", "Sex", "Age", "SibSp", "Parch"]]
apply_model(data, data_label )
test_preds_random = random_pipe.predict(z['clean'] )<categorify>
data = train_data.copy()
print(data["Ticket"].head(10))
z['type'] = encoder.inverse_transform(test_preds_random)
 z.head()<data_type_conversions>
def drop_ticket(data):
return data.drop(["Ticket"], axis=1)
 data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = data[["Pclass", "Miss", "Mr", "Mrs", "Sex", "Age", "SibSp", "Parch"]]
apply_model(data, data_label )
z['E or I'] = z.apply(lambda x: x['type'][0], axis = 1)
 z['N or S'] = z.apply(lambda x: x['type'][1], axis = 1)
 z['T or F'] = z.apply(lambda x: x['type'][2], axis = 1)
 z['J or P'] = z.apply(lambda x: x['type'][3], axis = 1)
 mind = z['E or I'].astype(str ).apply(lambda x: x[0] == 'E' ).astype('int')
 energy = z['N or S'].astype(str ).apply(lambda x: x[0] == 'N' ).astype('int')
 nature = z['T or F'].astype(str ).apply(lambda x: x[0] == 'T' ).astype('int')
 tactics = z['J or P'].astype(str ).apply(lambda x: x[0] == 'J' ).astype('int')
 z.head()<prepare_output>
def handle_fare(data):
new_data = data
new_data["Fare"] = new_data["Fare"].fillna(new_data["Fare"].mean())
 new_data["Fare"] = new_data["Fare"]/ 20
new_data["Fare"] = new_data["Fare"].astype(int)
 new_data["Fare"] = preprocessing.maxabs_scale(data["Fare"])
 return new_data
test_data = pd.read_csv(TRAIN_FILE)
 groups = handle_fare(test_data ).groupby("Fare" ).size()
groups.plot.bar()
df_random = pd.DataFrame({"id":test_df['id'], "mind":mind, "energy":energy, "nature":nature, 'tactics':tactics})
 df_random.head()<save_to_csv>
data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = handle_fare(data)
 data = data[["Pclass", "Mr", "Mrs", "Miss", "Sex", "Age", "SibSp", "Parch", "Fare"]]
apply_model(data, data_label )
df_random.to_csv('EDSA_Team_8_Classification_random.csv', index = False )<set_options>
data = train_data.copy()
print(data["Cabin"].head(15))
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
def handle_cabin(data):
new_data = data
new_data["Cabin"] = new_data["Cabin"].isna().astype(int)
 return new_data
data = train_data
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = handle_fare(data)
 data = handle_cabin(data)
 data = data[["Pclass", "Mr", "Mrs", "Miss", "Sex", "Age", "SibSp", "Parch","Fare", "Cabin"]]
apply_model(data, data_label )
train_df = pd.read_csv('.. /input/train.csv' )<load_from_csv>
print("Train: {} null(on {})".format(train_data["Embarked"].isnull().sum() , len(train_data)))
 print("Test: {} null(on {})".format(test_data["Embarked"].isnull().sum() , len(test_data)))
 print("")
 print("Correlation:")
 embarked = train_data[["Embarked", "Survived"]]
embarked["NotEmbarked"] = embarked["Embarked"].isna().astype(int)
 embarked["Embarked"] = embarked["Embarked"].fillna("")
 embarked['Southampton'] = embarked["Embarked"].str.contains("S" ).astype(int)
 embarked['Queenstown'] = embarked["Embarked"].str.contains("Q" ).astype(int)
 embarked['Cherbourg'] = embarked["Embarked"].str.contains("C" ).astype(int)
 embarked = embarked.drop("Embarked", axis=1)
 corr = embarked.corr()
print(corr)
 sns.heatmap(corr,
xticklabels=corr.columns.values,
yticklabels=corr.columns.values )
test_df=pd.read_csv('.. /input/test.csv' )<concatenate>
def handle_embarked(data):
new_data = data
new_data["NotEmbarked"] = new_data["Embarked"].isna().astype(int)
 new_data["Embarked"] = new_data["Embarked"].fillna("")
 new_data['Southampton'] = new_data["Embarked"].str.contains("S" ).astype(int)
 new_data['Queenstown'] = new_data["Embarked"].str.contains("Q" ).astype(int)
 new_data['Cherbourg'] = new_data["Embarked"].str.contains("C" ).astype(int)
 new_data = new_data.drop("Embarked", axis=1)
 return new_data
print(handle_embarked(train_data)[["NotEmbarked", "Southampton", "Queenstown", "Cherbourg"]].head() )
combined=pd.concat([train_df[['posts']],test_df[['posts']]] )<feature_engineering>
def process_data(data):
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = handle_fare(data)
 data = handle_cabin(data)
 data = handle_embarked(data)
 return data
data = train_data.copy()
data = process_data(data)
 print(data.head())
 apply_model(data, data_label )
vectorizer = CountVectorizer(stop_words='english',analyzer = "word",tokenizer = None,preprocessor = None,max_features = 10000)
 combined=vectorizer.fit_transform(combined['posts'] )<prepare_x_and_y>
model = KNeighborsClassifier(n_neighbors=2)
 X_train = pd.read_csv(TRAIN_FILE)
 y_train = X_train["Survived"]
X_train = process_data(X_train)
 X_test = pd.read_csv(TEST_FILE)
 test_labels = X_test[["PassengerId"]]
X_test = process_data(X_test)
 model.fit(X_train, y_train)
 result = model.predict(X_test)
 print(len(result))
df = pd.DataFrame()
df['PassengerId'] = test_labels.astype(int)
 df['Survived'] = result.astype(int)
 print(df.head())
 df.to_csv("submission.csv", index=False)
 print("Done")
train = combined[:train_df.shape[0]]
test = combined[train_df.shape[0]:]
y=train_df['type']<split>
pd.options.mode.chained_assignment = None
TRAIN_FILE = ".. /input/train.csv"
TEST_FILE = ".. /input/test.csv"
train_data = pd.read_csv(TRAIN_FILE)
 test_data = pd.read_csv(TEST_FILE)
 def drop_survived(data):
return data.drop("Survived", axis=1, errors="ignore")
 def drop_passenger_id(data):
return data.drop("PassengerId", axis=1, errors="ignore")
 def apply_model(data, data_label):
model = KNeighborsClassifier(n_neighbors=2)
 scores = cross_val_score(model, data, data_label, cv=5, verbose=1, scoring='accuracy')
 print(scores.mean())
 def extract_survived(data):
return data["Survived"]
def handle_pclass(data):
new_data = data
new_data["Pclass"] = new_data["Pclass"] -1
new_data["Pclass"] = preprocessing.maxabs_scale(data["Pclass"])
 return new_data
def handle_name(data):
new_data = data
new_data["Name"] = data["Name"]
new_data["Name"] = new_data["Name"].str.replace(".",";")
 new_data["Name"] = new_data["Name"].str.replace(",",";")
 new_data["Name"] = new_data["Name"].str.split(';', expand=True)[1]
new_data["Name"] = new_data["Name"].str.replace("Capt","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Col","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Don","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Dr","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Jonkheer","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Rev","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Sir","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Mme","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Mlle","Miss")
 new_data["Name"] = new_data["Name"].str.replace("the Countess","Mme")
 new_data["Name"] = new_data["Name"].str.replace("Mme","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Ms","Mrs")
 new_data["Name"] = new_data["Name"].str.replace("Major","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Master","Mr")
 new_data["Name"] = new_data["Name"].str.replace("Lady","Miss")
 new_data["Miss"] = new_data["Name"].str.contains("Miss" ).astype(int)
 new_data["Mr"] = new_data["Name"].str.contains("Mr" ).astype(int)
 new_data["Mrs"] = new_data["Name"].str.contains("Mrs" ).astype(int)
 new_data = new_data.drop("Name", axis=1)
 return new_data
def handle_sex(data):
new_data = data
new_data["Sex"] = data["Sex"].str.contains("female" ).astype(int)
 return new_data
def handle_age(data):
new_data = data
new_data["Age"] = new_data["Age"].fillna(new_data["Age"].mean())
 new_data["Age"] = new_data["Age"]/15
new_data["Age"] = new_data["Age"].astype(int)
 new_data["Age"] = preprocessing.maxabs_scale(data["Age"])
 return new_data
def handle_sibsp(data):
new_data = data
new_data["SibSp"] = preprocessing.maxabs_scale(data["SibSp"])
 return new_data
def handle_parch(data):
new_data = data
new_data["Parch"] = preprocessing.maxabs_scale(data["Parch"])
 return new_data
def drop_ticket(data):
return data.drop(["Ticket"], axis=1)
 def handle_fare(data):
new_data = data
new_data["Fare"] = new_data["Fare"].fillna(new_data["Fare"].mean())
 new_data["Fare"] = new_data["Fare"]/ 20
new_data["Fare"] = new_data["Fare"].astype(int)
 new_data["Fare"] = preprocessing.maxabs_scale(data["Fare"])
 return new_data
def handle_cabin(data):
new_data = data
new_data["Cabin"] = new_data["Cabin"].isna().astype(int)
 return new_data
def handle_embarked(data):
new_data = data
new_data["NotEmbarked"] = new_data["Embarked"].isna().astype(int)
 new_data["Embarked"] = new_data["Embarked"].fillna("")
 new_data['Southampton'] = new_data["Embarked"].str.contains("S" ).astype(int)
 new_data['Queenstown'] = new_data["Embarked"].str.contains("Q" ).astype(int)
 new_data['Cherbourg'] = new_data["Embarked"].str.contains("C" ).astype(int)
 new_data = new_data.drop("Embarked", axis=1)
 return new_data
def process_data(data):
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = handle_fare(data)
 data = handle_cabin(data)
 data = handle_embarked(data)
 return data
data = train_data.copy()
data = process_data(data)
 apply_model(data, data_label)
 model = KNeighborsClassifier(n_neighbors=2)
 X_train = pd.read_csv(TRAIN_FILE)
 y_train = X_train["Survived"]
X_train = process_data(X_train)
 X_test = pd.read_csv(TEST_FILE)
 test_labels = X_test[["PassengerId"]]
X_test = process_data(X_test)
 model.fit(X_train, y_train)
 result = model.predict(X_test)
 print(len(result))
df = pd.DataFrame()
df['PassengerId'] = test_labels.astype(int)
 df['Survived'] = result.astype(int)
 print(df.head())
 df.to_csv("submission.csv", index=False)
<import_modules><EOS> 
warnings.filterwarnings("ignore")
 pd.options.mode.chained_assignment = None
TRAIN_FILE = ".. /input/train.csv"
TEST_FILE = ".. /input/test.csv"
train_data = pd.read_csv(TRAIN_FILE)
 test_data = pd.read_csv(TEST_FILE)
 def extract_survived(data):
return data["Survived"]
def drop_survived(data):
return data.drop("Survived", axis=1, errors="ignore")
 def drop_passenger_id(data):
return data.drop("PassengerId", axis=1, errors="ignore")
 def handle_pclass(data):
new_data = data
new_data["Pclass"] = new_data["Pclass"] -1
new_data["Pclass"] = preprocessing.maxabs_scale(data["Pclass"])
 return new_data
def replace_multi(string, separators, new_separator):
for s in separators:
string = string.replace(s, new_separator)
 def filter_data_contains(data, column, contain, target_column=None):
new_data = data
if target_column == None:
new_data[column] = new_data[column].str.contains(contain ).astype(int)
 else:
new_data[target_column] = new_data[column].str.contains(contain ).astype(int)
 return new_data
def extract_title(x):
return x.replace(".",";" ).replace(",",";" ).split(";")[1]
def handle_name(data):
new_data = data
new_data["Name"] = new_data["Name"].apply(extract_title)
 to_replace = {
"Mr": ["Capt", "Col", "Don", "Dr", "Jonkheer", "Rev", "Sir", "Major", "Master" ],
"Miss": ["Mlle", "Lady" ],
"Mrs" : ["Mme", "the Countess", "Ms" ]
}
for title in to_replace.keys() :
for t in to_replace[title]:
new_data["Name"] = new_data["Name"].str.replace(t, title)
 new_data = filter_data_contains(new_data, "Name", title, title)
 new_data = new_data.drop("Name", axis=1)
 return new_data
def fill_na_with_mean(data, column):
new_data = data
new_data[column] = new_data[column].fillna(new_data[column].mean())
 return new_data
def fill_na_with_mean(data, column):
new_data = data
new_data[column] = new_data[column].fillna(new_data[column].mean())
 return new_data
def is_na(data, column):
new_data = data
new_data[column] = new_data[column].isna().astype(int)
 return new_data
def handle_sex(data):
return filter_data_contains(data, "Sex", "female")
 def handle_age(data):
new_data = data
new_data = fill_na_with_mean(new_data, "Age")
 new_data["Age"] =new_data["Age"] / 15
new_data["Age"] = preprocessing.maxabs_scale(data["Age"])
 return new_data
def handle_sibsp(data):
new_data = data
new_data["SibSp"] = preprocessing.maxabs_scale(data["SibSp"])
 return new_data
def handle_parch(data):
new_data = data
new_data["Parch"] = preprocessing.maxabs_scale(data["Parch"])
 return new_data
def drop_ticket(data):
return data.drop(["Ticket"], axis=1)
 def handle_fare(data):
new_data = data
new_data["Fare"] = fill_na_with_mean(new_data, "Fare")
 new_data["Fare"] = new_data["Fare"]/ 20
new_data["Fare"] = preprocessing.maxabs_scale(data["Fare"])
 return new_data
def handle_cabin(data):
new_data = data
new_data = is_na(new_data,"Cabin")
 return new_data
def handle_embarked(data):
new_data = data
new_data["NotEmbarked"] = new_data["Embarked"].isna().astype(int)
 new_data["Embarked"] = new_data["Embarked"].fillna("")
 new_data = filter_data_contains(new_data, "Embarked", "S", "Southampton")
 new_data = filter_data_contains(new_data, "Embarked", "Q", "Queenstown")
 new_data = filter_data_contains(new_data, "Embarked", "C", "Cherbourg")
 new_data = new_data.drop("Embarked", axis=1)
 return new_data
def process_data(data):
data = drop_survived(data)
 data = drop_passenger_id(data)
 data = handle_pclass(data)
 data = handle_name(data)
 data = handle_sex(data)
 data = handle_age(data)
 data = handle_sibsp(data)
 data = handle_parch(data)
 data = drop_ticket(data)
 data = handle_fare(data)
 data = handle_cabin(data)
 data = handle_embarked(data)
 return data
data = train_data.copy()
data_label = extract_survived(data)
 data = process_data(data)
 classifiers = {
"Nearest Neighbors" : KNeighborsClassifier(3),
"LinearRegression": linear_model.LinearRegression() ,
"Ridge": linear_model.Ridge(alpha =.5),
"Lasso": linear_model.Lasso(alpha = 0.1),
"ElasticNet": linear_model.ElasticNet(random_state=0),
"Lars": linear_model.Lars(n_nonzero_coefs=1),
"LassoLars": linear_model.LassoLars(alpha=.1),
"Omp": linear_model.OrthogonalMatchingPursuit(n_nonzero_coefs=1),
"BayesianRidge":linear_model.BayesianRidge() ,
"ARDRegression":linear_model.ARDRegression() ,
"LogisitcRegression":linear_model.LogisticRegression() ,
"SGDClassifier":linear_model.SGDClassifier() ,
"Perceptron": linear_model.Perceptron() ,
"PassiveAggressiveClassifier": linear_model.PassiveAggressiveClassifier() ,
"Theil-Sen": linear_model.TheilSenRegressor(random_state=42),
"RANSAC": linear_model.RANSACRegressor(random_state=42),
"Huber": linear_model.HuberRegressor() ,
"SVC linear": SVC(kernel="linear", C=0.025),
"SVC": SVC(gamma=2, C=1, probability=True),
"GuassianProcess":GaussianProcessClassifier(1.0 * RBF(1.0)) ,
"DecisionTree":DecisionTreeClassifier(max_depth=5),
"RandomForest":RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
"NeutraNet":MLPClassifier(alpha=1),
"ADABoost":AdaBoostClassifier() ,
"GaussianNB":GaussianNB() ,
"QDA":QuadraticDiscriminantAnalysis()
}
best_model_names = {}
for model_name in classifiers.keys() :
try:
model = classifiers[model_name]
scores = cross_val_score(model, data, data_label, cv=5, verbose=1, scoring='accuracy')
 score = scores.mean()
if score >.8:
best_model_names[model_name] = scores.mean()
print("{} {}".format(model_name, scores.mean()))
except:
pass
print(best_model_names)
 res = pd.DataFrame()
X_train = pd.read_csv(TRAIN_FILE)
 y_train = X_train["Survived"]
X_train = process_data(X_train)
 X_test = pd.read_csv(TEST_FILE)
 test_labels = X_test[["PassengerId"]]
X_test = process_data(X_test)
 res["PassengerId"] = test_labels["PassengerId"]
for model_name in best_model_names.keys() :
model = classifiers[model_name]
model.fit(X_train, y_train)
 result = model.predict_proba(X_test)[:,1]
print("{}: {} rows".format(model_name, len(result)))
 res[model_name] = result
models_list = list(best_model_names.keys())
 res['ProbaMin'] = res[models_list].min(axis=1)
 res['ProbaMax'] = res[models_list].max(axis=1)
 res['Accurate'] =(res['ProbaMin']<.20)|(res['ProbaMax']>.80)
 res['Survived'] =(res['ProbaMax']-0.5)>(0.5-res['ProbaMin'])
 res['Survived'] = res['Survived'].astype(int)
 res.to_csv("submission_detail.csv", index=False)
 res_filtered = res[["PassengerId","Survived"]]
res_filtered.to_csv("submission.csv", index=False)
 res.head(20 )