Kaggle competition: Jigsaw Unintended Bias in Toxicity Classification
accuracy_score(Y_train,votingC.predict(X_train))<import_modules>
EMBEDDING_FILES = [
'.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec',
'.. /input/glove840b300dtxt/glove.840B.300d.txt'
]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<load_from_csv>
NUM_MODELS = 2
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
EPOCHS = 4
MAX_LEN = 220
IDENTITY_COLUMNS = [
'male', 'female', 'homosexual_gay_or_lesbian', 'christian', 'jewish',
'muslim', 'black', 'white', 'psychiatric_or_mental_illness'
]
AUX_COLUMNS = ['target', 'severe_toxicity', 'obscene', 'identity_attack', 'insult', 'threat']
TEXT_COLUMN = 'comment_text'
TARGET_COLUMN = 'target'
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'
ds = pd.read_csv("/kaggle/input/titanic/train.csv" )<prepare_x_and_y>
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32')
 def load_embeddings(path):
with open(path)as f:
return dict(get_coefs(*line.strip().split(' ')) for line in f)
 def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
pass
return embedding_matrix
def build_model(embedding_matrix, num_aux_targets):
words = Input(shape=(None,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 hidden = concatenate([
GlobalMaxPooling1D()(x),
GlobalAveragePooling1D()(x),
])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 aux_result = Dense(num_aux_targets, activation='sigmoid' )(hidden)
 model = Model(inputs=words, outputs=[result, aux_result])
 model.compile(loss='binary_crossentropy', optimizer='adam')
 return model
Y = ds.Survived
X = ds.drop(['PassengerId','Name','Survived'],axis = 1 )<count_missing_values>
train_df = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv')
 test_df = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv')
print(X.isnull().sum() )<categorify>
x_train = train_df[TEXT_COLUMN].astype(str)
 y_train = train_df[TARGET_COLUMN].values
y_aux_train = train_df[AUX_COLUMNS].values
x_test = test_df[TEXT_COLUMN].astype(str )
X_temp = X
Sex_Embarked = {"Sex":{"male": 1.,"female": 0.},
"Embarked":{"S": 0.,"C": 1.,"Q": 2.}}
X_temp = X_temp.replace(Sex_Embarked, inplace=False)
 CT = ColumnTransformer(transformers = [('encoder',OrdinalEncoder() ,['Ticket'])],remainder = 'passthrough')
 X_temp = pd.DataFrame(CT.fit_transform(X_temp.drop(['Cabin'],axis = 1,inplace = False ).dropna(subset = ['Age','Embarked'],axis =0 ,inplace = False)) ,
columns=[ 'Ticket', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare','Embarked'],
dtype = 'float' )<categorify>
for column in IDENTITY_COLUMNS + [TARGET_COLUMN]:
train_df[column] = np.where(train_df[column] >= 0.5, True, False )
imputer = SimpleImputer(missing_values=np.nan, strategy = 'mean')
 ds.Age = imputer.fit_transform(ds.Age.values.reshape(-1,1))
X.Age = imputer.fit_transform(X.Age.values.reshape(-1,1))<categorify>
tokenizer = text.Tokenizer(filters=CHARS_TO_REMOVE)
 tokenizer.fit_on_texts(list(x_train)+ list(x_test))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN)
 sample_weights = np.ones(len(x_train), dtype=np.float32)
 sample_weights += train_df[IDENTITY_COLUMNS].sum(axis=1)
 sample_weights += train_df[TARGET_COLUMN] *(~train_df[IDENTITY_COLUMNS] ).sum(axis=1)
 sample_weights +=(~train_df[TARGET_COLUMN])* train_df[IDENTITY_COLUMNS].sum(axis=1)* 5
sample_weights /= sample_weights.mean()
ds.Embarked = SimpleImputer(missing_values=np.nan, strategy = 'most_frequent' ).fit_transform(ds.Embarked.values.reshape(-1,1))
X.Embarked = SimpleImputer(missing_values=np.nan, strategy = 'most_frequent' ).fit_transform(X.Embarked.values.reshape(-1,1))<drop_column>
embedding_matrix = np.concatenate(
 [build_matrix(tokenizer.word_index, f)for f in EMBEDDING_FILES], axis=-1)
X_Cabins = X.Cabin.dropna().str.contains('C',regex = False)
 X_Cabins = X.Cabin[X_Cabins.loc[X_Cabins == True].index]
print(X_Cabins )<drop_column>
checkpoint_predictions = []
weights = []
X = X.drop(['Cabin'],axis = 1)
 X<categorify>
for model_idx in range(NUM_MODELS):
model = build_model(embedding_matrix, y_aux_train.shape[-1])
 for global_epoch in range(EPOCHS):
model.fit(
 x_train,
[y_train, y_aux_train],
batch_size=BATCH_SIZE,
epochs=1,
verbose=2,
sample_weight=[sample_weights.values, np.ones_like(sample_weights)],
callbacks=[
LearningRateScheduler(lambda _: 1e-3 *(0.55 ** global_epoch))
] 
)
 checkpoint_predictions.append(model.predict(x_test, batch_size=2048)[0].flatten())
 weights.append(2 ** global_epoch )
X_temp = X
Sex_Embarked = {"Sex":{"male": 1.,"female": 0.},
"Embarked":{"S": 0.,"C": 1.,"Q": 2.},
"Deck":{"A": 1.,"B": 2.,"C": 3.,
"D": 4.,"E": 5.,"F": 6.,
"G": 7.,"T": 8.}}
X_temp = X_temp.replace(Sex_Embarked, inplace=False)
 X_temp = pd.DataFrame(CT.fit_transform(X_temp),
columns=[ 'Ticket', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare','Embarked','Deck'],
dtype = 'float' )<drop_column>
predictions = np.average(checkpoint_predictions, weights=weights, axis=0)
<drop_column><EOS> 
submission = pd.DataFrame.from_dict({
'id': test_df.id,
'prediction': predictions
})
 submission.to_csv('submission.csv', index=False )