Repeat last column in numpy array

Problem

I am trying to repeat the last column in an array (numpy). I wonder if there is a more “elegant” way than resizing an array, copying values ​​and repeating the last line x times.

What i want to achieve

Input Array:                        Output Array:
[[1,2,3],                           [[1,2,3,3,3],
 [0,0,0],    -> repeat(2-times) ->   [0,0,0,0,0],
 [0,2,1]]                            [0,2,1,1,1]]

How I solved the problem

x = np.array([[1,2,3],[0,0,0],[0,2,1]])
# to repeat last row two times two times
new_size = x.shape[1] + 2
new_x = np.zeros((3,new_size))
new_x[:,:3] = x

for i in range(x.shape[1],new_size):
    new_x[:,i] = x[:,-1]

Another way

Is there a way to solve this problem using the numpy repeat function? Or something shorter or more effective?

+4
source share
3 answers

One possible solution:

a = np.hstack((arr, np.tile(arr[:, [-1]], 2)))
print (a)
[[1 2 3 3 3]
 [0 0 0 0 0]
 [0 2 1 1 1]]
+3
source

Use numpy.repeat():

np.repeat(a, [1]*(a.shape[1]-1) +[3], axis=1)
+1
source

Broadcasting is often effective.

import numpy as np

A = np.random.randint(0, 100, (1000, 1000))

np.hstack((A, np.broadcast_to(A[:, -1][:, None], (A.shape[1], n))))

Some performance comparison if performance is a problem:

n = 1000
%timeit np.hstack((A, np.broadcast_to(A[:, -1][:, None], (A.shape[1], n))))  # 3.06 ms
%timeit np.hstack((A, np.tile(A[:, [-1]], n)))                               # 9.33 ms
%timeit np.repeat(A, [1]*(A.shape[1]-1) +[n], axis=1)                        # 12.9 ms
+1
source

Source: https://habr.com/ru/post/1693680/


All Articles