C+中的多项式特征+; 我使用Python中的SkEnk库的多项式特征实用程序,现在我需要用C++来转换它。我找到了这段代码,但它似乎对我不起作用: template <class T> std::vector<T>
C+中的多项式特征+;C+中的多项式特征+; 我使用Python中的SkEnk库的多项式特征实用程序,现在我需要用C++来转换它。我找到了这段代码,但它似乎对我不起作用: template <class T> std::vector<T>,python,c++,scikit-learn,Python,C++,Scikit Learn,C+中的多项式特征+; 我使用Python中的SkEnk库的多项式特征实用程序,现在我需要用C++来转换它。我找到了这段代码,但它似乎对我不起作用: template <class T> std::vector<T> polynomialFeatures( const std::vector<T>& input, unsigned int degree, bool interaction_only, bool include_bias
我使用Python中的SkEnk库的多项式特征实用程序,现在我需要用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;
}
模板
标准::向量多项式特征(常数标准::向量和输入、无符号整数度、仅布尔交互、布尔包含偏差)
{
标准::矢量特征=输入;
std::vector prev_chunk=输入;
向量索引(input.size());
std::iota(index.begin()、index.end()、0);
对于(int d=1;d
我想一个简单的函数,它很容易使用,因为我是C++初学者,我需要这个函数在二度,输入是27个值,输出需要是405个值。 预期输出例如:我调用函数polyFeatures(输入=[1,2,3],度=2,交互作用仅=False,包含偏差=False)=>output=[1,2,3,1,2,3,4,6,9](这意味着输入=[a,b,c],输出=[a,b,c,a^2,ab,ac,b^2,bc,c^2])。 在脚本中执行此代码时,会出现以下错误:
- 第一个错误在std::iota(index.begin()、index.end()、0)中:错误“iota”不是“std”的成员
- 我还在for(auto coef_it=prev_chunk.begin()…)上收到一条警告:警告“auto”更改在C++11中的含义,请将其删除[-Wc++x-compat] 然后出现一个错误:“coef_it”没有命名类型
- 新建块时出错。向后推(输入[i]**coef\u it);:一元“*”的类型参数无效(具有“int”)
- 最后是main:错误:没有匹配的函数调用“多项式特征(double[27],int,bool,bool)”,因为我已经将输入定义为double[27]