<jupyter_start><jupyter_text>Import Data<jupyter_code># Import csv data from drive
import pandas as pd

train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
sample_submission = pd.read_csv("sample_submission.csv")

# Helper functions
import string
import re
import nltk

nltk.download('wordnet')
nltk.download('stopwords')
lemmatizer = nltk.stem.WordNetLemmatizer()
stopword = nltk.corpus.stopwords.words('english')

def clean_data(text):
  # Removing urls
  text = re.sub(r"http\S+", "", text).strip()
  # Removing @username
  text = re.sub(r"@\S+", "", text).strip()
  # Removing non-ASCII
  text = text.encode("ascii", "ignore").decode().strip()
  # Removing punctuation marks
  no_punct = [word for word in text if word not in string.punctuation]
  text = "".join(no_punct).strip()
  # Lower the text
  text = text.lower()
  # Lemmatization
  lemm_text = [lemmatizer.lemmatize(word) for word in text.split()]
  text = " ".join(lemm_text).strip()
  # Removing stopwords
  stopwords = [word for word in text.split() if word not in stopword]
  text = " ".join(stopwords)
  # remove additional space from string 
  text = re.sub(' +', ' ', text)
  return text<jupyter_output>[nltk_data] Downloading package wordnet to /root/nltk_data...
[nltk_data]   Unzipping corpora/wordnet.zip.
[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Unzipping corpora/stopwords.zip.<jupyter_text>Cleaning the data<jupyter_code># Clean the data
train["text"] = train.apply(lambda x: clean_data(x["text"]), axis=1)
# Shuffle the data
train_shuffled = train.sample(frac=1)
train_shuffled.head()
# Split the data
from sklearn.model_selection import train_test_split
train_sentences, val_sentences, train_labels, val_labels = train_test_split(train_shuffled["text"].to_numpy(),
                                                                            train_shuffled["target"].to_numpy(),
                                                                            test_size=0.1)

print(train_sentences.shape, val_sentences.shape, train_labels.shape, val_labels.shape)
train_sentences[:10], train_labels<jupyter_output><empty_output><jupyter_text>Model Experiments<jupyter_code>import keras.backend as K

def f1_metric(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    recall = true_positives / (possible_positives + K.epsilon())
    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
    return f1_val
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers

# Create USE layer
sentence_encoder_layer = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4",
                                        input_shape=[],
                                        dtype=tf.string,
                                        trainable=False,
                                        name="USE")
# Build the model
model = tf.keras.Sequential([
    sentence_encoder_layer,
    layers.Dense(128, activation="relu"),
    layers.Dense(1, activation="sigmoid")
], name="model_1_USE")

# Comile the model
model.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy", f1_metric])
# Fit the model
history = model.fit(train_sentences,
                        train_labels,
                        epochs=10,
                        validation_data=(val_sentences, val_labels))
model.evaluate(val_sentences, val_labels)
## Generating submit csv
test_sentences = test.apply(lambda x: clean_data(x["text"]), axis=1).to_numpy()
preds = tf.squeeze(tf.round(model.predict(test_sentences)))
preds = tf.cast(preds, dtype=tf.int32)
sample_submission["target"] = preds
sample_submission.to_csv("submit.csv", index=False)
sample_submission["target"].value_counts()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup