参数numpy的的fromfunction [英] Parameters to numpy's fromfunction

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

问题描述

我没有在 numpy的 grokked的关键概念呢。

I haven't grokked the key concepts in numpy yet.

我想创建一个3维数组并填充每​​个单元有一个函数调用的结果 - 即功能将与不同指数多次被调用和返回不同的值

I would like to create a 3-dimensional array and populate each cell with the result of a function call - i.e. the function would be called many times with different indices and return different values.

我可以用零(或空)创建它,然后覆盖一个for循环的每个值,但它似乎更清洁,直接从功能来填充它。

I could create it with zeros (or empty), and then overwrite every value with a for loop, but it seems cleaner to populate it directly from the function.

fromfunction 听起来很完美。 阅读文档它听起来像函数被每个单元调用一次。

fromfunction sounds perfect. Reading the documentation it sounds like the function gets called once per cell.

但是当我真正尝试...

But when I actually try it...

from numpy import *

def sum_of_indices(x, y, z):
    # What type are X, Y and Z ? Expect int or duck-type equivalent.
    # Getting 3 individual arrays
    print "Value of X is:"
    print x

    print "Type of X is:", type(x)
    return x + y + z

a = fromfunction(sum_of_indices, (2, 2, 2))

我希望得到这样的:

I expect to get something like:

Value of X is:
0
Type of X is: int
Value of X is:
1
Type of X is: int

重复4次。

我得到:

Value of X is:
[[[ 0.  0.]
  [ 0.  0.]]

 [[ 1.  1.]
  [ 1.  1.]]]
[[[ 0.  0.]
  [ 1.  1.]]

 [[ 0.  0.]
  [ 1.  1.]]]
[[[ 0.  1.]
  [ 0.  1.]]

 [[ 0.  1.]
  [ 0.  1.]]]
Type of X is: <type 'numpy.ndarray'>

该功能只叫一次,似乎返回整个数组作为结果。

The function is only called once, and seems to return the entire array as result.

什么是基于多次调用索引的功能?

What is the correct way to populate an array based on multiple calls to a function of the indices?

推荐答案

我明明没有说清楚。我得到的反应是 fromfunc 实际工作作为我的测试code证明,我已经知道,因为我的测试code证明它。

I obviously didn't made myself clear. I am getting responses that fromfunc actually works as my test code demonstrates, which I already knew because my test code demonstrated it.

我一直在寻找的答案似乎是分为两部分:

The answer I was looking for seems to be in two parts:

fromfunc 文档是一种误导。它的工作原理来填充一次整个数组。

The fromfunc documentation is misleading. It works to populate the entire array at once.

在特别是,这条线在文档是不正确的(或在最起码,误导性)

In particular, this line in the documentation is incorrect (or at the very minimum, misleading)

例如,如果形状分别为(2,2),则依次参数是(0,0),(0,1),(1,0 ),(1,1)。

For example, if shape were (2, 2), then the parameters in turn be (0, 0), (0, 1), (1, 0), (1, 1).

没有。如果形状(即从背景下,第二个参数 fromfunction )分别为(2,2),参数会是(不是'又',但在唯一的调用):

No. If shape (i.e. from context, the second parameter to the fromfunction) were (2,2), the parameters would be (not 'in turn', but in the only call):

(array([[ 0.,  0.], [ 1.,  1.]]), array([[ 0.,  1.], [ 0.,  1.]]))

(我的简单的例子,从手册中的例子导出,可能是误导,因为 + 可以在阵列作为索引操作为好。这种不确定性是另一个原因为什么文档不清楚欲最终使用未阵列基于一个功能,但是基于单元的 - 比如每个值可能基于索引,或来自用户即使输入一个URL或数据库被读取)。

(My simple example, derived from the examples in the manual, may have been misleading, because + can operate on arrays as well as indices. This ambiguity is another reason why the documentation is unclear. I want to ultimately use a function that isn't array based, but is cell-based - e.g. each value might be fetched from a URL or database based on the indices, or even input from the user.)

返回的问题 - 这是我怎么能填充来自一个名为每个元素一次功能的阵列,答案似乎是:

Returning to the problem - which is how can I populate an array from a function that is called once per element, the answer appears to be:

您不能在实用的风格做到这一点。

You cannot do this in a functional style.

您可以在命令式/迭代风格吧 - 即嵌套书面for循环,并管理指数长度自己

You can do it in an imperative/iterative style - i.e. writing nested for-loops, and managing the index lengths yourself.

你也可以做到这一点作为一个迭代器,但仍迭代器需要跟踪自己的索引。

You could also do it as an iterator, but the iterator still needs to track its own indices.

这篇关于参数numpy的的fromfunction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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