<jupyter_start><jupyter_text>SVM<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X_train_transform, sentiment, test_size=0.3)
clf = SVC().fit(X_train, Y_train) 
predicted = clf.predict(X_test)

print(classification_report(Y_test, predicted))
kf = KFold(n_splits=10)
clf = SVC()
lista = []
for train_index, test_index in kf.split(X_train_transform):
    X_train, X_test = X_train_transform[train_index], X_train_transform[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    lista.append(clf.score(X_test, y_test))
    
    print(clf.score(X_test, y_test))
    
print(f' Média: {np.array(lista).mean()} \t Std: {np.array(lista).std()}  ')<jupyter_output>0.3835125448028674
0.45878136200716846
0.4121863799283154
0.43727598566308246
0.4659498207885305
0.44802867383512546
0.44086021505376344
0.4748201438848921
0.44964028776978415
0.48201438848920863
 Média: 0.44530698022227383 	 Std: 0.028021649950196455<jupyter_text>RL<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X_train_transform, sentiment, test_size=0.3)
clf = LogisticRegression(max_iter=1000).fit(X_train, Y_train) 
predicted = clf.predict(X_test)

print(classification_report(Y_test, predicted))
kf = KFold(n_splits=10)
clf = LogisticRegression(max_iter=1000)
lista = []
for train_index, test_index in kf.split(X_train_transform):
    X_train, X_test = X_train_transform[train_index], X_train_transform[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    lista.append(clf.score(X_test, y_test))
    
    print(clf.score(X_test, y_test))
    
print(f' Média: {np.array(lista).mean()} \t Std: {np.array(lista).std()}  ')
def pre_X(frases):
    lista = []
    
    for frase in frases:
        lista.append(frase)
    return lista

def pre_Y(number):
    lista = []
    
    for numb in number:
        lista.append(numb)
    return lista
#pré-processamento dos textos
def set_array(frases):
    
    vocab = []
    palavras = []
    for frase in frases:
        text_array = remove_user(frase)
        text_array = Tokenize(text_array)
        text_array = text_array.split(' ')
        for i in range(len(text_array)):
            vocab.append(text_array[i])
    return vocab

def Tokenize(f):  

    f = f.lower().replace('\n', '').replace('-','').replace('#','').replace('.','').replace(',','').replace('!','').replace('r\n','').replace('  ','')
    token = RegexpTokenizer(r"\w+")
    f = token.tokenize(f)
    stop_words = set(stopwords.words('portuguese'))
    new_word = [word for word in f if not word in stop_words]
    return ' '.join(new_word)

def remove_user(frase):
    return re.sub('@\w+','',frase)
model = tf.keras.Sequential([
    tf.keras.layers.Dense(50, activation='relu'),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(25, activation='relu'),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(10, activation='tanh'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(3 , activation='softmax')
])

model.compile(
    optimizer='adam',
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
)
Tweet = Tweet.apply(remove_user)
Tweet_preprocessed = Tweet.apply(Tokenize)
count_vect = CountVectorizer()
X_train = count_vect.fit_transform(Tweet_preprocessed)

tfidf_transformer = TfidfTransformer()
X_train_transform = tfidf_transformer.fit_transform(X_train) # Aplicando o TF-IDF


X_train, X_test, Y_train, Y_test = train_test_split(X_train_transform, sentiment, test_size=0.3)

X_train
X_test
one = OneHotEncoder(sparse=False)
y_one = one.fit_transform(Y_train.reshape(-1,1))
y_one_ = one.transform(Y_test.reshape(-1,1))
fit = model.fit(X_train.todense(), y_one, epochs=10, validation_data=(X_test.todense(), y_one_))
predicted = model.predict(X_test.todense())

print(classification_report(np.argmax(y_one_, axis=1), np.argmax(predicted, axis=1)))
plt.figure(figsize=(12,6))
plt.plot(fit.history['loss'], label='Loss', color='darkred')
plt.plot(fit.history['val_loss'], label='Val Loss', color='green')
plt.legend()
plt.ylabel('Loss')
plt.show()
plt.figure(figsize=(12,6))
plt.plot(fit.history['accuracy'], label='accuracy', color='darkred')
plt.plot(fit.history['val_accuracy'], label='Val accuracy', color='green')
plt.legend()
plt.ylabel('Accuracy')
plt.show()
kf = KFold(n_splits=10)
val_accuracy = []
index = []
cont = 0

for train_index, test_index in kf.split(X_train_transform):
    cont += 1
    
    model = tf.keras.Sequential([
    tf.keras.layers.Dense(50, activation='relu'),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(25, activation='relu'),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(10, activation='tanh'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(3 , activation='softmax')
    ])

    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.categorical_crossentropy,
        metrics=['accuracy']
    )
    
    X_train, X_test = X_train_transform[train_index], X_train_transform[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    y_train_one = one.fit_transform(y_train.reshape(-1,1))
    y_teste_one = one.fit_transform(y_test.reshape(-1,1))
    
    print("Iter: ",cont)
    print(" ")
    
    fit = model.fit(X_train.todense(), y_train_one, epochs=5, validation_data=(X_test.todense(), y_teste_one))
    print(" ")
    val_accuracy.append(fit.history['val_accuracy'])
    
    index.append((train_index,test_index))
def media_std(val_accuracy):
    matrix_acc = np.array(val_accuracy)
    lista = []
    for i in range(len(matrix_acc)):
        lista.append(matrix_acc[i][-1])
        print(f' Fold: {i}\t Ultimo valor acc: {lista[i]}')
    print("")
    print(f' Média: {np.array(lista).mean()} \t Std: {np.array(lista).std()}')
        
media_std(val_accuracy)
vectorize_layer = TextVectorization(
 max_tokens=15000,
 output_mode='int',
 output_sequence_length=len(max(df['text'])))

vocab = set_array(df['text'])

vectorize_layer.adapt(np.unique(vocab))
len(vectorize_layer.get_vocabulary())<jupyter_output><empty_output><jupyter_text>CONV1D<jupyter_code>model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,
        mask_zero=True),
    
    tf.keras.layers.Conv1D(32,6, activation='relu'),
    tf.keras.layers.MaxPooling1D(2),
    tf.keras.layers.Flatten(),
    
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')   
])

