gpt4 book ai didi

python - scikit 学习在一定数量的数据上崩溃

转载 作者:行者123 更新时间:2023-11-28 19:21:47 25 4
gpt4 key购买 nike

我正在尝试处理一个包含 71,000 行 200 列 float 的 numpy 数组,当我超过 5853 行时,我正在尝试的两个 sci-kit 学习模型都会给出不同的错误。我尝试删除有问题的行,但它仍然失败。 sci-kit 学习不能处理这么多数据,还是别的什么? X 是列表列表的 numpy 数组。

KNN:

nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)

错误:

File "knn.py", line 48, in <module>
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.py", line 642, in fit
return self._fit(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.py", line 180, in _fit
raise ValueError("data type not understood")

ValueError:数据类型不理解

K-均值:

kmeans_model = KMeans(n_clusters=2, random_state=1).fit(X)

错误:

Traceback (most recent call last):
File "knn.py", line 48, in <module>
kmeans_model = KMeans(n_clusters=2, random_state=1).fit(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/cluster/k_means_.py", line 702, in fit
X = self._check_fit_data(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/cluster/k_means_.py", line 668, in _check_fit_data
X = atleast2d_or_csr(X, dtype=np.float64)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 134, in atleast2d_or_csr
"tocsr", force_all_finite)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 111, in _atleast2d_or_sparse
force_all_finite=force_all_finite)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 91, in array2d
X_2d = np.asarray(np.atleast_2d(X), dtype=dtype, order=order)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 235, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

最佳答案

请检查矩阵 Xdtype,例如通过键入 X.dtype。如果是objectdtype('O'),则将X的行长度写入数组:

lengths = [len(line) for line in X]

然后通过调用查看所有行是否具有相同的长度

np.unique(lengths)

如果输出中有多个数字,那么你的行长度不同,例如从第 5853 行开始,但可能并非一直如此。

Numpy 数据数组仅在所有行都具有相同长度时才有用(如果不同,它们会继续工作,但不会按照您的期望进行。)。您应该检查导致此问题的原因,更正它,然后返回 knn

这是一个例子,说明如果行的长度不同会发生什么:

import numpy as np
rng = np.random.RandomState(42)
X = rng.randn(100, 20)
# now remove one element from the 56th line
X = list(X)
X[55] = X[55][:-1]
# turn it back into an ndarray
X = np.array(X)
# check the dtype
print X.dtype # returns dtype('O')

from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors()
nbrs.fit(X) # raises your first error

from sklearn.cluster import KMeans
kmeans = KMeans()
kmeans.fit(X) # raises your second error

关于python - scikit 学习在一定数量的数据上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com