<jupyter_start><jupyter_text>STEP 4: Création du modèle de Machine Learning<jupyter_code># splitting the data into target and feature
feature = data1.Comment.fillna(' ')
target = data1.Sentiment
# splitting into train and tests
X_train, X_test, Y_train, Y_test = train_test_split(feature, target, test_size =.2, random_state=100)
Y_train
X_train
from sklearn.feature_extraction.text import TfidfVectorizer
# create the transform
vectorizer = TfidfVectorizer()
# tokenize and build vocab
vectorizer.fit(X_train)
# summarize
print(vectorizer.vocabulary_)
print(vectorizer.idf_)
# encode document
vector = vectorizer.transform([X_train[0]])
# summarize encoded vector
print(vector.shape)
print(vector.toarray())
vector1 = vectorizer.transform(X_train)
vector2 = vectorizer.transform(X_test)

vector1
X_train_modif=vector1.toarray()
X_test_modif=vector2.toarray()
X_train_modif
# make pipeline
from sklearn.metrics import precision_score, recall_score, accuracy_score
pipe = make_pipeline(LogisticRegression(solver='lbfgs',max_iter=10000))
# make param grid
param_grid = {'logisticregression__C': [0.01, 0.1, 0.25, 0.75, 0.5, 1, 6,7,8,9,10,11,12,15,20, 100]}

# create and fit the model
model = GridSearchCV(pipe, param_grid, cv=5)
model.fit(X_train_modif,Y_train)
predictions = model.predict(X_test_modif)
# find accuracy, precision, recall:
from sklearn.metrics import confusion_matrix,classification_report
new = np.asarray(Y_test)
confusion_matrix(predictions,Y_test)
print(classification_report(predictions,Y_test))



# make prediction and print accuracy
prediction = model.predict(X_test_modif)
print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")
print(classification_report(Y_test, prediction))

precision = precision_score(Y_test, prediction, average='micro')
recall = recall_score(Y_test, prediction, average='micro')
accuracy = accuracy_score(Y_test, prediction)
print('Precision: {} / Recall: {} / Accuracy: {}'.format(round(precision, 3), round(recall, 3), round(accuracy, 3)))
# make pipeline
from sklearn.metrics import precision_score, recall_score, accuracy_score
from numpy import mean
from numpy import std
from sklearn.model_selection import cross_val_score

param_C= [0.01, 0.1, 0.25, 0.75, 0.5, 1, 6,7,8,9,10,11,12,15,20, 100]
algo=['newton-cg', 'liblinear','sag','saga','lbfgs']
# create and fit the model

for c in param_C:
    for al in algo:
        pipe = make_pipeline(TfidfVectorizer(),
                    LogisticRegression(multi_class='multinomial', solver=al,C=c, max_iter=10000))
    #model = GridSearchCV(pipe,  cv=5)
    #model.fit(X_train,Y_train)
        model.fit(X_train_modif,Y_train)
        scores = cross_val_score(pipe, X_train_modif, Y_train, scoring='accuracy', cv=3, n_jobs=-1)
# report performance
        print('Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))
        #pipe.fit(X_train, Y_train)
        #print('Accuracy for C=%s: %s'
        # % (c, accuracy_score(Y_test, pipe.predict(X_test))))





# make prediction and print accuracy
#prediction = model.predict(X_test)
#print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")
#print(classification_report(Y_test, prediction))

#precision = precision_score(Y_test, prediction, average='micro')
#recall = recall_score(Y_test, prediction, average='micro')
#accuracy = accuracy_score(Y_test, prediction)
#print('Precision: {} / Recall: {} / Accuracy: {}'.format(round(precision, 3), round(recall, 3), round(accuracy, 3)))<jupyter_output><empty_output><jupyter_text>Random Forest Classifier<jupyter_code>#pipe = make_pipeline(TfidfVectorizer(),
#                    RandomForestClassifier())

#param_grid = {'randomforestclassifier__n_estimators':[10, 100, 1000],
  #           'randomforestclassifier__max_features':['sqrt', 'log2']}

#rf_model = GridSearchCV(pipe, param_grid, cv=5)
#rf_model.fit(X_train,Y_train)

#prediction = rf_model.predict(X_test)
#print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")

#precision = precision_score(Y_test, predictionة average='micro')
#recall = recall_score(Y_test, predictionة average='micro')
#accuracy = accuracy_score(Y_test, prediction)
#print('Precision: {} / Recall: {} / Accuracy: {}'.format(round(precision, 3), round(recall, 3), round(accuracy, 3)))<jupyter_output><empty_output><jupyter_text>Naive Bayes Classifier (Multinomial)<jupyter_code>#pipe = make_pipeline(TfidfVectorizer(),
            #        MultinomialNB())
#pipe.fit(X_train,Y_train)
#prediction = pipe.predict(X_test)
#print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")
#print(classification_report(Y_test, prediction))<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>#pipe = make_pipeline(TfidfVectorizer(),
                   #  SVC())
#param_grid = {'svc__kernel': ['rbf', 'linear', 'poly'],
          ##   'svc__gamma': [0.1, 1, 10, 100],
          #   'svc__C': [0.1, 1, 10, 100]}

#svc_model = GridSearchCV(pipe, param_grid, cv=3)
#svc_model.fit(X_train, Y_train)

#prediction = svc_model.predict(X_test)
#print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")
#print(classification_report(Y_test, prediction))<jupyter_output><empty_output><jupyter_text>Tous les autres modèles<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
classifiers = [LinearSVC(), 
               SVC(gamma='auto'), 
               MultinomialNB(),
               LogisticRegression(solver='liblinear',max_iter=10000),
               BernoulliNB(), 
               SGDClassifier(), 
               DecisionTreeClassifier(max_depth=5),
               RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
               KNeighborsClassifier(3)
               ]
from sklearn.pipeline import Pipeline
from sklearn.metrics import confusion_matrix,classification_report
ngrams = (1, 2, 3)

results = []

for g in ngrams:
    #svc_model = GridSearchCV(pipe, param_grid, cv=3)
    #svc_model.fit(X_train, Y_train)

    #prediction = svc_model.predict(X_test)
    

    
    for alg in classifiers:
        pipeline = Pipeline([
        ('vect', TfidfVectorizer(min_df=0.0001, max_df=0.95,
                                 analyzer='word', lowercase=False,
                                 ngram_range=(1, g))),
        ('clf', alg), ])
        pipeline.fit(X_train, Y_train)
        feature_names = pipeline.named_steps['vect'].get_feature_names()
        prediction = pipeline.predict(X_test)
        print(f"**************************************")
        print(f"Classifier= "+str(alg)+" and the ngrams is= "+str(g))
        print(f"Accuracy score is {accuracy_score(Y_test, prediction):.2f}")
        print(classification_report(Y_test, prediction))
        # find accuracy, precision, recall:
        new = np.asarray(Y_test)
        print(confusion_matrix(prediction,Y_test))<jupyter_output>**************************************
Classifier= LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0) and the ngrams is= 1
Accuracy score is 0.73
              precision    recall  f1-score   support

           0       0.68      0.58      0.63       516
           1       0.77      0.67      0.72       506
           2       0.81      0.80      0.81       517
           3       0.67      0.84      0.74       540

    accuracy                           0.73      2079
   macro avg       0.73      0.72      0.72      2079
weighted avg       0.73      0.73      0.72      2079

[[300  79  30  34]
 [ 74 340  10  19]
 [ 54   9 416  33]
 [ 88  78  61 454]]
**************************************
Classifier= SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='aut[...]
{'fraction_non_alphanumeric': 0.1370545726389235, 'fraction_numerical': 0.038126090206827806, 'mean_word_length': 4.132352941176471, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '455510', 'license': 'Apache-2.0', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2710, 'n_words': 540, 'path': 'TSDA_ML.ipynb', 'repo_name': 'SBSsbs/Sentiment-analysis-project', 'size': '8026'}