Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
warnings.filterwarnings('ignore')
submission.to_csv("submission.csv", index=False)
 submission<load_from_csv>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 Id = test.PassengerId
submission = pd.read_csv('/kaggle/input/local-submission-files-m5/submission_46.csv' )<load_from_csv>
dataset = pd.concat([train, test], sort=False, ignore_index=True)
 dataset.isnull().mean().sort_values(ascending=False )
sales = pd.read_csv(f'/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv')
 ids = sorted(list(set(sales['id'])))
 d_cols = [f'd_{x}' for x in range(1258,1914)]<compute_test_metric>
dataset['Fare'].fillna(dataset['Fare'].median() , inplace=True)
 dataset['Embarked'] = dataset['Embarked'].fillna('S' )
def calc_coef(x,y, alpha):
y = np.mean(y, axis = 0)
 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
 line = slope*x+intercept
return 1 +(slope*y.shape[0]/alpha )<categorify>
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand = False)
 dataset['Title'].unique().tolist()
stores = ['CA_1', 'CA_2', 'CA_3', 'CA_4', 'TX_1', 'TX_2', 'TX_3', 'WI_1', 'WI_2', 'WI_3']
for store in stores:
submission.loc[submission['id'].str.contains(store), [f'F{x}' for x in range(1,29)]] *= coefs[store]<save_to_csv>
dataset['Title'].value_counts(normalize=True)*100
submission.to_csv('submission.csv', index = False )<import_modules>
dataset['Title'] = dataset['Title'].replace(['Capt', 'Col', 'Major', 'Dr', 'Rev'], 'Officer')
 dataset['Title'] = dataset['Title'].replace(['Jonkheer', 'Master'], 'Master')
 dataset['Title'] = dataset['Title'].replace(['Don', 'Sir', 'the Countess', 'Lady', 'Dona'], 'Royalty')
 dataset['Title'] = dataset['Title'].replace(['Mme', 'Ms', 'Mrs'], 'Mrs')
 dataset['Title'] = dataset['Title'].replace(['Mlle', 'Miss'], 'Miss')
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Title'].value_counts()
from datetime import datetime, timedelta
import gc
import numpy as np, pandas as pd
import lightgbm as lgb<define_variables>
dataset['Age'].fillna(dataset['Age'].median() , inplace=True )
CAL_DTYPES={"event_name_1": "category", "event_name_2": "category", "event_type_1": "category",
"event_type_2": "category", "weekday": "category", 'wm_yr_wk': 'int16', "wday": "int16",
"month": "int16", "year": "int16", "snap_CA": "float32", 'snap_TX': 'float32', 'snap_WI': 'float32' }
PRICE_DTYPES = {"store_id": "category", "item_id": "category", "wm_yr_wk": "int16","sell_price":"float32" }<set_options>
dataset['FamSize'] = dataset['SibSp'] + dataset['Parch'] + 1
pd.options.display.max_columns = 50<define_variables>
def family_label(s):
if(s >= 2)&(s <= 4):
return 2
elif(( s > 4)&(s <= 7)) |(s == 1):
return 1
elif(s > 7):
return 0
dataset['FamLabel']=dataset['FamSize'].apply(family_label)
 dataset.head()
h = 28
max_lags = 70
tr_last = 1913
fday = datetime(2016,4, 25)
 fday<load_from_csv>
dataset['Cabin'] = dataset['Cabin'].fillna('Unknown')
 dataset['Deck']=dataset['Cabin'].str.get(0 )
def create_dt(is_train = True, nrows = None, first_day = 1200):
prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES)
 for col, col_dtype in PRICE_DTYPES.items() :
if col_dtype == "category":
prices[col] = prices[col].cat.codes.astype("int16")
 prices[col] -= prices[col].min()
cal = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 cal["date"] = pd.to_datetime(cal["date"])
 for col, col_dtype in CAL_DTYPES.items() :
if col_dtype == "category":
cal[col] = cal[col].cat.codes.astype("int16")
 cal[col] -= cal[col].min()
start_day = max(1 if is_train else tr_last-max_lags, first_day)
 numcols = [f"d_{day}" for day in range(start_day,tr_last+1)]
catcols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numcol:"float32" for numcol in numcols}
dtype.update({col: "category" for col in catcols if col != "id"})
 dt = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_validation.csv",
nrows = nrows, usecols = catcols + numcols, dtype = dtype)
 for col in catcols:
if col != "id":
dt[col] = dt[col].cat.codes.astype("int16")
 dt[col] -= dt[col].min()
if not is_train:
for day in range(tr_last+1, tr_last+ 28 +1):
dt[f"d_{day}"] = np.nan
dt = pd.melt(dt,
id_vars = catcols,
value_vars = [col for col in dt.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 dt = dt.merge(cal, on= "d", copy = False)
 dt = dt.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 return dt<categorify>
dataset.drop(['Name', 'Ticket', 'SibSp', 'Parch', 'FamSize', 'Cabin'], axis=1, inplace=True )
def create_fea(dt):
lags = [7, 28]
lag_cols = [f"lag_{lag}" for lag in lags ]
for lag, lag_col in zip(lags, lag_cols):
dt[lag_col] = dt[["id","sales"]].groupby("id")["sales"].shift(lag)
 wins = [7, 28]
for win in wins :
for lag,lag_col in zip(lags, lag_cols):
dt[f"rmean_{lag}_{win}"] = dt[["id", lag_col]].groupby("id")[lag_col].transform(lambda x : x.rolling(win ).mean())
 date_features = {
"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day",
}
for date_feat_name, date_feat_func in date_features.items() :
if date_feat_name in dt.columns:
dt[date_feat_name] = dt[date_feat_name].astype("int16")
 else:
dt[date_feat_name] = getattr(dt["date"].dt, date_feat_func ).astype("int16" )<define_variables>
label = LabelEncoder()
for col in ['Sex', 'Embarked', 'Deck', 'Title']:
dataset[col] = label.fit_transform(dataset[col] )
FIRST_DAY = 800 
<correct_missing_values>
train = dataset[:len(train)]
test = dataset[len(train):]
test.drop(labels=['Survived'], axis=1, inplace=True )
df.dropna(inplace = True)
 df.shape<prepare_x_and_y>
train['Survived'] = train['Survived'].astype(int )
cat_feats = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + ["event_name_1", "event_name_2", "event_type_1", "event_type_2"]
useless_cols = ["id", "date", "sales","d", "wm_yr_wk", "weekday"]
train_cols = df.columns[~df.columns.isin(useless_cols)]
X_train = df[train_cols]
y_train = df["sales"]<create_dataframe>
y = train.Survived
X = train.drop('Survived', axis=1)
train_data = lgb.Dataset(X_train, label = y_train, categorical_feature=cat_feats, free_raw_data=False)
 fake_valid_inds = np.random.choice(len(X_train), 1000000)
 fake_valid_data = lgb.Dataset(X_train.iloc[fake_valid_inds], label = y_train.iloc[fake_valid_inds],categorical_feature=cat_feats,
free_raw_data=False )<init_hyperparams>
print("Logistic Regression:", cross_val_score(LogisticRegression() , X, y ).mean())
 print("SVC:", cross_val_score(SVC() , X, y ).mean())
 print("Random Forest:", cross_val_score(RandomForestClassifier() , X, y ).mean())
 print("GaussianNB:", cross_val_score(GaussianNB() , X, y ).mean())
 print("Decision Tree:", cross_val_score(DecisionTreeClassifier() , X, y ).mean() )
params = {
"objective" : "poisson",
"metric" :"rmse",
"force_row_wise" : True,
"learning_rate" : 0.075,
"sub_row" : 0.75,
"bagging_freq" : 1,
"lambda_l2" : 0.1,
"metric": ["rmse"],
'verbosity': 1,
'num_iterations' : 2500,
}<train_model>
select = SelectKBest(k = 'all')
 final_model = RandomForestClassifier(random_state = 10, warm_start = True,
n_estimators = 26,
max_depth = 6,
max_features = 'sqrt')
 pipeline = make_pipeline(select, final_model)
 cv_result = cross_validate(pipeline, X, y, cv= 10)
 print("CV Test Score : Mean - %.7g | Std - %.7g " %(np.mean(cv_result['test_score']), \
np.std(cv_result['test_score'])) )
%%time
m_lgb = lgb.train(params, train_data, valid_sets = [fake_valid_data], verbose_eval=100 )<save_model>
pipeline.fit(X, y)
 final_predictions = pipeline.predict(test )
<count_unique_values><EOS> 
output = pd.DataFrame({'PassengerId': Id, 'Survived': final_predictions})
 output.to_csv('submission.csv', index=False )