This is already implemented by np.vander :
import numpy as np
x=range(1,6)
p=4
np.vander(x, p+1, increasing=True)
array([[ 1, 1, 1, 1, 1],
[ 1, 2, 4, 8, 16],
[ 1, 3, 9, 27, 81],
[ 1, 4, 16, 64, 256],
[ 1, 5, 25, 125, 625]])
You could use nested list comprehensions:
import numpy as np
# example data
X = [1, 2, 3, 4]
p = 2
Phi = np.matrix([[X[i] ** j for j in range(p+1)] for i in range(len(X))])
Phi
matrix([[ 1, 1, 1],
[ 1, 2, 4],
[ 1, 3, 9],
[ 1, 4, 16]])
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.