列表到ndarray

2024-06-23 19:11:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用scipy中的kmeans集群,正是这里的一个:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html#scipy.cluster.vq.kmeans

我要做的是转换一个列表列表,如下所示:

data without_x[
[0, 0, 0, 0, 0, 0, 0, 20.0, 1.0, 48.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1224.0, 125.5, 3156.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 22.5, 56.0, 41.5, 85.5, 0, 0, 0, 0, 0, 0, 0, 0, 1495.0, 3496.5, 2715.0, 5566.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

进入ndarry以便与Kmeans方法一起使用。当我试图将list的列表转换为ndarray时,得到一个空数组,从而使整个分析无效。ndarray的长度是可变的,它取决于收集的样本数。但我可以很容易地 len(没有x的数据)

下面是返回空列表的代码片段。

import numpy as np
import "other functions"

data, data_without_x = data_preparation.generate_sampled_pdf()
nodes_stats, k, list_of_list= result_som.get_number_k()

data_array = np.array(data_without_x)
whitened = whiten(data_array)
centroids, distortion = kmeans(whitened, int(k), iter=100000)

这就是我在一个简单的日志文件中保存的输出:

___________________________
this is the data array[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
___________________________
This is the whitened array[[ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 ..., 
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]]
___________________________

有人知道当我试图将列表列表转换成numpy.array时会发生什么吗?

谢谢你的帮助


Tags: importnumpy列表datanpscipynanarray
3条回答

vq.whitenvq.kmeans需要一个形状数组(M, N),其中每行是一个观察值。所以把你的data_array转置:

import numpy as np
import scipy.cluster.vq as vq
np.random.seed(2013)    

data_without_x = [
    [0, 0, 0, 0, 0, 0, 0, 20.0, 1.0, 48.0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        1224.0, 125.5, 3156.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 22.5, 56.0, 41.5, 85.5, 0, 0, 0, 0, 0, 0, 0, 0, 1495.0,
        3496.5, 2715.0, 5566.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]


data_array = np.array(data_without_x).T
whitened = vq.whiten(data_array)

centroids, distortion = vq.kmeans(whitened, 5)
print(centroids)

收益率

[[  1.22649791e+00   2.69573144e+00]
 [  3.91943108e-03   5.57406434e-03]
 [  5.73668382e+00   4.83161524e+00]
 [  0.00000000e+00   1.29763133e+00]]

使用numpy的array函数。 很简单: 参考号:https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html

这正是在python中如何将列表列表转换为ndarray的方法。你确定没有x的数据填写正确吗?在我的机器上:

data = [[1,2,3,4],[4,5,6,7,8]]
data_arr = np.array(data)

data_arr
array([[1,2,3,4],
       [5,6,7,8]])

这就是我认为你期望的行为

看看你的输入你有很多零…记住打印出来的并不是全部。你可能只是从你的输入中看到了所有的“零”。检查特定的非零元素以确保

相关问题 更多 >

    热门问题