<jupyter_start><jupyter_text>Reuters_A - Newswire Topic ClassificationThe Reuters dataset contains 11,228 newswires from Reuters, labeled over 46 topics.   As with the IMDB dataset, each wire is encoded as a sequence of numbers.   Our task is to create a neural network that can classify which topic the piece of text came from. We will use an embedding layer to input the data. Approach: Sequence-of-words-integers directly feed into CNN<jupyter_code>import numpy as np
from keras.datasets import reuters
from keras.preprocessing.sequence import pad_sequences
maxlen=100
vocab_size=1000
(x_train, y_train), (x_test, y_test) = reuters.load_data(path="reuters.npz",
                                                         num_words=vocab_size, # use top 1000 frequent words
                                                         skip_top=5, # skip top frequency word
                                                         maxlen=None,
                                                         test_split=0.2,
                                                         seed=113,
                                                         start_char=1,
                                                         oov_char=2,
                                                         index_from=3)
x_train.shape
x_test.shape
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test =  pad_sequences(x_test, maxlen=maxlen)
x_train.shape
x_train
x_train.max()
x_test.shape
y_test
from keras.models import Sequential
from keras.layers import Flatten, Dense, Embedding, Dropout
from keras.layers.convolutional import Convolution1D, MaxPooling1D
model = Sequential()
model.add(Embedding(1000, 8, input_length=maxlen))
model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu')) #
model.add(Flatten())
model.add(Dense(512, activation='relu')) #
model.add(Dropout(0.5)) #
model.add(Dense(46, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()
x_train.shape
x_train[0]
y_train.shape
y_test.shape
y_train
from keras.utils import np_utils # one hot encode the y-label
y_train = np_utils.to_categorical(y_train, 46)
y_test = np_utils.to_categorical(y_test, 46)
y_train.shape
y_train
history = model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=0.1)
score = model.evaluate(x_test, y_test)
score
print('The test accuracy is:', round(score[1]*100,2))<jupyter_output>The test accuracy is: 65.49
{'fraction_non_alphanumeric': 0.08500400962309543, 'fraction_numerical': 0.026062550120288693, 'mean_word_length': 2.524011299435028, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '591007', 'license': 'MIT', 'n_tokens_mistral': 770, 'n_tokens_neox': 714, 'n_words': 188, 'path': 'Reuters_news_topic_classify_A.ipynb', 'repo_name': 'jsrpy/Sentiment_Analysis_WordEmbedding', 'size': '2494'}