Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
<SOS> metric: categorizationaccuracy Kaggle data source: ghouls,-goblins,-and-ghosts...-boo!<drop_column>
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.utils.np_utils import to_categorical
from sklearn.cross_validation import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
del train_feature, test_feature
del target_question_columns, target_answer_columns 
<compute_train_metric>
data = pd.read_csv(".. /input/train.csv")
 ids = data.pop("id")
 creature = data.pop("type")
 color = data.pop("color")
 data.head()
for column in target_columns:
print(np.sum(validation_counts[column]), np.min(validation_counts[column]), np.max(validation_counts[column]), column)
 validationTG = pd.DataFrame()
for column in target_columns:
preds = validation_datas[column]
count = validation_counts[column]
count = np.where(count < 0.5, 1.0, count)
 validationTG[column] = preds / count
mean_spearmanr_correlation_score(validationTG.values, train_df[target_columns].values)
 validationTG.head()<compute_test_metric>
coords = [[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]
add_names2 = ["bone_flesh", "bone_hair", "bone_soul", "flesh_hair", "flesh_soul", "hair_soul"]
for i in range(len(coords)) :
data[add_names2[i]] = data.values[:, coords[i][0]] *data.values[:, coords[i][1]]
scal = StandardScaler()
data = pd.DataFrame(scal.fit_transform(data))
data.head()
mean_spearmanr_correlation_score(validationTG.values, train_df[target_columns].values )<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(data, creature, test_size=0.33, random_state=42 )
test_predsTG = pd.read_csv(f"{folder}sample_submission.csv")
 for column in target_columns:
preds = test_pred_datas[column]
weight = test_pred_weights[column]
test_predsTG[column] = preds / weight
test_predsTG.head()<set_options>
creature_encoder = LabelEncoder()
creature_code = creature_encoder.fit(creature)
 y_train_code = creature_encoder.transform(y_train)
 y_test_code = creature_encoder.transform(y_test )
del target_columns, input_columns
del test_pred_datas, test_pred_weights
del validation_datas, validation_counts
torch.cuda.empty_cache()
gc.collect()<define_variables>
X_train = X_train.values
X_test = X_test.values
y_train_code = to_categorical(y_train_code)
 y_test_code = to_categorical(y_test_code )
folder = '.. /input/google-quest-challenge/'
pretrained_distilbert_base_uncased = ".. /input/pretrainedbertpytorch/pretrained-bert-pytorch/distilbert-base-uncased/"
universal_sentence_encoder_path = ".. /input/universalsentenceencoderlarge4/"<set_options>
def nn_model(in_dim):
model = Sequential()
model.add(Dense(300, input_dim=in_dim, init='uniform', activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(100, init='uniform', activation='sigmoid'))
model.add(Dropout(0.4))
model.add(Dense(3, init='uniform', activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
warnings.simplefilter('ignore' )<import_modules>
np.random.seed(42)
 model = nn_model(10)
 model.fit(X_train, y_train_code, verbose= 0, nb_epoch= 500)
 model.evaluate(X_train, y_train_code), model.evaluate(X_test, y_test_code)
import torch
from torch.utils.data import TensorDataset, DataLoader, Dataset
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data.sampler import SubsetRandomSampler
from torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau, CosineAnnealingLR
from torch.nn.utils.weight_norm import weight_norm
from scipy.stats import spearmanr<import_modules>
np.random.seed(42)
 model = nn_model(10)
 y = creature_code.transform(creature)
 y = to_categorical(y)
 model.fit(data.values, y, verbose= 0, nb_epoch= 1000)
 model.evaluate(data.values, y )
import tensorflow as tf
import tensorflow_hub as hub<normalization>
test = pd.read_csv(".. /input/test.csv")
 test_ids = test.pop("id")
 test_color = test.pop("color")
 for i in range(len(coords)) :
test[add_names2[i]] = test.values[:, coords[i][0]] *test.values[:, coords[i][1]]
test = scal.transform(test)
 test = pd.DataFrame(scal.fit_transform(test))
test.head()
print("transformers:", transformers.__version__ )<import_modules>
prediction_class = model.predict_classes(test.values )
from radam import RAdam<define_variables>
result = pd.DataFrame({"id": [], "type": []})
 result.id = np.array(np.array(test_ids))
result.type = creature_encoder.inverse_transform(prediction_class )
<set_options><EOS> 
result.to_csv("nn_result.csv", index= False )