Kaggle competition: Forest Cover Type Prediction
pretrained_net = EfficientNetB3(
 weights='imagenet',
input_shape=(32,32,3),
include_top=False,
pooling='avg' 
)
 model = Sequential()
model.add(pretrained_net)
 model.add(Dropout(rate = 0.1))
model.add(Dense(units = 1, activation='sigmoid'))<choose_model_class>
train_data = pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data.head()
model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )<train_model>
test_data = pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv')
 test_data.head()
history = model.fit_generator(
 train_generator,
epochs = 20,
steps_per_epoch = train_generator.n//train_generator.batch_size,
validation_data = valid_generator,
validation_steps = valid_generator.n//valid_generator.batch_size
)<set_options>
train_data['Cover_Type'].value_counts()
K.set_value(model.optimizer.lr,0.00001 )<train_model>
from sklearn.model_selection import train_test_split
history = model.fit_generator(
 train_generator,
epochs = 5,
steps_per_epoch = train_generator.n//train_generator.batch_size,
validation_data = valid_generator,
validation_steps = valid_generator.n//valid_generator.batch_size
)<predict_on_test>
from sklearn.model_selection import train_test_split
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )<prepare_output>
X = train_data.drop(labels = ['Id','Cover_Type'],axis = 1)
 Y = train_data['Cover_Type']
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )<save_to_csv>
X_train,X_val,Y_train,Y_val = train_test_split(X,Y,random_state = 40 )
submission.to_csv("submission.csv", index=False )<save_to_csv>
from sklearn.ensemble import RandomForestClassifier
submission.to_csv("submission.csv", index=False )<save_to_csv>
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(X_train,Y_train )
def clip(x):
if x>=0.5:
return 0.999999
else:
return 0
submission.has_cactus=submission.has_cactus.apply(clip)
 submission.to_csv("./submission_clipped.csv",index=False )<install_modules>
rfc.score(X_val,Y_val )
!pip install natsort<set_options>
predict=rfc.predict(test_data.drop(labels=['Id'],axis=1))
import numpy as np
import pandas as pd
from glob import glob
from skimage.io import imread
import skimage.io as sio
import os
from natsort import natsorted
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, roc_auc_score
from skimage.transform import resize, rotate
import warnings; warnings.filterwarnings("ignore" )<load_from_csv>
Submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 Submission.head()
root_dir = ".. /input"
train_dir = root_dir + "/train/train/"
test_dir = root_dir + "/test/test/"
csv_path = root_dir + "/train.csv"
sub_path = root_dir + "sample_submission.csv"
df = pd.read_csv(csv_path)
 x = np.array([ imread(train_dir+p)for p in df.id.values])
 y = df.has_cactus.values<split>
Submission['Id']=test_data['Id']
Submission.set_index('Id',inplace=True )
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.20,stratify=y )<categorify>
Submission.to_csv('Submission_first_time.csv' )