简体   繁体   English

从其他numpy数组创建numpy数组

[英]Create numpy array from other numpy array

I have this array: 我有这个数组:

data = [[  1.00000000e-01   1.00000000e-03]
 [  2.00000000e-01   2.00000000e-03]
 [  3.00000000e-01   3.00000000e-03]
 [  4.00000000e-01   4.00000000e-03]
 [  5.00000000e-01   5.00000000e-03]
 [  6.00000000e-01   6.00000000e-03]
 [  7.00000000e-01   7.00000000e-03]
 [  8.00000000e-01   8.00000000e-03]
 [  9.00000000e-01   9.00000000e-03]
 [  1.00000000e+00   1.00000000e-02]
 [  1.10000000e+00   1.10000000e-02]
 [  1.20000000e+00   1.20000000e-02]
 [  1.30000000e+00   1.30000000e-02]
 [  1.40000000e+00   5.23359560e-02]
 [  1.50000000e+00   1.04528463e-01]
 [  1.60000000e+00   1.56434465e-01]
 [  1.70000000e+00   2.07911691e-01]
 [  1.80000000e+00   2.58819045e-01]
 [  1.90000000e+00   3.09016994e-01]
 [  2.00000000e+00   3.58367950e-01]
 [  2.10000000e+00   4.06736643e-01]
 [  2.20000000e+00   4.53990500e-01]
 [  2.30000000e+00   5.00000000e-01]
 [  2.40000000e+00   5.44639035e-01]
 [  2.50000000e+00   5.87785252e-01]
 [  2.60000000e+00   6.29320391e-01]
 [  2.70000000e+00   6.69130606e-01]
 [  2.80000000e+00   7.07106781e-01]
 [  2.90000000e+00   7.43144825e-01]
 [  3.00000000e+00   7.77145961e-01]
 [  3.10000000e+00   8.09016994e-01]
 [  3.20000000e+00   8.38670568e-01]
 [  3.30000000e+00   8.66025404e-01]
 [  3.40000000e+00   8.91006524e-01]
 [  3.50000000e+00   9.13545458e-01]
 [  3.60000000e+00   9.33580426e-01]
 [  3.70000000e+00   9.51056516e-01]
 [  3.80000000e+00   9.65925826e-01]
 [  3.90000000e+00   9.78147601e-01]
 [  4.00000000e+00   9.87688341e-01]
 [  4.10000000e+00   9.94521895e-01]
 [  4.20000000e+00   9.98629535e-01]
 [  4.30000000e+00   1.00000000e+00]
 [  4.40000000e+00   1.00100000e+00]
 [  4.50000000e+00   1.00200000e+00]
 [  4.60000000e+00   1.00300000e+00]
 [  4.70000000e+00   1.00400000e+00]
 [  4.80000000e+00   1.00500000e+00]
 [  4.90000000e+00   1.00600000e+00]
 [  5.00000000e+00   1.00700000e+00]]

and I need to create a second array from it that is made of the first value of each row: 我需要从中创建第二个数组,该数组由每行的第一个值组成:

rows = [[1.00000000e-01], [2.00000000e-01], ...., [5.00000000e+00]]

Is there a one line way to do this? 有没有一种方法可以做到这一点? Thanks 谢谢

Set up a sample array: 设置一个样本数组:

In [17]: a = np.arange(10).reshape(5,2)

In [18]: a
Out[18]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

Then depending on the shape of the array resulting from the first value in each row: 然后,根据每行第一个值产生的数组形状:

In [19]: a[:,0]
Out[19]: array([0, 2, 4, 6, 8])

or 要么

In [20]: a[:,0][:,None] # or a[:,0,None] or a[:,0,np.newaxis]
Out[20]:
array([[0],
       [2],
       [4],
       [6],
       [8]])

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

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