ValueError: Unable to convert array of bytes/strings into decimal numbers with dtype='numeric'
ValueError: Unable to convert array of bytes/strings into decimal numbers with dtype='numeric'
我有这个管道:
diamonds = sns.load_dataset("diamonds")
# Build feature/target arrays
X, y = diamonds.drop("cut", axis=1), diamonds["cut"]
# Set up the colnames
to_scale = ["depth", "table", "x", "y", "z"]
to_log = ["price", "carat"]
categorical = X.select_dtypes(include="category").columns
scale_pipe = make_pipeline(StandardScaler())
log_pipe = make_pipeline(PowerTransformer())
categorical_pipe = make_pipeline(OneHotEncoder(sparse=False))
transformer = ColumnTransformer(
transformers=[
("scale", scale_pipe, to_scale),
("log_transform", log_pipe, to_log),
("oh_encode", categorical_pipe, categorical),
]
)
knn_pipe = Pipeline([("prep", transformer), ("knn", KNeighborsClassifier())])
# Fit/predict/score
_ = knn_pipe.fit(X_train, y_train)
preds = knn.predict(X_test)
当我 运行 它时,它非常适合数据,但我无法评分或做出预测,因为我收到此错误:
ValueError: could not convert string to float: 'G'
The above exception was the direct cause of the following exception:
ValueError: Unable to convert array of bytes/strings into decimal numbers with dtype='numeric'
这是一个分类问题,所以我认为错误的原因是因为我没有对目标进行编码。但即使在目标上使用 LabelEncode 之后,我仍然遇到同样的错误。
可能是什么原因?我也尝试过其他模型的管道。错误是一样的。顺便说一句,我正在使用 Seaborn 的内置钻石数据集。
您似乎没有用 knn_pipe
预测 X_test
的值。您在最后一行中使用的变量 knn
实际上在您提供的示例中未定义。我猜你已经在原始文件的某处定义了它,因此看到了这个错误消息。
总之,改变一下
preds = knn.predict(X_test)
到
preds = knn_pipe.predict(X_test)
它会起作用。
我有这个管道:
diamonds = sns.load_dataset("diamonds")
# Build feature/target arrays
X, y = diamonds.drop("cut", axis=1), diamonds["cut"]
# Set up the colnames
to_scale = ["depth", "table", "x", "y", "z"]
to_log = ["price", "carat"]
categorical = X.select_dtypes(include="category").columns
scale_pipe = make_pipeline(StandardScaler())
log_pipe = make_pipeline(PowerTransformer())
categorical_pipe = make_pipeline(OneHotEncoder(sparse=False))
transformer = ColumnTransformer(
transformers=[
("scale", scale_pipe, to_scale),
("log_transform", log_pipe, to_log),
("oh_encode", categorical_pipe, categorical),
]
)
knn_pipe = Pipeline([("prep", transformer), ("knn", KNeighborsClassifier())])
# Fit/predict/score
_ = knn_pipe.fit(X_train, y_train)
preds = knn.predict(X_test)
当我 运行 它时,它非常适合数据,但我无法评分或做出预测,因为我收到此错误:
ValueError: could not convert string to float: 'G'
The above exception was the direct cause of the following exception:
ValueError: Unable to convert array of bytes/strings into decimal numbers with dtype='numeric'
这是一个分类问题,所以我认为错误的原因是因为我没有对目标进行编码。但即使在目标上使用 LabelEncode 之后,我仍然遇到同样的错误。 可能是什么原因?我也尝试过其他模型的管道。错误是一样的。顺便说一句,我正在使用 Seaborn 的内置钻石数据集。
您似乎没有用 knn_pipe
预测 X_test
的值。您在最后一行中使用的变量 knn
实际上在您提供的示例中未定义。我猜你已经在原始文件的某处定义了它,因此看到了这个错误消息。
总之,改变一下
preds = knn.predict(X_test)
到
preds = knn_pipe.predict(X_test)
它会起作用。