Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
%matplotlib inline
warnings.filterwarnings("always")
trainingdate = pd.to_datetime(trainingdata["Date"])
 testdate = pd.to_datetime(testdata["Date"])
 print(trainingdate )<categorify>
DATASETS_DIRECTORY = ".. /input"
train_data = pd.read_csv(os.path.join(DATASETS_DIRECTORY, "train.csv"))
test_data = pd.read_csv(os.path.join(DATASETS_DIRECTORY, "test.csv"))
full_data = pd.concat([train_data,test_data])
 targets = train_data["Survived"]
train_data.drop("Survived", axis=1, inplace=True )
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx)) 
<categorify>
class CategoricalEncoder(BaseEstimator, TransformerMixin):

 def __init__(self, encoding='onehot', categories='auto', dtype=np.float64,
handle_unknown='error'):
self.encoding = encoding
self.categories = categories
self.dtype = dtype
self.handle_unknown = handle_unknown
def fit(self, X, y=None):

 if self.encoding not in ['onehot', 'onehot-dense', 'ordinal']:
template =("encoding should be either 'onehot', 'onehot-dense' "
"or 'ordinal', got %s")
 raise ValueError(template % self.handle_unknown)
 if self.handle_unknown not in ['error', 'ignore']:
template =("handle_unknown should be either 'error' or "
"'ignore', got %s")
 raise ValueError(template % self.handle_unknown)
 if self.encoding == 'ordinal' and self.handle_unknown == 'ignore':
raise ValueError("handle_unknown='ignore' is not supported for"
" encoding='ordinal'")
 X = check_array(X, dtype=np.object, accept_sparse='csc', copy=True)
 n_samples, n_features = X.shape
self._label_encoders_ = [LabelEncoder() for _ in range(n_features)]
for i in range(n_features):
le = self._label_encoders_[i]
Xi = X[:, i]
if self.categories == 'auto':
le.fit(Xi)
 else:
valid_mask = np.in1d(Xi, self.categories[i])
 if not np.all(valid_mask):
if self.handle_unknown == 'error':
diff = np.unique(Xi[~valid_mask])
 msg =("Found unknown categories {0} in column {1}"
" during fit".format(diff, i))
raise ValueError(msg)
 le.classes_ = np.array(np.sort(self.categories[i]))
self.categories_ = [le.classes_ for le in self._label_encoders_]
return self
def transform(self, X):

 X = check_array(X, accept_sparse='csc', dtype=np.object, copy=True)
 n_samples, n_features = X.shape
X_int = np.zeros_like(X, dtype=np.int)
 X_mask = np.ones_like(X, dtype=np.bool)
 for i in range(n_features):
valid_mask = np.in1d(X[:, i], self.categories_[i])
 if not np.all(valid_mask):
if self.handle_unknown == 'error':
diff = np.unique(X[~valid_mask, i])
 msg =("Found unknown categories {0} in column {1}"
" during transform".format(diff, i))
raise ValueError(msg)
 else:
X_mask[:, i] = valid_mask
X[:, i][~valid_mask] = self.categories_[i][0]
X_int[:, i] = self._label_encoders_[i].transform(X[:, i])
 if self.encoding == 'ordinal':
return X_int.astype(self.dtype, copy=False)
 mask = X_mask.ravel()
n_values = [cats.shape[0] for cats in self.categories_]
n_values = np.array([0] + n_values)
 indices = np.cumsum(n_values)
 column_indices =(X_int + indices[:-1] ).ravel() [mask]
row_indices = np.repeat(np.arange(n_samples, dtype=np.int32),
n_features)[mask]
data = np.ones(n_samples * n_features)[mask]
out = sparse.csc_matrix(( data,(row_indices, column_indices)) ,
shape=(n_samples, indices[-1]),
dtype=self.dtype ).tocsr()
if self.encoding == 'onehot-dense':
return out.toarray()
else:
return out
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names]
class MostFrequentImputer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
self.most_frequent = pd.Series([X[c].value_counts().index[0] for c in X],
index=X.columns)
 return self
