【问题标题】:OneHotEncoding for categorical dataOneHotEncoding 用于分类数据
【发布时间】:2021-09-14 15:20:59
【问题描述】:

我有一个这样的数据框

        time     label
-----------------------
     morning      good
   afternoon      good
       night       bad
       night      okay

我想为要在 svm 交叉验证中使用的数据应用 onehotencoding。我尝试如下

from sklearn.model_selection import ShuffleSplit
from sklearn.preprocessing import OneHotEncoder
from sklearn.svm import SVC

x = ds_df['time']
y = ds_df['label']

enc = OneHotEncoder()

X_vec = enc.fit_transform(X)

model = SVC(kernel='linear')

cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=69)
scores = cross_val_score(model, X_vec, y, cv=cv, scoring='precision_weighted')

然后,我收到一条警告

UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))

我该怎么办?我哪里做错了?

【问题讨论】:

标签: python machine-learning scikit-learn one-hot-encoding


【解决方案1】:

首先,这只是一个警告,而不是错误。一些标签不会出现在预测样本中。这意味着为这些标签计算的准确度设置为 0.0

正如我所提到的,这是一个警告,它的处理方式与 python 中的错误不同。大多数环境中的默认行为是仅显示一次特定警告。可以更改此行为:

import warnings
warnings.filterwarnings('ignore')  # "error", "ignore", "always", "default", "module" or "once"

你可以做的,就是对未预测到的标签分数不感兴趣,然后明确指定你感兴趣的标签。

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 2020-11-25
    • 2018-04-13
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多