-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_CNN.py
63 lines (39 loc) · 1.59 KB
/
sentiment_CNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Untitled15.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1yAnc6o7AXzZeWtD4xUraYMgwzchFtwmU
"""
import numpy as np
import pandas as pd
import seaborn as sns
df=pd.read_csv('/content/updated_sentiment_analysis(2).csv')
df.head()
df=df.sample(10000)
data=df
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
tokenizer = Tokenizer(num_words=500, split=' ')
tokenizer.fit_on_texts(df['review'].values)
x = tokenizer.texts_to_sequences(df['review'])
x = pad_sequences(x, maxlen=80)
y = df['sentiment_positive']
max_words=500
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=2021,test_size=0.1)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
cnn_model=Sequential()
cnn_model.add(Embedding(1000, 300, input_length=max_words))
cnn_model.add(Conv1D(64,3,activation='relu'))
cnn_model.add(MaxPooling1D(pool_size=2))
cnn_model.add(Conv1D(32,3,activation='relu'))
cnn_model.add(MaxPooling1D(pool_size=2))
cnn_model.add(Dense(8, activation='relu'))
cnn_model.add(Dense(1, activation='sigmoid'))
cnn_model.compile(optimizer='adam', loss="binary_crossentropy", metrics=['accuracy'])
cnn_model.summary()
cnn_model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=50, batch_size=128, verbose=2)