C++ 將float資料歸一化到[0,1]
阿新 • • 發佈:2019-02-07
<pre name="code" class="cpp">std::ofstream fileout(‘features_normalize.txt’,std::ios::app); float ymax = 1; //歸一化資料範圍 float ymin = 0; float feature_blob_data[5] = [-1.254,0.254,2.147,-3.456,4.257];//初始化浮點數 std::vector<float> features; //臨時特徵向量 for (int d = 0; d < 5; ++d) features.push_back(feature_blob_data[d]); //特徵歸一化 float dMaxValue = *max_element(features.begin(),features.end()); //求最大值 std::cout<<"maxdata"<<dMaxValue<<'\n'; float dMinValue = *min_element(features.begin(),features.end()); //求最小值 std::cout<<"mindata"<<dMinValue<<'\n'; for (int f = 0; f < features.size(); ++f) { features[f] = (ymax-ymin)*(features[f]-dMinValue)/(dMaxValue-dMinValue+1e-8)+ymin; //極小值限制 fileout<<f+1<<":"<< features[f]<<" "; } fileout<<"\n"; features.clear();//刪除容器