Home > OS >  Pythonic way to generate array with one fixed and one random column
Pythonic way to generate array with one fixed and one random column

Time:11-16

My task is to

Form array X of dimension 1000x2 by setting first column equal to 1 and drawing 1000 independent random numbers to second column from standard normal distribution.

My solution would be

import numpy as np
X1 = np.ones(1000)
X2 = np.random.randn(1000)
X = np.vstack([X1,X2]).transpose()

Now I'd like to know if there's a more elegant way to achieve the task. Maybe even a one liner?

CodePudding user response:

Doesn't simply

np.stack([np.ones(1000), np.random.randn(1000)])

do what you want?

CodePudding user response:

Here is a one-liner:

X = np.vstack([np.ones(1000),np.random.randn(1000)]).transpose()

One-liners are not necessarily better

  • Related