Hebben we het sentiment echt goed voorspeld?

Sentimentanalyse in Python

Violeta Misheva

Data Scientist

Train/test-split

totaal aantal observaties gesplitst in een trainings- en testset, waarbij de trainingsset het grootste blok is

  • Trainingsset: om het model te trainen (70–80% van alle data)
  • Testset: om de modelprestatie te evalueren
Sentimentanalyse in Python

Train/test in Python

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123, stratify=y)
  • X: features
  • y: labels
  • test_size: deel van data voor testen
  • random_state: seed voor de split
  • stratify: bewaart dezelfde klassenverdeling als in de input
Sentimentanalyse in Python

Logistische regressie met train/test-split

log_reg = LogisticRegression().fit(X_train, y_train)
print('Accuracy on training data: ', log_reg.score(X_train, y_train))
0.76
print('Accuracy on testing data: ', log_reg.score(X_test, y_test))
0.73
Sentimentanalyse in Python

Accuracy met train/test-split

from sklearn.metrics import accuracy_score
log_reg = LogisticRegression().fit(X_train, y_train)
y_predicted = log_reg.predict(X_test)
print('Accuracy score on test data: ', accuracy_score(y_test, y_predicted))
0.73
Sentimentanalyse in Python

Confusion matrix

een voorbeeld van een confusion matrix voor een binaire classificatie

Sentimentanalyse in Python

Confusion matrix in Python

from sklearn.metrics import confusion_matrix
log_reg = LogisticRegression().fit(X_train, y_train)
y_predicted = log_reg.predict(X_test)
print(confusion_matrix(y_test, y_predicted)/len(y_test))
[[0.3788 0.1224]
 [0.1352 0.3636]]
Sentimentanalyse in Python

Laten we oefenen!

Sentimentanalyse in Python

Preparing Video For Download...