开始使用免费开始使用

构建并评估模型:产品评论数据

在本练习中,您将使用 reviews 数据集来构建一个逻辑回归模型。该数据集包含用户对 Amazon 产品的评论。数组 y 存放情感标签:正向为 1,否则为 0。数组 X 包含使用 BOW 方法创建的所有数值特征。您可以在 IPython Shell 中自由探索。

您的任务是构建一个逻辑回归模型,并使用测试集计算准确率和混淆矩阵。

逻辑回归模型与训练/测试集拆分函数已为您导入。

本练习是课程的一部分

Python 中的情感分析

查看课程

练习说明

  • 导入准确率和混淆矩阵函数。
  • 将数据拆分为训练集和测试集,其中 30% 作为测试集,并将随机种子设为 42
  • 训练一个逻辑回归模型。
  • 使用测试数据打印准确率和混淆矩阵。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the accuracy and confusion matrix
____

# Split the data into training and testing
X_train, X_test, y_train, y_test = ____(____, ____, ____=0.3, ____=42)

# Build a logistic regression
log_reg = ____._____

# Predict the labels 
y_predict = log_reg.predict(X_test)

# Print the performance metrics
print('Accuracy score of test data: ', ____(____, ____))
print('Confusion matrix of test data: \n', ____(____, ____)/len(y_test))
编辑并运行代码