0

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?

2 Answers 2

1

Doesn't simply

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

do what you want?

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a one-liner:

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

One-liners are not necessarily better

7 Comments

So, you suggest using my solution, i.e. three lines?
If it makes it more readable, sure
You could use better variable names, e.g. replace X1 with constant and X2 with random_normal
Why do you use transpose()?
ok thank you, I just wanted to know why transpose is used. @XDAF
|

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.