Kaggle competition: Tabular Playground Series - Feb 2021
learn.save('Model-1' )<choose_model_class>
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/test.csv')
learn = cnn_learner(data,
models.resnet34,
metrics=[accuracy, AUROC() ],
callback_fns=[partial(SaveModelCallback)],
path = '.')
 learn = learn.load('Model-1' )<train_model>
lr = 1e-6
learn.fit_one_cycle(10, lr )<predict_on_test>
df = pd.concat([train_df.drop(['target','id'],axis=1),test_df.drop(['id'],axis=1)],axis=0)
 df.head()
probability, classification = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = probability.numpy() [:, 0]
test_df.head()<save_to_csv>
df.isnull().sum()
test_df.to_csv("submission.csv", index=False )<install_modules>
def divideFeatures(df):
numerical_features = df.select_dtypes(include=[np.number])
 categorical_features = df.select_dtypes(include=[np.object])
 return numerical_features, categorical_features
!pip install tensorflow-gpu==2.0.0-beta0
!pip install pandas<import_modules>
cont_features, cat_features =divideFeatures(df )
import tensorflow as tf
from tensorflow import keras<compute_test_metric>
from sklearn.preprocessing import OneHotEncoder , LabelEncoder
from sklearn.pipeline import make_pipeline ,Pipeline
from sklearn.feature_selection import SelectKBest
from sklearn.compose import make_column_transformer ,ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split ,cross_val_score,validation_curve,learning_curve
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import GridSearchCV ,RandomizedSearchCV
from sklearn.tree import plot_tree
from sklearn.ensemble import RandomForestRegressor,AdaBoostRegressor,GradientBoostingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.linear_model import LogisticRegression
def focal_loss_fn(gamma=2.,alpha=0.25):
EPSILON = 1e-6
def ce(y_true,y_pred,weights=None):
mask = y_pred < EPSILON
true_vals = tf.fill(tf.shape(y_pred),EPSILON)
 y_pred = tf.where(mask,true_vals,y_pred)
 ce = y_true*(-tf.math.log(y_pred)) +(1-y_true)*(-tf.math.log(1-y_pred))
if weights is not None:
ce = ce*weights
ce_loss = tf.reduce_mean(ce+EPSILON)
 return ce_loss
def focal_loss_fixed(y_true,y_pred):
t = y_true
p = y_pred
pt = p*t +(1-p)*(1-t)
 w = alpha*t +(1-alpha)*(1-t)
 w = tf.pow(( 1-pt),gamma)
 fl = ce(y_true,y_pred,w)
 return fl
return focal_loss_fixed<install_modules>
one = OneHotEncoder(handle_unknown='ignore')
 scaler =StandardScaler()
fs = SelectKBest()
le =LabelEncoder()
!pip install imutils<define_variables>
def lencode(df):
for i in df.columns:
for k in cat_features.columns:
if i ==k:
df[i]= le.fit_transform(df[i])
test_imgs_dir = ".. /input/aerial-cactus-identification/test/test" 
<define_variables>
X_train = df[:300000]
Xf_test = df[300000:]
y = train_df['target']
img_paths = list(paths.list_images(test_imgs_dir)) 
<import_modules>
X_train, X_test, y_train, y_test = train_test_split(X_train, y, test_size=0.1, random_state=42 )
import cv2<normalization>
linear = LinearRegression()
random = RandomForestRegressor(n_estimators=100,random_state=42,n_jobs=-1,max_features=4)
img_size = 96
images = []
img_names = []
for img_path in img_paths:
img_name = img_path.split(os.path.sep)[-1]
img = cv2.imread(img_path)
 img = cv2.resize(img,(img_size,img_size))
img = keras.preprocessing.image.img_to_array(img)
 images.append(img)
 img_names.append(img_name)
 images = np.array(images,dtype='float')/ 255.
 print(len(img_names),len(images))
