Adobe Scan 08 Jan 2025
Adobe Scan 08 Jan 2025
no:05
Aim:
To design and implement a Convolutional Neural Network (CNN) for sentiment analysis on textual data
and evaluate its performance in predicting the sentiment (positive or negative) of text.
Procedure:
model = Sequential()
model.add(embedding_layer)
model.add(ConvID(128, 5, activation='relu))
model.add(MaxPooling lD(pool_size=2)
model.add(Flatten())
model. add(Dense( 128, activation=relu')
model. add(Dense(1, activation='sigmoid))
23
model.fit(X_train, y_train, epochs=5, batch_size=64, validation_data=(X_test, y_test))
Program:
import pandas as pd
data = pd.read_csv('imdb_labelled.tsv',he ader =
None,
delimiter=\t)
data.columns = (Text', 'Label']df.head()
pos = [)
neg = |
for l in data. Label:
ifl == 0:
pos.append(0)
neg.append( 1)
elif | == l:
pos.append(1)
neg.append(0)data[ Pos- pos
data('Neg'l= neg
data =datal[Text_Final', tokens, Label', Pos, Negl]data. head()
data_train, data_test = train_test _split(data,test_size=0.10,
random_state=42)
33/62
print("Max sentence length is %s" % max(test_sentence_lengths))
word2vec_path = GoogleNews-vectors-negative300.bin.gz'
word2vec = models. Keyed Vectors.load_word2vec_format(word2vec_path, binary-True)
tokenizer = Tokenizer(num_words=len(TRAINING_VOCAB), lower=True, char_level=False)
tokenizer.fit on_texts(data_train["Text_Final"],tolist()
training_sequences =
tokenizer.texts to_sequences(data_train["Text _Final"], tolist())train_word index =
tokenizer.word_index
print('Found %s unique tokens. % len(train_word_index))train_cnn_data =
pad_sequences(training_sequences,
maxlen=MAX_SEQUENCE_LENGTH)
train_embedding_weights = np.zeros(len(train_word_index)+1,
Dropout(0.1)(1_merge)
X= Dense(128, activation='relu )(x)x =
Dropout(0.2X×)
preds = Dense(labels_index, activation='sigmoid')(x) model =
model = ConvNet(train_embedding_weights,
MAX_SEQUENCE_LENGTH,
len(train_word_index)+1,
EMBEDDING_DIM,
len(list(label_names))
num_epochs = 3
batch_size = 32
hist = model.fit(x_train,y_tr,
epochs=num_epochs,
validationsplit=0.1,
shuffle=True,
batch_size=batch_size)
predictions = model.predict(test_cnn_data,
batch_size=1024,
verbose=l) labels
=[1,0]
prediction_labels=) for p
in predictions:
prediction_ abels.append(labels[np. argmax(p))sumdata_test.Label-prediction_label
s/len(prediction_labels)
OUTPUT:
Result:
35/62
The CNN model was successfully implemented for sentiment analysis on textual data. olut1
layers, the model was able to capture important patterns in the text and classify the sentiment as positive o
negative. The experiment demonstrated that CNNs, which are often used in image processing, can also
effectively handle NLP tasks like sentiment analysis by learning local dependencies in text. The model
performed well, achieving good accuracy in predicting the sentiment of the reviews.