Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<string_transform>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
def split_attached_words(tweet):
words = wordninja.split(tweet)
 return" ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : split_attached_words(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : split_attached_words(x))<string_transform>
X_train = pd.read_csv('.. /input/X_train.csv')
 Y_train = pd.read_csv('.. /input/y_train.csv')
 X_pred = pd.read_csv('.. /input/X_test.csv' )
def stopwords_shortwords(tweet):
words = tweet.split()
stop_words = set(stopwords.words('english'))
words = [w for w in words if not w in stop_words]
for word in words:
if word.isalpha() :
words = [word for word in words if len(word)> 1 ]
else:
words = [word for word in words]
return" ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : stopwords_shortwords(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : stopwords_shortwords(x))<split>
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z
train_df2, validate_df = train_test_split(train_df, test_size=0.075, random_state=0)
 train_df2 = train_df2.reset_index(drop=True)
 validate_df = validate_df.reset_index(drop=True )<prepare_x_and_y>
def fe_step0(actual):
actual['norm_quat'] =(actual['orientation_X']**2 + actual['orientation_Y']**2 + actual['orientation_Z']**2 + actual['orientation_W']**2)
 actual['mod_quat'] =(actual['norm_quat'])**0.5
actual['norm_X'] = actual['orientation_X'] / actual['mod_quat']
actual['norm_Y'] = actual['orientation_Y'] / actual['mod_quat']
actual['norm_Z'] = actual['orientation_Z'] / actual['mod_quat']
actual['norm_W'] = actual['orientation_W'] / actual['mod_quat']
return actual
all_df=pd.concat([train_df,test_df])
 X_all = all_df['clean_text']
X_train = train_df2['clean_text']
y_train = train_df2['target_relabeled'].astype(int)
 X_validate= validate_df['clean_text']
y_validate = validate_df['target_relabeled'].astype(int)
 X_test = test_df['clean_text']<string_transform>
X_train = fe_step0(X_train)
 X_pred = fe_step0(X_pred)
 print(X_train.shape)
 X_train.head()
def create_tokenizer(lines):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(lines)
 return tokenizer
tokenizer = create_tokenizer(X_all)
 word_index=tokenizer.word_index
max_words = len(word_index)+ 1
print('unique words are : %d' % max_words )<categorify>
def fe_step1(actual):

 x, y, z, w = actual['norm_X'].tolist() , actual['norm_Y'].tolist() , actual['norm_Z'].tolist() , actual['norm_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 actual['euler_x'] = nx
actual['euler_y'] = ny
actual['euler_z'] = nz
return actual
def encode_data(tokenizer, max_length, data):
encoded = tokenizer.texts_to_sequences(data)
 padded = pad_sequences(encoded, maxlen=max_length, padding= 'post')
 return padded
Xtrain = encode_data(tokenizer, max_length, X_train)
 Xvalidate = encode_data(tokenizer, max_length, X_validate)
 Xtest = encode_data(tokenizer, max_length, X_test )<feature_engineering>
X_train = fe_step1(X_train)
 X_pred = fe_step1(X_pred)
 print(X_train.shape)
 X_train.head()
glove_dir = '.. /input/glove6b'
embeddings_index = {}
f = open(os.path.join(glove_dir, 'glove.6B.100d.txt'))
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))<define_variables>
def mean_abs_change(x):
return np.mean(np.abs(np.diff(x)) )
embedding_dim = 100
embedding_matrix = np.zeros(( max_words, embedding_dim))
for word, i in word_index.items() :
if i < max_words:
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector<choose_model_class>
def get_features(df):
result_df = pd.DataFrame()
for col in df.columns:
if col in ['row_id', 'series_id', 'measurement_number']:
continue
result_df['{}_mean'.format(col)] = df.groupby(['series_id'])[col].mean()
result_df['{}_max'.format(col)] = df.groupby(['series_id'])[col].max()
result_df['{}_min'.format(col)] = df.groupby(['series_id'])[col].min()
result_df['{}_sum'.format(col)] = df.groupby(['series_id'])[col].sum()
result_df['{}_mean_abs_change'.format(col)] = df.groupby(['series_id'])[col].apply(mean_abs_change)
 return result_df
model = Sequential()
model.add(Embedding(max_words, 100, input_length=max_length))
model.add(Bidirectional(LSTM(64, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)))
 model.add(Bidirectional(LSTM(64, dropout=0.2, recurrent_dropout=0.2,)))
 model.add(Dense(64, activation= 'relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()<load_pretrained>
train_df = get_features(X_train)
 pred_df = get_features(X_pred )
model.layers[0].set_weights([embedding_matrix])
 model.layers[0].trainable = True<choose_model_class>
train_df = train_df.merge(Y_train, on='series_id', how='inner' )
model.compile(loss= 'binary_crossentropy', optimizer=optimizers.Adam(lr=.0001), metrics=[ 'accuracy' ] )<train_model>
surfaces =(train_df['surface'].value_counts() ).index
model.fit(Xtrain, y_train, epochs=10, callbacks=callbacks, validation_data=(Xvalidate,y_validate))<save_to_csv>
features = []
for feature in train_df.columns.values[1:-2]:
if 'euler' in feature:
features.append(feature )
sample_submission=pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 y_pre=model.predict(Xtest)
 y_pre=np.round(y_pre ).astype(int ).reshape(3263)
 sub=pd.DataFrame({'id':sample_submission['id'].values.tolist() ,'target':y_pre})
 sub.to_csv('tweet5a.csv',index=False )<load_from_url>
label_encoder = LabelEncoder()
train_df['surface'] = label_encoder.fit_transform(train_df['surface'] )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
train_df.drop(['series_id', 'group_id'], axis=1, inplace=True )
import tensorflow as tf
import tensorflow_hub as hub
import tokenization
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model
import tensorflow.keras.optimizers as optimizers<choose_model_class>
X = train_df.drop('surface', axis=1)
 y = train_df['surface']
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<feature_engineering>
rf = RandomForestClassifier(random_state=42, n_estimators=100 )
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<categorify>
def evaluate_model(model, X, y):
score = cross_val_score(rf, X, y, cv=6)
 print(score)
 print(score.mean() )
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<categorify>
evaluate_model(rf, X, y )
train_input = bert_encode(train_df.text.values, tokenizer, max_len=128)
 test_input = bert_encode(test_df.text.values, tokenizer, max_len=128)
 train_labels = train_df.target_relabeled.values<choose_model_class>
def predict_and_submit(model, X, y, x_pred, label_encoder, file_name):
model.fit(X, y)
 predictions = label_encoder.inverse_transform(model.predict(x_pred))
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['surface'] = predictions
submission.to_csv(file_name, index=False )
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(optimizers.Adam(lr=2e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
predict_and_submit(rf, X, y, pred_df, label_encoder, 'submitWithEulers01.csv' )