Kaggle competition: Aerial Cactus Identification
from datetime import datetime
from sklearn.preprocessing import RobustScaler
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import mean_squared_error , make_scorer
from sklearn.linear_model import ElasticNetCV, LassoCV, RidgeCV
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.svm import SVR
from mlxtend.regressor import StackingCVRegressor
from sklearn.linear_model import LinearRegression
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor 
<compute_train_metric>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/sample_submission.csv')
 print(train_df.shape, test_df.shape )
kfolds = KFold(n_splits=18, shuffle=True, random_state=42)
 def cv_rmse(model, X=X):
rmse = np.sqrt(-cross_val_score(model, X, y,scoring="neg_mean_squared_error",cv=kfolds))
return(rmse)
 def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<choose_model_class>
train_df['has_cactus'].value_counts()
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=9000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
 e_alphas = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
alphas_alt = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas2 = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=alphas_alt, cv=kfolds))
lasso = make_pipeline(RobustScaler() , LassoCV(max_iter=1e7,
alphas=alphas2,random_state=42, cv=kfolds))
elasticnet = make_pipeline(RobustScaler() , ElasticNetCV(max_iter=1e7,
alphas=e_alphas, cv=kfolds, l1_ratio=e_l1ratio))
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, elasticnet, lightgbm),
meta_regressor=elasticnet,
use_features_in_secondary=True)
 svr = make_pipeline(RobustScaler() , SVR(C= 20, epsilon= 0.008, gamma=0.0003))
models = {'Ridge': ridge,
'Lasso': lasso,
'ElasticNet': elasticnet,
'lightgbm': lightgbm,
'Svd': svr}
predictions = {}
scores = {}<compute_train_metric>
train_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'
for name, model in models.items() :
model.fit(X, y)
 predictions[name] = np.expm1(model.predict(X))
score = cv_rmse(model, X=X)
 scores[name] =(score.mean() , score.std() )<compute_test_metric>
def prep_cnn_data(df, n_x, n_c, path):

 tensors = np.zeros(( df.shape[0], n_x, n_x, n_c))
for i in range(df.shape[0]):
pic = load_img(path+df.iloc[i]['id'])
 pic_array = img_to_array(pic)
 tensors[i,:] = pic_array
tensors = tensors / 255.
 return tensors
print('---- Score with CV_RMSLE-----')
 score = cv_rmse(ridge)
 print("Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(elasticnet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(lightgbm)
 print("lightgbm score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = cv_rmse(svr)
 print("SVR: {:.4f}({:.4f})".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
print('----START Fit----',datetime.now())
 print('Elasticnet')
 elastic_model = elasticnet.fit(X, y)
 print('Lasso')
 lasso_model = lasso.fit(X, y)
 print('Ridge')
 ridge_model = ridge.fit(X, y)
 print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X, y)
 print('Svr')
 svr_model_full_data = svr.fit(X, y)
 print('stack_gen')
 stack_gen_model = stack_gen.fit(np.array(X), np.array(y)) 
<predict_on_test>
train_pic_array = prep_cnn_data(train_df, 32, 3, path='.. /input/train/train/')
 train_Y = train_df['has_cactus'].values
def blend_models_predict(X):
return(( 0.16 * elastic_model.predict(X)) + \ 
(0.16 * lasso_model.predict(X)) + \ 
(0.11 * ridge_model.predict(X)) + \ 
(0.2 * lgb_model_full_data.predict(X)) + \ 
(0.1 * svr_model_full_data.predict(X)) + \ 
(0.27 * stack_gen_model.predict(np.array(X))))<compute_test_metric>
test_pic_array = prep_cnn_data(test_df, 32, 3, path='.. /input/test/test/' )
print('RMSLE score on train data:')
 print(rmsle(y, blend_models_predict(X)) )<load_from_csv>
data_augment = ImageDataGenerator(zoom_range=0.1,
width_shift_range=0.1, height_shift_range=0.1,
horizontal_flip=True, vertical_flip=True )
print('Predict submission')
 submission = pd.read_csv(".. /input/sample_submission.csv")
 submission.iloc[:,1] =(np.expm1(blend_models_predict(X_test)) )<import_modules>
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=3, padding='same', activation='relu', input_shape=(32, 32, 3)))
 model.add(layers.Conv2D(32, kernel_size=3, padding='valid', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2), strides=2))
model.add(layers.Dropout(rate=0.4))
model.add(layers.Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Conv2D(64, kernel_size=5, padding='valid', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2), strides=2))
model.add(layers.Dropout(rate=0.4))
model.add(layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'))
model.add(layers.Conv2D(128, kernel_size=3, padding='valid', activation='relu'))
model.add(layers.Flatten())
 model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dropout(rate=0.4))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
import pandas as pd<import_modules>
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'] )
import pandas as pd<set_options>
epochs = 150
batch_size = 1024
history = model.fit_generator(data_augment.flow(rem_X_train, rem_Y_train, batch_size=batch_size),
epochs=epochs, steps_per_epoch=rem_X_train.shape[0]//batch_size,
validation_data=(X_dev, Y_dev))
pd.set_option('display.max_rows', 1000)
 %pip install ppscore
seed =2055
plt.style.use('fivethirtyeight' )<load_from_csv>
pred_dev = model.predict(X_dev)
 pred_dev =(pred_dev > 0.5 ).astype(int )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )<feature_engineering>
result = pd.DataFrame(train_Y[:3500], columns=['Y_dev'])
 result['Y_pred'] = pred_dev
result['correct'] = result['Y_dev'] - result['Y_pred']
errors = result[result['correct'] != 0]
error_list = errors.index
print('Number of errors is ', len(errors))
print('The indices are ', error_list )
def basic_analysis(df1, df2):

 b = pd.DataFrame()
b['First df_mean'] = round(df1.mean() ,2)
 b['Second df_mean'] = round(df2.mean() ,2)
 c =(b['First df_mean']/b['Second df_mean'])
 if [c<=1]:
b['Variation, %'] = round(( 1-(( b['First df_mean']/b['Second df_mean'])))*100)
 else:
b['Variation, %'] = round(((b['First df_mean']/b['Second df_mean'])-1)*100)
 b['Influence'] = np.where(abs(b['Variation, %'])<= 9, "feature's effect on the target is not defined",
"feature value affects the target")
 return b<feature_engineering>
predictions = model.predict(test_pic_array)
 print(predictions.shape )
train.loc[train['PassengerId'] == 631, 'Age'] = 48
train.loc[train['PassengerId'] == 69, ['SibSp', 'Parch']] = [0,0]
test.loc[test['PassengerId'] == 1106, ['SibSp', 'Parch']] = [0,0]<sort_values>
test_df['has_cactus'] = predictions
test_df.head()
train[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<define_variables>
test_df.to_csv('submission.csv', index=False )