[PYTHON] Negative / Positive Analysis 3 Twitter Negative / Positive Analysis (2)

Aidemy 2020/10/30

Introduction

Hello, it is Yope! I am a liberal arts student, but I was interested in the possibilities of AI, so I went to the AI-specialized school "Aidemy" to study. I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This is the third post in the negative-positive analysis. Nice to meet you.

What to learn this time ・ Implementation of LSTM model ・ Negative / positive analysis using the constructed model

Learn with LSTM

LSTM ・ (Review) LSTM is a kind of RNN and can store the data entered at the beginning. That is, it is an RNN model capable of long-term memory.

Creating a training dataset

-Use the data created in __Chapter2 as a training data set __. The training data X is created from __words quantified __ (features), and the teacher label y is created from __negative / positive quantified __ (label). -By multiplying the length of features by 0.8, set the length that is the reference for __division __. Based on this, the training data is created with the "~ 0.8" part of features as __ "train_X" __ and the rest as __ "val_X" __. Similarly, create a teacher label with __ "~ 0.8" __ part of labels as __ "train_y" __ and the rest as __ "val_y" __. -In addition, __half __ of val_X and val_y shall be __ "test_X" and "test_y" __ for accuracy evaluation. (The other half is used as validation data)

-Data to be trained by LSTM needs to be transformed into __ (number of samples, sequence length, number of dimensions) __. __ Sequence length __ refers to the number of data __ that the model learns in each step. This time, one sentence is learned in one step, so it becomes __ "1" __. -For training data X, shape conversion is performed with __ "np.reshape ()" __. The transformation data itself is passed to the first argument, and the content of the transformation, that is, the thing corresponding to the above (number of samples, sequence length, number of dimensions) is passed to the second argument. -Since the shape of __data X is (number of samples, number of dimensions) __, the number of samples can be obtained with __ "shape [0]" __, and the number of dimensions is __ "shape [1] You can get it with __. -For the teacher label y, one of the numerical values of "0, 1, 2" is stored, so these values are treated equally when __analyzing, and converted to a __one-hot vector. __. (If not converted, the analysis will be such that "1" is "between 0 and 2") -Conversion to one-hot vector is performed by __ "to_categorical (data, number of vectors)" __. The number of vectors this time is __3 __ of "0,1,2".

・ Code![Screenshot 2020-10-28 16.55.42.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/771f56a0-2c27-6b77- 730a-11b1474a0fcf.png)

・ The shape of each data![Screenshot 2020-10-28 16.56.16.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/fc589dba-327a -91cb-0868-70397bbfe331.png)

Implement LSTM in Keras

Definition of LSTM layer

-Since we will use Keras's Sequential model this time, unlike the Functional API, we can easily add the LSTM layer by using __ "add" __. -Since the first layer is the input layer, it is necessary to specify __, "shape of input data" __. In the case of 2D data, only the number of dimensions needs to be specified by input_dim, but in the case of 3D data like this time, the sequence length must also be specified by input_length. -Also, __input_shape = (sequence length, number of dimensions) __ can be set.

・ Code![Screenshot 2020-10-28 18.33.34.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/6870e342-6542-c0c7- 79fa-b2754121c393.png)

-Add an LSTM layer to the second layer and a fully connected layer (output layer) to the third layer. When defining the LSTM layer, it is necessary to specify True in return_sequences if the next layer is also LSTM, and specify False if it is not the LSTM layer.

・ Code![Screenshot 2020-10-28 18.39.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/7733ae98-2a75-ef61- ca33-17ea96ea3357.png)

Compile, learn, evaluate

-After adding the layer, _ compile and set the __ model training process. Compile with __ "model.compile ()" __, and specify the loss function loss and activation function optimizer as arguments. Since this is a classification model, "__cross entropy error __" is used for the loss function. Use 'rmsprop' for the activation function, and specify __ ['accuracy'] __ for metrics, which is an index for model evaluation.

-Model learning is performed with "__model.fit () __". Determine the number of data to be input to the model at one time batch_size is 32, and the number of times the model is trained epochs is 2.

・ Finally, evaluate the model. This is done with "__model.evaluate () __". You can pass the test data to this.

・ Code![Screenshot 2020-10-28 18.57.49.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/f529fc7b-6c87-9c53- cbfc-4d705ec18ac7.png)

・ Result![Screenshot 2020-10-28 18.58.15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/27ca0b26-900e-43c4- 8d86-25999751c5dd.png)

Try using the model for different data

Processing of input data

-From here, __ new data will be passed to the created model to make a negative / positive diagnosis __. -Store English text in "text" as input data and process it. First, pass text to the morphological analysis function __ "tweet_to_words ()" __ created in Chapter 2, and then use __split () __ to divide each word. -Next, __ Convert the divided words to numbers __. Refer to the database __ "vocab_to_ints" __ created in Chapter 2, and store the numerical value if it exists in the database, or 0 in the empty list words_int. -Finally, as we did in Chapter 2, we unified the length of the __ list __ and completed it.

·code スクリーンショット 2020-10-28 20.32.30.png

・ Result![Screenshot 2020-10-28 20.32.46.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/1eb9d879-8cde-b7c7- abef-f86233abaecd.png)

-After that, apply the trained model __ "lstm_model" __ to these features and predict with predict, and output the one with the highest predicted value among the three categories as answer. -The one with the highest predicted value can be obtained by using __ "np.argmax ()" __ which returns the index number of the maximum value.

-Code (results are returned as "negative") スクリーンショット 2020-10-28 20.52.35.png

Summary

-When implementing the model, use the data created in __Chapter2 as a training data set __. The training data X is created from __words quantified __ (features), and the teacher label y is created from __negative / positive quantified __ (label). -Data to be trained by LSTM must be __3D __ (number of samples, sequence length, number of dimensions) __. Of these, sequence length refers to the number of data that the model learns in each step. -Implement the model with Sequential model. __ Add a layer with "add" __, compile with __ "compile" __, learn with __ "fit" __ and you're done. The completed model is evaluated with __ "evaluate" __. -When passing another text data to the created model, format the __ data by using the function in the previous section, and then pass it to the __ model. Negative and positive predictions are made with __ "predict" __.

This time is over. Thank you for reading until the end.

Recommended Posts

Negative / Positive Analysis 2 Twitter Negative / Positive Analysis (1)
Negative / Positive Analysis 3 Twitter Negative / Positive Analysis (2)
Python: Negative / Positive Analysis: Twitter Negative / Positive Analysis Using RNN-Part 1
Negative / Positive Analysis 1 Application of Text Analysis
Python: Negative / Positive Analysis: Text Analysis Application
Scraping & Negative Positive Analysis of Bunshun Online Articles
Is Vtuber Positive? Is it negative?
Analyzing Twitter Data | Trend Analysis
Create a word cloud with only positive / negative words on Twitter
Starbucks Twitter Data Location Visualization and Analysis
Program for Twitter Trend Analysis (Personal Note)
Creation of negative / positive classifier using BERT