我知道我可以使用cython进行快速而肮脏的改进,但在此之前有没有办法使用pythonic方式加速代码?
该代码旨在以Hermite为基础生成多项式的特征,可以扩展到任何维度,这是针对多项式中所有可能情况的广义特征生成。
data_row = 4000;
n_components = 14;
q = n_components;
degree = 1
x= np.random.rand(data_row, n_components)
feature_list = []
feature_array = np.zeros((data_row, (degree + 1)**q))
from itertools import product
num = 0
for feature_combination in product(xrange(degree+1), repeat = q):
# iterate over all feature combinations
single_combination_feature = 1;
for i_component, current_hermite_degree in enumerate(feature_combination):
single_combination_feature *= polyval(hermitenorm(current_hermite_degree), x[:, i_component])
feature_array[:, num] = single_combination_feature
num += 1