Kaggle competition: Titanic - Machine Learning from Disaster
np.sum(pd.isnull(X_test))<import_modules>
sns.set()
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator)
 class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = quadratic_weighted_kappa(y, X_p)
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
def rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))<init_hyperparams>
print('Out of 891 entries, cabin is missing 687 so I'm droping it, also droping ticket and embarked because it isnt relevant')
 train.isna().sum()
params = {'application': 'regression',
'boosting': 'gbdt',
'metric': 'rmse',
'num_leaves': 70,
'max_depth': 9,
'learning_rate': 0.01,
'bagging_fraction': 0.85,
'feature_fraction': 0.8,
'min_split_gain': 0.02,
'min_child_samples': 150,
'min_child_weight': 0.02,
'lambda_l2': 0.0475,
'verbosity': -1,
'data_random_seed': 17}
early_stop = 500
verbose_eval = 100
num_rounds = 10000
n_splits = 5<prepare_x_and_y>
train = train.drop(['Cabin', 'Ticket', 'Embarked'], axis=1)
 test = test.drop(['Cabin', 'Ticket', 'Embarked'], axis=1 )
kfold = StratifiedKFold(n_splits=n_splits, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_index, valid_index in kfold.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_index, :]
X_val = X_train.iloc[valid_index, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 print('
y_tr distribution: {}'.format(Counter(y_tr)))
 d_train = lgb.Dataset(X_tr, label=y_tr)
 d_valid = lgb.Dataset(X_val, label=y_val)
 watchlist = [d_train, d_valid]
print('training LGB:')
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop)
 val_pred = model.predict(X_val, num_iteration=model.best_iteration)
 test_pred = model.predict(X_test, num_iteration=model.best_iteration)
 oof_train[valid_index] = val_pred
oof_test[:, i] = test_pred
i += 1<statistical_test>
print('filling age with age mean')
 train.isna().sum()
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
pred_test_y_k = optR.predict(oof_train, coefficients)
 print("
Valid Counts = ", Counter(X_train['AdoptionSpeed'].values))
print("Predicted Counts = ", Counter(pred_test_y_k))
print("Coefficients = ", coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, pred_test_y_k)
 print("QWK = ", qwk )<predict_on_test>
train['Age'] = train['Age'].fillna(np.mean(train['Age']))
coefficients_ = coefficients.copy()
coefficients_[0] = 1.645
coefficients_[1] = 2.115
coefficients_[3] = 2.84
train_predictions = optR.predict(oof_train, coefficients_ ).astype(int)
 print('train pred distribution: {}'.format(Counter(train_predictions)))
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_)
 print('test pred distribution: {}'.format(Counter(test_predictions)) )<count_values>
print('age and fare filling with mean')
 test.isna().sum()
print("True Distribution:")
 print(pd.value_counts(X_train['AdoptionSpeed'], normalize=True ).sort_index())
 print("
Train Predicted Distribution:")
 print(pd.value_counts(train_predictions, normalize=True ).sort_index())
 print("
Test Predicted Distribution:")
 print(pd.value_counts(test_predictions, normalize=True ).sort_index() )<save_to_csv>
test['Age'] = test['Age'].fillna(np.mean(test['Age']))
test['Fare'] = test['Fare'].fillna(np.mean(test['Fare']))
submission = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions.astype(np.int32)})
 submission.head()
submission.to_csv('submission.csv', index=False )<set_options>
selected_features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare']
%matplotlib inline
plt.rc('figure', figsize=(20.0, 10.0))<load_from_csv>
X_train = train[selected_features]
X_test = test[selected_features]
X_train = pd.get_dummies(X_train)
 X_test = pd.get_dummies(X_test)
 y_train = train['Survived']
train_df = pd.read_csv(os.path.join(INPUT_DIR, 'train', 'train.csv'))
X_test = pd.read_csv(os.path.join(INPUT_DIR, 'test', 'test.csv'))<categorify>
model = svm.LinearSVC()
model.fit(X_train, y_train )
class DataFrameColumnMapper(BaseEstimator, TransformerMixin):

 def __init__(self, column_name, mapping_func, new_column_name=None, drop_original=True):

 self.column_name = column_name
self.mapping_func = mapping_func
self.new_column_name = new_column_name if new_column_name is not None else self.column_name
self.drop_original = drop_original
def fit(self, X, y=None):
return self
def transform(self, X):
transformed_column = X.transform({self.column_name: self.mapping_func})
 Y = X.copy()
Y = Y.assign(**{self.new_column_name: transformed_column})
 if self.column_name != self.new_column_name and self.drop_original:
Y = Y.drop(self.column_name, axis=1)
 return Y<categorify>
model = svm.SVC()
model.fit(X_train, y_train )
class CategoricalToOneHotEncoder(BaseEstimator, TransformerMixin):

 def __init__(self, columns=None):
self.columns = columns
self.mappings_ = None
def fit(self, X, y=None):
if self.columns is None:
self.columns = X.select_dtypes(exclude='number')
 mappings = {}
for col in self.columns:
labels, uniques = X.loc[:, col].factorize()
int_and_cat = list(enumerate(uniques))
cat_and_int = [(x[1], x[0])for x in int_and_cat]
mappings[col] = {'int_to_cat': dict(int_and_cat), 'cat_to_int': dict(cat_and_int)}
self.mappings_ = mappings
return self
def transform(self, X):
Y = X.copy()
for col in self.columns:
transformed_col = Y.loc[:, col].transform(lambda x: self.mappings_[col]['cat_to_int'][x])
 for key, val in self.mappings_[col]['cat_to_int'].items() :
one_hot =(transformed_col == val)+ 0
Y = Y.assign(**{'{}_{}'.format(col, key): one_hot})
 Y = Y.drop(col, axis=1)
 return Y<train_model>
model = svm.NuSVC()
model.fit(X_train, y_train )
class CategoricalTruncator(BaseEstimator, TransformerMixin):

 def __init__(self, column_name, n_values_to_keep=5):
self.column_name = column_name
self.n_values_to_keep = n_values_to_keep
self.values_ = None
def fit(self, X, y=None):
self.values_ = list(X[self.column_name].value_counts() [:self.n_values_to_keep].keys())
 return self
def transform(self, X):
transform = lambda x: x if x in self.values_ else 'Other'
Y = X.copy()
y = Y.transform({self.column_name: transform})
 return Y.assign(**{self.column_name: y} )<prepare_x_and_y>
model = neighbors.KNeighborsClassifier()
model.fit(X_train, y_train )
class DataFrameColumnDropper(BaseEstimator, TransformerMixin):

 def __init__(self, column_names):
self.column_names = column_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X.copy().drop(self.column_names, axis=1 )<train_model>
model = GradientBoostingClassifier(n_estimators=130, learning_rate=0.05)
 model.fit(X_train, y_train )
class ColumnByFeatureImportancePicker(BaseEstimator, TransformerMixin):

 def __init__(self, n_features: int = 20, classifier=RandomForestClassifier(n_estimators=100, random_state=42)) :
self.n_features = n_features
self.classifier = classifier
self.attributes_ = None
def fit_and_compute_importances(self, X_df, y):

 X_numeric = X_df.select_dtypes(include='number')
 X = X_numeric.values
self.classifier.fit(X, y)
 feature_importances = self.classifier.feature_importances_
feature_names = list(X_numeric)
 feature_importances_with_names = [(feature_name, feature_importance)for feature_name, feature_importance in zip(feature_names, feature_importances)]
feature_importances_with_names.sort(key=lambda x: x[1], reverse=True)
 return feature_importances_with_names
def fit(self, X, y=None):
if self.n_features is None:
self.attributes_ = list(X)
 return self
assert y is not None, "Feature importances cannot be computed without y!"
feature_importances_with_names = self.fit_and_compute_importances(X, y)
 self.attributes_ = [feature_name for feature_name, _ in feature_importances_with_names[:self.n_features]]
return self
def transform(self, X):
return X.copy().loc[:, self.attributes_]<categorify>
predictions = model.predict(X_test )
<split><EOS> 
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")
 output