如何更改numpy数组的值

时间:2017-11-19 14:41:15

标签: python arrays numpy

我有一个numpy数组'X'的形状(826,2)。我有另一个numpy数组的零'X_expanded'形状(X.shape [0],6)。我想用feature0,feature1,feature0 ^ 2,feature1 ^ 2,feature1 * feature2,数组X中的1替换X_expanded的元素。

例如

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

我希望X_expanded为

([[ 0.    ,  0.    ,  0.    ,  0.    ,  0.    ,  1.],
  [ 1.    ,  0.    ,  1.    ,  0.    ,  0.    ,  1.],
  [ 2.61  , -1.28  ,  6.8121,  1.6384, -3.3408,  1.],
  [-0.59  ,  2.1   ,  0.3481,  4.41  , -1.239 ,  1.]])

4 个答案:

答案 0 :(得分:0)

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


X_e = np.zeros((X.shape[0], 6)) 

# Retain first two columns from X
X_e[:,:2] = X
# Set 2nd and 3rd columns equals to squares of X's 1st and 2nd column 
X_e[:,2:4] = X**2
# Set 5th column equal to product of X's 1st and 2nd columns
X_e[:,4] = X[:,0] * X[:,1]
# Set 6th column to all ones
X_e[:,5] = 1 

答案 1 :(得分:0)

您可以从头开始构建数组,也可以创建一个空数组并使用切片填充值

import numpy as np
X = np.array([[0,0],[1,0],[2.61,-1.28],[-0.59,2.1]])
Xe = np.block([X,X**2,np.prod(X,axis=1,keepdims=1),np.ones([X.shape[0],1])])

答案 2 :(得分:0)

您还可以根据需要使用np.hstack水平堆叠列。

X_extended = np.hstack((X, X**2, np.prod(X,axis=1,keepdims=1), np.ones([X.shape[0],1])))
print(X_extended)

[[ 0.      0.      0.      0.      0.      1.    ]
 [ 1.      0.      1.      0.      0.      1.    ]
 [ 2.61   -1.28    6.8121  1.6384 -3.3408  1.    ]
 [-0.59    2.1     0.3481  4.41   -1.239   1.    ]]

<强>定时:

X = np.random.random((100000,2))

# np.hstack() @akilat90
%timeit np.hstack((X, X**2, np.prod(X,axis=1,keepdims=1), np.ones([X.shape[0],1])))    
100 loops, best of 3: 4.92 ms per loop

# np.block() @percusse
%timeit np.block([X,X**2,np.prod(X,axis=1,keepdims=1),np.ones([X.shape[0],1])])
100 loops, best of 3: 5.01 ms per loop

# @Bert's Answer
def bert(X):
   X_e = np.zeros((X.shape[0], 6)) 

   X_e[:,:2] = X 
   X_e[:,2] = X[:,0] * X[:,0]
   X_e[:,3] = X[:,1] * X[:,1]
   X_e[:,4] = X[:,0] * X[:,1]
   X_e[:,5] = 1 

   return X_e

%timeit bert(X)
100 loops, best of 3: 3.63 ms per loop

# list comprehension @Joe Iddon
%timeit np.array([[i, j, i**2, j**2, i*j] for i,j in X])
1 loop, best of 3: 246 ms per loop

虽然有点冗长,但@Bert的答案似乎最快,而np.hstack()np.block()几乎相同,列表理解最慢。

答案 3 :(得分:0)

我会用一个非常易读的list-comprehension做到这一点IMO:

np.array([[i, j, i**2, j**2, i*j] for i,j in X])

给出:

array([[ 0.    ,  0.    ,  0.    ,  0.    ,  0.    ],
       [ 1.    ,  0.    ,  1.    ,  0.    ,  0.    ],
       [ 2.61  , -1.28  ,  6.8121,  1.6384, -3.3408],
       [-0.59  ,  2.1   ,  0.3481,  4.41  , -1.239 ]])