Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
%matplotlib inline
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False )<load_from_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 combine = [train_df,test_df]
print(combine )
items = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/items.csv')
 shops = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/shops.csv')
 cats = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/item_categories.csv')
 train = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sales_train.csv')
 test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv')
<feature_engineering>
train_df[['Pclass','Survived']].groupby(['Pclass'],as_index=False ).mean().sort_values(by='Survived',ascending=False )
train = train[train.item_price<100000]
train = train[train.item_cnt_day<1001]
train["item_cnt_day"]=train["item_cnt_day"].fillna(0)
 train = train[train.item_cnt_day>0]
median = train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].item_price.median()
train.loc[train.item_price<0, 'item_price'] = median<count_missing_values>
train_df[['Sex','Survived']].groupby(['Sex'],as_index=False ).mean().sort_values(by='Survived',ascending=False )
def check_nulls(df_train):
for d in df_train.columns:
if df_train[d].isnull().values.any() :
print("column "+d)
 if df_train[d].dtype.kind in 'bifc':
df_train[d].fillna(0,inplace = True)
 else:
print("column "+d)
 df_train[d].fillna("NULL_VALUE", inplace = True )<categorify>
train_df[['SibSp','Survived']].groupby(['SibSp'],as_index=False ).mean().sort_values(by='Survived',ascending=False )
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops['city_code'] = LabelEncoder().fit_transform(shops['city'])
 cats['split'] = cats['item_category_name'].str.split('-')
 cats['type'] = cats['split'].map(lambda x: x[0].strip())
 cats['type_code'] = LabelEncoder().fit_transform(cats['type'])
 cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id','type_code', 'subtype_code']]<groupby>
train_df[['Parch','Survived']].groupby(['Parch'],as_index=False ).mean().sort_values(by='Survived',ascending=False )
check_count = shops.groupby(["shop_id"])['shop_id'].size()
print(check_count[check_count > 1])
 check_count = cats.groupby(["item_category_id"])['item_category_id'].size()
print(check_count[check_count > 1])
<groupby>
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
check_count = train.groupby(["date","date_block_num","shop_id","item_id"])['shop_id'].size()
print(check_count[check_count > 1])
<filter>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
train.query("date_block_num==16 and shop_id==50 and item_id==3423" )<merge>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_df.head()
test["date_block_num"]=34
test = pd.merge(test, shops, on=['shop_id'], how='left',suffixes=('', '_x'))
test = pd.merge(test, items, on=['item_id'], how='left',suffixes=('', '_y'))
test = pd.merge(test, cats, on=['item_category_id'], how='left')
 test.columns<groupby>
train_df = train_df.drop(['Name', 'PassengerId'], axis=1)
 test_df = test_df.drop(['Name'], axis=1)
 combine = [train_df, test_df]
train_df.shape, test_df.shape
check_count = test.groupby(["shop_id","item_id"])['shop_id'].size()
print(check_count[check_count > 1])
<create_dataframe>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()
train_2 = pd.DataFrame(np.vstack(train_2)) 
<rename_columns>
guess_ages = np.zeros(( 2,3))
guess_ages
train_2.rename(columns={0: "date_block_num", 1: "shop_id", 2: "item_id"} , inplace = True)
 train_2<merge>
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_df.head()
cols=['date_block_num','shop_id','item_id']
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=cols, how='left')
 train_2['item_cnt_month'] =(train_2['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float32))<merge>
train_df['AgeBand'] = pd.cut(train_df['Age'], 5)
 train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
train_2 = pd.merge(train_2, shops, on=['shop_id'], how='left',suffixes=('', '_x'))
train_2 = pd.merge(train_2, items, on=['item_id'], how='left',suffixes=('', '_y'))
train_2 = pd.merge(train_2, cats, on=['item_category_id'], how='left')
<groupby>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train_df.head()
check_count = train_2.groupby(["date_block_num","shop_id","item_id"])['shop_id'].size()
print(check_count[check_count > 1])
<concatenate>
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()
cols = ['shop_id','item_id']
train_2 = pd.concat([train_2, test], ignore_index=True, sort=False, keys=cols )<groupby>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
def sum_df(df,columns,target):
x= []
name=target+"_sum"
data = columns.split(",")
 for col in data:
x.append(col)
 print(x)
 return df.groupby(x)[target].sum().to_frame(name ).reset_index()
def mean_df(df,columns,target):
x= []
name=target+"_mean"
data = columns.split(",")
 for col in data:
x.append(col)
 print(x)
 return df.groupby(x)[target].mean().to_frame(name ).reset_index() 
<merge>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df<merge>
train_df = train_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [train_df, test_df]
train_df.head()
group = train_2.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num'], how='left')
 train_2['date_avg_item_cnt'] = train_2['date_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_avg_item_cnt')
 train_2.drop(['date_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num','item_id'], how='left')
 train_2['date_item_avg_item_cnt'] = train_2['date_item_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1,2,3,6,12], 'date_item_avg_item_cnt')
 train_2.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num','shop_id'], how='left')
 train_2['date_shop_avg_item_cnt'] = train_2['date_shop_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1,2,3,6,12], 'date_shop_avg_item_cnt')
 train_2.drop(['date_shop_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_cat_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num','item_category_id'], how='left')
 train_2['date_cat_avg_item_cnt'] = train_2['date_cat_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_cat_avg_item_cnt')
 train_2.drop(['date_cat_avg_item_cnt'], axis=1, inplace=True)
<merge>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head(10 )
group = train_2.groupby(['date_block_num', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'city_code'], how='left')
 train_2['date_city_avg_item_cnt'] = train_2['date_city_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_city_avg_item_cnt')
 train_2.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'item_id', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'item_id', 'city_code'], how='left')
 train_2['date_item_city_avg_item_cnt'] = train_2['date_item_city_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_item_city_avg_item_cnt')
 train_2.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_type_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'type_code'], how='left')
 train_2['date_type_avg_item_cnt'] = train_2['date_type_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_type_avg_item_cnt')
 train_2.drop(['date_type_avg_item_cnt'], axis=1, inplace=True)
 group = train_2.groupby(['date_block_num', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_subtype_avg_item_cnt' ]
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'subtype_code'], how='left')
 train_2['date_subtype_avg_item_cnt'] = train_2['date_subtype_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_subtype_avg_item_cnt')
 train_2.drop(['date_subtype_avg_item_cnt'], axis=1, inplace=True )<merge>
