Python ValueError:使用序列设置数组元素。在DBSCAN上,没有缺失维度
我在一个数据集上使用DBSCAN.fit(),该数据集实际上是一个包含向量化单词的单列,所有维度都相同,30。看起来是这样的:Python ValueError:使用序列设置数组元素。在DBSCAN上,没有缺失维度,python,pandas,valueerror,dbscan,Python,Pandas,Valueerror,Dbscan,我在一个数据集上使用DBSCAN.fit(),该数据集实际上是一个包含向量化单词的单列,所有维度都相同,30。看起来是这样的: df['column'] 2 [-0.003417029886667123, -0.0016105849274073794... 3 [-0.24330333298729837, 0.48110865717035506, 0.... 4 [-0.0017016271879120766, 0.01266130386650884, ...
df['column']
2 [-0.003417029886667123, -0.0016105849274073794...
3 [-0.24330333298729837, 0.48110865717035506, 0....
4 [-0.0017016271879120766, 0.01266130386650884, ...
5 [0.002174357210089775, 0.004633570752676618, 0...
6 [0.008567001972125537, 0.0012244984475515731, ...
matrix = df['column'].as_matrix()
#DBSCAN inplementation
db = DBSCAN(eps=0.06, min_samples=1)
db.fit(matrix)
clusters = db.labels_.tolist()
然而,在拟合数据后,我得到以下回溯:
----> 4 db.fit(matrix)
5 clusters = db.labels_.tolist()
/opt/conda/lib/python3.6/site-packages/sklearn/cluster/dbscan_.py in fit(self, X, y, sample_weight)
280
281 """
--> 282 X = check_array(X, accept_sparse='csr')
283 clust = dbscan(X, sample_weight=sample_weight,
284 **self.get_params())
/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
431 force_all_finite)
432 else:
--> 433 array = np.array(array, dtype=dtype, order=order, copy=copy)
434
435 if ensure_2d:
ValueError: setting an array element with a sequence.
我已经读到这个错误与一个或多个数组与其他数组的len不同有关。然而,就我而言,这似乎不是问题所在,请看以下内容:
set(np.array([m]).shape[0] for m in matrix)
>> {1}
set(np.array([m]).shape[1] for m in matrix)
>> {30}
如您所见,所有阵列都具有相同的len。因此,问题可能出在哪里?将功能转换为数组的方式并没有将其转换为数组,而是转换为列表数组,这就是您看到此错误的原因
您可以做的是将内部列表转换为数组事实上,我最后要做的是np.appay(df['column'].to_list())