无论如何,要加速这个指数级昂贵的python代码?

无论如何,要加速这个指数级昂贵的python代码?,python,performance,Python,Performance,我知道我可以使用cython进行快速而肮脏的改进,但在此之前,有没有任何方法可以使用python方式来加速代码 该代码的目标是在hermite基础上生成多项式的特征,可以扩展到任意维,其目标是为多项式中所有可能的情况生成广义特征 data_row = 4000; n_components = 14; q = n_components; degree = 1 x= np.random.rand(data_row, n_components) feature_list = [] feature_

我知道我可以使用cython进行快速而肮脏的改进,但在此之前,有没有任何方法可以使用python方式来加速代码

该代码的目标是在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

试试。如果您决定将其移动到代码审阅,请确保添加一个说明,说明您的程序应该解决什么问题。对于循环,使用numpy(而不是python)可以对其进行矢量化,但可能会以内存为代价。试试。如果您决定将其移动到代码审阅,确保您添加了一个关于您的程序应该解决什么问题的描述。对于循环,这可能是使用numpy而不是python进行矢量化的,但可能是以内存为代价的。