freq_port = train_df.Embarked.dropna().mode() [0]
freq_port
group = train_2.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_cat_avg_item_cnt']
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 train_2['date_shop_cat_avg_item_cnt'] = train_2['date_shop_cat_avg_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_shop_cat_avg_item_cnt')
 train_2.drop(['date_shop_cat_avg_item_cnt'], axis=1, inplace=True )<merge>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
group = train_2.groupby(['date_block_num', 'shop_id', 'item_id'] ).agg({'item_cnt_month': ['median']})
 group.columns = ['date_shop_item_med_item_cnt']
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'shop_id', 'item_id'], how='left')
 train_2['date_shop_item_med_item_cnt'] = train_2['date_shop_item_med_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1,2,3,6,12], 'date_shop_item_med_item_cnt')
 train_2.drop(['date_shop_item_med_item_cnt'], axis=1, inplace=True)
<merge>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()
group = train_2.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month': ['median']})
 group.columns = ['date_shop_med_item_cnt']
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'shop_id'], how='left')
 train_2['date_shop_med_item_cnt'] = train_2['date_shop_med_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_shop_med_item_cnt')
 train_2.drop(['date_shop_med_item_cnt'], axis=1, inplace=True )<merge>
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True)
 test_df.head()
group = train_2.groupby(['date_block_num', 'shop_id','item_category_id'] ).agg({'item_cnt_month': ['median']})
 group.columns = ['date_shop_cat_med_item_cnt']
group.reset_index(inplace=True)
 train_2 = pd.merge(train_2, group, on=['date_block_num', 'shop_id','item_category_id'], how='left')
 train_2['date_shop_cat_med_item_cnt'] = train_2['date_shop_cat_med_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_shop_cat_med_item_cnt')
 train_2.drop(['date_shop_cat_med_item_cnt'], axis=1, inplace=True )<drop_column>
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)
 train_df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False ).mean().sort_values(by='FareBand', ascending=True )
train_2.columns 
<data_type_conversions>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df = train_df.drop(['FareBand'], axis=1)
 combine = [train_df, test_df]
train_df.head(10 )
group = train_2.groupby(['date_block_num', 'shop_id','item_id'])['item_cnt_month'].quantile (.25)
 group.columns = ['date_shop_item_q25_item_cnt']
dfx=pd.DataFrame()
dfx["date_shop_item_q25_item_cnt"]=group
print(train_2.columns)
 train_2 = pd.merge(train_2, dfx, on=['date_block_num', 'shop_id','item_id'], how='left')
 train_2['date_shop_item_q25_item_cnt'] = train_2['date_shop_item_q25_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_shop_item_q25_item_cnt')
 train_2.drop(['date_shop_item_q25_item_cnt'], axis=1, inplace=True )<data_type_conversions>
X_train = train_df.drop("Survived", axis=1)
 Y_train = train_df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape
group = train_2.groupby(['date_block_num', 'shop_id','item_id'])['item_cnt_month'].quantile (.75)
 group.columns = ['date_shop_item_q75_item_cnt']
