简体   繁体   English

如何将 numpy 数组 [(x1,y1),(x2,y2),(x3,y3),...(xn,yn)] 转换为 [(x1,y1,x1^2,y1^2),(x2 ,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)]

[英]How to convert a numpy array [(x1,y1),(x2,y2),(x3,y3),…(xn,yn)] to [(x1,y1,x1^2,y1^2),(x2,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)]

I tried this我试过这个

x = np.array([
    [0,0],
    [1,0],
    [2.61,-1.28],
    [-0.59,2.1]
])

for i in X:
  X = np.append(X[i], X[i][0]**2, axis = 1)

print(X)

But i am getting this但我得到了这个

IndexError                                Traceback (most recent call last)
<ipython-input-12-9bfd33261d84> in <module>()
      6     ])
      7 for i in X:
----> 8     X = np.append(X[i], X[i][0]**2, axis = 1)
      9 
     10 print(X)

IndexError: arrays used as indices must be of integer (or boolean) type

Someone please help!有人请帮忙!

How about concatenate:如何连接:

np.concatenate((x,x**2))

Output: Output:

array([[ 0.    ,  0.    ],
       [ 1.    ,  0.    ],
       [ 2.61  , -1.28  ],
       [-0.59  ,  2.1   ],
       [ 0.    ,  0.    ],
       [ 1.    ,  0.    ],
       [ 6.8121,  1.6384],
       [ 0.3481,  4.41  ]])
In [210]: x = np.array([ 
     ...:     [0,0], 
     ...:     [1,0], 
     ...:     [2.61,-1.28], 
     ...:     [-0.59,2.1] 
     ...: ]) 
     ...:                                                                                              
In [211]: x            # (4,2) array                                                                                            
Out[211]: 
array([[ 0.  ,  0.  ],
       [ 1.  ,  0.  ],
       [ 2.61, -1.28],
       [-0.59,  2.1 ]])
In [212]: for i in x:       # iterate on rows
     ...:     print(i)        # i is a row, not an index x[i] would be wrong 
     ...:                                                                                              
[0. 0.]
[1. 0.]
[ 2.61 -1.28]
[-0.59  2.1 ]

Look at one row:看一行:

In [214]: x[2]                                                                                         
Out[214]: array([ 2.61, -1.28])

You can join that row with its square with:您可以将该行与其正方形连接:

In [216]: np.concatenate((x[2], x[2]**2))                                                              
Out[216]: array([ 2.61  , -1.28  ,  6.8121,  1.6384])

And doing the same for the whole array.并对整个阵列做同样的事情。 Where possible in numpy work with the whole array, not rows and elements.numpy中尽可能使用整个数组,而不是行和元素。 It's simpler, and faster.它更简单,更快。

In [217]: np.concatenate((x, x**2), axis=1)                                                            
Out[217]: 
array([[ 0.    ,  0.    ,  0.    ,  0.    ],
       [ 1.    ,  0.    ,  1.    ,  0.    ],
       [ 2.61  , -1.28  ,  6.8121,  1.6384],
       [-0.59  ,  2.1   ,  0.3481,  4.41  ]])

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

相关问题 切片 numpy 数组的多个帧,具有多个 y1:y2, x1:x2 - Slice multiple frame of numpy array with multiple y1:y2, x1:x2 如何将 x1、x2、y1、y2 数组转换为矩形顶点数组? - How do I turn x1, x2, y1, y2 array into rectangular vertices array? Python中如何将YOLO格式标注转换为x1,y1,x2,y2坐标? - How to convert YOLO format annotations to x1, y1, x2, y2 coordinates in Python? 比较两个numpy数组:(x1,y1)和(x2,y2),以检查元素是否相交 - Comparing two numpy arrays: (x1, y1) and (x2, y2) to check whether elements intersect 如何从列表中表达 x1, y1, x2, y2 - How to express x1, y1, x2, y2 from a list 使用matplotlib.pyplot [[x1,y1],[x2,y2]]绘制点 - draw points using matplotlib.pyplot [[x1,y1],[x2,y2]] 如何将 yolov3 补丁坐标数组转换为边界框数组 (x1,y1, x2,y2) - how to convert yolov3 patch coordinates array to bounding box array (x1,y1, x2,y2) 通过完全控制自动将鼠标从 X1、Y1 移动到 X2、Y2 - Automate Mouse Movement from X1,Y1 to X2,Y2 with complete control 如何从 python 中编码为 [[x,y], [x1,y1], ....[xn,yn]] 的文本文件中读取多边形的坐标 - How to read Coordinates of a polygon from a text file which are encoded as [[x,y], [x1,y1], ....[xn,yn]] in python 将二维数组转换为 [ [ x0 y0 ] [x1 y1] [x2 y2] ] 形式 - Converting 2D array into [ [ x0 y0 ] [x1 y1] [x2 y2] ] form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM