Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<filter>
!pip install seaborn
pseudo_label_df = pseudo_df[pseudo_df['has_cactus'] > 0.99]
pseudo_label_df.append(pseudo_df[pseudo_df['has_cactus'] < 0.01])
 pseudo_label_df.shape<data_type_conversions>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from xgboost import XGBClassifier
pseudo_label_df['has_cactus'] = pseudo_label_df['has_cactus'].astype(np.int64 )<create_dataframe>
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' )
label_src = ImageList.from_df(pseudo_label_df, path = data_folder/'test', folder='test', cols='id' )<choose_model_class>
train.nunique()
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy], callback_fns=callbacks ).mixup().to_fp16()<train_model>
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()
lr = 1e-02
learn.fit_one_cycle(3, slice(lr), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )<train_model>
df_viz = train.iloc[:, 0:15]
df_viz = df_viz.drop(['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3',
'Wilderness_Area4'], axis = 1)
 df_viz.head()
learn.unfreeze()
learn.fit_one_cycle(16, slice(1e-05, 1e-03), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )<categorify>
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.to_fp32()<save_to_csv>
train = add_feature(train)
 test = add_feature(test )
preds,_ = learn.TTA(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv('submission.csv', index=False )<load_from_csv>
X_train = train.drop(['Id','Cover_Type','soil_type','Wilderness_Area'], axis = 1)
 y_train = train.Cover_Type
X_test = test.drop(['Id'], axis = 1 )
train_df = pd.read_csv(".. /input/train.csv")
 train_df.head()<train_model>
%%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_ )
print(f"Train Size: {len(os.listdir('.. /input/train/train')) }")
 print(f"Test Size: {len(os.listdir('.. /input/test/test')) }" )<define_variables>
%%time
rf_pipe = Pipeline(
 steps = [ 
('classifier', RandomForestClassifier(n_estimators=500))
] 
)
 param_grid = {
'classifier__min_samples_leaf': [2, 3, 4, 8],
'classifier__max_depth': [30, 32, 34],
}
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_ )
train_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'<categorify>
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))
class CreateDataset(Dataset):
def __init__(self, df_data, data_dir = './', transform=None):
super().__init__()
self.df = df_data.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name,label = self.df[index]
img_path = os.path.join(self.data_dir, img_name)
 image = cv2.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label<create_dataframe>
rf = rf_grid_search.best_estimator_.steps[0][1]
transforms_train = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(10),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 train_data = CreateDataset(df_data=train_df, data_dir=train_path, transform=transforms_train )<define_variables>
%%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_ )
batch_size = 64
valid_size = 0.2
num_train = len(train_data)
 indices = list(range(num_train))
np.random.shuffle(indices)
 split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler )<load_from_csv>
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))
transforms_test = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 test_data = CreateDataset(df_data=sample_sub, data_dir=test_path, transform=transforms_test)
 test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False )<define_variables>
final_model = xgd_grid_search.best_estimator_.steps[0][1]
classes = [ 'No Cactus','Cactus']<choose_model_class>
final_model.fit(X_train, y_train)
 y_pred = final_model.predict(X_test )
class CNN(nn.Module):
def __init__(self):
super(CNN, self ).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
 self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
 self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
 self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
 self.pool = nn.MaxPool2d(2, 2)
 self.fc1 = nn.Linear(128*2*2, 512)
 self.fc2 = nn.Linear(512, 2)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = self.pool(F.relu(self.conv3(x)))
 x = self.pool(F.relu(self.conv4(x)))
 x = x.view(-1, 128 * 2 * 2)
 x = self.dropout(x)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = self.fc2(x)
 return x<train_model>
Counter(y_pred )
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...' )<choose_model_class>
submission = pd.DataFrame({'Id': test.Id,
'Cover_Type': y_pred})
 submission.head()
model = CNN()
print(model)
 if train_on_gpu: model.cuda()<choose_model_class>
submission.to_csv('submission.csv', index=False )
<train_model><EOS> 
submission.to_csv('submission.csv', index=False )