问题描述
我正在尝试生成27个浮点数数组的多项式特征,以获取405个浮点数数组作为输出。我正在使用C ++ 98的旧版本(无法升级)。我曾经在C ++ 11中使用这段代码来生成这些功能:
#include <vector>
#include <iostream>
//#include <numeric>
using namespace std;
template <class T>
std::vector<T> polynomialFeatures( const std::vector<T>& input,unsigned int
degree,bool interaction_only,bool include_bias )
{
std::vector<T> new_chunk; // declare new_chunk here
std::vector<T> features = input;
std::vector<T> prev_chunk = input;
std::vector<size_t> indices( input.size() );
for ( int d = 1 ; d < degree ; ++d )
{
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 ++的了解有限,我很难在C ++ 98中适应此功能。我通过删除不需要的向量尝试了以下方法:
float polynomialFeatures( float input[27],bool include_bias )
{
float new_chunk; // declare new_chunk here
float features = input;
float prev_chunk = input;
float indices( input.size() );
for ( int d = 1 ; d < degree ; ++d )
{
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(),1 );
return features;
}
我得到那些错误:
In function 'float polynomialFeatures(float*,unsigned int,bool,bool)':
error: cannot convert 'float*' to 'float' in initialization
float features = input;
^
error: cannot convert 'float*' to 'float' in initialization
float prev_chunk = input;
^
error: request for member 'size' in 'input',which is of non-class type 'float*'
float indices( input.size() );
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for ( int d = 1 ; d < degree ; ++d )
error: request for member 'size' in 'input',which is of non-class type 'float*'
for ( size_t i = 0 ; i < input.size() - ( interaction_only ? d : 0 ) ; ++i )
error: request for member 'size' in 'new_chunk',which is of non-class type 'float'
size_t next_index = new_chunk.size();
warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
for ( auto coef_it = prev_chunk.begin() + indices[i + ( interaction_only ? 1 : 0 )] ; coef_it != prev_chunk.end() ; ++coef_it )
error: 'coef_it' does not name a type
for ( auto coef_it = prev_chunk.begin() + indices[i + ( interaction_only ? 1 : 0 )] ; coef_it != prev_chunk.end() ; ++coef_it )
error: expected ';' before 'coef_it'
for ( auto coef_it = prev_chunk.begin() + indices[i + ( interaction_only ? 1 : 0 )] ; coef_it != prev_chunk.end() ; ++coef_it )
error: 'coef_it' was not declared in this scope
error: request for member 'end' in 'prev_chunk',which is of non-class type 'float'
for ( auto coef_it = prev_chunk.begin() + indices[i + ( interaction_only ? 1 : 0 )] ; coef_it != prev_chunk.end() ; ++coef_it )
error: request for member 'push_back' in 'new_chunk',which is of non-class type 'float'
new_chunk.push_back( input[i]**coef_it );
error: invalid types 'float[size_t {aka long unsigned int}]' for array subscript
indices[i] = next_index;
error: request for member 'reserve' in 'features',which is of non-class type 'float'
features.reserve( features.size() + std::distance( new_chunk.begin(),error: request for member 'size' in 'features',error: 'distance' is not a member of 'std'
features.reserve( features.size() + std::distance( new_chunk.begin(),error: request for member 'begin' in 'new_chunk',error: request for member 'end' in 'new_chunk',which is of non-class type 'float'
new_chunk.end() ) );
error: request for member 'insert' in 'features',which is of non-class type 'float'
features.insert( features.end(),new_chunk.end() );
error: request for member 'end' in 'features',new_chunk.end() );
error: request for member 'begin' in 'new_chunk',new_chunk.end() );
error: request for member 'end' in 'new_chunk',new_chunk.end() );
error: request for member 'insert' in 'features',which is of non-class type 'float'
features.insert( features.begin(),1 );
error: request for member 'begin' in 'features',1 );
我还想在特征生成过程中将每个生成的特征与另一个具有405个值的浮点数组的系数相乘,然后将所有这些相乘相加,最后获得两个数组的标量积(多项式功能数组'features [405]'和已知数组'abc [405]'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)