I’m working on Polynomial Transform for a homework assignment. I’m using a document from vanderbilt.edu as my starting point. Polynomial Transform
I have a set of points:
square_points = (
# x, y
(37, 44 ), # x1,y1
(67, 74 ), # x2,y2
(97,104 ), # x3,y3
(247,194), # x4,y4
(157, 97), # x5,y5
)
that I’d like to turn into a Numpy array, rows as the polynomials:
[[1, x1, y1, x1*y1],
[1, x2, y2, x2*y2],
[1, x3, y3, x3*y3],
[1, x4, y4, x4*y4],
[1, x5, y5, x5*y5]]
I’m still learning Numpy. I’d like to learn a clean way to build such an array from my list of points. (As opposed to building the array from hardcoded square_points[0][1], etc.)
So far I have:
P = np.ones((5,5))
P[:,1] = [ n[0] for n in square_points ]
P[:,2] = [ n[1] for n in square_points ]
P[:,3] = [ n[0]*n[1] for n in square_points ]
which seems a bit cumbersome. Is there a cleaner, more Numpy-y way to create such an array?
Sure! Just do what you’re already doing, but leave out the list comprehension…
E.g.
Or if you’d prefer, you can even do it in one line: