<jupyter_start><jupyter_text>Download data from https://www.kaggle.com/crowdflower/twitter-airline-sentiment<jupyter_code>data_frame = pd.read_csv('data/Tweets.csv')
print("{} Tweets in dataset".format(len(data_frame)))
data_frame.head()
tweets_text = data_frame['text']
tweets_sentiment = data_frame['airline_sentiment']
print("Tweet: {}".format(tweets_text[18]))
print("Sentiment: {}".format(tweets_sentiment[18]))

print ""
print("Tweet: {}".format(tweets_text[17]))
print("Sentiment: {}".format(tweets_sentiment[17]))
print("Tweet: {}".format(tweets_text[3]))
print("Sentiment: {}".format(tweets_sentiment[3]))
from keras.preprocessing.text import Tokenizer
docs = tweets_text
vocab_size = 5000
t = Tokenizer(nb_words=vocab_size)
t.fit_on_texts(docs)
sequences = t.texts_to_sequences(docs)
print(sequences[0])
print(sequences[10])
max_review_length = 20
X = sequence.pad_sequences(sequences, maxlen=max_review_length)
print(X[10,...])
y_mapping = {'negative':[1, 0, 0], 'neutral':[0, 1, 0], 'positive':[0, 0, 1]}
y_list = [y_mapping[sentiment.lower()] for sentiment in tweets_sentiment]
y = np.array(y_list)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/4., random_state=1)
print(X_train.shape)
print(y_train.shape)
print(y_train.shape)
print(X_train[0:])
embedding_vecor_length = 64
model = Sequential()
model.add(Embedding(vocab_size, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(128, return_sequences=True))
model.add(LSTM(64, dropout=0.2))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, nb_epoch=5, batch_size=64,  validation_data=(X_test, y_test))
# Final evaluation of the model
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

y_train_pred_class = np.argmax(y_train_pred, axis=1)
y_test_pred_class = np.argmax(y_test_pred, axis=1)
y_train_class = np.argmax(y_train, axis=1)
y_test_class = np.argmax(y_test, axis=1)

train_accuracy = sklearn.metrics.accuracy_score(y_train_class, y_train_pred_class)
test_accuracy = sklearn.metrics.accuracy_score(y_test_class, y_test_pred_class)
print("Train Accuracy {}%".format(train_accuracy*100.0))
print("Test Accuracy {}%".format(test_accuracy*100))

cf_matrix = confusion_matrix(y_test_class, y_test_pred_class)
print("Confusion Matrix")
print(cf_matrix)
# adapted from:
# https://github.com/scikit-learn/scikit-learn/blob/master/examples/model_selection/plot_confusion_matrix.py
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()
    
plot_confusion_matrix(cf_matrix, classes=["Negative", "Neutral", "Positive"],
                      title='Confusion matrix, normalization', normalize=True)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1468459152016546, 'fraction_numerical': 0.016804550155118926, 'mean_word_length': 5.230273752012883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '481748', 'license': 'MIT', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1380, 'n_words': 234, 'path': 'Sec 6/6.3 Notebook.ipynb', 'repo_name': 'PacktPublishing/Machine-learning-with-Sci-kit-Learn-and-Tensorflow-V-', 'size': '3868'}