如何提取单项式特征?

How do extract Monomial Features?

我想实现下面的等式,但是不知道对不对。这个等式的实现是否正确使用以下函数?

import numpy as np
def monomial_features(max_degree, inputs):

  features = np.zeros((inputs.shape[0],20))
  val = 0
  
  for i, x1 in enumerate(inputs):
    x1 = x1.flatten()
    n = 0
    for j in range(20):
      n += 1
      if n <= max_degree:
        # print(n) 
        val = np.dot(x1.T, x1) ** n 
      elif n > max_degree: 
        val = np.dot(x1.T, x1) 

      features[i, j] =  val 

  return features

max_degree = 16
inputs = np.array([[1.1, 0.1],[0.2, 1.3],[1.3, 1.1],[1.6, 1.1],[1. , 0.6],[0.7, 0.9],[1.5, 0.4],[0.6, 1. ],[1.1, 0.8],[0.7, 1. ],
                   [0.2, 0.9],[0.7, 0.3],[1.6, 0.9],[0.4, 0.9], [1.3, 0.5],[1.3, 0.7],[1.2, 1. ],[1.2, 0.9],[0.2, 0.3],[0.4, 1. ],[0.8, 0.3]])

featureas = monomial_features(max_degree, inputs)
print(featureas)
import numpy as np
def monomial_features(max_degree, inputs):
  features = np.zeros((inputs.shape[0],0))
  degrees = np.zeros((1,2))
  for degree0 in range(max_degree + 1):
    new_feature0 = inputs[:,0] ** degree0
    for degree1 in range(max_degree + 1 - degree0):
      new_feature1 = inputs[:,1] ** degree1
      new_feature = new_feature0 * new_feature1
      features = np.concatenate((features, new_feature[:,None]),1)
  return features

max_degree = 16
inputs = np.array([[1.1, 0.1],[0.2, 1.3],[1.3, 1.1],[1.6, 1.1],[1. , 0.6],[0.7, 0.9],[1.5, 0.4],[0.6, 1. ],[1.1, 0.8],[0.7, 1. ],
                   [0.2, 0.9],[0.7, 0.3],[1.6, 0.9],[0.4, 0.9], [1.3, 0.5],[1.3, 0.7],[1.2, 1. ],[1.2, 0.9],[0.2, 0.3],[0.4, 1. ],[0.8, 0.3]])

featureas = monomial_features(max_degree, inputs)
print(featureas)

print(featureas.shape)

输出:

[[1.00000000e+00 1.00000000e-01 1.00000000e-02 ... 4.17724817e+00
  4.17724817e-01 4.59497299e+00]
 [1.00000000e+00 1.30000000e+00 1.69000000e+00 ... 3.27680000e-11
  4.25984000e-11 6.55360000e-12]
 [1.00000000e+00 1.10000000e+00 1.21000000e+00 ... 5.11858930e+01
  5.63044823e+01 6.65416609e+01]
 ...
 [1.00000000e+00 3.00000000e-01 9.00000000e-02 ... 3.27680000e-11
  9.83040000e-12 6.55360000e-12]
 [1.00000000e+00 1.00000000e+00 1.00000000e+00 ... 1.07374182e-06
  1.07374182e-06 4.29496730e-07]
 [1.00000000e+00 3.00000000e-01 9.00000000e-02 ... 3.51843721e-02
  1.05553116e-02 2.81474977e-02]]

(21, 153)