-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkelime_tabanlı_tfidf_1_gram.py
68 lines (50 loc) · 2.31 KB
/
kelime_tabanlı_tfidf_1_gram.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
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""kelime_tabanlı_tfidf_1 gram.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1sh9DlJTq-0LJopX4zz9Fas6w23FoKmxH
"""
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
import xgboost
from sklearn import model_selection, preprocessing, linear_model, naive_bayes, metrics, svm
from sklearn import decomposition, ensemble
"""BU PROGRAMDA KELİME TABANLI 1-GRAM TF-IDF VECTORIZER ICIN KLASİK YÖNTEMLERİN BAŞARI SONUÇLARI ELDE EDİLMİŞTİR"""
train=pd.read_excel("clean_tweet_train.xlsx")
test=pd.read_excel("clean_tweet_test.xlsx")
Train = train.append(test, ignore_index=True).fillna(' ')
test.dropna(inplace=True)
test.reset_index(drop=True,inplace=True)
test.info()
train.dropna(inplace=True)
train.reset_index(drop=True,inplace=True)
train.info()
x_train=train.text.tolist()
y_train=train.sentiment.tolist()
x_test=test.text.tolist()
y_test=test.sentiment.tolist()
# kelime tabanlı tf-idf
tfidf = TfidfVectorizer(analyzer='word', ngram_range=(1,1))
tfidf.fit(Train['text'])
xtrain_tfidf = tfidf.transform(x_train)
xtest_tfidf = tfidf.transform(x_test)
def model_training(classifier, vector_train, y_train, vector_test):
classifier.fit(vector_train, y_train)
predictions = classifier.predict(vector_test)
return metrics.accuracy_score(predictions, y_test)
# Naive Bayes
accuracy = model_training(naive_bayes.MultinomialNB(), xtrain_tfidf, y_train, xtest_tfidf )
print ("NB, kelime tabanlı TF-IDF:% ", accuracy*100)
# Logistic Regression
accuracy = model_training(linear_model.LogisticRegression(solver='newton-cg',multi_class='multinomial'), xtrain_tfidf, y_train, xtest_tfidf)
print ("LR, kelime tabanlı TF-IDF:%", accuracy*100)
# SVM
accuracy = model_training(svm.SVC(kernel='linear'), xtrain_tfidf, y_train, xtest_tfidf)
print ("SVM, kelime tabanlı TF-IDF::%", accuracy*100)
# Random forest
accuracy = model_training(ensemble.RandomForestClassifier(n_estimators=100), xtrain_tfidf, y_train, xtest_tfidf)
print ("RF, kelime tabanlı TF-IDF:% ", accuracy*100)
# Extereme Gradient Boosting
accuracy = model_training(xgboost.XGBClassifier(booster='gblinear'), xtrain_tfidf.tocsc(), y_train, xtest_tfidf.tocsc())
print ("Xgb, kelime tabanlı TF-IDF:% ", accuracy*100)