为什么数组形状会出现这个错误,有其他解决办法吗?

问题描述 投票:0回答:1

我不断收到此错误,但我通过重塑数组解决了问题:

data = data.reshape(-1, 1)

我的输出:

Traceback (most recent call last):
  File "C:\Users\USER\Desktop\python\machine-learning\bot4.py", line 93, in <module>
    predictions = model.predict(data)
  File "C:\Users\USER\Desktop\python\machine-learning\machine-learningVenv\lib\site-packages\sklearn\naive_bayes.py", line 105, in predict
    X = self._check_X(X)
  File "C:\Users\USER\Desktop\python\machine-learning\machine-leaningVenv\lib\site-packages\sklearn\naive_bayes.py", line 579, in _check_X
    return self._validate_data(X, accept_sparse="csr", reset=False)
  File "C:\Users\USER\Desktop\python\machine-learning\machine-learningVenv\lib\site-packages\sklearn\base.py", line 546, in _validate_data
    X = check_array(X, input_name="X", **check_params)
  File "C:\Users\USER\Desktop\python\machine-learning\machine-learningVenv\lib\site-packages\sklearn\utils\validation.py", line 902, in check_array
    raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=['The cat is sleeping in the sun.' 'The dog is barking at the moon.'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我期待输出: [{“猫”:“睡觉”,“狗”:“吠叫”}]

python machine-learning scikit-learn
1个回答
0
投票

Scikit 期望二维向量输入,因此 N 个样本的维度为 Nx1,或者具有 F 个特征的单个样本的维度为 1xF。

像您这样的列表

["a", "b"]
没有 2D 形状,这会导致错误。

正如 Snoopy 博士的评论所说,您通常不能传递字符串,您需要对其进行预处理,例如使用 LabelEncoder 和/或 OneHotEncoder

© www.soinside.com 2019 - 2024. All rights reserved.