dfx=pd.DataFrame()
dfx["date_shop_item_q75_item_cnt"]=group
print(train_2.columns)
 train_2 = pd.merge(train_2, dfx, on=['date_block_num', 'shop_id','item_id'], how='left')
 train_2['date_shop_item_q75_item_cnt'] = train_2['date_shop_item_q75_item_cnt'].astype(np.float16)
 train_2 = lag_feature(train_2, [1], 'date_shop_item_q75_item_cnt')
 train_2.drop(['date_shop_item_q75_item_cnt'], axis=1, inplace=True )<feature_engineering>
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred1 = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 acc_log
train_2['month'] = train_2['date_block_num'] % 12<correct_missing_values>
svc = SVC()
svc.fit(X_train, Y_train)
 Y_pred2 = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, Y_train)* 100, 2)
 acc_svc
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
train_2 = fill_na(train_2 )<drop_column>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred3 = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn
final=train_2[[
'date_block_num',
'shop_id',
'item_id',
'item_cnt_month',
'city_code',
'item_category_id',
'type_code',
'subtype_code',
'item_cnt_month_lag_1', 'item_cnt_month_lag_2', 'item_cnt_month_lag_3', 'item_cnt_month_lag_6', 'date_item_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_2', 'date_item_avg_item_cnt_lag_3', 'date_item_avg_item_cnt_lag_6', 'date_cat_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1', 'date_subtype_avg_item_cnt_lag_1',
'date_shop_cat_avg_item_cnt_lag_1', 'date_shop_item_med_item_cnt_lag_1',
'date_shop_item_med_item_cnt_lag_2', 'date_shop_item_med_item_cnt_lag_3',
'date_shop_item_med_item_cnt_lag_6', 'date_shop_cat_med_item_cnt_lag_1',
'date_shop_item_q25_item_cnt_lag_1', 'date_shop_item_q75_item_cnt_lag_1'
]] 
<train_on_grid>
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred4 = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian
def One_way_ANOVA(df):
lister12 = []
for col in df.columns:
if df[col].dtype.kind not in 'bifc':
model = ols('item_cnt_month ~'+col,data=df ).fit()
table = sm.stats.anova_lm(model, typ=2)
 if table["PR(>F)"][0] < 0.05:
lister12.append(col)
 return lister12<data_type_conversions>
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 Y_pred5 = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, Y_train)* 100, 2)
 acc_perceptron
train_check=train_2[(train_2.date_block_num<=5)]
train_check['item_category_id_char']=train_check['item_category_id'].apply(str)
 train_check['type_code_char']=train_check['type_code'].apply(str)
 train_check['subtype_code_char']=train_check['subtype_code'].apply(str )<compute_test_metric>
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred6 = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc
One_way_ANOVA(train_check[["city","shop_name","item_category_id_char","subtype_code_char","type_code_char","item_cnt_month"]] )<prepare_x_and_y>
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred7 = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 acc_sgd
final.rename(columns = {'item_cnt_month':'item_monthly_sum_x'}, inplace = True)
 final["item_monthly_sum_x"] = final["item_monthly_sum_x"]
X_train = final[final.date_block_num < 33].drop(['item_monthly_sum_x'], axis=1)
 Y_train = final[final.date_block_num < 33]['item_monthly_sum_x']
X_valid = final[final.date_block_num == 33].drop(['item_monthly_sum_x'], axis=1)
 Y_valid = final[final.date_block_num == 33]['item_monthly_sum_x']
X_test = final[final.date_block_num == 34].drop(['item_monthly_sum_x'], axis=1 )<save_to_csv>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_pred8 = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree
Y_test = model.predict(X_test)
 submission = pd.DataFrame({
"ID": test.ID,
"item_cnt_month": Y_test.clip(0, 20)
 })
 submission.to_csv('xgb_submission.csv', index=False)
<load_from_csv>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred9 = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest
PATH = 'competitive-data-science-predict-future-sales'
items = pd.read_csv('.. /input/' + PATH + '/items.csv')
 shops = pd.read_csv('.. /input/' + PATH + '/shops.csv')
 cats = pd.read_csv('.. /input/' + PATH + '/item_categories.csv')
 train = pd.read_csv('.. /input/' + PATH + '/sales_train.csv')
 test = pd.read_csv('.. /input/' + PATH + '/test.csv' ).set_index('ID')
 print('------------- train info ------------'); print(train.info() , '
')
 print('------------- test info ------------'); print(test.info() , '
')
 print('------------- items info ------------'); print(items.info() , '
')
 print('------------- shops info ------------'); print(shops.info() , '
')
 print('------------- categories info ------------'); print(cats.info() , '
' )<count_missing_values>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_svc, acc_knn, acc_log,
acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
 models.sort_values(by='Score', ascending=False )
print(train.isna().sum() , '
')
 print(test.isna().sum() )<feature_engineering>
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred9
})
 submission.to_csv('submission_titanic.csv', index=False )