Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
train = pd.read_csv('.. /input/train.csv')
 train.info()
TARGETS = ['reactivity', 'deg_Mg_pH10', 'deg_pH10', 'deg_Mg_50C', 'deg_50C']
SCORED_TARGETS = [0, 1, 3]
NUM_TARGETS = len(TARGETS)
 SEQ_SCORED_PUBLIC = 68
SEQ_SCORED_PRIVATE = 91
SEQ_LEN_PUBLIC = 107
SEQ_LEN_PRIVATE = 130<define_variables>
def prep_data(df):
to_be_dropped = ['Name', 'Cabin', 'Ticket']
df['Embarked'] = df['Embarked'].fillna('S')
 df['Age'] = df['Age'].fillna(median_age)
 df['Fare'] = df['Fare'].fillna(median_fare)
 df['Companions'] = df['Parch'] + df['SibSp']
to_be_dropped.extend(['Parch', 'SibSp'])
 df.loc[ df['Age'] <= 16, 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[ df['Age'] > 64, 'Age'] = 4
df['Age'] = df['Age'].astype(int)
 df.loc[ df['Fare'] <= 7.91, 'Fare'] = 0
df.loc[(df['Fare'] > 7.91)&(df['Fare'] <= 14.454), 'Fare'] = 1
df.loc[(df['Fare'] > 14.454)&(df['Fare'] <= 31), 'Fare'] = 2
df.loc[ df['Fare'] > 31, 'Fare'] = 3
df['Fare'] = df['Fare'].astype(int)
 df['Title'] = df['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].fillna('NoTitle')
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 df['Sex'] = df['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 df['Title'] = df['Title'].map({'NoTitle': 0, 'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Rare': 5} ).astype(int)
 df['CabinCode'] = df['Cabin'].astype(str ).str[0]
df['CabinCode'] = df['CabinCode'].map({'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E':1, 'F':1, 'G': 1, 'T': 0, 'n' : 0} ).astype(int)
 df = df.set_index('PassengerId')
 df = df.drop(to_be_dropped, axis = 1)
 return df
BASE_PATH = ".. /input/stanford-covid-vaccine/"
CP_PATH = ""
PRETRAINED_PATH = ".. /input/covid-pretrained/pretrained_model.pt"
DEVICE = torch.device('cuda')
 TODAY = str(datetime.date.today() )<load_from_disk>
y = train['Survived']
X = train.drop('Survived', axis = 1)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state=7 )
train_df = pd.read_json(str(Path(BASE_PATH)/ 'train.json'), lines=True)
 test_df = pd.read_json(str(Path(BASE_PATH)/ 'test.json'), lines=True)
 public_df = test_df[test_df["seq_length"] == SEQ_LEN_PUBLIC].reset_index(drop=True)
 private_df = test_df[test_df["seq_length"] == SEQ_LEN_PRIVATE].reset_index(drop=True )<define_variables>
warnings.filterwarnings("ignore", category=DeprecationWarning)
 warnings.filterwarnings("ignore", category=UserWarning)
 lgbm = lgb.LGBMClassifier(nthread = 4, boosting_type = 'dart')
 param_grid = {'learning_rate': [0.08, 0.09, 0.1]}
grid_search = GridSearchCV(lgbm, param_grid, scoring='roc_auc', cv=10)
 grid_result = grid_search.fit(X, y)
 lgbm.set_params(learning_rate = grid_result.best_params_['learning_rate'])
 lgbm_param = lgbm.get_params()
lgbm_train = lgb.Dataset(X_train, y_train,
categorical_feature = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Title', 'CabinCode'])
 cvresult = lgb.cv(lgbm_param, lgbm_train, num_boost_round=1000, metrics=['auc'], early_stopping_rounds=50)
 lgbm.set_params(n_estimators=len(cvresult['auc-mean']))
lgbm.fit(X_train, y_train)
 print(lgbm )
target_cols = ['reactivity', 'deg_Mg_pH10', 'deg_pH10', 'deg_Mg_50C', 'deg_50C']
input_cols = ['sequence', 'structure', 'predicted_loop_type']
error_cols = ['reactivity_error', 'deg_error_Mg_pH10', 'deg_error_Mg_50C', 'deg_error_pH10', 'deg_error_50C']
token_dicts = {
"sequence": {x: i for i, x in enumerate("ACGU")},
"structure": {x: i for i, x in enumerate('().')},
"predicted_loop_type": {x: i for i, x in enumerate("BEHIMSX")}
}<set_options>
accuracy = accuracy_score(y_test, lgbm.predict(X_test))
print("Accuracy: %.2f%%" %(accuracy * 100.0))
def set_seed(seed=42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
SEED = 1234
set_seed(SEED )<categorify>
test = pd.read_csv('.. /input/test.csv')
 test.info()
<train_model><EOS> 
predictions = lgbm.predict(test)
 submission = pd.DataFrame({
"PassengerId": test.index,
"Survived": predictions
})
 submission.to_csv('submission.csv', index=False )