Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_pretrained>
df = pd.read_csv('.. /input/titanic/train.csv' )
learn.load('stage-1');<train_model>
df_cat['Cabin'].value_counts()
learn.fit_one_cycle(5, max_lr=slice(5e-6, lr/10))<save_model>
def splitter(cols):
return re.split('\, |\.',cols)[1]
def remove_digits(col):
for char in col:
if not char.isdigit() :
return col[0]
else:
return char
df_cat['title']= df_cat['Name'].apply(splitter)
 df_cat['deck'] = df_cat['Cabin'].apply(lambda x: str(x ).split(' ')[0] ).apply(remove_digits )
learn.save('stage-2' )<predict_on_test>
print(df_cat['title'].value_counts() ,'
',df_cat['deck'].value_counts() )
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]<save_to_csv>
df_cat['title'] = df_cat['title'].apply(lambda x: 'Other' if x not in ['Mr','Master','Miss','Mrs'] else x)
 print(df_cat['title'].value_counts() )
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-2.csv', index=False )<load_pretrained>
df_cat['deck'] = df_cat['deck'].map({'A':'ABC','B':'ABC','C':'ABC','D':'DE','E':'DE','F':'FG','G':'FG','n':'U','T':'U'} )
learn.load('stage-2');<normalization>
df_num['alone'] =(df['SibSp'] + df['Parch'] ).apply(lambda x: 1 if x==0 else 0 )
data =(src.transform(get_transforms() , size=256)
.databunch(path=Path(".. /")) 
.normalize(imagenet_stats))<categorify>
df_new = pd.concat([df_num,df_cat],axis=1 )
learn.data = data
learn = learn.to_fp16()<define_search_space>
df_new['age_imp'] = df_new[['Age','title']].apply(imp,axis=1 )
lr = 5e-3<train_model>
df_new[df_new['Embarked'].isnull() ]
learn.fit_one_cycle(5, max_lr=slice(lr))<save_model>
df_new.loc[81,'Embarked'] = 'S'
df_new.loc[829,'Embarked'] = 'S'
learn.save('stage-256-1' )<predict_on_test>
dummies = pd.get_dummies(df_new[['Embarked','Sex','title','deck']],drop_first=True )
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]<save_to_csv>
df_final = pd.concat([df_new,dummies],axis=1 ).drop(
 ['PassengerId','Age','SibSp','Parch','Fare','fare_bins_15','Name','Sex','Ticket','Cabin','Embarked','title','age_imp','deck'],axis=1 )
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-3.csv', index=False )<load_pretrained>
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.model_selection import cross_val_score
learn.load('stage-256-1');<train_model>
X = df_final.drop('Survived',axis=1)
 y = df_final['Survived']
learn.fit_one_cycle(5, max_lr=slice(1e-5, 1e-4))<save_model>
lr = LogisticRegression(solver='liblinear',max_iter=200)
 knn = KNeighborsClassifier(n_neighbors=10)
 forest = RandomForestClassifier(n_estimators=200)
 svc = SVC()
learn.save('stage-256-2' )<predict_on_test>
models = [lr, knn, forest, svc]
scores = []
for model in models:
scores.append(cross_val_score(model,X,y,cv=10))
for score in scores:
print("Accuracy: %0.2f(+/- %0.2f)" %(score.mean() , score.std() * 2))
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]<save_to_csv>
vote = VotingClassifier(estimators=[('knn',knn),('forest',forest),('svc',svc),('lr',lr)] )
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-4.csv', index=False )<predict_on_test>
score = cross_val_score(vote,X,y,cv=10 )
preds,y,losses = learn.get_preds(with_loss=True)
 interp = ClassificationInterpretation(learn.to_fp32() , preds, y, losses )<feature_engineering>
print("Accuracy: %0.2f(+/- %0.2f)" %(score.mean() , score.std() * 2))
test_probs, _ = learn.TTA(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]<save_to_csv>
test_df = pd.read_csv('.. /input/titanic/test.csv' )
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-5.csv', index=False )<set_options>
p_id = test_df['PassengerId']
test_df = test_df.drop('PassengerId',axis=1 )
pd.set_option("display.max_columns", 500)
 pd.set_option("display.max_rows", 200)
 plt.rcParams['figure.figsize'] = [15, 6]
sns.set_style("darkgrid" )<install_modules>
test_df['title']= test_df['Name'].apply(splitter)
 test_df['deck'] = test_df['Cabin'].apply(lambda x: str(x ).split(' ')[0] ).apply(remove_digits)
 test_df['title'] = test_df['title'].apply(lambda x: 'Other' if x not in ['Mr','Master','Miss','Mrs'] else x)
 test_df['alone'] =(test_df['SibSp'] + test_df['Parch'] ).apply(lambda x: 1 if x==0 else 0)
 test_df['age_imp'] = test_df[['Age','title']].apply(imp,axis=1)
 test_df['fare_bins'] = pd.qcut(x=test_df['Fare'],q=10,labels=[1,2,3,4,5,6,7,8,9,10],duplicates='drop')
 test_df['age_bins'] = pd.qcut(x=test_df['age_imp'],q=10,labels=[1,2,3,4,5,6,7,8,9],duplicates='drop' )
!pip install pandas-profiling<load_from_csv>
test_df['deck'] = test_df['deck'].map({'A':'ABC','B':'ABC','C':'ABC','D':'DE','E':'DE','F':'FG','G':'FG','n':'U'} )
online_sales = pd.read_csv('/kaggle/input/uisummerschool/Online_sales.csv', sep=',')
 online_sales.head()<define_variables>
test_dummies = pd.get_dummies(test_df[['Embarked','Sex','title','deck']],drop_first=True)
 test_df_final = pd.concat([test_df,test_dummies],axis=1 ).drop(
 ['Age','SibSp','Parch','Fare','Name','Sex','Ticket','Cabin','Embarked','title','age_imp','deck'],axis=1 )
online_sales [['Date', 'Product SKU', 'Quantity', 'Revenue', 'Tax']]<define_variables>
test_df_final[test_df_final['fare_bins'].isnull() ]
test = online_sales [['Date', 'Product SKU', 'Quantity', 'Revenue', 'Tax']] 
<filter>
test_df_final.loc[152,'fare_bins'] = X['fare_bins_10'].mode() [0]
condition1= online_sales['Product SKU'] == 'GGOENEBQ079099'
online_sales[(condition1)]
condition2= online_sales['Quantity'] > 2
online_sales[(condition1)&(condition2)]<feature_engineering>
vote.fit(X,y)
 preds = vote.predict(test_df_final )
<feature_engineering><EOS> 
submission = pd.DataFrame({'PassengerId':p_id,'Survived':preds})
 submission.to_csv('20201126_vote.csv',index=False )