Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<train_model>
!pip install seaborn
learn.load('fit-first-epoch');<train_model>
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV,RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier,ExtraTreesClassifier
from xgboost import XGBClassifier
learn.fit_one_cycle(11, slice(1e-4, 2e-3))<save_model>
submission_sample = pd.read_csv('.. /input/forest-cover-type-prediction/sampleSubmission.csv')
 train = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 test = pd.read_csv('.. /input/forest-cover-type-prediction/test.csv' )
learn.save('after-11-epochs');<predict_on_test>
train.nunique()
preds,_ = learn.get_preds(ds_type=DatasetType.Test )<load_from_csv>
train.isna().sum()
test_df = pd.read_csv(".. /input/sample_submission.csv")
 test_df.has_cactus = preds.numpy() [:, 1]<categorify>
def outlier_function(df, col_name):

 first_quartile = np.percentile(np.array(df[col_name].tolist()), 25)
 third_quartile = np.percentile(np.array(df[col_name].tolist()), 75)
 IQR = third_quartile - first_quartile
upper_limit = third_quartile+(3*IQR)
 lower_limit = first_quartile-(3*IQR)
 outlier_count = 0
for value in df[col_name].tolist() :
if(value < lower_limit)|(value > upper_limit):
outlier_count +=1
return lower_limit, upper_limit, outlier_count
to_np(p )<save_to_csv>
trees = train[(train['Horizontal_Distance_To_Fire_Points'] > outlier_function(train, 'Horizontal_Distance_To_Fire_Points')[0])& 
(train['Horizontal_Distance_To_Fire_Points'] < outlier_function(train, 'Horizontal_Distance_To_Fire_Points')[1])]
trees.shape
test_df.to_csv('submission.csv', index=False )<import_modules>
size=10
Uni = []
for i in range(size+1,len(train.columns)-1):
Uni.append(pd.unique(train[train.columns[i]].values))
import numpy as np
import pandas as pd<import_modules>
x = train.iloc[:,15:55]
y = train.iloc[:,11:15]
y = pd.DataFrame(y)
 x = pd.DataFrame(x)
 s2 = pd.Series(x.columns[np.where(x!=0)[1]])
 s3 = pd.Series(y.columns[np.where(y!=0)[1]])
 train['soil_type'] = s2
train['Wilderness_Area'] = s3
train.head()
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch<define_variables>
df_viz = train.iloc[:, 0:15]
df_viz = df_viz.drop(['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3',
'Wilderness_Area4'], axis = 1)
 df_viz.head()
data_folder = Path(".. /input")
<load_from_csv>
train=trees
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<choose_model_class>
def add_feature(data):
data['Ele_minus_VDtHyd'] = data.Elevation-data.Vertical_Distance_To_Hydrology
data['Ele_plus_VDtHyd'] = data.Elevation+data.Vertical_Distance_To_Hydrology
data['Distanse_to_Hydrolody'] =(data['Horizontal_Distance_To_Hydrology']**2+data['Vertical_Distance_To_Hydrology']**2)**0.5
data['Hydro_plus_Fire'] = data['Horizontal_Distance_To_Hydrology']+data['Horizontal_Distance_To_Fire_Points']
data['Hydro_minus_Fire'] = data['Horizontal_Distance_To_Hydrology']-data['Horizontal_Distance_To_Fire_Points']
data['Hydro_plus_Road'] = data['Horizontal_Distance_To_Hydrology']+data['Horizontal_Distance_To_Roadways']
data['Hydro_minus_Road'] = data['Horizontal_Distance_To_Hydrology']-data['Horizontal_Distance_To_Roadways']
data['Fire_plus_Road'] = data['Horizontal_Distance_To_Fire_Points']+data['Horizontal_Distance_To_Roadways']
data['Fire_minus_Road'] = data['Horizontal_Distance_To_Fire_Points']-data['Horizontal_Distance_To_Roadways']
return data
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy] )<train_model>
train = add_feature(train)
 test = add_feature(test )
lr = 3e-02
learn.fit_one_cycle(5, slice(lr))<predict_on_test>
X_train = train.drop(['Id','Cover_Type'], axis = 1)
 y_train = train.Cover_Type
X_test = test.drop(['Id'], axis = 1 )
preds,_ = learn.get_preds(ds_type=DatasetType.Test )<filter>
%%time
lr_pipe = Pipeline(
 steps = [ 
('scaler', MinMaxScaler()), 
('classifier', LogisticRegression(solver='lbfgs', n_jobs=-1))
] 
)
 lr_param_grid = {
'classifier__C': [1, 10, 100,1000],
}
np.random.seed(1)
 grid_search = GridSearchCV(lr_pipe, lr_param_grid, cv=5, refit='True')
 grid_search.fit(X_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_)
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
%%time
rf_pipe = Pipeline(
 steps = [ 
('classifier', RandomForestClassifier(n_estimators=500))
] 
)
 param_grid = {
'classifier__min_samples_leaf': [1,4,7],
'classifier__max_depth': [34,38,32],
}
np.random.seed(1)
 rf_grid_search = GridSearchCV(rf_pipe, param_grid, cv=5, refit='True', n_jobs=-1)
 rf_grid_search.fit(X_train, y_train)
 print(rf_grid_search.best_score_)
 print(rf_grid_search.best_params_ )
