Python 如何高效地连接2个Numpy阵列?

Python 如何高效地连接2个Numpy阵列?,python,numpy,Python,Numpy,我有两个Numpy数组,形状为(10,)(10,6),我想将第一个数组与第二个数组合并。下面提供的numpy阵列 r1 ['467c8100-7f13-4244-81ee-5e2a0f8218a8', '71a4b5b2-80d6-4c12-912f-fc71be8d923e', '7a3e0168-e47d-4203-98f2-a54a46c62ae0', '7dfd43e7-ced1-435f-a0f9-80cfd00ae246', '85dbc70e-c773-43ee-b434

我有两个Numpy数组
,形状为
(10,)(10,6)
,我想将第一个数组与第二个数组合并。下面提供的numpy阵列

r1 
['467c8100-7f13-4244-81ee-5e2a0f8218a8',
 '71a4b5b2-80d6-4c12-912f-fc71be8d923e',
 '7a3e0168-e47d-4203-98f2-a54a46c62ae0',
 '7dfd43e7-ced1-435f-a0f9-80cfd00ae246',
 '85dbc70e-c773-43ee-b434-8f458d295d10',
 'a56b2bc3-4a81-469e-bc5f-b3aaa520db05',
 'a9e8996f-ff35-4bfb-bbd9-ede5ffecd4d8',
 'c3037410-0c2e-40f8-a844-ac0664a05783',
 'c5618563-10c0-425b-a11b-2fcf931f0ff7',
 'f65e6cea-892e-4335-8e86-bf7f083b5f53'] 

r2 
[[1.55000000e+02, 5.74151515e-01, 1.55000000e+02, 5.74151515e-01, 3.49000000e+02, 1.88383585e+00],
 [5.00000000e+00, 1.91871554e-01, 1.03000000e+02, 1.22893828e+00, 2.95000000e+02, 3.21148368e+00],
 [7.10000000e+01, 1.15231270e-01, 2.42000000e+02, 5.78527276e-01, 4.09000000e+02, 2.67915246e+00],
 [3.60000000e+01, 7.10066720e-01, 2.42000000e+02, 1.80213634e+00, 4.12000000e+02, 4.16314391e+00],
 [1.15000000e+02, 1.05120284e+00, 1.30000000e+02, 1.71697773e+00, 2.53000000e+02, 2.73640301e+00],
 [4.70000000e+01, 2.19434656e-01, 3.23000000e+02, 4.84093786e+00, 5.75000000e+02, 7.00530186e+00],
 [5.50000000e+01, 1.22614463e+00, 1.04000000e+02, 1.55392099e+00, 4.34000000e+02, 4.13661261e+00],
 [3.90000000e+01, 3.34816889e-02, 1.10000000e+02, 2.54431753e-01, 2.76000000e+02, 1.52322736e+00],
 [3.43000000e+02, 2.93550948e+00, 5.84000000e+02, 5.27968165e+00, 7.45000000e+02, 7.57657633e+00],
 [1.66000000e+02, 1.01436635e+00, 2.63000000e+02, 2.69197514e+00, 8.13000000e+02, 7.96477735e+00]]
我试图使用命令
np进行连接。concatenate((r1,r2))
,它返回的消息是
ValueError:所有输入数组必须具有相同数量的维度,我不理解。因为,
r1
可能与
r2
相结合,并可以形成一个全新的数组,从而形成一个新的
10x7
数组

如何解决此问题?

您可以
r1
使其成为二维,并指定阵列应沿其连接的
轴:

将numpy导入为np
r1=np.one((10,))
r2=np.零((10,6))
np.连接((r1.重塑(10,1),r2),轴=1)
您可以
r1
使其成为二维,并指定阵列应沿其连接的
轴:

将numpy导入为np
r1=np.one((10,))
r2=np.零((10,6))
np.连接((r1.重塑(10,1),r2),轴=1)

Numpy提供了一种沿第二个轴连接的简单方法

np.c_[r2,r1]

Numpy提供了沿第二个轴连接的简单方法

np.c_[r2,r1]

这两个阵列的数据类型和形状不匹配:

In [174]: r1.shape
Out[174]: (10,)
In [175]: r1.dtype
Out[175]: dtype('<U36')

In [177]: r2.shape
Out[177]: (10, 6)
In [178]: r2.dtype
Out[178]: dtype('float64')
混合字符串和浮点的一种方法是使用结构化数组,例如:

In [185]: res=np.zeros((10,),dtype='U36,(6)f')
In [186]: res.dtype
Out[186]: dtype([('f0', '<U36'), ('f1', '<f4', (6,))])
In [187]: res['f0']=r1
In [188]: res['f1']=r2
In [192]: res.shape
Out[192]: (10,)
In [193]: res[0]
Out[193]: ('467c8100-7f13-4244-81ee-5e2a0f8218a8', [ 155.        ,    0.57415152,  155.        ,    0.57415152,  349.        ,    1.88383579])
[185]中的
:res=np.zero((10,),dtype='U36,(6)f')
In[186]:res.dtype

Out[186]:数据类型([('f0','p>这两个数组的数据类型和形状不匹配:

In [174]: r1.shape
Out[174]: (10,)
In [175]: r1.dtype
Out[175]: dtype('<U36')

In [177]: r2.shape
Out[177]: (10, 6)
In [178]: r2.dtype
Out[178]: dtype('float64')
混合字符串和浮点的一种方法是使用结构化数组,例如:

In [185]: res=np.zeros((10,),dtype='U36,(6)f')
In [186]: res.dtype
Out[186]: dtype([('f0', '<U36'), ('f1', '<f4', (6,))])
In [187]: res['f0']=r1
In [188]: res['f1']=r2
In [192]: res.shape
Out[192]: (10,)
In [193]: res[0]
Out[193]: ('467c8100-7f13-4244-81ee-5e2a0f8218a8', [ 155.        ,    0.57415152,  155.        ,    0.57415152,  349.        ,    1.88383579])
[185]中的
:res=np.zero((10,),dtype='U36,(6)f')
In[186]:res.dtype

Out[186]:数据类型([('f0','结果数组应该是什么样子?
10 x 7
维度和
r1
的每个元素,在numpy数组中有前面的行,例如:
['467c8100-7f13-4244-81ee-5e2a0f8218a8',1.55000000e+02 5.74151515e-01 1.55000000e+02 5.74151515e-01 3.490000e+02 1.88383585e+00]
Quick question-是否继续将列串联到循环中的结果?如果是这样,您应该尝试重新构造程序,这样您就不需要这样做。这是处理NumPy数组可能最不有效的方法之一,因为您每次都需要重新复制所有数据。这就是我所拥有的,我用我们想从这里连接。每一行应该像
['467c8100-7f13-4244-81ee-5e2a0f8218a8',1.55000000e+02 5.74151515e-01 1.55000000e+02 5.74151515e-01 3.4900000E+02 1.88383585e+00]
结果数组应该是什么样子?
10 x 7
维度和
r1
的每个元素,以及numpy数组中的前置行,例如:
['467c8100-7f13-4244-81ee-5e2a0f8218a8',1.55000000e+02 5.74151515e-01 1.55000000e+02 5.74151515e-01 3.4900000E+02 1.88383585e+00]
Quick question-是否继续将列串联到循环中的结果?如果是这样,您应该尝试重新构造程序,这样您就不需要这样做。这是处理NumPy数组可能最不有效的方法之一,因为您每次都需要重新复制所有数据。这就是我所拥有的,我用我们想从这里连接。每一行应该像
['467c8100-7f13-4244-81ee-5e2a0f8218a8',1.55000000e+02 5.74151515e-01 1.55000000e+02 5.74151515e-01 3.4900000E+02 1.88383585e+00]