model.compile(
    optimizer= tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
)
X_train, X_test, Y_train, Y_test = train_test_split(Tweet_preprocessed, sentiment, test_size=0.3)
y_one = one.fit_transform(Y_train.reshape(-1,1))
y_one_ = one.transform(Y_test.reshape(-1,1))
fit = model.fit(np.asarray(pre_X(X_train)), y_one, epochs=10, batch_size=128 ,validation_data=(np.asarray(pre_X(X_test)),y_one_))
predicted = model.predict(X_test)

print(classification_report(np.argmax(y_one_, axis=1), np.argmax(predicted, axis=1)))
kf = KFold(n_splits=10)
val_accuracy = []
index = []
cont = 0

for train_index, test_index in kf.split(Tweet_preprocessed):
    cont += 1
    
    model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,
        mask_zero=True),
    
    tf.keras.layers.Conv1D(32,6, activation='relu'),
    tf.keras.layers.MaxPooling1D(2),
    tf.keras.layers.Flatten(),
    
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')   
    ])

    model.compile(
    optimizer= tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
    )
    
    X_train, X_test = Tweet_preprocessed[train_index], Tweet_preprocessed[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    y_train_one = one.fit_transform(y_train.reshape(-1,1))
    y_teste_one = one.fit_transform(y_test.reshape(-1,1))
    
    print("Iter: ",cont)
    print(" ")
    
    fit = model.fit(X_train, y_train_one, epochs=5, validation_data=(X_test, y_teste_one))
    print(" ")
    val_accuracy.append(fit.history['val_accuracy'])
    
    index.append((train_index,test_index))
media_std(val_accuracy)<jupyter_output>Fold: 0	 Ultimo valor acc: 0.41577062010765076
 Fold: 1	 Ultimo valor acc: 0.379928320646286
 Fold: 2	 Ultimo valor acc: 0.40143370628356934
 Fold: 3	 Ultimo valor acc: 0.3476702570915222
 Fold: 4	 Ultimo valor acc: 0.4444444477558136
 Fold: 5	 Ultimo valor acc: 0.379928320646286
 Fold: 6	 Ultimo valor acc: 0.379928320646286
 Fold: 7	 Ultimo valor acc: 0.3525179922580719
 Fold: 8	 Ultimo valor acc: 0.3884892165660858
 Fold: 9	 Ultimo valor acc: 0.3201438784599304

 Média: 0.3810255080461502 	 Std: 0.03367019710445899<jupyter_text>BDR<jupyter_code>model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,mask_zero=True),
    
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(50)),
    
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(
    optimizer= tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
)
X_train, X_test, Y_train, Y_test = train_test_split(Tweet_preprocessed, sentiment, test_size=0.3)
y_one = one.fit_transform(Y_train.reshape(-1,1))
y_one_ = one.transform(Y_test.reshape(-1,1))
fit = model.fit(np.asarray(pre_X(X_train)), y_one, epochs=10, batch_size=128 ,validation_data=(np.asarray(pre_X(X_test)),y_one_))
predicted = model.predict(X_test)

