<jupyter_start><jupyter_text>dataset https://www.kaggle.com/gdberrio/spooky-authors-csvPredict author of sentence (3 different authors)<jupyter_code>from string import punctuation

import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.preprocessing import LabelEncoder

from nltk.corpus import stopwords
from nltk import wordpunct_tokenize, word_tokenize
from nltk.stem import PorterStemmer

from nltk.tokenize import WordPunctTokenizer
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
maxlen = 100  # sentences with length > 100 'words' will be cropped
df = pd.read_csv("../datasets/authors.csv")
df.head()
df['author'].value_counts()
texts = df['text']
le = LabelEncoder().fit(df['author'])
authors = le.transform(df['author'])
np.bincount(authors)
labels = to_categorical(authors)
print(authors[:3])
print(labels[:3])
# lowercase, punct padded with spaces
texts_lower_punct = []
for i in texts:
    texts_lower_punct.append(' '.join([x for x in word_tokenize(i.lower())]))
texts_lower_punct[1000]
X_train, X_test, y_train, y_test = train_test_split(texts_lower_punct, labels, stratify=labels, test_size=0.2)
print(len(X_train), y_test.shape)
tokenizer = Tokenizer(filters='\t\n')  # save the punctuation
tokenizer.fit_on_texts(X_train)

wi = tokenizer.word_index
wir = dict([(v, k) for k,v in wi.items()])

X_train = tokenizer.texts_to_sequences(X_train)
X_train = pad_sequences(X_train, maxlen = maxlen)
[wir.get(x, x) for x in X_train[10]]
from tensorflow.keras import layers, models, callbacks
from sklearn.metrics import log_loss
max_features = max(wi.values()) + 1
model = models.Sequential()

model.add(layers.Embedding(max_features, 16, input_length = maxlen))
# model.add(layers.LSTM(32, activation='relu', dropout=0.2))
# model.add(layers.Flatten())
model.add(layers.GlobalAveragePooling1D())
model.add(layers.Dropout(0.3))

model.add(layers.Dense(3, activation = 'softmax'))
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['acc'])
model.summary()
cb = [callbacks.EarlyStopping(monitor='val_loss', patience=5), 
      callbacks.ModelCheckpoint('../saved_models/authors_fast_text.hdf5', monitor='val_loss', save_best_only=True)]
history = model.fit(X_train, y_train, epochs = 100, batch_size = 64, validation_split=0.2, 
                   callbacks=cb)
# dropout = 0.3
model.load_weights('../saved_models/authors_fast_text.hdf5')
X_test_check = tokenizer.texts_to_sequences(X_test)
X_test_check = pad_sequences(X_test_check, maxlen = maxlen)
preds = model.predict_proba(X_test_check)
print(log_loss(y_test, preds))
print(model.evaluate(X_test_check, y_test))<jupyter_output>0.3814878599256562
3916/3916 [==============================] - 0s 71us/sample - loss: 0.3815 - acc: 0.8463
[0.38148786466991086, 0.8462717]
{'fraction_non_alphanumeric': 0.13209425821440424, 'fraction_numerical': 0.0345170925987388, 'mean_word_length': 6.669211195928753, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '414185', 'license': 'MIT', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1059, 'n_words': 214, 'path': 'notebooks/authors NN sort of fasttext.ipynb', 'repo_name': 'Levich125/ml_dl_tests', 'size': '3013'}