test_df.to_csv('submission.csv', index=False )<import_modules>
rf_model = rf_grid_search.best_estimator_
cv_score = cross_val_score(rf_model, X_train, y_train, cv = 5)
 print(cv_score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(cv_score.mean() , cv_score.std() * 2))
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from tqdm import tqdm<categorify>
rf = rf_grid_search.best_estimator_.steps[0][1]
def format_prediction_string(image_id, result):
prediction_strings = []
for i in range(len(result['detection_scores'])) :
class_name = result['detection_class_names'][i].decode("utf-8")
 corrected_coordinates = [result['detection_boxes'][i][1], result['detection_boxes'][i][0], result['detection_boxes'][i][3], result['detection_boxes'][i][2]]
boxes = corrected_coordinates
score = result['detection_scores'][i]
prediction_strings.append(
 f"{class_name} {score} " + " ".join(map(str, boxes)) 
)
 prediction_string = " ".join(prediction_strings)
 return {
"ImageID": image_id,
"PredictionString": prediction_string
}<load_from_csv>
%%time
xgd_pipe = Pipeline(
 steps = [ 
('classifier', XGBClassifier(n_estimators=50, subsample=0.5))
] 
)
 param_grid = {
'classifier__learning_rate' : [0.45],
'classifier__min_samples_split' : [8, 16, 32],
'classifier__min_samples_leaf' : [2],
'classifier__max_depth': [15]
}
np.random.seed(1)
 xgd_grid_search = GridSearchCV(xgd_pipe, param_grid, cv=5,
refit='True', verbose = 10, n_jobs=-1)
 xgd_grid_search.fit(X_train, y_train)
 print(xgd_grid_search.best_score_)
 print(xgd_grid_search.best_params_ )
sample_submission_df = pd.read_csv('.. /input/sample_submission.csv')
 image_ids = sample_submission_df['ImageId']
predictions = []
with tf.Graph().as_default() :
image_string_placeholder = tf.placeholder(tf.string)
 decoded_image = tf.image.decode_jpeg(image_string_placeholder)
 decoded_image_float = tf.image.convert_image_dtype(image=decoded_image, dtype=tf.float32)
 image_tensor = tf.expand_dims(decoded_image_float, 0)
 model_url = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
detector = hub.Module(model_url)
 detector_output = detector(image_tensor, as_dict=True)
 init_ops = [tf.global_variables_initializer() , tf.tables_initializer() ]
sess = tf.Session()
sess.run(init_ops)
 for image_id in tqdm(image_ids):
image_path = f'.. /input/test/{image_id}.jpg'
with tf.gfile.Open(image_path, "rb")as binfile:
image_string = binfile.read()
result_out = sess.run(
 detector_output,
feed_dict={image_string_placeholder: image_string} 
)
 predictions.append(format_prediction_string(image_id, result_out))
sess.close()
pred_df = pd.DataFrame(predictions)
 pred_df.to_csv('submission.csv', index=False )<import_modules>
xgd_model = xgd_grid_search.best_estimator_
cv_score = cross_val_score(xgd_model, X_train, y_train, cv = 5)
 print(cv_score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(cv_score.mean() , cv_score.std() * 2))
import pandas as pd
import numpy as np
import cv2
from zipfile import ZipFile
from fastai import *
from fastai.vision import *<load_pretrained>
xrf_pipe = Pipeline(
 steps = [ 
('classifier', ExtraTreesClassifier(n_estimators=500,random_state=0, criterion = 'entropy'))
] 
)
 xrf_param_grid = {
'classifier__min_samples_leaf': [1,4,7],
'classifier__max_depth': [34,38,32],
}
np.random.seed(1)
 xrf_grid_search = GridSearchCV(xrf_pipe, xrf_param_grid, cv=5, refit='True', n_jobs=-1)
 xrf_grid_search.fit(X_train, y_train)
 print(xrf_grid_search.best_score_)
 print(xrf_grid_search.best_params_ )
def unzip_folder(path=None,folder_name='train',extract_to=None):

 with ZipFile(path+folder_name+'.zip', 'r')as zip:
print('Extracting all the files now from '+folder_name+'...')
 zip.extractall(path=extract_to)
 print('Done!' )<load_pretrained>
xrf_model = xrf_grid_search.best_estimator_
cv_score = cross_val_score(xrf_model, X_train, y_train, cv = 5)
 print(cv_score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(cv_score.mean() , cv_score.std() * 2))
unzip_folder(path='/kaggle/input/aerial-cactus-identification/',folder_name='train')
 unzip_folder(path='/kaggle/input/aerial-cactus-identification/',folder_name='test')
<load_from_csv>
final_model = xrf_grid_search.best_estimator_.steps[0][1]
final_model.fit(X_train, y_train)
df=pd.read_csv('/kaggle/input/aerial-cactus-identification/train.csv' )<count_values>
y_pred = final_model.predict(X_test )
df['has_cactus'].value_counts()<count_missing_values>
Counter(y_pred )
df.isnull().sum()<choose_model_class>
submission = pd.DataFrame({'Id': test.Id,
'Cover_Type': y_pred})
 submission.head()
lean_den161=cnn_learner(data, models.densenet161, metrics=accuracy )<train_model>
submission.to_csv('submission.csv', index=False )
lean_den161.fit_one_cycle(7 )<load_pretrained>
submission.to_csv('submission.csv', index=False )