0

I have a 1-d array "arr" arr = np.array([1, 2, 3, 4, 5]) and I generated a list from degree=3 deg = list(range(1, degree+1))

I want a matrix with shape(arr, degree) that is from from the rows:

[arr^d0...] // [1,2,3,4,5]
[arr^d1...] // [1,4,9,16,25]
[arr^d2...] // [1,8,27,64,125]

I understand I should use a for loop but I don't know-how.

2 Answers 2

1

Let numpy broadcast it for you:

(arr[:, None] ** deg).T

Output:

array([[  1,   2,   3,   4,   5],
       [  1,   4,   9,  16,  25],
       [  1,   8,  27,  64, 125]])
Sign up to request clarification or add additional context in comments.

Comments

0

This should do the trick:

arr = np.array([1, 2, 3, 4, 5])
degree = 3
deg = list(range(1, degree+1))
A = np.stack([arr**i for i in deg])
print(A) 

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.