def transform(self, X, y=None):
return X.fillna(self.most_frequent)
 class CreateFamilySize(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
FamilySize = X['Parch'] + X['SibSp'] + 1
Familytype = FamilySize.map(lambda s: "Single" if s == 1 else("Small" if 2 <= s <= 4 else "Large"))
New_Df = pd.DataFrame(data=Familytype, columns=["Familytype"])
 return New_Df
class FillMissingCabin(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
New_Df = X.copy()
New_Df.fillna("U", inplace=True)
 return pd.DataFrame(data=New_Df["Cabin"], columns=["Cabin"])
 class ExtractCabin(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
New_Df = X.copy()
New_Df["Cabin"] = New_Df.Cabin.apply(lambda x: x[0])
 New_Df["Cabin"] = New_Df["Cabin"].str.replace("T", "U")
 New_Df["Cabin"] = New_Df["Cabin"].str.replace("F", "U")
 New_Df["Cabin"] = New_Df["Cabin"].str.replace("G", "U")
 return pd.DataFrame(data=New_Df["Cabin"], columns=["Cabin"])
 class HasCabin(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
New_Df = X.copy()
New_Df["Has_Cabin"] = [0 if pd.isnull(x)else 1 for x in New_Df["Cabin"]]
return pd.DataFrame(data=New_Df["Has_Cabin"], columns=["Has_Cabin"])
 class ExtractTitle(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
Title_Dictionary = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Royalty",
"Lady" : "Royalty"
}
New_Df = X.copy()
New_Df["Title"] = New_Df.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
 New_Df["Title"] = New_Df.Title.map(Title_Dictionary)
 New_Df.fillna("Mrs", inplace=True)
 return pd.DataFrame(data=New_Df["Title"], columns=["Title"])
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx)) 
<feature_engineering>
full_pipeline = Pipeline(steps=[ 
("features", make_union(
 make_pipeline(DataFrameSelector(["Embarked"]), MostFrequentImputer() , CategoricalEncoder(encoding='onehot-dense')) ,
make_pipeline(DataFrameSelector(["Pclass", "Sex"]), CategoricalEncoder(encoding='onehot-dense')) ,
make_pipeline(DataFrameSelector(["Age", "Fare"]), Imputer(strategy="median"), StandardScaler()),
make_pipeline(DataFrameSelector(["Name"]), ExtractTitle() , CategoricalEncoder(encoding='onehot-dense')) ,
make_pipeline(DataFrameSelector(["Cabin"]), HasCabin()),
make_pipeline(DataFrameSelector(["SibSp", "Parch"]), CreateFamilySize() , CategoricalEncoder(encoding='onehot-dense')) , 
)) , 
("poly", PolynomialFeatures()), 
("clf", RandomForestClassifier(random_state=42))
])
train.insert(6,"Month",m,False)
 train.insert(7,"Day",d,False)
 test.insert(4,"Month",mt,False)
 test.insert(5,"Day",dt,False )<count_unique_values>
X_train, X_test, y_train, y_test = train_test_split(train_data, targets, test_size=0.2, random_state=42 )
print("Training Data")
 traindays = trainingdata['Date'].nunique()
print("Number of Country_Region: ", trainingdata['Country_Region'].nunique())
 print("Number of Province_State: ", trainingdata['Province_State'].nunique())
 print("Number of Days: ", traindays)
 notrain = trainingdata['Id'].nunique()
print("Number of datapoints in train:", notrain)
 lotrain = int(notrain/traindays)
 print("L Trains:", lotrain)
<count_unique_values>
param_grid = [
{
'clf__bootstrap':[True],
'clf__max_depth':[80],
'clf__max_features':[2],
'clf__min_samples_leaf':[4],
'clf__min_samples_split':[10],
'clf__n_estimators':[100]
},
]
grid_search = GridSearchCV(full_pipeline, param_grid, cv=5, scoring='roc_auc', refit=True)
 grid_search.fit(X_train, y_train)
 final_model = grid_search.best_estimator_
test_predictions = final_model.predict(X_test)
 accuracy = accuracy_score(y_test, test_predictions)
 precision = precision_score(y_test, test_predictions)
 recall = recall_score(y_test, test_predictions)
 f1 = f1_score(y_test, test_predictions)
 auc_roc = roc_auc_score(y_test, test_predictions)
 print("accuracy_score = {a}, precision_score = {b}, recall_score = {c}, f1_score = {d}, AUC ROC = {e}" 
.format(a=accuracy, b=precision, c=recall, d=f1, e=auc_roc))
print("Test Data")
 testdays = testdata['Date'].nunique()
print("Number of Days: ", testdays)
 notest = testdata['ForecastId'].nunique()
print("Number of datapoints in test:", notest)
 lotest = int(notest/testdays)
 print("L Test:", lotest)
<define_variables>
y_scores = cross_val_predict(final_model, X_test, y_test, cv=5, method="predict_proba")
 fpr, tpr, thresholds = roc_curve(y_test, y_scores[:, 1] )
zt = testdate[0]
daycount = []
for i in range(0,lotrain):
for j in range(1,traindays+1):
daycount.append(j)
<define_variables>
print('Best accuracy: %.3f' % grid_search.best_score_ )
<prepare_output><EOS> 
final_predictions = final_model.predict(test_data)
 output = pd.DataFrame({ 'PassengerId' : test_data["PassengerId"], 'Survived': final_predictions })
 output.to_csv('titanic-predictions.csv', index = False)
 output.head()