print(np.max(images),np.min(images))<predict_on_test>
xgbr = xgb.XGBRegressor(tree_method='gpu_hist')
predictions = model.predict(images )<concatenate>
def objective(trial):
params = {
'random_state': 0,
'n_estimators': trial.suggest_categorical('n_estimators', [10000]),
'max_depth': trial.suggest_int('max_depth', 3, 8),
'learning_rate': trial.suggest_float('learning_rate', 0.001, 1.0),
'reg_lambda': trial.suggest_float('reg_lambda', 0.0, 10),
'reg_alpha': trial.suggest_float('reg_alpha', 0.0, 10),
'gamma': trial.suggest_float('gamma', 0.0, 10),
'subsample': trial.suggest_categorical('subsample', [0.8, 0.9, 1.0]),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.1, 0.2, 0.3, 0.4, 0.5]),
'tree_method':'gpu_hist'
}
model = xgb.XGBRegressor(**params)
 model.fit(X_train, y_train, eval_set=[(X_test,y_test)], early_stopping_rounds=1000, verbose=0)
 y_pred = model.predict(X_test)
 rmse = mean_squared_error(y_test, y_pred, squared=False)
 return rmse
solution = np.concatenate(( img_names,predictions),axis=1)
 solution.shape<save_to_csv>
%%time
study = optuna.create_study(direction='minimize',sampler=optuna.samplers.TPESampler(seed=0))
study.optimize(objective, n_trials=100)
 print('Number of finished trials:', len(study.trials))
print('Best parameters:', study.best_trial.params)
 print('Best RMSE:', study.best_trial.value )
submit_csv = pd.DataFrame(solution,columns=["id","has_cactus"])
 submit_csv.to_csv("submission_resnet50.csv",index=False )<set_options>
params = study.best_params
params['random_state'] = 0
params['n_estimators'] = 10000
params['tree_method'] = 'gpu_hist'
model3 = xgb.XGBRegressor(**params)
 model3.fit(X_train,y_train,eval_set=[(X_test, y_test)],early_stopping_rounds=1000,verbose=2)
%matplotlib inline<load_from_csv>
model = make_pipeline(linear)
 model.fit(X_train,y_train)
 model2 = make_pipeline(xgbr)
 model2.fit(X_train,y_train )
train_df = pd.read_csv(".. /input/train.csv")
 train_df.head() 
<load_from_csv>
models ={'linear':model}
models['xgbr'] = model2
models['optuna_xbg'] =model3
test_df = pd.read_csv(".. /input/sample_submission.csv")
 test_img = ImageList.from_df(test_df, path=".. /input", folder='test/test')
 tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75 )<categorify>
def rmse(y_test,y_pred):
rmse= np.sqrt(mean_squared_error(y_test,y_pred))
return rmse
data =(ImageList.from_df(train_df,path=".. /input/train",folder="train")
.split_by_rand_pct() 
.label_from_df() 
.add_test(test_img)
.transform(tfms,size = 128)
.databunch(path='.',bs=64)
.normalize(imagenet_stats)
 )<define_variables>
def cross_val_rmse(model):
model =clone(model)
 five_fold = KFold(n_splits=5)
 rmse_val =[]
for tr_ind,val_ind in five_fold.split(X_train):
model.fit(X_train.iloc[tr_ind,:],y.iloc[tr_ind])
 rmse_val.append(rmse(y.iloc[val_ind],model.predict(X_train.iloc[val_ind,:])))
 return np.mean(rmse_val )
data.show_batch(rows=3,figsize =(5,5))<find_best_params>
y_pred = model3.predict(X_test)
 y_pred =np.round(y_pred,6)
 print('min',y_pred.min())
 print('max',y_pred.max())
 RMSE = np.sqrt(mean_squared_error(y_test,y_pred))
print('RMSE',RMSE)
 print(y_pred.tolist() )
data.valid_ds.classes<choose_model_class>
ys_pred =model3.predict(Xf_test)
 ys_pred =np.round(ys_pred,6)
 ys_pred.tolist()
<train_model><EOS> 
sub = pd.DataFrame({'id':test_df.id,'target':ys_pred})
 sub.to_csv('TPS.csv',index=False)
 sub.head()