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.
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]])
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.