Kaggle competition: Titanic - Machine Learning from Disaster
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 = cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
y = y.cpu().apply_(lambda x: 0 if x==0 else 1 if x==0.25 else 2 if x==0.5 else 3 if x==0.75 else 4)
 loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.125,0.375,0.625,0.875]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
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']
opt = OptimizedRounder()<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(10)
 train_data.tail(10)
 train_data.query('Cabin == Cabin' ).shape
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
learn.dl(DatasetType.Test ).dataset.tfms=[]
op = _TTA(learn,ds_type=DatasetType.Test)
 op_1 = [2*p-4 for p in op]<load_pretrained>
len(train_data.loc[(train_data.Sex == 'female')&(train_data.Survived == 1)]["Survived"])
 train_data.loc[:,["Name", "Age", "Pclass"]]
train_data[["Name", "Age", "Pclass"]]
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived=", rate_men )
!cp.. /input/l1-aptos/L1.pkl export.pkl
learn = load_learner('',test=ImageList.from_df(test_df,
'test_processed/',
folder='',
suffix='.png', 
))
learn.dl(DatasetType.Test ).dataset.tfms=[]
op_2 = _TTA(learn,ds_type=DatasetType.Test )<feature_engineering>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 actual = model.predict(X)
 predictions = model.predict(X_test)
 print("X data accuracy: " , round(accuracy_score(y, actual)* 100, 2), "%")
p = [0] * len(op_1)
 for i in range(len(op_1)) :
p[i] =(op_1[i]+op_2[i])/2<save_to_csv>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Embarked"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 X['Age'] = train_data['Age']
X['Fare'] = train_data['Fare']
X['Cabin'] = np.where(train_data['Cabin'].isna() ==True,0,1)
 X.groupby('Cabin' ).count()
X_test['Age'] = test_data['Age']
X_test['Fare'] = test_data['Fare']
X_test['Cabin'] = np.where(test_data['Cabin'].isna() ==True,0,1)
 X['Age'] = X['Age'].fillna(X['Age'].mean())
 X_test['Age'] = X_test['Age'].fillna(X_test['Age'].mean())
 X_test['Fare'] = X_test['Fare'].fillna(X_test['Fare'].mean())
 model = RandomForestClassifier(n_estimators=1000, max_depth=5, random_state=1)
 model.fit(X, y)
 actual = model.predict(X)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("X data accuracy: " , round(accuracy_score(y, actual)* 100, 2), "%")
 print("Your submission was successfully saved!" )
<groupby><EOS> 
np.random.seed(7)