【问题标题】:python(numpy) -- another way to produce array (from another array)python(numpy)——另一种产生数组的方法(从另一个数组)
【发布时间】:2011-10-28 15:39:15
【问题描述】:

我做了这个代码:

from scitools.std import *


npoints=10

vectorpoint=array(random.uniform(-1,1,[1,2]))
experiment=array(random.uniform(-1,1,[npoints,2]))
print("vectorpoint=",vectorpoint)
print("experiment=",experiment)
print(vectorpoint.shape)
print(experiment.shape)

效果很好。 我想问一下“experiment”数组是否可以写成另一种方式,例如“experiment=[vectorpoint,npoints]”。我想使用vectorpoint数组。

(我不想再写一遍“random.uniform(-1,1,[npoints,2])”。

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    如果您希望experiment 是一个数组,其中npoints 行都等于vectorpoint,您可以使用

    experiment = vstack([vectorpoint] * npoints)
    

    如果您希望experiment 拥有由random.uniform() 独立生成的npoints 行,则必须再次调用后一个函数,因为vectorpoint 仅包含random.uniform() 返回的数值,并且没有关于如何操作的信息它是生成的。如果重复困扰您,您可以将其移至函数:

    def uniform(lines):
        return random.uniform(-1, 1, [lines, 2])
    

    (请注意,您对array 的使用是多余的——random.uniform() 的返回值已经是一个数组。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多