如何深度2元组转换为2D numpy的阵列? [英] How to convert a tuple of depth 2 to a 2D Numpy array?

查看:178
本文介绍了如何深度2元组转换为2D numpy的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code不会产生我想要的东西;为了每个元组转换成一个元组里面一个numpy的阵列,因此让我与多个索引检索值的选项。

 导入numpy的是NP
一个= np.asarray([1,2,3],[2,3,4,5])
打印一

输出为错误:

  IndexError:太多的指标

不过,我希望它恢复是什么1,因为第一个元组第一元组第一价值观是其中之一。我应该怎样做这样的转换发生的?

更新:
有趣的是,当我尝试类似:

  A = np.asarray([np.asarray([1,2,3]),np.asarray([2,3,4,5])])
B = np.asarray([np.asarray([1,2,3]),np.asarray([2,3,4,5])])
打印np.multiply(A,B)

这是生成所需的输出!它通过元素乘法是元素。

  [阵列([1,4,9])阵列([4,9,16,25])]


解决方案

您不能直接转换成你的榜样,为numpy的数组,因为你有不同的长度。你得到的结果是一维数组numpy的持有Python列表对象。我见过你想什么做被称为锯齿数组,但不知道这是任何一种为官一任的。

您可以垫零或元素用一个稀疏矩阵,或者干脆无法转换为numpy的。取决于你的总体目标。

为让您一开始这里是你如何设置从交错数组一个蒙面的数组,并计算沿轴的总和。有人谁使用这个模块比我更可能是能够提出一些更有效或成语:

 >>>一个= np.array([[[1,2,3],[2,3,4,5],[2,2]],[[3,4,5,6,7],[1], [2,3,10]]])
>>> D = MAX(LEN(X)对于x y中的在Y)
>>>软垫= [[X + [0] *(D-LEN(X))为在Y X] y的在一个]
>>>掩模= [[[0] *的len(x)的+ [1] *(D-LEN(X))为在Y X] y的在一个]
>>>结果= np.ma.masked_array(填充,np.array(面具,DTYPE = np.bool))
>>>结果
masked_array(数据=
 [[[1 2 3 - - ]
  [2 3 4 5 - ]
  [2 2 - - - ]] [[3 4 5 6 7]
  [1 - - - - ]
  [2 3 10 - - ]]],
             面膜=
 [[[假假假真真]
  [假假假假真]
  [伪假真真真] [假假假假假]
  [假真真真真]
  [假假假真真]]],
       fill_value = 999999)>>> np.sum(结果,轴= -1)
masked_array(数据=
 [6月14日4]
 [25 1 15],
             面膜=
 [假假假]
 [假假假],
       fill_value = 999999)>>>

The following code does not generate what I want; To convert each tuple inside a tuple to a Numpy array therefore giving me the option to retrieve the values with multiple indexes.

import numpy as np
a=np.asarray([[1,2,3],[2,3,4,5]])
print a

Output is the error:

IndexError: too many indices 

However what I want it to retrieve is 1, because the first tuples first tuples first values is one. How should I make such a conversion to happen?

Update: Interestingly when I try something like:

a=np.asarray([np.asarray([1,2,3]),np.asarray([2,3,4,5])])
b=np.asarray([np.asarray([1,2,3]),np.asarray([2,3,4,5])])
print np.multiply(a,b)

That generates the desired output! which is element by element multiplication.

[array([1, 4, 9]) array([ 4,  9, 16, 25])]

解决方案

You can't convert your example directly to a NumPy array because you have differing lengths. The result you are getting is a 1d NumPy array which holds Python list objects. I've seen what you're trying to do referred to as a jagged array but not sure if that's any kind of official term.

You could pad the elements with zeros or use a sparse matrix, or simply not convert to NumPy. Depends on your overall goal.

To get you started here's how you can set up a masked array from a jagged array and compute the sum along an axis. Someone who uses this module more than me may be able to suggest something more efficient or idiomatic:

>>> a = np.array([[[1,2,3],[2,3,4,5], [2, 2]],[[3,4,5,6,7],[1],[2,3,10]]])
>>> D = max(len(x) for x in y for y in a)
>>> padded = [[x + [0] * (D-len(x)) for x in y] for y in a]
>>> mask = [[[0] * len(x) + [1] * (D-len(x)) for x in y] for y in a]
>>> result = np.ma.masked_array(padded, np.array(mask, dtype=np.bool))
>>> result
masked_array(data =
 [[[1 2 3 -- --]
  [2 3 4 5 --]
  [2 2 -- -- --]]

 [[3 4 5 6 7]
  [1 -- -- -- --]
  [2 3 10 -- --]]],
             mask =
 [[[False False False  True  True]
  [False False False False  True]
  [False False  True  True  True]]

 [[False False False False False]
  [False  True  True  True  True]
  [False False False  True  True]]],
       fill_value = 999999)

>>> np.sum(result, axis=-1)
masked_array(data =
 [[6 14 4]
 [25 1 15]],
             mask =
 [[False False False]
 [False False False]],
       fill_value = 999999)

>>> 

这篇关于如何深度2元组转换为2D numpy的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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