Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
df_all.drop(['Alley','FireplaceQu','PoolQC','Fence','MiscFeature'],axis=1,inplace=True )<categorify>
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
df_all = df_all.replace({"MSSubClass" : {20 : "SC20", 30 : "SC30", 40 : "SC40", 45 : "SC45",
50 : "SC50", 60 : "SC60", 70 : "SC70", 75 : "SC75",
80 : "SC80", 85 : "SC85", 90 : "SC90", 120 : "SC120",
150 : "SC150", 160 : "SC160", 180 : "SC180", 190 : "SC190"}} )<categorify>
torch.cuda.is_available()
df_all = df_all.replace({"MoSold" : {1 : "January", 2 : "February", 3 : "March", 4 : "April",
5 : "May", 6 : "June", 7 : "July", 8 : "August",
9 : "September", 10 : "October", 11 : "November", 12 : "December"}} )<define_variables>
torch.backends.cudnn.enabled
cat_feats = df_all.dtypes[df_all.dtypes == "object"].index.tolist()
print(str(len(cat_feats)) + ' categoric features' )<define_variables>
dog_indexes = []
cat_indexes = []
count = 0
cat_count = 1
dog_count = 1
num_feats = df_all.dtypes[df_all.dtypes != "object"].index.tolist()
print(str(len(num_feats)) + ' numeric features' )<count_values>
for name in os.listdir('.. /input/dogs-vs-cats-redux-kernels-edition/train/'):
if 'cat' in name:
cat_indexes.append(name.split('.')[1])
 if 'dog' in name:
dog_indexes.append(name.split('.')[1] )
for i in cat_feats_missing:
print(i)
 print(df_all[i].value_counts(dropna=False))
print("" )<data_type_conversions>
train_dir = ".. /input/dogs-vs-cats-redux-kernels-edition/train/"
test_dir = ".. /input/dogs-vs-cats-redux-kernels-edition/test/"
cat_train_dir = ".. /working/dogcats/train/cats/"
cat_valid_dir = ".. /working/dogcats/valid/cats/"
dog_train_dir = ".. /working/dogcats/train/dogs/"
dog_valid_dir = ".. /working/dogcats/valid/dogs/"
dogcats_test = ".. /working/dogcats/test/"
df_all.MSZoning.fillna('RL', inplace = True)
 df_all.drop('Utilities',axis=1,inplace=True)
 df_all.Exterior1st.fillna('VinylSd', inplace = True)
 df_all.Exterior2nd.fillna('VinylSd', inplace = True)
 df_all.MasVnrType.fillna('None', inplace = True)
 df_all.BsmtQual.fillna('No basement',inplace = True)
 df_all.BsmtCond.fillna('No basement',inplace = True)
 df_all.BsmtExposure.fillna('No basement',inplace = True)
 df_all.BsmtFinType1.fillna('No basement',inplace = True)
 df_all.BsmtFinType2.fillna('No basement',inplace = True)
 df_all.Electrical.fillna('SBrkr',inplace = True)
 df_all.KitchenQual.fillna('TA',inplace = True)
 df_all.Functional.fillna('Typ',inplace = True)
 df_all.GarageType.fillna('No garage',inplace = True)
 df_all.GarageFinish.fillna('No garage',inplace = True)
 df_all.GarageQual.fillna('No garage',inplace = True)
 df_all.GarageCond.fillna('No garage',inplace = True)
 df_all.SaleType.fillna('WD',inplace = True)
 df_all.head(5 )<categorify>
