Logistic regression: ทบทวน

Sentiment Analysis ด้วย Python

Violeta Misheva

Data Scientist

โมเดลที่ซับซ้อนและ Regularization

โมเดลที่ซับซ้อน:

  • โมเดลที่ซับซ้อนจนจับ noise ในข้อมูล (overfitting)
  • มี feature หรือพารามิเตอร์จำนวนมาก

Regularization:

  • วิธีลดความซับซ้อนของโมเดลให้เหมาะสม
Sentiment Analysis ด้วย Python

Regularization ใน Logistic Regression

from sklearn.linear_model import LogisticRegression
# Regularization arguments
LogisticRegression(penalty='l2', C=1.0)
  • L2: ดึงค่าสัมประสิทธิ์ทั้งหมดให้เข้าใกล้ศูนย์
  • ค่า C สูง: penalization ต่ำ โมเดล fit ข้อมูล training ได้ดี
  • ค่า C ต่ำ: penalization สูง โมเดลยืดหยุ่นน้อยลง
Sentiment Analysis ด้วย Python

การพยากรณ์ความน่าจะเป็น vs. การพยากรณ์คลาส

log_reg = LogisticRegression().fit(X_train, y_train)
# Predict labels 
y_predicted = log_reg.predict(X_test)
# Predict probability
y_probab = log_reg.predict_proba(X_test)
Sentiment Analysis ด้วย Python

การพยากรณ์ความน่าจะเป็น vs. การพยากรณ์คลาส

y_probab
array([[0.5002245, 0.4997755],
       [0.4900345, 0.5099655],
        ...,
       [0.7040499, 0.2959501]])
# Select the probabilities of class 1
y_probab = log_reg.predict_proba(X_test)[:, 1]
array([0.4997755, 0.5099655 ..., 0.2959501]])
Sentiment Analysis ด้วย Python

เมตริกของโมเดลกับค่าความน่าจะเป็นที่พยากรณ์

  • เกิด ValueError เมื่อใช้กับค่าความน่าจะเป็น
  • Accuracy score และ confusion matrix ใช้งานกับคลาส
# Default probability encoding:
# If probability >= 0.5, then class 1 Else class 0
Sentiment Analysis ด้วย Python

มาฝึกกันเถอะ!

Sentiment Analysis ด้วย Python

Preparing Video For Download...