import java.text.DecimalFormat; // 用于格式化数字
import java.util.ArrayList; // 用于存储数据
import java.util.Collections; // 用于排序
import java.util.Comparator; // 用于排序
import java.util.List; // 用于存储数据

import org.apache.poi.ss.usermodel.Cell; // Excel 单元格
import org.apache.poi.ss.usermodel.CellType; // Excel 单元格类型
import org.apache.poi.ss.usermodel.Row; // Excel 行
import org.apache.poi.ss.usermodel.Sheet; // Excel 工作表
import org.apache.poi.ss.usermodel.Workbook; // Excel 工作簿

public class KNNAlgorithm {

    // 计算KNN邻近算法填充的值
    public static double calculateKNN(Sheet sheet, int rowIndex, int columnIndex) {
        List<Double> data = new ArrayList<Double>(); // 存储数据
        for (int i = 0; i <= sheet.getLastRowNum(); i++) { // 对每一行进行处理
            Row row = sheet.getRow(i); // 获取行对象
            if (row != null) {
                Cell cell = row.getCell(columnIndex); // 获取指定列的单元格
                if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                    data.add(cell.getNumericCellValue()); // 将数据添加到列表中
                }
            }
        }
        if (data.size() > 0) { // 如果存在数据
            double missingValue = 0; // 缺失值
            Row row = sheet.getRow(rowIndex); // 获取当前行对象
            if (row != null) {
                Cell cell = row.getCell(columnIndex); // 获取指定列的单元格
                if (cell == null || cell.getCellType() == CellType.BLANK) { // 如果单元格为空
                    missingValue = 0; // 缺失值为 0
                } else if (cell.getCellType() == CellType.NUMERIC) {
                    missingValue = cell.getNumericCellValue(); // 缺失值为单元格中的值
                } else if (cell.getCellType() == CellType.STRING) {
                    try {
                        missingValue = Double.parseDouble(cell.getStringCellValue()); // 转换为数字类型
                    } catch (NumberFormatException e) {
                        missingValue = 0; // 转换失败则缺失值为 0
                    }
                }
            }
            if (missingValue > 0) { // 如果缺失值大于 0
                return missingValue; // 直接返回缺失值
            } else {
                List<Double> distances = new ArrayList<Double>(); // 存储距离
                for (double value : data) { // 遍历数据
                    double distance = Math.abs(value - missingValue); // 计算距离
                    distances.add(distance); // 将距离添加到列表中
                }
                Collections.sort(distances, new Comparator<Double>() { // 对距离进行排序
                    @Override
                    public int compare(Double o1, Double o2) {
                        return Double.compare(o1, o2);
                    }
                });
                int k = 3; // 取前三个最近的邻居
                double sum = 0; // 总和
                int count = 0; // 计数器
                for (int i = 0; i < k && i < distances.size(); i++) { // 对前 k 个最近的邻居进行处理
                    double value = data.get(distances.indexOf(distances.get(i))); // 获取对应的值
                    sum += value; // 累加值
                    count++; // 计数器加 1
                }
                if (count > 0) { // 如果计数器大于 0
                    DecimalFormat df = new DecimalFormat("#.##"); // 创建 Decimal 格式化器,用于保留两位小数
                    return Double.parseDouble(df.format(sum / count)); // 返回平均值
                } else {
                    return 0; // 否则返回 0
                }
            }
        } else {
            return 0; // 如果不存在数据,则返回 0
        }
    }
}
KNN 算法实现:Excel 数据填充

原文地址: https://www.cveoy.top/t/topic/f2ec 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录