Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<split>
%matplotlib inline
train, valid = train_test_split(train_df, stratify=train_df['has_cactus'], test_size=0.2)
 train_tf = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
])
 valid_tf = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
])
 train_data = CactusDataset(
 labels=train,
data_dir=train_path,
transform=train_tf, 
)
 valid_data = CactusDataset(
 labels=valid,
data_dir=train_path,
transform=valid_tf,
)<choose_model_class>
train=pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 test=pd.read_csv('.. /input/forest-cover-type-prediction/test.csv' )
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.pool = nn.MaxPool2d(2, 2)
 self.conv1 = nn.Conv2d(
 in_channels=3,
out_channels=16,
kernel_size=3,
stride=1,
padding=1 
)
 self.conv2 = nn.Conv2d(
 in_channels=16,
out_channels=32,
kernel_size=3,
stride=1,
padding=1 
)
 self.conv3 = nn.Conv2d(
 in_channels=32,
out_channels=64,
kernel_size=3,
stride=1,
padding=1 
)
 self.fc1 = nn.Linear(64 * 8 * 8, 512)
 self.output = nn.Linear(512, 2)
 def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool(F.relu(self.conv2(x)))
 x = self.pool(F.relu(self.conv3(x)))
 x = x.view(-1, 64 * 8 * 8)
 x = F.relu(self.fc1(x))
x = self.output(x)
 return x<set_options>
train.Slope=np.sqrt(train.Slope+1 )
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 n_epochs = 20
n_classes = 2
batch_size = 50
learning_rate = 0.001<choose_model_class>
test.Slope=np.sqrt(test.Slope+1 )
model = Net().to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=learning_rate )<load_pretrained>
train['dist_hydr']=np.sqrt(train['Vertical_Distance_To_Hydrology']**2 + train['Horizontal_Distance_To_Hydrology']**2)
 test['dist_hydr']=np.sqrt(test['Vertical_Distance_To_Hydrology']**2 + test['Horizontal_Distance_To_Hydrology']**2 )
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
 valid_loader = DataLoader(dataset=valid_data, batch_size=batch_size, shuffle=False )<train_model>
train['dist_hydr']=np.sqrt(1+train['dist_hydr'])
 test['dist_hydr']=np.sqrt(1+test['dist_hydr'])
steps = len(train_loader)
 results = []
for epoch in range(n_epochs):
t_start = datetime.now()
model.train()
epoch_loss = 0
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_loss = epoch_loss / len(train_loader)
 t_elapsed = datetime.now() - t_start
model.eval()
correct = 0
total = 0
with torch.no_grad() :
for images, labels in valid_loader:
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).sum().item()
epoch_acc = correct / total
results.append({'Epoch': epoch+1, 'Loss': epoch_loss, 'Accuracy': epoch_acc})
 print('Epoch [{:0>2d}/{}], Loss: {:.4f}, Accuracy: {:.4f}, Epoch Time: {}'.format(
 epoch+1, n_epochs, epoch_loss, epoch_acc, t_elapsed, 
))<load_from_csv>
train.Horizontal_Distance_To_Hydrology=np.sqrt(1+train.Horizontal_Distance_To_Hydrology)
 test.Horizontal_Distance_To_Hydrology=np.sqrt(1+test.Horizontal_Distance_To_Hydrology )
result_df = pd.read_csv('.. /input/sample_submission.csv')
 test_data = CactusDataset(
 labels=result_df,
data_dir=test_path,
transform=valid_tf 
)
 test_loader = DataLoader(
 dataset=test_data,
batch_size=batch_size,
shuffle=False
)<save_to_csv>
train['Horizontal_Distance_To_Fire_Points']=np.log(1+train.Horizontal_Distance_To_Fire_Points)
 test['Horizontal_Distance_To_Fire_Points']=np.log(1+test.Horizontal_Distance_To_Fire_Points)
model.eval()
predictions = []
for batch_i,(data, target)in enumerate(test_loader):
data = data.to(device)
 target = target.to(device)
 output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
predictions.append(i)
 result_df['has_cactus'] = predictions
result_df.to_csv('submission.csv', index=False )<import_modules>
sd=StandardScaler()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import scipy
import cv2
import keras<import_modules>
df_train=train.iloc[:,1:11]
df_train['dist_hydr']=train['dist_hydr']
df_train.info()
import random<load_from_csv>
df_test=test.iloc[:,1:11]
df_test['dist_hydr']=test['dist_hydr']
df_test.info()
train_data = pd.read_csv('.. /input/train.csv' )<count_values>
sd.fit(df_train)
 df_train=sd.transform(df_train )
train_data.has_cactus.value_counts()<define_variables>
train['dist_hydr']=df_train[:,10]
positive_examples = train_data[train_data.has_cactus==1]
negative_examples = train_data[train_data.has_cactus==0]<define_variables>
df_test=sd.transform(df_test)
 test.iloc[:,1:11]=df_test[:,0:10]
