<jupyter_start><jupyter_text>Problem  We attempt to classify real youtube (artist videos) comments as spam or not spam. Using datasets: youtube-spam-dataset<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score, KFold
from sklearn.decomposition import PCA
import keras
from keras import Sequential
from keras.layers import Dense
from keras.losses import *
from keras.optimizers import *
from keras.preprocessing.text import Tokenizer
from sklearn.feature_extraction.text import CountVectorizer
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from keras.callbacks import TensorBoard
from sklearn.metrics import accuracy_score, log_loss
from keras.metrics import categorical_crossentropy
path = "/Users/aaditkapoor/Desktop/youtube-spam-dataset"
data1 = pd.read_csv(path+"/1.csv")
data2 = pd.read_csv(path+"/2.csv")
data3 = pd.read_csv(path+"/3.csv")
data4 = pd.read_csv(path+"/4.csv")

# The combined data
data = pd.concat([data1, data2, data3, data4])
data.shape
data1.shape
data2.shape
data3.shape
data4.shape
data.columns
# Removing useless columns
data.drop(columns=['COMMENT_ID','AUTHOR','DATE'], axis=1, inplace=True)
data.head()<jupyter_output><empty_output><jupyter_text>- 1 is positive(spam) and 0 is negative(not spam)<jupyter_code>features = data.CONTENT.values
labels = data.CLASS.values
labels = to_categorical(labels)
# Vectorizing data
tokenizer = Tokenizer()
tokenizer.fit_on_texts(features)
features = tokenizer.texts_to_matrix(features, mode='tfidf')
features.shape # 4377 as input_dim we can also reduce it.
pca = PCA()
pca.fit(features)
# Plotting
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel("number of features")
plt.ylabel("accuracy (feature use %)")
# We will test pca afterwards on text data
# TODO
pca = PCA(n_components=1400)
features = pca.fit_transform(features)
# Creating custom callback
class StopAtHundered(keras.callbacks.Callback):
    def on_epoch_end(self, epoch,logs={}):
        if logs.get('acc') == 1.0:
            print ("Stopped")
            print ("Accuracy is:", logs.get('acc'))
            model.is_training = False



custom = StopAtHundered()
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, random_state=3, shuffle=True)
# Building
model = Sequential()
model.add(Dense(100, activation="relu", input_dim=features_train.shape[1]))
model.add(Dense(100, activation="relu"))
model.add(Dense(100, activation="relu"))
model.add(Dense(2, activation="softmax"))
model.compile(optimizer="adam", loss=keras.losses.categorical_crossentropy, metrics=['acc'])
tensorboard = TensorBoard(log_dir='./youtube_spam_ham_logs')
# Fitting
history = model.fit(features_train, labels_train, epochs=1000, batch_size=512, callbacks=[tensorboard, custom], validation_data=(features_test, labels_test))
predictions = model.predict(features_test)
predictions = np.argmax(predictions, axis=1)
labels_test = np.argmax(labels_test, axis=1)
print ("The accuracy is: ", accuracy_score(labels_test, predictions))
print ("The log loss is: ", log_loss(labels_test, predictions))
model.save("youtube_spam_ham.h5")
# Loss
plt.plot(history.history['loss'], "r")
plt.plot(history.history['val_loss'],"b")
# acc
plt.plot(history.history['acc'], "r")
plt.plot(history.history['val_acc'],"b")
model.summary()
len(model.get_weights())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12496384148105294, 'fraction_numerical': 0.014752675730402082, 'mean_word_length': 6.805869074492099, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '495406', 'license': 'Apache-2.0', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1141, 'n_words': 256, 'path': 'youtube-spam-ham.ipynb', 'repo_name': 'akapoorx00/machinelearning-stuff', 'size': '3457'}