1

I have one array with shape: (15,2). I also have another array with values: [0, 3, 5].

I want to create another column in the first array with values from the second array, where the first 5 rows has value 0, rows 6-10 has value 3, and the last 5 rows has value 5.

like so:

[0,
 0,
 0,
 0,
 0,
 3,
 3,
 3,
 3,
 3,
 5,
 5,
 5,
 5,
 5]

is there any numpy method that does that?

Thanks!

3 Answers 3

2

You can use numpy's built-in repeat and stacking:

a = np.zeros((15,2))
b = np.array([0,3,5])
np.hstack((a, np.repeat(b,5)[:,None]))

output:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 3.]
 [0. 0. 3.]
 [0. 0. 3.]
 [0. 0. 3.]
 [0. 0. 3.]
 [0. 0. 5.]
 [0. 0. 5.]
 [0. 0. 5.]
 [0. 0. 5.]
 [0. 0. 5.]]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! One question, what does it mean the [:,None] slicing?
@joann2555 None adds an extra dimension to array (in the place it is added, here 2nd dimension in [:,None]). It is an alias for np.newaxis. Basically, we are adding another dimension so that when we stack, we stack correct dimensions together.
0
rand = np.random.random((15,2)) # shape is (15,2)
vals = np.array([0,3,5]) # shape is (3,)
res = np.concatenate(([np.full((rand.shape[0]//vals.shape[0],), val) for val in vals]), axis=0)
res

output:

array([0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5])

Finally,

final = np.hstack((rand, res.reshape(15,1)))
final

output:

array([[0.29759807, 0.60548479, 0.        ],
       [0.61242249, 0.46456274, 0.        ],
       [0.86172011, 0.2963868 , 0.        ],
       [0.91728575, 0.36366023, 0.        ],
       [0.56488556, 0.82130321, 0.        ],
       [0.59482141, 0.46148353, 3.        ],
       [0.7762271 , 0.25415058, 3.        ],
       [0.09176551, 0.9687253 , 3.        ],
       [0.06473259, 0.34686598, 3.        ],
       [0.69542414, 0.15540001, 3.        ],
       [0.02880707, 0.23169327, 5.        ],
       [0.90004256, 0.22145591, 5.        ],
       [0.61596969, 0.46807342, 5.        ],
       [0.02263769, 0.68522023, 5.        ],
       [0.81777274, 0.58145853, 5.        ]])

Comments

0

Use np.concatenate.

  • Convert your second array int a 2d "vector" array where each row has 1 column
a = [0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5] # your list of [0, 3, 5]
a = np.array([a]).T # [ [0], [0], [0], ..., [5] ] the "vector" array

then simply

b = np.array([[i, i +1] for i in range(15)]) # some example 15x2 array
print(np.concatenate((b, a), axis=1)) 

The output is

array([[ 0,  1,  0],
       [ 1,  2,  0],
       [ 2,  3,  0],
       [ 3,  4,  0],
       [ 4,  5,  0],
       [ 5,  6,  3],
       [ 6,  7,  3],
       [ 7,  8,  3],
       [ 8,  9,  3],
       [ 9, 10,  3],
       [10, 11,  5],
       [11, 12,  5],
       [12, 13,  5],
       [13, 14,  5],
       [14, 15,  5]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.