如何为数组正确使用 numpy.c_ [英] How to use numpy.c_ properly for arrays

查看:51
本文介绍了如何为数组正确使用 numpy.c_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要矢量化的函数.在函数内部,我有以下代码.

I have a function which I am trying to vectorize. Inside the function I have the following code.

A = np.c_[xdata, ydata, np.ones(len(zdata))]

其中 x_data、y_data、z_data 都是 1x5 数组,例如.[1,2,3,4,5].A 的结果输出为

Where x_data, y_data, z_data are all 1x5 array, eg. [1,2,3,4,5]. The resulting output for A would be

array([[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]])

我想将函数的这一部分转换为处理 x、y、z 的输入数组(例如 5 列的 1000 行).我天真地试图将数组输入到这个函数中,第一行的输出如下.

I would like to convert this part of the function to work on an array of inputs (eg. 1000 rows of 5 columns) for x, y, z. I naively tried to just feed the arrays into this function with the following output for the first row.

array([1.90155189, 2.44148892, 1.65259189, 2.52045732, 1.53516213,
       1.64412979, 1.73851717, 2.10693759, 2.30939049, 2.39788003,
       1.        ])

这是第一个结果的输入示例:

Here is an example for the inputs for the first result:

x=[1.90155189 2.44148892 1.65259189 2.52045732 1.53516213]
y=[1.64412979 1.73851717 2.10693759 2.30939049 2.39788003]
z=[0.23273446 0.57301046 0.89755946 0.07169598 0.41394575]

假设现在我有第二种方法的以下数据:

Let's say now I have the following data for the second method:

x_array = [[1.90155189 2.44148892 1.65259189 2.52045732 1.53516213],
           [1.90155189 2.44148892 1.65259189 2.52045732 1.53516213],
           [1.90155189 2.44148892 1.65259189 2.52045732 1.53516213]]
y_array = [[1.64412979 1.73851717 2.10693759 2.30939049 2.39788003],
           [1.64412979 1.73851717 2.10693759 2.30939049 2.39788003],
           [1.64412979 1.73851717 2.10693759 2.30939049 2.39788003]]
z_array = [[0.23273446 0.57301046 0.89755946 0.07169598 0.41394575],
           [0.23273446 0.57301046 0.89755946 0.07169598 0.41394575],
           [0.23273446 0.57301046 0.89755946 0.07169598 0.41394575]]

预期输出为

     [[[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]],
       [[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]],
      [[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]]]

推荐答案

你可以使用这个:

# new_A = np.stack((x,y,np.ones_like(z)), axis=1).swapaxes(1,2)
new_A = np.stack((x,y,np.ones_like(z)), axis=2)

测试:

THOUSAND = 6
x = np.random.randint(1,5,size=(THOUSAND,5))
y = np.random.randint(1,5,size=(THOUSAND,5))
z = np.random.randint(1,5,size=(THOUSAND,5))

print (x)
print (y)
print (z)

new_A = np.stack((x,y,np.ones_like(z)), axis=1).swapaxes(1,2)
print (new_A)

输出:

[[1 2 2 1 1]      # print(x)
 [4 4 4 4 4]
 [1 2 1 3 3]
 [2 3 1 4 4]
 [1 1 4 1 4]
 [4 1 3 3 2]]
[[2 2 3 4 4]      # print(y)
 [1 1 4 2 1]
 [3 3 1 1 2]
 [1 1 2 1 3]
 [3 2 1 4 3]
 [4 4 1 3 2]]
[[3 4 3 2 2]      # print(z)
 [4 2 4 3 3]
 [3 3 4 1 4]
 [4 3 3 3 1]
 [4 1 1 3 3]
 [4 1 4 3 3]]

# new_A output

[[[1 2 1]      # print(new_A)
  [2 2 1]
  [2 3 1]
  [1 4 1]
  [1 4 1]]

 [[4 1 1]
  [4 1 1]
  [4 4 1]
  [4 2 1]
  [4 1 1]]

 [[1 3 1]
  [2 3 1]
  [1 1 1]
  [3 1 1]
  [3 2 1]]

 [[2 1 1]
  [3 1 1]
  [1 2 1]
  [4 1 1]
  [4 3 1]]

 [[1 3 1]
  [1 2 1]
  [4 1 1]
  [1 4 1]
  [4 3 1]]

 [[4 4 1]
  [1 4 1]
  [3 1 1]
  [3 3 1]
  [2 2 1]]]

这篇关于如何为数组正确使用 numpy.c_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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