【发布时间】:2020-11-01 00:24:29
【问题描述】:
我目前正在进行推文情绪分析,并且对正确的步骤顺序有一些疑问。请假设数据已经进行了相应的预处理和准备。所以这就是我将如何进行:
- 使用
train_test_split(80:20 比例)保留测试 数据集。 - 矢量化
x_train,因为推文不是数字。
在接下来的步骤中,我想确定最佳分类器。请假设那些已经导入。所以我会继续:
- 超参数化(网格搜索),包括交叉验证方法。 在这一步中,我想确定每个参数的最佳参数 分类器。 KNN的代码如下:
model = KNeighborsClassifier()
n_neighbors = range(1, 10, 2)
weights = ['uniform', 'distance']
metric = ['euclidean', 'manhattan', 'minkowski']
# define grid search
grid = dict(n_neighbors=n_neighbors, weights=weights ,metric=metric)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(train_tf, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
- 比较分类器的准确度(取决于最佳超参数)
- 选择最佳分类器
- 获取保留的测试数据集(来自
train_test_split())并在测试数据上使用最佳分类器
这是正确的方法还是您会建议更改某些内容(例如,单独进行交叉验证而不是在超参数化中)?将测试数据作为最后一步进行测试是否有意义,还是我应该提前进行以评估未知数据集的准确性?
【问题讨论】:
标签: python machine-learning classification sentiment-analysis text-classification