Is there a built-in function to join two 1D arrays into a 2D array? Consider an example:
是否有内置函数将两个1D阵列连接成2D阵列?考虑一个例子:
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))
2 个解决方案
#1
2
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 X和y的大小时,np.vstack(X,y).T是最快的。
#2
1
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]]
#1
2
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 X和y的大小时,np.vstack(X,y).T是最快的。
#2
1
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]]