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.

asked Dec 7, 2020 at 5:29

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]])
answered Dec 7, 2020 at 5:35
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) 
answered Dec 7, 2020 at 5:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.