Kaggle competition: Forest Cover Type Prediction
x_input = tf.placeholder("float32",shape=(None,32,32,3))
y_label = tf.placeholder("uint8",shape=(None))
y_onehot= tf.one_hot(y_label, depth=2)
 h1 = conv(x_input, filters=16, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h1 = conv(h1, filters=16, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h1 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h1)
 h2 = conv(h1, filters=32, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h2 = conv(h2, filters=32, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h2 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h2)
 h3 = conv(h2, filters=64, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h3 = conv(h3, filters=64, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h3 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h3)
 h4 = conv(h3, filters=128, kernel_size=3, strides=1, padding="same", batchnorm=True, activation="leakyrelu")
 h4 = conv(h4, filters=128, kernel_size=3, strides=1, padding="valid", batchnorm=True, activation="leakyrelu")
 h4 = tf.layers.MaxPooling2D(pool_size=2,strides=2 )(h4)
 h5 = tf.layers.Flatten()(h4)
 y_logits = tf.layers.Dense(2 )(h5)
 y_prob = tf.nn.softmax(y_logits)
 y_pred = tf.arg_max(y_logits, -1 )<compute_train_metric>
train_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 test_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv' )
hinge_loss = tf.losses.hinge_loss(labels=y_onehot, logits=y_logits,)
 ce_loss = tf.losses.softmax_cross_entropy(y_onehot, y_logits)
 total_loss = 0.0*hinge_loss + ce_loss
opt = tf.train.AdamOptimizer(learning_rate=0.001, ).minimize(total_loss)
 sess = tf.Session()
global_init = tf.global_variables_initializer()
local_init = tf.local_variables_initializer()
_ = sess.run([global_init, local_init] )<define_variables>
X=train_data.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_data['Cover_Type']
epochs = 100
batchsize = 32
n_train = len(x_train)
 n_batches = len(x_train)//batchsize
for e in range(epochs):
shuffled_idx0 = np.random.permutation(np.where(y_train == 0)[0])
 shuffled_idx1 = np.random.permutation(np.where(y_train == 1)[0])
 run_metrics = {"loss":[],"accuracy":[], "recall":[], "precision":[],"f1":[], "roc":[]}
print("+-"*15+"\t epoch = %0.3d \t"%e+"-+"*15)
 for i in range(n_batches):
batch0_idx = np.random.randint(0, len(shuffled_idx0), batchsize//2)
 batch0_idx = shuffled_idx0[batch0_idx]
batch1_idx = np.random.randint(0, len(shuffled_idx1), batchsize//2)
 batch1_idx = shuffled_idx1[batch1_idx]
x0_batch = x_train[batch0_idx]
x1_batch = x_train[batch1_idx]
y0_batch = y_train[batch0_idx]
y1_batch = y_train[batch1_idx]
x_batch = np.concatenate([x0_batch, x1_batch])
 x_batch = augmenter(x_batch)
 y_batch = np.concatenate([y0_batch, y1_batch])
 _, y_pred_batch, y_prob_batch, loss_batch = sess.run([opt, y_pred, y_prob, total_loss],
feed_dict={x_input: x_batch,
y_label: y_batch.reshape(-1,1)})
 acc = accuracy_score(y_batch, y_pred_batch,)
 prec = precision_score(y_batch, y_pred_batch)
 rec = recall_score(y_batch, y_pred_batch)
 f1 = f1_score(y_batch, y_pred_batch)
 roc = roc_auc_score(y_batch, y_prob_batch[:,1])
 run_metrics["loss"].append(loss_batch)
 run_metrics["accuracy"].append(acc)
 run_metrics["recall"].append(rec)
 run_metrics["precision"].append(prec)
 run_metrics["f1"].append(f1)
 run_metrics["roc"].append(roc)
 print("loss = %0.3f \t acc = %0.3f \t roc = %0.3f \t precision = %0.3f \t recall = %0.3f \t f1 = %0.3f"% 
(np.mean(run_metrics["loss"]),
np.mean(run_metrics["accuracy"]),
np.mean(run_metrics["roc"]),
np.mean(run_metrics["precision"]),
np.mean(run_metrics["recall"]),
np.mean(run_metrics["f1"])) ,end="\r")
 print("train :: loss = %0.3f :: acc = %0.3f :: roc = %0.3f :: precision = %0.3f :: recall = %0.3f :: f1 = %0.3f"% 
(np.mean(run_metrics["loss"]),
np.mean(run_metrics["accuracy"]),
np.mean(run_metrics["roc"]),
np.mean(run_metrics["precision"]),
np.mean(run_metrics["recall"]),
np.mean(run_metrics["f1"])))
 y_pred_batch, y_prob_batch, loss_batch = sess.run([y_pred,y_prob, total_loss],
feed_dict={x_input: x_val,
y_label: y_val.reshape(-1,1)})
 acc = accuracy_score(y_val, y_pred_batch,)
 prec = precision_score(y_val, y_pred_batch)
 rec = recall_score(y_val, y_pred_batch)
 f1 = f1_score(y_val, y_pred_batch)
 roc = roc_auc_score(y_val, y_prob_batch[:,1])
 print("valid :: loss = %0.3f :: acc = %0.3f :: roc = %0.3f :: precision = %0.3f :: recall = %0.3f :: f1 = %0.3f
"% 
(loss_batch,acc,roc, prec, rec,f1))<categorify>
x_train,x_val,y_train,y_val=train_test_split(X,y,random_state=40 )
saver = tf.train.Saver()
saver.save(sess, "cactus_net.ckpt" )<load_from_csv>
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(x_train,y_train )
df_test = pd.read_csv('.. /input/sample_submission.csv')
 x_test = np.array([ imread(test_dir+p)for p in df_test.id.values])
 x_test = np.array(x_test)
 batchsize = 32
n_batches =(len(x_test)// batchsize)+ batchsize
y_pred_test = []
last_iter = False
for i in tqdm(range(n_batches)) :
start = i*batchsize
end =(i+1)*batchsize
if end >= len(x_test):
if last_iter:
break
end = len(x_test)
 last_iter = True
y_prob_batch = sess.run([y_prob], feed_dict={x_input: x_test[start:end]})[0]
y_prob_batch = y_prob_batch[:,1]
y_pred_test.extend(y_prob_batch)
 y_pred_test = np.array(y_pred_test)
 df_test['has_cactus'] = y_pred_test
df_test.to_csv('cactus_net_submission.csv', index=False )<import_modules>
rfc.score(x_val,y_val )
import os
from datetime import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
import torchvision
import torchvision.transforms as transforms
from sklearn.model_selection import train_test_split<load_from_csv>
predict=rfc.predict(test_data.drop(labels=['Id'],axis=1))
print(os.listdir(".. /input"))
test_path = '.. /input/test/test/'
train_path = '.. /input/train/train/'
train_df = pd.read_csv('.. /input/train.csv' )<count_values>
submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 submission['Id']=test_data['Id']
submission.set_index('Id',inplace=True )
<categorify><EOS> 
submission.to_csv('Submission.csv' )