【发布时间】: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