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
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
Chris
29.8k3 gold badges34 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Piyush Singh
2,97213 silver badges29 bronze badges
Comments
lang-py