收到一个奇怪的错误 'Reshape your data either using array.reshape(-1, 1)'
Getting a weird error that says 'Reshape your data either using array.reshape(-1, 1)'
我正在测试这段代码。
# Import the necessary packages
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Normalizer
from sklearn.cluster import KMeans
# Define a normalizer
normalizer = Normalizer()
# Create Kmeans model
kmeans = KMeans(n_clusters = 10,max_iter = 1000)
# Make a pipeline chaining normalizer and kmeans
pipeline = make_pipeline(normalizer,kmeans)
# Fit pipeline to daily stock movements
pipeline.fit(score)
labels = pipeline.predict(score)
这一行抛出错误:
pipeline.fit(score)
这是我看到的错误:
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.
我不知道这个错误是什么意思。我用谷歌搜索,没有找到任何有用的东西。这是我的一小部分数据样本:
array=[1. 1. 1. ... 8. 1. 1.].
我正在按照下面 link 中的示例进行操作。
当我 运行 来自 link 的代码时,一切正常。我不确定为什么当我 运行 我自己数据上的代码时它会崩溃,这只是:
1, 1.9, 2.62, 3.5, 4.1, 7.7, 9.75, etc, etc.
从 1 到 10。仅此而已。
问题可能出在您的数据格式上。大多数模型都需要一个数据框
任何 sklearn.Transformer
都需要一个 [sample size, n_features]
大小的数组。所以有两种情况你将不得不重塑你的数据,
- 如果只有一个样本,则需要将其整形为 [1, n_features] 大小的数组
- 如果您只有一个特征,则需要将其重塑为 [样本大小,1] 大小的数组
所以你需要做适合问题的事情。您正在传递一维向量。
[1. 1. 1. ... 8. 1. 1.]
如果这是单个样本,将其整形为 (1, -1) 大小的数组,就可以了。但话虽如此,您可能需要考虑以下内容。
- 如果这是单个样本,则用单个样本拟合模型没有意义。你不会得到任何好处。
- 如果这是一组具有单一特征的样本,我真的看不出在这样的数据集上执行 K-means 有什么好处。
我正在测试这段代码。
# Import the necessary packages
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Normalizer
from sklearn.cluster import KMeans
# Define a normalizer
normalizer = Normalizer()
# Create Kmeans model
kmeans = KMeans(n_clusters = 10,max_iter = 1000)
# Make a pipeline chaining normalizer and kmeans
pipeline = make_pipeline(normalizer,kmeans)
# Fit pipeline to daily stock movements
pipeline.fit(score)
labels = pipeline.predict(score)
这一行抛出错误:
pipeline.fit(score)
这是我看到的错误:
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.
我不知道这个错误是什么意思。我用谷歌搜索,没有找到任何有用的东西。这是我的一小部分数据样本:
array=[1. 1. 1. ... 8. 1. 1.].
我正在按照下面 link 中的示例进行操作。
当我 运行 来自 link 的代码时,一切正常。我不确定为什么当我 运行 我自己数据上的代码时它会崩溃,这只是:
1, 1.9, 2.62, 3.5, 4.1, 7.7, 9.75, etc, etc.
从 1 到 10。仅此而已。
问题可能出在您的数据格式上。大多数模型都需要一个数据框
任何 sklearn.Transformer
都需要一个 [sample size, n_features]
大小的数组。所以有两种情况你将不得不重塑你的数据,
- 如果只有一个样本,则需要将其整形为 [1, n_features] 大小的数组
- 如果您只有一个特征,则需要将其重塑为 [样本大小,1] 大小的数组
所以你需要做适合问题的事情。您正在传递一维向量。
[1. 1. 1. ... 8. 1. 1.]
如果这是单个样本,将其整形为 (1, -1) 大小的数组,就可以了。但话虽如此,您可能需要考虑以下内容。
- 如果这是单个样本,则用单个样本拟合模型没有意义。你不会得到任何好处。
- 如果这是一组具有单一特征的样本,我真的看不出在这样的数据集上执行 K-means 有什么好处。