处理多维数组并尝试有效地连接
Dealing a multidimensional array and trying to concatenate as desired efficiently
我们有:
import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])
data = [a,b,c]
我们想要的:
[[ 1 2 3 4 5 6]
[ 11 22 33 44 55 66]
[ 111 222 333 444 555 666]
[1111 2222 3333 4444 5555 6666]]
根据需要执行任务的代码:
new_arr=[]
for i in range(data[0].shape[0]):
index = []
for j in range(len(data)):
index.extend(data[j][i])
new_arr.append(index)
new_data = np.array(new_arr)
print(new_data)
注意:解决方案是正确的并且得到了所需的输出,但是有没有其他更有效(更快)的方法?
问题:我正在执行一个机器学习任务,训练一个模型需要经过很多次这些循环,使得训练过程非常缓慢。有什么办法可以更有效地解决这个问题?
numpy.hstack
就是你想要的。
import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])
d = np.hstack([a, b, c])
print(d)
输出:
[[ 1 2 3 4 5 6]
[ 11 22 33 44 55 66]
[ 111 222 333 444 555 666]
[1111 2222 3333 4444 5555 6666]]
您可以将 np.concatenate
与 axis=1
一起使用
new_arr = np.concatenate([a, b, c], axis=1)
输出:
>>> new_arr
array([[ 1, 2, 3, 4, 5, 6],
[ 11, 22, 33, 44, 55, 66],
[ 111, 222, 333, 444, 555, 666],
[1111, 2222, 3333, 4444, 5555, 6666]])
我们有:
import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])
data = [a,b,c]
我们想要的:
[[ 1 2 3 4 5 6]
[ 11 22 33 44 55 66]
[ 111 222 333 444 555 666]
[1111 2222 3333 4444 5555 6666]]
根据需要执行任务的代码:
new_arr=[]
for i in range(data[0].shape[0]):
index = []
for j in range(len(data)):
index.extend(data[j][i])
new_arr.append(index)
new_data = np.array(new_arr)
print(new_data)
注意:解决方案是正确的并且得到了所需的输出,但是有没有其他更有效(更快)的方法?
问题:我正在执行一个机器学习任务,训练一个模型需要经过很多次这些循环,使得训练过程非常缓慢。有什么办法可以更有效地解决这个问题?
numpy.hstack
就是你想要的。
import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])
d = np.hstack([a, b, c])
print(d)
输出:
[[ 1 2 3 4 5 6]
[ 11 22 33 44 55 66]
[ 111 222 333 444 555 666]
[1111 2222 3333 4444 5555 6666]]
您可以将 np.concatenate
与 axis=1
new_arr = np.concatenate([a, b, c], axis=1)
输出:
>>> new_arr
array([[ 1, 2, 3, 4, 5, 6],
[ 11, 22, 33, 44, 55, 66],
[ 111, 222, 333, 444, 555, 666],
[1111, 2222, 3333, 4444, 5555, 6666]])