import pandas as pd
data = pd.read_csv("data/training_data_tweets.csv")
data.dropna()
import seaborn as sns
# you can also add your own stopwords in this step using append()
import io
import unidecode
with io.open('data/stopwords_german.txt', mode='r', encoding='utf-8') as f:
stopwords = f.readlines()
stopwords = [x.strip() for x in stopwords]
stopwords = [unidecode.unidecode(x) for x in stopwords]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data.text, data.sentiment, test_size=0.15, random_state=1)
print(len(y_train[y_train == "negative"]))
836
X_train_new = X_train[y_train == "positive"][:836]
y_train_new = y_train[y_train == "positive"][:836]
X_train_new= X_train_new.append(X_train[y_train == "neutral"][:836] )
y_train_new= y_train_new.append(y_train[y_train == "neutral"][:836] )
X_train_new= X_train_new.append(X_train[y_train == "negative"][:836] )
y_train_new= y_train_new.append(y_train[y_train == "negative"][:836] )
print(X_train_new)
440 Lebih happy nntn spongebob dri pda nntn bola :D
2908 understandable:)
2538 yo macht mal Album yo ^^ das yo verleiht dem ...
2860 Wieder kleine :) die großen waren zu groß ;)
1230 Anmeldungen noch möglich!: „: Buchhandel - Web...
...
753 ich mag kein Knoppas -,-
4764 Neukölln: Brutale Räuber quälen Taxifahrer mit...
6652 wieso zeigt twitter jetzt einfach so fotos an?...
2797 Was macht Götze eigentlich so. Hab gar nichts ...
5157 sorry meine Internetverbindung ist schlecht de...
Name: text, Length: 2508, dtype: object
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
(
'vect',
CountVectorizer(analyzer='word', strip_accents='unicode', lowercase=True, stop_words=stopwords)
),
(
'tfidf',
TfidfTransformer(use_idf=True, smooth_idf=True)
),
(
'rf',
RandomForestClassifier()
)
])
# fit pipeline to training data
pipeline.fit(X_train, y_train)
from sklearn.metrics import accuracy_score, f1_score, precision_score, hamming_loss, confusion_matrix
def score_model(true, pred):
print('Accuracy:', accuracy_score(true, pred))
print('F1:', f1_score(true, pred, average='weighted'))
print('Precision:', precision_score(true, pred, average='weighted'))
print('Hamming loss', hamming_loss(true, pred))
preds = pipeline.predict(X_test.values.astype('U'))
score_model(y_test, preds)
Accuracy: 0.6792079207920793
F1: 0.6539130902199176
Precision: 0.6553492680773328
Hamming loss 0.3207920792079208
# plot a confucion matrix to visualize true positives, true negatives, ...
# https://en.wikipedia.org/wiki/Confusion_matrix
from conf_matrix import pplot_cm
pplot_cm(y_test, preds)
Accuracy: 0.6792079207920793
F1: 0.6539130902199176
Precision: 0.6553492680773328
Hamming loss 0.3207920792079208
# use your pipeline to create class predictions for the three example texts given in the readme
ex = ["Bin rundum begeistert. Insbesondere die Musikwiedergabe ist eine tolle Sache. Die Bedienung lässt keine Wünsche übrig und ist kinderleicht. Beste Grüße, Max"
,"Startet leider nicht mehr. Ich hatte den Echo leider nur ca. 3 Stunden einwandfrei in Betrieb, danach ging er leider nicht mehr. Kann mir vl jemand sagen wie ich ihn wieder zum Laufen bringe oder ob er einfach nur kaputt ist?"
,"Gut, aber im Alltag noch nicht ausgereift. Amazon Echo ist eine praktische Sache allerdings wenn man kein Smart-Home hat ist der Einsatzbereich begrenzt"]
pred = pipeline.predict(ex)
print(pred)
['positive' 'negative' 'neutral']