Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<choose_model_class>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'] )<train_model>
data_train = pd.read_csv(".. /input/train.csv")
 data_test = pd.read_csv(".. /input/test.csv")
 data_train.head()
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))<predict_on_test>
features_soil = ['Soil_Type1', 'Soil_Type2', 'Soil_Type3',
'Soil_Type4', 'Soil_Type5', 'Soil_Type6', 'Soil_Type7', 'Soil_Type8',
'Soil_Type9', 'Soil_Type10', 'Soil_Type11', 'Soil_Type12',
'Soil_Type13', 'Soil_Type14', 'Soil_Type15', 'Soil_Type16',
'Soil_Type17', 'Soil_Type18', 'Soil_Type19', 'Soil_Type20',
'Soil_Type21', 'Soil_Type22', 'Soil_Type23', 'Soil_Type24',
'Soil_Type25', 'Soil_Type26', 'Soil_Type27', 'Soil_Type28',
'Soil_Type29', 'Soil_Type30', 'Soil_Type31', 'Soil_Type32',
'Soil_Type33', 'Soil_Type34', 'Soil_Type35', 'Soil_Type36',
'Soil_Type37', 'Soil_Type38', 'Soil_Type39', 'Soil_Type40']
data_train["Soil_Count"] = data_train[features_soil].apply(sum, axis=1)
 data_train.head()
pred_dev = model.predict(X_dev)
 pred_dev =(pred_dev > 0.5 ).astype(int )<prepare_output>
data_train["Soil_Type"] = data_train[features_soil].apply(np.argmax, axis=1)
 data_train.head()
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 )<predict_on_test>
data_train["Soil_Type"] = data_train["Soil_Type"].apply(lambda x: x.split("Soil_Type")[-1])
 data_train.head()
predictions = model.predict(test_pic_array)
 print(predictions.shape )<feature_engineering>
data_train["Wilderness_Area"] = data_train[features_wilderness].apply(np.argmax, axis=1)
 data_train["Wilderness_Area"] = data_train["Wilderness_Area"].apply(lambda x: x.split("Wilderness_Area")[-1])
 data_train.Wilderness_Area.head()
test_df['has_cactus'] = predictions
test_df.head()<save_to_csv>
data_train["Elevation_Aspect"] = np.sqrt(data_train.Elevation * data_train.Aspect)
 sns.distplot(data_train["Elevation_Aspect"])
 plt.show()
test_df.to_csv('submission.csv', index=False )<load_from_csv>
def clear_dataset(dataset):
features_soil = ['Soil_Type1', 'Soil_Type2', 'Soil_Type3',
'Soil_Type4', 'Soil_Type5', 'Soil_Type6', 'Soil_Type7', 'Soil_Type8',
'Soil_Type9', 'Soil_Type10', 'Soil_Type11', 'Soil_Type12',
'Soil_Type13', 'Soil_Type14', 'Soil_Type15', 'Soil_Type16',
'Soil_Type17', 'Soil_Type18', 'Soil_Type19', 'Soil_Type20',
'Soil_Type21', 'Soil_Type22', 'Soil_Type23', 'Soil_Type24',
'Soil_Type25', 'Soil_Type26', 'Soil_Type27', 'Soil_Type28',
'Soil_Type29', 'Soil_Type30', 'Soil_Type31', 'Soil_Type32',
'Soil_Type33', 'Soil_Type34', 'Soil_Type35', 'Soil_Type36',
'Soil_Type37', 'Soil_Type38', 'Soil_Type39', 'Soil_Type40']
features_wilderness = ['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3','Wilderness_Area4']
dataset["Soil_Type"] = dataset[features_soil].apply(np.argmax, axis=1)
 dataset["Soil_Type"] = dataset["Soil_Type"].apply(lambda x: x.split("Soil_Type")[-1] ).astype(int)
 dataset = dataset.drop(["Soil_Type15", "Soil_Type7"], axis=1)
 dataset["Wilderness_Area"] = dataset[features_wilderness].apply(np.argmax, axis=1)
 dataset["Wilderness_Area"] = dataset["Wilderness_Area"].apply(lambda x: x.split("Wilderness_Area")[-1] ).astype(int)
 dataset["Hillshade_1"] =(dataset.Hillshade_Noon * dataset.Hillshade_9am)
 dataset["Hillshade_1_sqrt"] = np.sqrt(dataset["Hillshade_1"])
 dataset["Hillshade_2"] =(dataset.Hillshade_3pm * dataset.Hillshade_9am)
 dataset["Hillshade_2_sqrt"] = np.sqrt(dataset["Hillshade_2"])
 dataset["Hillshade_3"] =(dataset.Hillshade_3pm * dataset.Hillshade_Noon * dataset.Hillshade_9am)
 dataset["Hillshade_3_sqrt"] = np.sqrt(dataset["Hillshade_3"])
 dataset.Hillshade_1 = dataset.Hillshade_1.astype(float)
 dataset["DistanceToHydrology"] = np.sqrt(dataset.Horizontal_Distance_To_Hydrology ** 2 + dataset.Vertical_Distance_To_Hydrology ** 2)
 dataset["Horizontal_Distance_To_Roadways_sqrt"]= np.sqrt(dataset["Horizontal_Distance_To_Roadways"])
 dataset["Horizontal_Distance_To_Fire_Points_sqrt"]= np.sqrt(dataset["Horizontal_Distance_To_Fire_Points"])
 dataset["Slope_sqrt"] = np.sqrt(dataset["Slope"])
 dataset["Hillshade_9am_cube"] = dataset["Hillshade_9am"].apply(lambda x: x**3)
 dataset["Hillshade_Noon_cube"] = dataset["Hillshade_Noon"].apply(lambda x: x**3)
 dataset["Aspect_Slope_cbrt"] = np.cbrt(dataset.Aspect * dataset.Slope)
 dataset["Elevation_Slope_sqrt"] = np.sqrt(dataset.Elevation * dataset.Slope)
 dataset["Elevation_Aspect_sqrt"] = np.sqrt(dataset.Elevation * dataset.Aspect)
 dataset["Elevation_sqrt"] = np.sqrt(dataset.Elevation)
 return dataset