PATH = ".. /working/dogcats/"
sz=224
df_all = df_all.replace({"Alley" : {"Grvl" : 1, "Pave" : 2},
"BsmtCond" : {"No basement" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"No basement" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"No basement" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4,
"ALQ" : 5, "GLQ" : 6},
"BsmtFinType2" : {"No basement" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4,
"ALQ" : 5, "GLQ" : 6},
"BsmtQual" : {"No basement" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5,
"Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageCond" : {"No garage" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"No garage" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"LandSlope" : {"Sev" : 1, "Mod" : 2, "Gtl" : 3},
"LotShape" : {"IR3" : 1, "IR2" : 2, "IR1" : 3, "Reg" : 4},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"PoolQC" : {"No" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3, "Ex" : 4},
"Street" : {"Grvl" : 1, "Pave" : 2},
"Utilities" : {"ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3, "AllPub" : 4}}
)<drop_column>
for jpgfile in iglob(os.path.join(test_dir, "*.jpg")) :
shutil.copy(jpgfile, dogcats_test )
df_all.drop('BsmtCond',axis=1,inplace=True)
 df_all.drop('GarageCond',axis=1,inplace=True )<count_values>
gc.collect()
cat_feats = df_all.dtypes[df_all.dtypes == "object"].index.tolist()
print(str(len(cat_feats)) + ' categoric features')
 print("")
 for i in cat_feats:
print(i)
 print(df_all[i].value_counts(dropna=False))
print("" )<categorify>
tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1 )
cat_feats = df_all.dtypes[df_all.dtypes == "object"].index.tolist()
print(str(len(cat_feats)) + ' categoric features')
 df_all = pd.get_dummies(df_all)
 df_all.head(5 )<count_missing_values>
arch=resnet34
df_all[null_columns].isnull().sum()<data_type_conversions>
data = ImageClassifierData.from_paths(PATH, tfms=tfms, test_name='test')
 learn = ConvLearner.pretrained(arch, data, precompute=True )
to_set_to_zero = ['MasVnrArea','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF','TotalBsmtSF','BsmtFullBath','BsmtHalfBath','GarageCars','GarageArea']
df_all[to_set_to_zero] = df_all[to_set_to_zero].fillna(0)
 df_all.drop('GarageYrBlt',axis=1,inplace = True)
 df_all = df_all.fillna(df_all.median() )<drop_column>
learn.fit(1e-2, 1 )
num_feats.remove('GarageYrBlt' )<sort_values>
learn.precompute=False
skewed_feats = df_all[num_feats].apply(lambda x: skew(x))
skewed_feats.sort_values()<filter>
learn.fit(1e-2, 3, cycle_len=1 )
skewed_feats = skewed_feats[skewed_feats > 0.75].index
skewed_feats<define_variables>
gc.collect()
skewed_feats=skewed_feats.tolist()
skewed_feats = [e for e in skewed_feats if e not in ['GrLivArea','LotArea','1stFlrSF']]<normalization>
lr=np.array([1e-4,1e-3,1e-2] )
pt = PowerTransformer('yeo-johnson',standardize=False)
 print(pt.fit(df_all[skewed_feats]))
print("")
 print(pt.lambdas_ )<feature_engineering>
learn.fit(lr, 3, cycle_len=1, cycle_mult=2 )
df_all[skewed_feats] = pt.transform(df_all[skewed_feats])
 df_all[['GrLivArea','LotArea','1stFlrSF']]=df_all[['GrLivArea','LotArea','1stFlrSF']].apply(np.log)
 skewed_feats = skewed_feats + ['GrLivArea','LotArea','1stFlrSF']
df_all[skewed_feats].skew().sort_values()<concatenate>
gc.collect()
pd.concat([X_train,y],axis=1 ).corr().iloc[:,-1].sort_values(ascending=False ).head(10 )<prepare_x_and_y>
log_preds,y = learn.TTA()
probs = np.mean(np.exp(log_preds),0 )
index_to_drop = X_train[(X_train['GrLivArea']>8.3)&(y<12.5)].index.tolist()
X_train = X_train.drop(index_to_drop,axis=0)
 y = y.drop(index_to_drop)
 index_to_drop = X_train[(X_train['GrLivArea']>6.5)&(y<10.7)].index.tolist()
X_train = X_train.drop(index_to_drop,axis=0)
 y = y.drop(index_to_drop )<normalization>
accuracy_np(probs, y )
scaler = RobustScaler()
scaler.fit(X_train)
 scaled_features_train = scaler.transform(X_train)
 scaled_features_test = scaler.transform(X_test)
 X_train = pd.DataFrame(scaled_features_train,columns = X_train.columns)
 X_test = pd.DataFrame(scaled_features_test,index = X_test.index, columns = X_test.columns)
 X_train.head(5 )<import_modules>
preds = np.argmax(probs, axis=1)
 probs = probs[:,1]
from sklearn.metrics import mean_squared_error, make_scorer
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.linear_model import LinearRegression, Ridge, RidgeCV, Lasso, LassoCV, ElasticNetCV<split>
temp = learn.predict(is_test=True )
Xt_train, Xt_test, yt_train, yt_test, = train_test_split(X_train, y, test_size = 0.3 )<compute_train_metric>
pred_test = np.argmax(temp, axis=1 )
def evaluate_model(model):
yt_train_pred = model.predict(Xt_train)
 yt_test_pred = model.predict(Xt_test)
 rmse_train = np.sqrt(mean_squared_error(yt_train,yt_train_pred))
rmse_test = np.sqrt(mean_squared_error(yt_test,yt_test_pred))
print("RMSE on Training set :", rmse_train)
 print("RMSE on Test set :", rmse_test)
 plt.figure(figsize=[20,8])
 plt.subplot(1,2,1)
 plt.scatter(yt_train_pred, yt_train_pred - yt_train, c = "blue", marker = "s", label = "Training data")
 plt.scatter(yt_test_pred, yt_test_pred - yt_test, c = "lightgreen", marker = "s", label = "Validation data")
 plt.title(model)
 plt.xlabel("Predicted values")
 plt.ylabel("Residuals")
 plt.legend(loc = "upper left")
 plt.hlines(y = 0, xmin = 10.5, xmax = 13.5, color = "red")
 plt.subplot(1,2,2)
 plt.scatter(yt_train_pred, yt_train, c = "blue", marker = "s", label = "Training data")
 plt.scatter(yt_test_pred, yt_test, c = "lightgreen", marker = "s", label = "Validation data")
 plt.title(model)
 plt.xlabel("Predicted values")
 plt.ylabel("Real values")
 plt.legend(loc = "upper left")
 plt.plot([10.5, 13.5], [10.5, 13.5], c = "red" )<compute_train_metric>
probs = np.exp(temp[:,1] )
def cross_validation_evaluation(model):
scores = -cross_val_score(model, Xt_train, yt_train, cv=10,scoring="neg_mean_squared_error")
 return scores.mean()<train_model>
submission = pd.DataFrame({'id':os.listdir(f'{PATH}test'), 'label':probs} )
linear_regression_model = LinearRegression()
linear_regression_model.fit(Xt_train, yt_train )<prepare_output>
submission['id'] = submission['id'].map(lambda x: x.split('.')[0] )
evaluate_model(linear_regression_model )<train_model>
submission['id'] = submission['id'].astype(int )
ridge_regression_model = Ridge(alpha=1.0)
 ridge_regression_model.fit(Xt_train, yt_train )<prepare_output>
submission = submission.sort_values('id' )
<train_model><EOS> 
submission.to_csv('.. /working/output.csv', index=False )