归一化 [a] 到 [b] 区间

#include <algorithm>
//https://en.wikipedia.org/wiki/Feature_scaling
std::vector<double> rescaling_normalization(const std::vector<double> &in,double left_interval/*左区间*/, double right_interval/*右区间*/) {
	const auto min_max = std::minmax_element(in.begin(), in.end());
	double &a = left_interval;
	double &b = right_interval;
	double min = *min_max.first;
	double max = *min_max.second;

	std::vector<double> nor;
	for (const double &x : in) {
		nor.push_back(a + ((x - min)*(b - a)) / (max - min));
	}
	return nor;
}

使用

std::vector<double> in = { 0.0,15.0,30.0,66.0,72.0,98.0 };
in = rescaling_normalization(in, -1, 1);
for (auto &&v : in) {
  std::cout << v << std::endl;
}
posted @ 2021-03-04 10:03  學海無涯  阅读(201)  评论(0编辑  收藏  举报