C ++中的多项式特征

时间:2020-08-14 08:36:15

标签: python c++ scikit-learn

我在Python中使用了sklearn库中的多项式特征实用程序,现在需要在C ++中对其进行转换。我找到了这段代码,但是似乎对我不起作用:

template <class T>
std::vector<T> polynomialFeatures( const std::vector<T>& input, unsigned int degree, bool interaction_only, bool include_bias )
{
    std::vector<T> features = input;
    std::vector<T> prev_chunk = input;
    std::vector<size_t> indices( input.size() );
    std::iota( indices.begin(), indices.end(), 0 );

    for ( int d = 1 ; d < degree ; ++d )
    {
        // Create a new chunk of features for the degree d:
        std::vector<T> new_chunk;
        // Multiply each component with the products from the previous lower degree:
        for ( size_t i = 0 ; i < input.size() - ( interaction_only ? d : 0 ) ; ++i )
        {
            // Store the index where to start multiplying with the current component at the next degree up:
            size_t next_index = new_chunk.size();
            for ( auto coef_it = prev_chunk.begin() + indices[i + ( interaction_only ? 1 : 0 )] ; coef_it != prev_chunk.end() ; ++coef_it )
            {
                new_chunk.push_back( input[i]**coef_it );
            }
            indices[i] = next_index;
        }
        // Extend the feature vector with the new chunk of features:
        features.reserve( features.size() + std::distance( new_chunk.begin(), new_chunk.end() ) );
        features.insert( features.end(), new_chunk.begin(), new_chunk.end() );

        prev_chunk = new_chunk;
    }
    if ( include_bias )
        features.insert( features.begin(), 1 );

    return features;
}

我想要一个易于使用的简单函数,因为我是C ++的初学者,我需要第二级的函数,输入是27个值,输出需要是405个值。 例如预期的输出:我调用函数polyFeatures(input = [1,2,3],degree = 2,interaction_only = False,include_bias = False)==>输出= [1,2,3,1,2,3 ,4,6,9](表示input = [a,b,c],output = [a,b,c,a ^ 2,ab,ac,b ^ 2,bc,c ^ 2])。 当我在脚本中执行此代码时,出现几个错误:

  • 第一个错误存在于std :: iota(index.begin(),index.end(),0)中:错误“ iota”不是“ std”的成员。
  • 我也在处收到警告(auto coef_it = prev_chunk.begin()..):警告'auto'在C ++ 11中发生了变化,请删除它[-Wc ++ x-compat] 然后出现错误:'coef_it'没有命名类型。
  • new_chunk.push_back(输入[i] ** coef_it)处发生错误; :一元'*'(具有'int')的无效类型参数
  • 最后在main中:错误:没有匹配函数可调用“ polynomialFeatures(double [27],int,bool,bool)”,这是因为我已经将输入定义为double [27]

0 个答案:

没有答案
相关问题