Kaggle competition: Titanic - Machine Learning from Disaster
log_reg =LogisticRegression(multi_class='multinomial',
random_state=my_randome_state)
 gsearch = GridSearchCV(estimator=log_reg,
param_grid = parameters,
scoring="neg_log_loss",
n_jobs=4,cv=5, verbose=7 )<train_model>
%matplotlib inline
gsearch.fit(X_train, y_train )<find_best_params>
train_df=pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df=pd.read_csv('/kaggle/input/titanic/test.csv')
 train_df.head()
best_max_iter = gsearch.best_params_.get('max_iter')
 best_tol = gsearch.best_params_.get('tol')
 best_C = gsearch.best_params_.get('C')
 best_solver = gsearch.best_params_.get('solver')
 best_penalty = gsearch.best_params_.get('penalty')
 best_max_iter,best_tol,best_C,best_penalty,best_solver<choose_model_class>
print(train_df.isnull().sum())
 print('\t\t\t')
 print(test_df.isnull().sum() )
final_model = LogisticRegression(max_iter=best_max_iter,
random_state=my_randome_state,
tol=best_tol,
C=best_C,
solver=best_solver,
penalty=best_penalty )<train_model>
train_df.isnull().sum()
final_model.fit(X_train, y_train )<load_from_csv>
train_gp=train_df.groupby(['Sex','Pclass'])['Age'].mean()
print(train_gp)
 test_gp=test_df.groupby(['Sex','Pclass'])['Age'].mean()
print(test_gp)
X_test=pd.read_csv(".. /input/leaf-classification/test.csv.zip" )<drop_column>
def fillAgeNa(df):
for i in range(len(df)) :
if pd.isnull(df.loc[i, "Age"]):
if(df.loc[i,'Sex']=='female')and(df.loc[i,'Pclass']==1):
df.loc[i,'Age']=37
elif(df.loc[i,'Sex']=='female')and(df.loc[i,'Pclass']==2):
df.loc[i,'Age']=26
elif(df.loc[i,'Sex']=='female')and(df.loc[i,'Pclass']==3):
df.loc[i,'Age']=22
elif(df.loc[i,'Sex']=='male')and(df.loc[i,'Pclass']==1):
df.loc[i,'Age']=40
elif(df.loc[i,'Sex']=='male')and(df.loc[i,'Pclass']==2):
df.loc[i,'Age']=30
elif(df.loc[i,'Sex']=='male')and(df.loc[i,'Pclass']==3):
df.loc[i,'Age']=25
return df
test_ids = X_test.id
X_test = X_test.drop(['id'], axis =1 )<normalization>
ndf=train_df.copy()
train_df=fillAgeNa(ndf)
 train_df.isnull().sum()
ndf=test_df.copy()
test_df=fillAgeNa(ndf)
 train_df['Embarked'].fillna('S',inplace=True )
X_test = scaler.transform(X_test )<predict_on_test>
train_df.isnull().sum()
final_predictions = final_model.predict_proba(X_test )<prepare_output>
test_df['Fare'].fillna(test_df['Fare'].mean() ,inplace=True)
 test_df.isnull().sum()
test_df.shape
submission = pd.DataFrame(final_predictions, columns=classes)
 submission.insert(0, 'id', test_ids)
 submission<save_to_csv>
train_data=train_df.drop('Cabin',axis=1)
 test_data=test_df.drop('Cabin',axis=1)
 test_data.shape
submission.to_csv('submission_log_reg.csv', index = False)
 print("done" )<define_variables>
train_data.drop(['PassengerId','Name','Ticket'],inplace=True,axis=1)
 test_data.drop(['PassengerId','Name','Ticket'],inplace=True,axis=1 )
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" }<load_from_csv>
encd1=pd.get_dummies(train_data[['Sex','Embarked']],drop_first=True)
 encd2=pd.get_dummies(test_data[['Sex','Embarked']],drop_first=True )
