MinMaxNormalize规一化算法

public class MinMaxNormalize {

	/**
	 * 线性归一化 公式:X(norm) = (X - min) / (max - min)
	 *
	 * @param points     原始数据
	 * @param conversion 是否行转列
	 * @return 归一化后的数据
	 */
	public static double[] normalize(double[][] points, boolean conversion) {
		if (points == null || points.length < 1) {
			return new double[0];
		}
		//新数组
		double[][] newPoints = new double[points[0].length][points.length];
		if (conversion) {
			for (int i = 0; i < points.length; i++) {
				for (int j = 0; j < points[i].length; j++) {
					newPoints[j][i] = points[i][j];
				}
			}
		} else {
			newPoints = points;
		}

		Arrays.stream(newPoints).forEach(val -> {
			log.info("newPoints val:{}", val);
		});

		double[][] p = new double[newPoints.length][newPoints[0].length];
		for (int j = 0; j < newPoints[0].length; j++) {
			for (int i = 0; i < newPoints.length; i++) {
				p[i] = minMax(newPoints[i]);
			}
		}

		Arrays.stream(p).forEach(val -> {
			log.info("p val:{}", val);
		});
		double[] sumArry = Arrays.stream(p).mapToDouble(val -> Arrays.stream(val).sum()).toArray();
		double[] norArry = proportion(sumArry);
		log.info("norArry:{}", norArry);
		return norArry;
	}

	private static double[] minMax(double[] points) {
		double[] p = new double[points.length];
		double maxV = maxV(points);
		double minV = minV(points);
		for (int i = 0; i < points.length; i++) {
			p[i] = maxV == minV ? minV : (points[i] - minV) / (maxV - minV);
		}
		return p;
	}

	private static double[] proportion(double[] points) {
		double[] p = new double[points.length];
		double sumNum = Arrays.stream(points).sum();
		log.info("sumNum:{}", sumNum);
		for (int i = 0; i < points.length; i++) {
			p[i] = NumberUtil.round(points[i] / sumNum, 3).doubleValue();
		}
		return p;
	}


	/**
	 * 获取矩阵的某一列
	 *
	 * @param points points
	 * @param column column
	 * @return double[]
	 */
	public static double[] getMatrixCol(double[][] points, int column) {
		double[] matrixJ = new double[points.length];
		for (int i = 0; i < points.length; i++) {
			matrixJ[i] = points[i][column];
		}
		return matrixJ;
	}

	/**
	 * 获取数组中的最小值
	 *
	 * @param matrixJ matrixJ
	 * @return v
	 */
	public static double minV(double[] matrixJ) {
		return Arrays.stream(matrixJ).min().getAsDouble();
	}

	/**
	 * 获取数组中的最大值
	 *
	 * @param matrixJ matrixJ
	 * @return v
	 */
	public static double maxV(double[] matrixJ) {
		return Arrays.stream(matrixJ).max().getAsDouble();
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
上次更新: 2024-11-06, 19:27:10
最近更新
01
java playwright爬虫
11-06
02
连接chrome调试
07-23
03
连接chrome调试
07-23
更多文章>