转换整数列表,元组到一个数组numpy的 [英] Converting a list of ints, tuples into an numpy array

查看:1038
本文介绍了转换整数列表,元组到一个数组numpy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表[浮动(浮动,浮动,浮动..)] ...这基本上是每个点的适应值沿n维点。
对于如。

I have a list of [float, (float,float,float..) ] ... Which is basically an n-dimensional point along with a fitness value for each point. For eg.

4.3, (2,3,4)
3.2, (1,3,5)
.
.
48.2, (23,1,32)

我要随机抽样基于适应值一分。我决定做这将是使用 numpy.random.choice(范围(N),1,plist中[:,:1,:1])的最佳方式

不过,我需要将其转换成numpy的数组,为此,我试图

However, i need to convert this into an numpy array, for which i tried

>> pArr = np.array( plist ) 
ValueError: setting an array element with a sequence

我得到了np.asarray(的plist)相同的错误以及..任何建议??

I got the same error for np.asarray(plist) as well.. any suggestions??

推荐答案

下面应该工作:

A = np.array([tuple(i) for i in initial_list],dtype=[('fitness',float),('point',(float,3))])

initial_list = [[4.3,(2,3,4)],[3.2,(1,3,5)],...] 。需要注意的是,我们需要initial_list 变换的每一个项目成一个元组的把戏工作,否则numpy的无法识别的结构。

with initial_list = [[4.3, (2, 3, 4)], [3.2, (1, 3, 5)], ...]. Note that we need to transform each item of initial_list into a tuple for that trick to work, else NumPy cannot recognize the structure.

您的健身条目为 A现在访问['健身'] ,以及相应的点作为 A ['点'] 。如果您选择了实际的健身条目列表,指数,对应的点由 A ['点'] [指数] ,这是一个简单的(N,3)阵列。

Your fitness entries are now accessible as A['fitness'], with the corresponding points as A['point']. If you select a list of actual fitness entries, indices, the corresponding points are given by A['point'][indices], which is a simple (n,3) array.

这篇关于转换整数列表,元组到一个数组numpy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