Kaggle competition: Titanic - Machine Learning from Disaster
%%time
group = new_train.groupby(['date_block_num', 'item_id'])['item_cnt_month'].mean().rename('item_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'item_id'], how='left')
 new_train = generate_lag(new_train, [1,2,3,4,5,6,12], 'item_month_mean')
 new_train.drop(['item_month_mean'], axis=1, inplace=True )<merge>
import pandas as pd
import numpy as np
from scipy.stats import mode
from sklearn.svm import SVC
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
%%time
group = new_train.groupby(['date_block_num', 'shop_id'])['item_cnt_month'].mean().rename('shop_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'shop_id'], how='left')
 new_train = generate_lag(new_train, [1,2,3,6,12], 'shop_month_mean')
 new_train.drop(['shop_month_mean'], axis=1, inplace=True )<merge>
titanic=pd.read_csv("/kaggle/input/train.csv")
 df=titanic.copy()
df.head()
%%time
group = new_train.groupby(['date_block_num', 'shop_id', 'item_category_id'])['item_cnt_month'].mean().rename('item_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 new_train = generate_lag(new_train, [1, 2], 'item_category_month_mean')
 new_train.drop(['item_category_month_mean'], axis=1, inplace=True )<merge>
df.isnull().sum()
%%time
group = new_train.groupby(['date_block_num', 'main_category_id'])['item_cnt_month'].mean().rename('main_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'main_category_id'], how='left')
 new_train = generate_lag(new_train, [1], 'main_category_month_mean')
 new_train.drop(['main_category_month_mean'], axis=1, inplace=True )<merge>
test=pd.read_csv("/kaggle/input/test.csv")
 test_df=test.copy()
test_df.isnull().sum()
%%time
group = new_train.groupby(['date_block_num', 'sub_category_id'])['item_cnt_month'].mean().rename('sub_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'sub_category_id'], how='left')
 new_train = generate_lag(new_train, [1], 'sub_category_month_mean')
 new_train.drop(['sub_category_month_mean'], axis=1, inplace=True )<categorify>
df["Initial"]=df["Name"].str.extract('([A-Za-z]+)\.')
 print(df["Initial"].unique())
 df["Initial"].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True )
new_train = downcast_dtypes(new_train )<import_modules>
df.groupby("Initial")["Age"].mean()
import xgboost as xgb<filter>
df.loc[(df.Age.isnull())&(df.Initial=='Master'),"Age"]=5
df.loc[(df.Age.isnull())&(df.Initial=='Miss'),"Age"]=22
df.loc[(df.Age.isnull())&(df.Initial=='Mr'),"Age"]=33
df.loc[(df.Age.isnull())&(df.Initial=='Mrs'),"Age"]=36
df.loc[(df.Age.isnull())&(df.Initial=='Other'),"Age"]=46
new_train = new_train[new_train.date_block_num > 11]<set_options>
print(df.Embarked.mode())
 df.Embarked.fillna("S",inplace=True )
gc.collect()<data_type_conversions>
df["Family_size"]=df["SibSp"]+df["Parch"]
df["Alone"]=0
df.loc[df.Family_size==0,"Alone"]=1
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
df[col].fillna(0, inplace=True)
 return df<correct_missing_values>
df['Age_band']=0
df.loc[df['Age']<=16,'Age_band']=0
df.loc[(df['Age']>16)&(df['Age']<=32),'Age_band']=1
df.loc[(df['Age']>32)&(df['Age']<=48),'Age_band']=2
df.loc[(df['Age']>48)&(df['Age']<=64),'Age_band']=3
df.loc[df['Age']>64,'Age_band']=4
new_train = fill_na(new_train)
<train_model>
df['Fare_cat']=0
df.loc[df['Fare']<=7.91,'Fare_cat']=0
df.loc[(df['Fare']>7.91)&(df['Fare']<=14.454),'Fare_cat']=1
df.loc[(df['Fare']>14.454)&(df['Fare']<=31),'Fare_cat']=2
df.loc[(df['Fare']>31)&(df['Fare']<=513),'Fare_cat']=3
def xgtrain() :
regressor = xgb.XGBRegressor(n_estimators = 5000,
learning_rate = 0.01,
max_depth = 10,
subsample = 0.5,
colsample_bytree = 0.5)
 regressor_ = regressor.fit(new_train[new_train.date_block_num < 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num < 33]['item_cnt_month'].values,
eval_metric = 'rmse',
eval_set = [(new_train[new_train.date_block_num < 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num < 33]['item_cnt_month'].values), 
(new_train[new_train.date_block_num == 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num == 33]['item_cnt_month'].values)
 ],
verbose=True,
early_stopping_rounds = 20 
)
 return regressor_<train_model>
df['Sex'].replace(['male','female'],[0,1],inplace=True)
 df['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
 df['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True )
%%time
regressor_ = xgtrain()<predict_on_test>
df.drop(['Name','Age','Ticket','Fare','Cabin','Fare_Range','PassengerId'],axis=1,inplace=True )
predictions = regressor_.predict(new_train[new_train.date_block_num == 34].drop(['item_cnt_month'], axis = 1 ).values )<load_from_csv>
df.isnull().sum()
submission = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sample_submission.csv' )<feature_engineering>
test_df.loc[(test_df.Age.isnull())&(test_df.Initial=='Master'),"Age"]=5
test_df.loc[(test_df.Age.isnull())&(test_df.Initial=='Miss'),"Age"]=22
test_df.loc[(test_df.Age.isnull())&(test_df.Initial=='Mr'),"Age"]=33
test_df.loc[(test_df.Age.isnull())&(test_df.Initial=='Mrs'),"Age"]=36
test_df.loc[(test_df.Age.isnull())&(test_df.Initial=='Other'),"Age"]=46
test_df.Embarked.fillna("S",inplace=True )
submission['item_cnt_month'] = predictions<save_to_csv>
test_df["Family_size"]=test_df["SibSp"]+test_df["Parch"]
test_df["Alone"]=0
test_df.loc[df.Family_size==0,"Alone"]=1
submission.to_csv('saleslearn.csv', index=False )<set_options>
test_df['Age_band']=0
test_df.loc[test_df['Age']<=16,'Age_band']=0
test_df.loc[(test_df['Age']>16)&(test_df['Age']<=32),'Age_band']=1
test_df.loc[(test_df['Age']>32)&(test_df['Age']<=48),'Age_band']=2
test_df.loc[(test_df['Age']>48)&(test_df['Age']<=64),'Age_band']=3
test_df.loc[test_df['Age']>64,'Age_band']=4
%matplotlib inline<load_from_csv>
test_df[test_df.Fare.isnull() ]
items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 shops = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv')
 categories = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 train = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 test = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv')
 submission = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sample_submission.csv')
 train.head()<count_values>
test_df['Fare_cat']=0
test_df.loc[test_df['Fare']<=7.91,'Fare_cat']=0
test_df.loc[(test_df['Fare']>7.91)&(test_df['Fare']<=14.454),'Fare_cat']=1
test_df.loc[(test_df['Fare']>14.454)&(test_df['Fare']<=31),'Fare_cat']=2
test_df.loc[(test_df['Fare']>31)&(test_df['Fare']<=513),'Fare_cat']=3
train['item_id'].value_counts(ascending = False)[:5]<filter>
test_df['Sex'].replace(['male','female'],[0,1],inplace=True)
 test_df['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
 test_df['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True )
items.loc[items.item_id == 20949]<filter>
test_df.drop(['Name','Age','Ticket','Fare','Cabin','PassengerId'],axis=1,inplace=True )
categories.loc[categories.item_category_id == 71]<sort_values>
test_df.isnull().sum()
train['item_cnt_day'].sort_values(ascending=False)[:5]<filter>
ytrain=df["Survived"]
del df["Survived"]
train[train.item_cnt_day == 2169]<filter>
xtrain=df.values
xtest=test_df.values
items[items.item_id == 11373]<filter>
n_estimators=list(range(100,1100,100))
learn_rate=[0.05,0.1,0.2,0.3,0.25,0.4,0.5,0.6,0.7,0.8,0.9,1]
hyper={'n_estimators':n_estimators,'learning_rate':learn_rate}
gd=GridSearchCV(estimator=AdaBoostClassifier() ,param_grid=hyper,verbose=True)
 gd.fit(xtrain,ytrain)
 print(gd.best_score_)
 print(gd.best_estimator_ )
train = train[train.item_cnt_day < 2000]<sort_values>
clf=AdaBoostClassifier(n_estimators=200,random_state=0,learning_rate=0.05)
 clf.fit(xtrain,ytrain)
 print(clf.score(xtrain,ytrain))
ypred=clf.predict(xtest )
train['item_price'].sort_values(ascending = False)[:5]<filter>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": ypred
})
 submission.to_csv('submission.csv', index=False )