Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<save_to_csv>
warnings.filterwarnings('ignore')
 %matplotlib inline
def choosen_KNN(neighbors,p):
KNN = KNeighborsClassifier(n_neighbors=neighbors,p=p)
 model = KNN.fit(X_train,np.ravel(y_train))
results_KNN = model.predict(test_data[features])
 print("Precisão ",round(( model.score(X_train,y_train)) *100,2),"%
")
 output_KNN = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': results_KNN})
 output_KNN.to_csv('my_submission_KNN.csv', index=False)
 print(pd.crosstab(results_KNN,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))<import_modules>
with zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r')as zip_ref:
zip_ref.extractall('/kaggle/working/' )
from sklearn.tree import DecisionTreeClassifier<find_best_params>
with zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r')as zip_ref:
zip_ref.extractall('/kaggle/working/' )
def DTC(depth):
depth = range(1,depth+1,1)
 for i in depth:
DTC = DecisionTreeClassifier(max_depth=i)
 model = DTC.fit(X_train, y_train)
 predictions_DTC = model.predict(X_test)
 print("Para uma profundidade de:", i,"a precisão do modelo foi de ",round(( model.score(X_train,y_train)) *100,2),"%" )<save_to_csv>
train_fnames = get_image_files('/kaggle/working/train')
 len(train_fnames )
def choosen_DTC(depth):
DTC = DecisionTreeClassifier(max_depth=depth)
 model = DTC.fit(X_train, y_train)
 predictions_DTC = model.predict(X_test)
 print(pd.crosstab(predictions_DTC, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_DTC))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC})
 output_DTC.to_csv('my_submission_DTC.csv', index=False )<import_modules>
labels = [('cat' if 'cat.' in str(fname)else 'dog')for fname in train_fnames]
labels[:5]
from sklearn.model_selection import cross_val_predict,cross_val_score,cross_validate
from sklearn import metrics<compute_train_metric>
np.random.seed(123)
 data = ImageDataBunch.from_lists(
 path='/kaggle/working/train',
fnames=train_fnames,
labels=labels,
valid_pct=0.2,
ds_tfms=get_transforms(flip_vert=False),
size=224,
bs=32
).normalize(imagenet_stats )
def DTC_CROSS(depth, folds):
model_DTC = DecisionTreeClassifier(max_depth=depth)
 model_DTC.fit(X_train, y_train)
 cross_validate(model_DTC,X_train,y_train,cv=folds)
 predictions_DTC_cross = model_DTC.predict(X_test)
 accuracy = metrics.accuracy_score(y_test,predictions_DTC_cross)
 print("
")
 print(pd.crosstab(predictions_DTC_cross, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_DTC_cross))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_cross})
 output_DTC.to_csv('my_submission_DTC_cross.csv', index=False)
 return print("accuracy = ",accuracy*100,"%" )<import_modules>
data.show_batch(rows=3, figsize=(12, 12))
from sklearn import svm<define_variables>
torch.cuda.is_available()
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']<prepare_x_and_y>
torch.backends.cudnn.enabled
X_train = train_data[features]
X_test = test_data[features]
X_train.head()<prepare_x_and_y>
learner = cnn_learner(data, models.resnet50, metrics=error_rate ).to_fp16()
y_train = train_data[targets]
y_test = submission['Survived']
y_train.head()<find_best_model_class>
learner.lr_find(start_lr=1e-6 )
def choosen_SVM(C, gamma):
model_SVM = svm.SVC(C=C,gamma=gamma,random_state=0)
 model_SVM.fit(X_train,y_train)
 predictions_SVM = model_SVM.predict(X_test)
 print("Para C = ",C,"e gamma = ", gamma," Precisão = ",round(( model_SVM.score(X_train,y_train)) *100,2),"% ")
 print(pd.crosstab(predictions_SVM,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_SVM))
output_SVM = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM})
 output_SVM.to_csv('my_submission_SVM.csv', index=False )<count_values>
max_lr_choice = 5e-4
train_data['Survived'].value_counts()<import_modules>
learner.fit_one_cycle(4, max_lr=max_lr_choice )
from imblearn.under_sampling import NearMiss<prepare_x_and_y>
learner.save('imgsize224-stage1' )
X = train_data[features]
X_test = test_data[features]
y = train_data[targets]
y_test = submission[targets]<find_best_params>
interp = ClassificationInterpretation.from_learner(learner )
nrm = NearMiss()<train_model>
accuracy =(interp.confusion_matrix() [0, 0] + interp.confusion_matrix() [1, 1])/ len(data.valid_ds)
 accuracy
