Kaggle competition: Aerial Cactus Identification
logy = np.log(y)
 all_data = prunned_data.drop('SalePrice',axis=1)
 all_data['GrLivArea'] = np.log(all_data['GrLivArea'])
 all_data['1stFlrSF'] = np.log(all_data['1stFlrSF'])
 all_data['ExterQual'] = np.log(all_data['ExterQual'])
 all_data['GarageCars'] = np.power(all_data['GarageCars'],2)
 all_data['BsmtQual'] = np.power(all_data['BsmtQual'],2)
 all_data['OverallQual'] = np.power(all_data['OverallQual'],2)
 X_train_final, X_test_final, logy_train_final, logy_test_final = train_test_split(all_data,logy,random_state=2)
 imputer = SimpleImputer()
X_train_final = imputer.fit_transform(X_train_final)
 X_test_final = imputer.fit_transform(X_test_final )<train_model>
train_imgs_path = '.. /input/train/train'
test_imgs_path = '.. /input/test/test'
labels_path = '.. /input/train.csv'
in_path = '.. /input/'
regr = linear_model.LinearRegression(normalize=False)
 regr.fit(X_train_final,logy_train_final)
 plot(regr,X_train_final,logy_train_final,X_test_final,logy_test_final,title='Linear Regression',log=True )<train_on_grid>
df = pd.read_csv(labels_path)
 df['id'] = 'train/train/' + df['id']
df.head()
ridge = linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0),normalize=True)
 ridge.fit(X_train_final,logy_train_final)
 plot(ridge,X_train_final,logy_train_final,X_test_final,logy_test_final,title='RidgeCV',log=True)
 print(ridge.alpha_)
 ridge2 = linear_model.RidgeCV(alphas=(0.5, 1.0, 2., 5., 8.) ,normalize=True)
 ridge2.fit(X_train_final,logy_train_final)
 plot(ridge2,X_train_final,logy_train_final,X_test_final,logy_test_final,title='RidgeCV',log=True)
 print(ridge2.alpha_ )<train_on_grid>
data = vision.ImageDataBunch.from_df(in_path, df, ds_tfms=vision.get_transforms() , size=224)
 data = data.normalize(vision.imagenet_stats )
lasso = linear_model.LassoCV(normalize=True)
 lasso.fit(X_train_final,logy_train_final)
 plot(lasso,X_train_final,logy_train_final,X_test_final,logy_test_final,title='LassoCV',log=True)
 print(lasso.alpha_ )<train_on_grid>
data.show_batch(rows=3, figsize=(10, 8))
lassolars = linear_model.LassoLarsCV(normalize=True)
 lassolars.fit(X_train_final,logy_train_final)
 plot(lassolars,X_train_final,logy_train_final,X_test_final,logy_test_final,title='LassoLarsCV',log=True)
 print(lassolars.alpha_ )<train_model>
learn = vision.cnn_learner(data, vision.models.resnet34, metrics=metrics.accuracy)
 learn.fit(2 )
bayes = linear_model.BayesianRidge(normalize=True)
 bayes.fit(X_train_final,logy_train_final)
 plot(bayes,X_train_final,logy_train_final,X_test_final,logy_test_final,title='Bayes',log=True )<feature_engineering>
interp = vision.ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
submission_data = pd.read_csv('.. /input/test.csv')
 ids = submission_data.Id
submission_data = submission_data.drop('Id',axis=1)
 submission_data = qualitative_to_quantitative(submission_data)
 submission_data = submission_data.drop('PoolQC',axis=1)
 submission_data = submission_data.drop('GarageYrBlt',axis=1)
 submission_data['GrLivArea'] = np.log(submission_data['GrLivArea'])
 submission_data['1stFlrSF'] = np.log(submission_data['1stFlrSF'])
 submission_data['ExterQual'] = np.log(submission_data['ExterQual'])
 submission_data['GarageCars'] = np.power(submission_data['GarageCars'],2)
 submission_data['BsmtQual'] = np.power(submission_data['BsmtQual'],2)
 submission_data['OverallQual'] = np.power(submission_data['OverallQual'],2)
 imputer = SimpleImputer()
X_submission = imputer.fit_transform(submission_data)
 predicted_prices_log = lassolars.predict(X_submission)
 predicted_prices = np.exp(predicted_prices_log)
 fig, ax = plt.subplots(1,1)
 ax.plot(ids.values,predicted_prices,marker='o',linestyle='None',label='train' )<save_to_csv>
submission_df = pd.read_csv('.. /input/sample_submission.csv')
 files = submission_df['id'].values
img_paths =('.. /input/test/test/' + submission_df['id'] ).values
np.savetxt('submission_lassolars.csv',np.dstack(( ids,predicted_prices)).reshape(1459 , 2),delimiter=',',fmt='%d,%f',header='Id,SalePrice',comments='' )<load_from_csv>
preds = []
for p in tqdm(img_paths):
pred = learn.predict(vision.open_image(p)) [-1].numpy()
preds.append(pred )
warnings.filterwarnings('ignore')
 %matplotlib inline
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
<feature_engineering>
submission_df['has_cactus'] = np.array(preds)[:, 1]
submission_df.head()
<data_type_conversions><EOS> 
submission_df.to_csv('submission.csv', index=False )