使用 Twitter 数据的逻辑回归
在本练习中,您将使用 tweets 数据集构建一个逻辑回归模型。目标变量是 airline_sentiment:负面推文为 0,中性为 1,正面为 2。因此,这是一个多分类任务。我们针对二分类问题学到的一切,同样适用于多分类问题。
您将用幻灯片中的两种方法来评估模型的准确率。
逻辑回归函数和准确率评分函数已为您导入。
本练习是课程的一部分
Python 中的情感分析
练习说明
- 使用已定义的
X和y构建并拟合逻辑回归模型。 - 计算该逻辑回归模型的准确率。
- 进行标签预测。
- 使用预测标签与真实标签计算"准确率评分"。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define the vector of targets and matrix of features
y = tweets.airline_sentiment
X = tweets.drop('airline_sentiment', axis=1)
# Build a logistic regression model and calculate the accuracy
log_reg = ____.____(X, y)
print('Accuracy of logistic regression: ', log_reg.____)
# Create an array of prediction
y_predict = log_reg.____
# Print the accuracy using accuracy score
print('Accuracy of logistic regression: ', ____(___, ____))