sell_prices = pd.read_csv("/kaggle/input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES)
 calendar_df = pd.read_csv("/kaggle/input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 sales_train_validation = pd.read_csv("/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv")
 subm = pd.read_csv("/kaggle/input/m5-forecasting-accuracy/sample_submission.csv" )<import_modules>
train_data=pd.concat([train_data,encd1],axis=1)
 test_data=pd.concat([test_data,encd2],axis=1)
from datetime import datetime, timedelta
import gc<define_search_space>
train_data['FamilySize']=train_data['SibSp']+train_data['Parch']+1
test_data['FamilySize']=test_data['SibSp']+test_data['Parch']+1
h = 28
max_lags = 57
tr_last = 1913
fday = datetime(2016,4, 25 )<load_from_csv>
train_data['Isalone']=train_data['FamilySize'].apply(lambda x : 1 if x>1 else 0)
 test_data['Isalone']=test_data['FamilySize'].apply(lambda x : 1 if x>1 else 0 )
def create_dt(is_train = True, nrows = None, first_day = 1200):
prices = pd.read_csv("/kaggle/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("/kaggle/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>
from sklearn.preprocessing import LabelEncoder
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>
train_data.drop(['Sex','Embarked','FamilySize'],axis=1,inplace=True)
 test_data.drop(['Sex','Embarked','FamilySize'],axis=1,inplace=True)
FIRST_DAY = 350<correct_missing_values>
scaler=MinMaxScaler()
scaler.fit(train_data[['Fare']])
 train_data['Fare']=scaler.transform(train_data[['Fare']])
 train_data['Age']=scaler.fit_transform(train_data[['Age']])
 test_data['Fare']=scaler.fit_transform(test_data[['Fare']])
 test_data['Age']=scaler.fit_transform(test_data[['Age']] )
df.dropna(inplace = True)
 df.shape<prepare_x_and_y>
train_data['SibSp']=train_data['SibSp'].apply(lambda x: 1 if x>0 else 0)
 test_data['SibSp']=test_data['SibSp'].apply(lambda x: 1 if x>0 else 0 )
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"]<import_modules>
train_data['nw']=train_data['Age']*train_data['Pclass']
test_data['nw']=test_data['Age']*test_data['Pclass']
import lightgbm as lgb<create_dataframe>
X_train=train_data.drop('Survived',axis=1)
 y_train=train_data[['Survived']]
print('shape of x train and y train')
 print(X_train.shape,y_train.shape)
 X_test=test_data
print('shape of x test')
 print(X_test.shape)
%%time
np.random.seed(777)
 fake_valid_inds = np.random.choice(X_train.index.values, 2_000_000, replace = False)
 train_inds = np.setdiff1d(X_train.index.values, fake_valid_inds)
 train_data = lgb.Dataset(X_train.loc[train_inds] , label = y_train.loc[train_inds],
categorical_feature=cat_feats, free_raw_data=False)
 fake_valid_data = lgb.Dataset(X_train.loc[fake_valid_inds], label = y_train.loc[fake_valid_inds],
categorical_feature=cat_feats,
free_raw_data=False )<prepare_x_and_y>
model1=RandomForestClassifier()
model2=XGBClassifier()
model3=LogisticRegression()
model4=SVC(kernel='poly',gamma=1,C=0.1)
 model5=KNeighborsClassifier(n_neighbors=23,leaf_size=23,p=1)
 model=[model1,model2,model3,model4,model5]
del df, X_train, y_train, fake_valid_inds,train_inds ; gc.collect()<init_hyperparams>
c=0
for m in model:
c+=1
m.fit(X_train,y_train)
 accur=round(m.score(X_train,y_train)*100,2)
 print('Model',c)
 print('accuracy =',accur )
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' : 1200,
'num_leaves': 128,
"min_data_in_leaf": 100,
}<train_model>
model=RandomForestClassifier(n_estimators= 2000,
min_samples_split= 5,
min_samples_leaf= 2,
max_features= 'sqrt',
max_depth= None)
 model.fit(X_train,y_train)
 y_pred=model.predict(X_test)
 y_pred=pd.Series(y_pred)
 y_pred=y_pred.apply(lambda x: 1 if x else 0)
 accur=round(model.score(X_train,y_train)*100,2)
 accur
%%time
m_lgb = lgb.train(params, train_data, valid_sets = [fake_valid_data], verbose_eval=20 )<save_model>
dataframe=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 dataframe.head()
m_lgb.save_model("save_model.lgb" )<set_options>
subm=pd.concat([test_df['PassengerId'],y_pred],axis=1)
 subm.rename(columns={'PassengerId':'PassengerId',0:'Survived'},inplace=True )
%matplotlib inline
plt.style.use('seaborn-darkgrid' )<load_from_csv>
subm['Survived'].value_counts()
df_cal = pd.read_csv('.. /input/m5-forecasting-accuracy/calendar.csv')
 df_eval = pd.read_csv('.. /input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 df_price = pd.read_csv('.. /input/m5-forecasting-accuracy/sell_prices.csv')
 df_sample_output = pd.read_csv('.. /input/m5-forecasting-accuracy/sample_submission.csv' )<define_variables>
subm.to_csv('submission.csv',index=False )