print(classification_report(np.argmax(y_one_, axis=1), np.argmax(predicted, axis=1)))
kf = KFold(n_splits=10)
val_accuracy = []
index = []
cont = 0

for train_index, test_index in kf.split(Tweet_preprocessed):
    cont += 1
    
    model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,mask_zero=True),
    
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(50)),
    
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
    
    ])

    model.compile(
        
    optimizer= tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
        
    )
    
    X_train, X_test = Tweet_preprocessed[train_index], Tweet_preprocessed[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    y_train_one = one.fit_transform(y_train.reshape(-1,1))
    y_teste_one = one.fit_transform(y_test.reshape(-1,1))
    
    print("Iter: ",cont)
    print(" ")
    
    fit = model.fit(X_train, y_train_one, epochs=5, validation_data=(X_test, y_teste_one))
    print(" ")
    val_accuracy.append(fit.history['val_accuracy'])
    
    index.append((train_index,test_index))
media_std(val_accuracy)
plt.figure(figsize=(12,6))
plt.plot(fit.history['loss'], label='Loss', color='darkred')
plt.plot(fit.history['val_loss'], label='Val Loss', color='green')
plt.legend()
plt.grid()
plt.ylabel('Loss')
plt.show()
plt.figure(figsize=(12,6))
plt.plot(fit.history['accuracy'], label='accuracy', color='darkred')
plt.plot(fit.history['val_accuracy'], label='Val accuracy', color='green')
plt.legend()
plt.grid()
plt.ylabel('Accuracy')
plt.show()<jupyter_output><empty_output><jupyter_text>LSTM<jupyter_code>model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,mask_zero=True),
    
    tf.keras.layers.LSTM(50, activation='relu' ,return_sequences=True),
    tf.keras.layers.Dropout(0.3),
    
    tf.keras.layers.LSTM(10 , activation='tanh'),
    tf.keras.layers.Dropout(0.2),
    
    tf.keras.layers.Dense(3, activation='softmax')
    ])

model.compile(
    optimizer= tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.categorical_crossentropy,
    metrics=['accuracy']
)
X_train, X_test, Y_train, Y_test = train_test_split(Tweet_preprocessed, sentiment, test_size=0.3)
y_one = one.fit_transform(Y_train.reshape(-1,1))
y_one_ = one.transform(Y_test.reshape(-1,1))
fit = model.fit(np.asarray(pre_X(X_train)), y_one, epochs=10, batch_size=128 ,validation_data=(np.asarray(pre_X(X_test)),y_one_))
predicted = model.predict(X_test)