X, y = nrm.fit_sample(X,y )<compute_train_metric>
data_no_split = ImageDataBunch.from_lists(
 path='/kaggle/working/train',
fnames=train_fnames,
labels=labels,
valid_pct=0,
ds_tfms=get_transforms(flip_vert=False),
size=224,
bs=32
).normalize(imagenet_stats )
lr_us = LogisticRegression()
model = lr_us.fit(X,y)
 predictions_lr_us = model.predict(X_test)
 print(pd.crosstab(predictions_lr_us,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_us))<save_to_csv>
%%capture
learner_no_split = cnn_learner(data_no_split, models.resnet50 ).to_fp16()
learner_no_split.load('imgsize224-stage1' )
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_lr_us})
 output.to_csv("My_submission_Logistic_US.csv", index=False )<import_modules>
dataset, file_indices = DatasetFormatter.from_toplosses(learner_no_split )
from imblearn.over_sampling import SMOTE<define_variables>
ImageCleaner(dataset, file_indices, Path('/kaggle/working/train'))
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']<define_search_space>
cleaned = pd.read_csv('/kaggle/working/train/cleaned.csv')
 cleaned.head()
sampling = np.linspace(0.65,1,8)
 sampling<train_on_grid>
def create_download_link(df, title="Download CSV file", filename="dogs_vs_cats_cleaned.csv"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload, title=title, filename=filename)
 return HTML(html )
def Over_samp(train, test, submisson):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
sampling = np.linspace(0.65,1,16)
 X = train[features]
X_test = test[features]
y = train[targets]
y_test = submission[targets]
for i in sampling:
print("For sampling strategy = ",i*100,"%
")
 smt = SMOTE(sampling_strategy = i)
 X, y = smt.fit_sample(X,y)
 lr_os = LogisticRegression()
model = lr_os.fit(X,y)
 predictions_lr_os = model.predict(X_test)
 print(pd.crosstab(predictions_lr_os,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_os))
print("
" )<compute_test_metric>
create_download_link(cleaned)
Over_samp(train_data,test_data, submission )<prepare_x_and_y>
cleaned = pd.read_csv('https://storage.googleapis.com/cleaned-data/dogs_vs_cats_cleaned.csv')
 cleaned.head()
X = train_data[features]
X_test = test_data[features]
y = train_data[targets]
y_test = submission[targets]<save_to_csv>
np.random.seed(123)
 data_cleaned = ImageDataBunch.from_df(
 path='/kaggle/working/train',
df=cleaned,
valid_pct=0.2,
ds_tfms=get_transforms(flip_vert=False),
size=224,
bs=32
).normalize(imagenet_stats )
smt = SMOTE(sampling_strategy = 0.65)
 X, y = smt.fit_sample(X,y)
 lr_os = LogisticRegression()
model = lr_os.fit(X,y)
 predictions_lr_os = model.predict(X_test)
 print(pd.crosstab(predictions_lr_os,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_os))
output = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Survived': predictions_lr_os})
 output.to_csv("My_submission_Logistic_OS.csv", index=False )<train_model>
learner = cnn_learner(data_cleaned, models.resnet50, metrics=error_rate ).to_fp16()
def DTC_US(depth):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
X,y = nrm.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 DTC = DecisionTreeClassifier(max_depth=depth)
 model = DTC.fit(X, y)
 predictions_DTC_US = model.predict(X_test)
 print(pd.crosstab(predictions_DTC_US, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_DTC_US))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_US})
 output_DTC.to_csv('my_submission_DTC_US.csv', index=False )<find_best_model_class>
learner.lr_find(start_lr=1e-6 )
def DTC_OS(depth):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
smt = SMOTE(sampling_strategy = 0.70)
 X,y = smt.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 DTC = DecisionTreeClassifier(max_depth=depth, random_state=42)
 model = DTC.fit(X, y)
 predictions_DTC_OS = model.predict(X_test)
 print(pd.crosstab(predictions_DTC_OS, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_OS})
 output_DTC.to_csv('my_submission_DTC_OS.csv', index=False )<find_best_model_class>
max_lr_choice = 5e-4
def SVM_US(C, gamma):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
X,y = nrm.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 sv = svm.SVC(C=C, gamma=gamma)
 model = sv.fit(X, y)
 predictions_SVM_US = model.predict(X_test)
 print(pd.crosstab(predictions_SVM_US, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_SVM_US))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM_US})
 output_DTC.to_csv('my_submission_SVM_US.csv', index=False )<set_options>
learner.fit_one_cycle(4, max_lr=max_lr_choice )
SVM_US(5,0.01 )<find_best_model_class>
learner.save('imgsize224-stage2' )
def SVM_OS(C,gamma):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
smt = SMOTE(sampling_strategy = 0.75)
 X,y = smt.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 sv = svm.SVC(C=C, gamma=gamma)
 model = sv.fit(X, y)
 predictions_SVM_OS = model.predict(X_test)
 print(pd.crosstab(predictions_SVM_OS, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_SVM_OS))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM_OS})
 output_DTC.to_csv('my_submission_SVM_OS.csv', index=False )<set_options>