train_csv = pd.read_csv(path/'train.csv')
 test_df = pd.read_csv(path/'sample_submission.csv')
 test_data = ImageList.from_df(test_df, path=path/'test', folder='test')
 data = ImageDataBunch.from_csv(path, csv_labels='train.csv', folder='train/train', size=32)
 data.add_test(test_data)
 data.normalize(imagenet_stats)
 data.show_batch(rows=3, figsize=(5,5))<choose_model_class>
data_train = pd.read_csv(".. /input/train.csv")
 final_train = clear_dataset(data_train )
learn = cnn_learner(data, models.resnet34, metrics=error_rate, model_dir=model_path )<train_model>
x_data = final_train.drop(["Cover_Type", "Id"], axis=1)
 y_data = final_train["Cover_Type"]
learn.fit_one_cycle(4 )<save_model>
pca = PCA(n_components = 2)
 x_pca = pca.fit_transform(x_data)
 print("variance ratio: ", pca.explained_variance_ratio_)
 print("sum: ",sum(pca.explained_variance_ratio_))
learn.save('first' )<train_model>
x_train, x_val, y_train, y_val = train_test_split(x_data, y_data, test_size=0.25, random_state=42 )
learn.load('first')
 learn.unfreeze()
learn.fit_one_cycle(3, max_lr=5e-5 )<predict_on_test>
def get_metrics(y_test, y_predicted):
precision = precision_score(y_test, y_predicted, pos_label=None,
average='weighted')
 recall = recall_score(y_test, y_predicted, pos_label=None,
average='weighted')
 f1 = f1_score(y_test, y_predicted, pos_label=None, average='weighted')
 accuracy = accuracy_score(y_test, y_predicted)
 return accuracy, precision, recall, f1
preds, _ = learn.get_preds(ds_type=DatasetType.Test )<save_to_csv>
clf = RandomForestClassifier(n_estimators=100, max_depth=19, max_features=11,n_jobs=-1, random_state=42)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val )
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv('submission.csv', index=False )<set_options>
accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
%matplotlib inline
plt.style.use('ggplot')
 %config InlineBackend.figure_format = "retina"
InteractiveShell.ast_node_interactivity = "all"
%load_ext autoreload
%autoreload 2
%precision 2<import_modules>
clf = XGBClassifier(n_estimators=200, learning_rate=0.3, max_depth=3,n_jobs=-1, seed=42, objective="multi:softmax")
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch<load_from_csv>
clf = LGBMClassifier(n_estimators=200, learning_rate=0.3, max_depth=3,n_jobs=-1, seed=42, objective="multi:softmax")
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/sample_submission.csv' )<define_variables>
data_test = pd.read_csv(".. /input/test.csv")
 final_test = clear_dataset(data_test )
test_img = ImageList.from_df(test_df,path=data_folder/'test',folder='test')
 trfm = 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>
clf = RandomForestClassifier(n_estimators=100,max_depth=11, max_features=21,min_samples_leaf=0.001,criterion="entropy",n_jobs=-1, random_state=42)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
train_img =(ImageList.from_df(train_df, path=data_folder/'train', folder='train')
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
.transform(trfm, size=128)
.databunch(path='.', bs=64, device= torch.device('cuda:0')) 
.normalize(imagenet_stats)
 )<define_variables>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
train_img.show_batch(rows=5, figsize=(12,12))<choose_model_class>
clf = RandomForestClassifier(n_estimators=100,max_depth=11, max_features=21,min_samples_leaf=0.001,criterion="entropy",n_jobs=-1, random_state=42)
 clf.fit(x_data, y_data)
 test_preds = clf.predict(final_test.drop(["Id"], axis=1))
output = pd.DataFrame({'Id': data_test.Id,
'Cover_Type': test_preds})
 output.to_csv('rf_submission.csv', index=False )