加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – ndarray列表的列表

发布时间:2020-12-16 21:48:30 所属栏目:Python 来源:网络整理
导读:我试图在scipy中使用kmeans聚类,正是这里的一个: http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html#scipy.cluster.vq.kmeans 我想要做的是转换列表列表,如下所示: data without_x[[0,20.0,1.0,48.0,1224.0,125.5,3156.0

我试图在scipy中使用kmeans聚类,正是这里的一个:

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

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

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

进入一个ndarry,以便与Kmeans方法一起使用.当我尝试将列表列表转换为ndarray时,我得到一个空数组,从而排除了整个分析. ndarray的长度是可变的,它取决于收集的样本数量.但是我可以轻松搞定
????LEN(data_without_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.]]
___________________________
This is the whitened array[[ nan  nan  nan ...,nan  nan  nan]
 [ nan  nan  nan ...,nan  nan  nan]
 ...,[ nan  nan  nan ...,nan  nan  nan]]
___________________________

有没有人知道当我尝试将列表列表转换为numpy.array时会发生什么?

谢谢你的帮助

最佳答案
这正是如何将列表列表转换为python中的ndarray.您确定您的data_without_x填写正确吗?在我的机器上:

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

data_arr
array([[1,[5,8]])

这是我认为你期待的行为

看看你的输入你有很多零…请记住,打印输出并没有显示所有这些.您可能只是看到输入中的所有“零”.检查特定的非零元素以确定

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读