test['dist_hydr']=df_test[:,10]
def augment_img(img):
batch_input = []
batch_input += [img]
batch_input += [img[::-1, :, :]]
batch_input += [img[:, ::-1, :]]
batch_input += [np.rot90(img)]
temp_img = np.zeros_like(img)
 temp_img[:28, :, :] = img[4:, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, :28, :] = img[:, 4:, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[4:, :, :] = img[:28, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, 4:, :] = img[:, :28, :]
batch_input += [temp_img]
batch_input += [cv2.resize(img[2:30, 2:30, :],(32, 32)) ]
batch_input += [scipy.ndimage.interpolation.rotate(img, 10, reshape=False)]
batch_input += [scipy.ndimage.interpolation.rotate(img, 5, reshape=False)]
return batch_input<define_search_model>
train.drop(columns=['Id'],axis=1,inplace=True )
def image_generator(batch_size = 8, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
indexes = positive_examples.index.tolist()
neg_indexes = negative_examples.index.tolist()
if shuffle:
np.random.shuffle(indexes)
 np.random.shuffle(neg_indexes)
 N = int(len(indexes)/(batch_size/2))
neg_N = int(len(neg_indexes)/(batch_size/2))
j = 0
for i in range(N):
current_indexes = indexes[i*(batch_size//2):(i+1)*(batch_size//2)]
current_neg_indexes = neg_indexes[j*(batch_size//2):(j+1)*(batch_size//2)]
j =(j + 1)% neg_N
batch_input = []
batch_output = []
for ind in range(len(current_indexes)) :
index = current_indexes[ind]
neg_index = current_neg_indexes[ind]
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input.extend(augment_img(img))
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
neg_img = mpimg.imread('.. /input/train/train/' + train_data.id[neg_index])
 batch_input.extend(augment_img(neg_img))
for _ in range(11):
batch_output += [train_data.has_cactus[neg_index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))<choose_model_class>
Id=test['Id']
test.drop(columns=['Id'],axis=1,inplace=True )
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,(5, 5), input_shape=(32, 32, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(64,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(512,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(100))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Dense(1, activation='sigmoid'))<choose_model_class>
print(train_corr.loc['Soil_Type38','Soil_Type39'])
 print(train_corr.loc['Soil_Type38','Wilderness_Area1'])
 print(train_corr.loc['Soil_Type39','Wilderness_Area1'] )
opt = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )<load_pretrained>
train['soil_type38,39']=train['Soil_Type38']+train['Soil_Type39']
train['soil_38_Wilde_area_1']=train['Soil_Type38']+train['Wilderness_Area1']
train['soil_39_Wilde_area_1']=train['Soil_Type39']+train['Wilderness_Area1']
test['soil_type38,39']=test['Soil_Type38']+test['Soil_Type39']
test['soil_38_Wilde_area_1']=test['Soil_Type38']+test['Wilderness_Area1']
test['soil_39_Wilde_area_1']=test['Soil_Type39']+test['Wilderness_Area1']
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
X=train.drop(columns='Cover_Type',axis=1)
 y=train['Cover_Type']
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[::-1, :, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
x_train,x_valid,y_train,y_valid=train_test_split(X,y,test_size=0.25 )
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[:, ::-1, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<define_variables>
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.naive_bayes import GaussianNB
test_files = os.listdir('.. /input/test/test/' )<predict_on_test>
clf_accuracy=[]
batch = 40
all_out = []
for i in range(int(4000/batch)) :
images = []
for j in range(batch):
img = mpimg.imread('.. /input/test/test/'+test_files[i*batch + j])
 images += [img]
out = model.predict(np.array(images))
all_out += [out]<create_dataframe>
lg=LogisticRegression(max_iter=1000)
 lg.fit(x_train,y_train)
 pred=lg.predict(x_valid)
 clf_accuracy.append(accuracy_score(y_valid,pred))
print(accuracy_score(y_valid,pred))
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )<save_to_csv>
model=SVC(kernel='rbf')
 model.fit(x_train,y_train)
 pred=(model.predict(x_valid))
clf_accuracy.append(accuracy_score(y_valid,pred))
print(accuracy_score(y_valid,pred))
sub_file.to_csv('sample_submission.csv', index=False )<import_modules>
rand=RandomForestClassifier()
rand.fit(x_train,y_train)
 pred=rand.predict(x_valid)
 clf_accuracy.append(accuracy_score(y_valid,pred))
print(accuracy_score(y_valid,pred))
from fastai.vision import *<load_from_csv>
xgb=XGBClassifier(max_depth=7)
 xgb.fit(x_train,y_train)
 pred=xgb.predict(x_valid)
 clf_accuracy.append(accuracy_score(y_valid,pred))
print(accuracy_score(y_valid,pred))
path = Path(".. /input/")
 tfms = get_transforms(flip_vert=True, do_flip=True)
 data = ImageDataBunch.from_csv(path=path, csv_labels='train.csv', folder='train/train',
test='test/test',ds_tfms = tfms)
 data<choose_model_class>
nb=GaussianNB()
nb.fit(x_train,y_train)
 pred=nb.predict(x_valid)
 clf_accuracy.append(accuracy_score(y_valid,pred))
print(accuracy_score(y_valid,pred))
learn = cnn_learner(data, models.resnet50, model_dir="/tmp/model/",
metrics = [error_rate, accuracy] )<find_best_params>
classifier_list=['log_regression','knn','svm','rforest','xgboost','nbayes']
learn.lr_find()<train_model>
rand=RandomForestClassifier()
rand.fit(X,y)
 pred=rand.predict(test )
<train_model><EOS> 
leela_submission=pd.DataFrame({'Id': Id,'Cover_Type':pred})
 leela_submission.to_csv('leela_submision.csv',index=False )