print(classification_report(np.argmax(y_one_, axis=1), np.argmax(predicted, axis=1)))
kf = KFold(n_splits=10)
val_accuracy = []
index = []
cont = 0

for train_index, test_index in kf.split(Tweet_preprocessed):
    cont += 1
    
    model = tf.keras.Sequential([
    vectorize_layer,
    tf.keras.layers.Embedding(
        input_dim=len(vectorize_layer.get_vocabulary()),
        output_dim=64,mask_zero=True),
    
    tf.keras.layers.LSTM(50, activation='relu' ,return_sequences=True),
    tf.keras.layers.Dropout(0.3),
    
    tf.keras.layers.LSTM(10 , activation='tanh'),
    tf.keras.layers.Dropout(0.2),
    
    tf.keras.layers.Dense(3, activation='softmax')
    ])

    model.compile(
        optimizer= tf.keras.optimizers.Adam(),
        loss=tf.keras.losses.categorical_crossentropy,
        metrics=['accuracy']
    )
    
    X_train, X_test = Tweet_preprocessed[train_index], Tweet_preprocessed[test_index]
    y_train, y_test = sentiment[train_index], sentiment[test_index]
    y_train_one = one.fit_transform(y_train.reshape(-1,1))
    y_teste_one = one.fit_transform(y_test.reshape(-1,1))
    
    print("Iter: ",cont)
    print(" ")
    
    fit = model.fit(X_train, y_train_one, epochs=5, validation_data=(X_test, y_teste_one))
    print(" ")
    val_accuracy.append(fit.history['val_accuracy'])
    
    index.append((train_index,test_index))
media_std(val_accuracy)
TextClassifier = ak.TextClassifier(
    num_classes=3, 
    multi_label=True, 
    loss=tf.keras.losses.categorical_crossentropy, 
    metrics=['accuracy'],
    project_name="text_classifier",
    max_trials=1,
    objective="val_loss"
)

fit = TextClassifier.fit(
    x=np.asarray(pre_X(X_train)), y=one.fit_transform(Y_train.reshape(-1,1)), epochs=10, validation_split=0.2, validation_data=(np.asarray(pre_X(X_test)),one.fit_transform(Y_test.reshape(-1,1)) )
)<jupyter_output>INFO:tensorflow:Reloading Oracle from existing project .\text_classifier\oracle.json
INFO:tensorflow:Reloading Tuner from .\text_classifier\tuner0.json
INFO:tensorflow:Oracle triggered exit
Epoch 1/10
61/61 [==============================] - 6s 96ms/step - loss: 0.6423 - accuracy: 0.3564 - val_loss: 0.6409 - val_accuracy: 0.3536
Epoch 2/10
61/61 [==============================] - 6s 92ms/step - loss: 0.6305 - accuracy: 0.3744 - val_loss: 0.6393 - val_accuracy: 0.3668
Epoch 3/10
61/61 [==============================] - 6s 100ms/step - loss: 0.6014 - accuracy: 0.5287 - val_loss: 0.6308 - val_accuracy: 0.3967
Epoch 4/10
61/61 [==============================] - 6s 95ms/step - loss: 0.4302 - accuracy: 0.7492 - val_loss: 0.7543 - val_accuracy: 0.3823
Epoch 5/10
61/61 [==============================] - 6s 93ms/step - loss: 0.1863 - accuracy: 0.9026 - val_loss: 0.9689 - val_accuracy: 0.3978
Epoch 6/10
61/61 [==============================] - 6s 95ms/step - loss: 0.0705 - accuracy: 0.9662 - val[...]
{'fraction_non_alphanumeric': 0.15974543584469014, 'fraction_numerical': 0.0522627924916431, 'mean_word_length': 4.536298932384342, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '666982', 'license': 'MIT', 'n_tokens_mistral': 6547, 'n_tokens_neox': 6032, 'n_words': 829, 'path': 'Tash-PT/RNN_tash.ipynb', 'repo_name': 'letisousa/SentimentAnalysisUpdates', 'size': '15556'}