简体   繁体   English

将1D阵列合并为2D阵列

[英]Merging 1D arrays into a 2D array

Is there a built-in function to join two 1D arrays into a 2D array? 是否有内置函数将两个1D阵列连接成2D阵列? Consider an example: 考虑一个例子:

X=np.array([1,2])
y=np.array([3,4])
result=np.array([[1,3],[2,4]])

I can think of 2 simple solutions. 我可以想到两个简单的解决方案。 The first one is pretty straightforward. 第一个非常简单。

np.transpose([X,y])

The other one employs a lambda function. 另一个使用lambda函数。

np.array(list(map(lambda i: [a[i],b[i]], range(len(X)))))

While the second one looks more complex, it seems to be almost twice as fast as the first one. 虽然第二个看起来更复杂,但它似乎几乎是第一个的两倍。

Edit A third solution involves the zip() function. 编辑第三个解决方案涉及zip()函数。

np.array(list(zip(X, y)))

It's faster than the lambda function but slower than column_stack solution suggested by @Divakar. 它比lambda函数更快,但比@Divakar建议的column_stack解决方案慢。

np.column_stack((X,y))

Take into consideration scalability. 考虑可扩展性。 If we increase the size of the arrays, fully numpy command solutions are quite faster: 如果我们增加数组的大小,那么完全numpy命令解决方案会更快:

np.random.seed(1234)
X = np.random.rand(10000)
y = np.random.rand(10000)

%timeit np.array(list(map(lambda i: [X[i],y[i]], range(len(X)))))
6.64 ms ± 32.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit np.array(list(zip(X, y)))
4.53 ms ± 33.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit np.column_stack((X,y))
19.2 µs ± 30.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit np.transpose([X,y])
16.2 µs ± 247 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit np.vstack((X, y)).T
14.2 µs ± 94.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Taking into account all proposed solutions, np.vstack(X,y).T is the fastest when increasing the size of arrayas X and y . 考虑到所有提出的解决方案,当增加arrayas Xy的大小时, np.vstack(X,y).T是最快的。

This is one way: 这是一种方式:

import numpy as np
X = np.array([1,2])
y = np.array([3,4])
result = np.vstack((X, y)).T
print(result)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM