Java实现最大最小值归一化代码
时间: 2023-07-16 16:17:04 浏览: 57
最大最小值归一化是常见的数据预处理方式,Java代码实现如下:
```java
public class MinMaxScaler {
private double[] maxVals; // 存放每列数据的最大值
private double[] minVals; // 存放每列数据的最小值
public void fit(double[][] data) {
int nCols = data[0].length;
maxVals = new double[nCols];
minVals = new double[nCols];
// 初始化最大最小值
for (int i = 0; i < nCols; i++) {
maxVals[i] = Double.MIN_VALUE;
minVals[i] = Double.MAX_VALUE;
}
// 遍历所有行和列,找到每列的最大最小值
for (double[] row : data) {
for (int j = 0; j < nCols; j++) {
maxVals[j] = Math.max(maxVals[j], row[j]);
minVals[j] = Math.min(minVals[j], row[j]);
}
}
}
public double[][] transform(double[][] data) {
int nRows = data.length;
int nCols = data[0].length;
double[][] normalizedData = new double[nRows][nCols];
// 对每列进行归一化处理
for (int j = 0; j < nCols; j++) {
double maxVal = maxVals[j];
double minVal = minVals[j];
double diff = maxVal - minVal;
for (int i = 0; i < nRows; i++) {
normalizedData[i][j] = (data[i][j] - minVal) / diff;
}
}
return normalizedData;
}
}
```
使用方法:
```java
double[][] data = {{1, 3, 5}, {2, 4, 6}, {3, 6, 9}};
MinMaxScaler scaler = new MinMaxScaler();
scaler.fit(data);
double[][] normalizedData = scaler.transform(data);
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)