np.random.seed(123)
 data = ImageDataBunch.from_df(
 path='/kaggle/working/train',
df=cleaned,
valid_pct=0.2,
ds_tfms=get_transforms(flip_vert=False),
size=300,
bs=32
).normalize(imagenet_stats )
SVM_OS(100,0.01 )<load_from_csv>
%%capture
learner = cnn_learner(data, models.resnet50, metrics=error_rate ).to_fp16()
learner.load('imgsize224-stage2' )
df = pd.read_csv('.. /input/train.csv')
 df.head()<drop_column>
learner.lr_find(start_lr=1e-6, end_lr=1e-2, stop_div=False )
d = df[['Survived','Pclass','Sex', 'Age', 'SibSp',
'Parch','Embarked']]
d = d.dropna()<import_modules>
max_lr_choice = 5e-5
from sklearn import preprocessing<import_modules>
learner.fit_one_cycle(4, max_lr=max_lr_choice )
import seaborn as sns
import matplotlib.pyplot as plt<feature_engineering>
learner.save('imgsize300-stage1' )
df['title'] = df.title.replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['title'] = df['title'].replace('Mlle', 'Miss')
 df['title'] = df['title'].replace('Ms', 'Miss')
 df['title'] = df['title'].replace('Mme', 'Mrs')
 df[['title','Survived']].groupby('title' ).mean()<categorify>
learner.unfreeze()
learner.lr_find(start_lr=1e-6, end_lr=1e-2, stop_div=False )
df.title = df.title.fillna(0)
 df['title'] = df.title.map({'Rare':0, 'Master':1, 'Miss':2, 'Mr':3, 'Mrs':4})
 df.Sex = df.Sex.map({'female':0, 'male':1} ).astype(int )<data_type_conversions>
learner.fit_one_cycle(4, max_lr=slice(1e-5, 1e-4))
df.Age = df.Age.fillna(df.Age.mean())
 df.Embarked = df.Embarked.fillna('S' )<data_type_conversions>
learner.save('imgsize300-stage2' )
df = df.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Cabin'])
 df['Embarked'] = df.Embarked.map({'S':0,'C':1,'Q':2} ).astype(int)
 df.Age = round(df.Age ).astype(int)
 df.head() 
<feature_engineering>
learner = learner.to_fp32()
learner.export()
df.loc[(round(df['Age'])<=16),'Age'] = 0
df.loc[(round(df['Age'])>16)&(round(df['Age'])<=32),'Age'] = 1
df.loc[(round(df['Age'])>32)&(round(df['Age'])<=48),'Age'] = 2
df.loc[(round(df['Age'])>48)&(round(df['Age'])<=64),'Age'] = 3
df.loc[(round(df['Age'])>64)&(round(df['Age'])<=80),'Age'] = 4<groupby>
test_fnames = get_image_files('/kaggle/working/test')
 len(test_fnames )
df[['Age','Survived']].groupby('Age',as_index = False ).mean()<feature_engineering>
ids = [int(re.findall(r'\d+', str(fname)) [0])for fname in test_fnames]
ids[:5]
df.loc[(round(df['Fare'])<=7.9),'Fare'] = 0
df.loc[(round(df['Fare'])>7.9)&(round(df['Fare'])<=14.45),'Fare'] = 1
df.loc[(round(df['Fare'])>14.45)&(round(df['Fare'])<=31.0),'Fare'] = 2
df.loc[(round(df['Fare'])>31.0),'Fare'] = 3<groupby>
learner.path
df[['Fare','Survived']].groupby('Fare' ).mean()<groupby>
learner = load_learner(path=learner.path, test=test_fnames )
df['Familysize'] = df['SibSp'] + df['Parch'] + 1
df[['Familysize','Survived']].groupby('Familysize' ).mean()<feature_engineering>
preds, labels = learner.get_preds(ds_type=DatasetType.Test)
 preds[:5]
df['Familysize'] = df['SibSp'] + df['Parch'] + 1
df.loc[df['Familysize'] == 1,'Familysize'] = 0
df.loc[df.Familysize >1,'Familysize'] = 1<groupby>
d = {'id': ids, 'label': preds[:, 1]}
submission = pd.DataFrame(data=d)
 submission = submission.sort_values(by='id')
 submission.head()
df[['Familysize','Survived']].groupby('Familysize' ).mean()<drop_column>
submission.to_csv('submission.csv', index=False )
<drop_column><EOS> 
submission.to_csv('submission.csv', index=False )