python-3.x - 管道OrdinalEncoder ValueError找到未知类别

标签 python-3.x scikit-learn pipeline valueerror ordinal

请放心我。我正在将职业转移到数据科学领域,没有CS或编程背景,所以我可能做的事情非常愚蠢。我研究了几个小时都没有成功。

目标:使Pipeline与OrdinalEncoder一起运行。

问题:带有OrdinalEncoder调用的代码无法运行。它确实不带OrdinalEncoder运行。据我所知,我可以传递两个参数,即category和dtype。都没有帮助。

我正在将公开的diabetes data设置传递给模型。这是问题吗? IOW,将高基数功能传递给OrdinalEncoder会在模型建立后在火车/测试数据之间引起问题,即测试分割的值是否为火车集合没有的值?

from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OrdinalEncoder
from sklearn.ensemble import RandomForestClassifier

pipe = Pipeline([
    ('imputer', SimpleImputer()),
    ('ordinal_encoder', OrdinalEncoder()),
    ('classifier', RandomForestClassifier(criterion='gini', n_estimators=100))])

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Construct model
model = pipe.fit(X_train, y_train)

# Show results
print("Hold-out AUC score: %.3f" %roc_auc_score(model.predict_proba(X_test),y_test))

这是我遇到的错误:
ValueError: Found unknown categories [17.0] in column 0 during transform

我究竟做错了什么?

设置:
The scikit-learn version is 0.20.2.
3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
[Clang 6.0 (clang-600.0.57)]
sys.version_info(major=3, minor=7, micro=2, releaselevel='final', serial=0)

最佳答案

我迟到了游戏,但是我登陆了此页面,所以我认为我还是会回答。

您在评论中说过:“对于给定的测试/训练拆分,糖尿病数据集的许多功能中的值太多,以至于都无法反射(reflect)所有值”

当测试集包含训练期间未看到的数据时,编码器会发生此错误。

关于python-3.x - 管道OrdinalEncoder ValueError找到未知类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54836051/

相关文章:

GitLab:在线 rebase 后如何跳过或允许管道步骤

mips - 在 MIPS 中组织流水线

python - sklearn - 如何检索 PCA 组件并解释传递给 GridSearchCV 的管道内部的方差

python-3.x - Flask套接字io私有(private)聊天

python-3.x - 将 pyqtgraph(绘图)添加到 QApplication

python-3.x - 无法在 Spyder 中调整编辑器窗口的大小

python - 预测: Time-series prediction of future events using SVR module

python - 如何修复 ValueError : read of closed file exception?

machine-learning - 无法处理多类和连续的混合

python - scikit-学习 PCA : matrix transformation produces PC estimates with flipped signs