【问题标题】:How do extract Monomial Features?如何提取单项特征?
【发布时间】:2021-02-01 08:06:51
【问题描述】:

我想实现下面的等式,但我不确定它是对还是错。使用以下函数实现这个方程是否正确?

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)

【问题讨论】:

    标签: python numpy feature-extraction


    【解决方案1】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2016-05-15
      • 2022-12-29
      • 2011-07-25
      • 2017-09-12
      • 2019-05-26
      • 2021-10-23
      相关资源
      最近更新 更多