Skip to content

Instantly share code, notes, and snippets.

@rickhg12hs
Created March 14, 2022 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickhg12hs/3ae86f4e8b4a9af9b0335c6bf6ff8120 to your computer and use it in GitHub Desktop.
Save rickhg12hs/3ae86f4e8b4a9af9b0335c6bf6ff8120 to your computer and use it in GitHub Desktop.
`PolynomialFeatures(2)` with out `sklearn`
new_M = np.apply_along_axis(lambda x: np.array([x[k1]*x[k2] for k2 in range(len(x)) for k1 in range(len(x)) if k1>=k2]), axis=1, arr=M)
N.B.: ^ could be simpler and more efficient
Is _roughly_ equivalent to:
[pseudocode]
new_M = empty array
Foreach row in M:
row_length = len(row)
new_row = empty row
for k1 in range(row_length):
for k2 in range(k1, row_length);
append row[k1]*row[k2] to new_row
append new_row to new_M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment