Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何动态减去numpy数组_Arrays_Python 3.x_Numpy - Fatal编程技术网

Arrays 如何动态减去numpy数组

Arrays 如何动态减去numpy数组,arrays,python-3.x,numpy,Arrays,Python 3.x,Numpy,我有一个方法返回一些numpy数组,如下所示 def numpy_array(): ...... ...... return true, test_1, test_2, test_3 def subtraction(): true, test_1, test_2, test_3 = numpy_array() sub_1 = np.subtract(true, test_1) sub_2 = np.subtract(true, test_2) ...... ..

我有一个方法返回一些numpy数组,如下所示

def numpy_array():
  ......
  ......
  return true, test_1, test_2, test_3
def subtraction():
  true, test_1, test_2, test_3 = numpy_array()
  sub_1 = np.subtract(true, test_1)
  sub_2 = np.subtract(true, test_2)
  ......
  ......
  return sub_1, sub_2
我有另一种方法,可以计算
true
其余数组
之间的减法,如下所示

def numpy_array():
  ......
  ......
  return true, test_1, test_2, test_3
def subtraction():
  true, test_1, test_2, test_3 = numpy_array()
  sub_1 = np.subtract(true, test_1)
  sub_2 = np.subtract(true, test_2)
  ......
  ......
  return sub_1, sub_2
问题是,在
def numpy\u array()
方法中可能有很多数组。我想以动态方式编写
def减法()
方法。因此我不需要手动减去数组(
np.subtract(true,test_1)
np.subtract(true,test_2)
,等等)


你能告诉我怎么做吗?

numpy\u array或任何具有多个参数的python函数都将返回一个元组。 然后可以迭代元组以获得所需的差异:

def减法():
矩阵=numpy_数组()
#一些numpy数组的元组
第一个=矩阵[0]
#第一个数组是“true”
结果=[范围(1,len(矩阵))中i的第一矩阵[i]
#第一个数组和后续数组之间的差异
返回元组(结果)

如果您编写的函数没有输入值,那么无论您如何操作,都会遇到问题。最好是
new\u arr=减法(arr1,arr2)
。。。但是numpy已经为您编写了这个函数,
np.subtract
。我看不出你写的函数有什么价值。我不明白你的意思。您是否介意详细说明一下?您是否考虑过将
test\N
阵列转换为单个3x阵列?如果是这样的话,numpy的广播功能可以一步完成所有的减法。什么是
True
应该做的呢?@hpaulj没什么!非常感谢你。但是使用
tuple
的原因是什么?否则它将返回一个列表对象,由列表创建。在python中返回多个参数时,元组是默认值,但如果需要列表,则不需要该位。