Excel 数据修复: 使用 EM 算法填充缺失值并插入缺失日期

本程序使用 Java 和 Apache POI 库来修复 Excel 文件中的缺失数据。主要功能包括:

  1. 使用 EM 算法填充数值型缺失值:对于表格中的数值型缺失值,利用 EM 算法计算缺失值的估计值,并将其填充到表格中。
  2. 插入缺失的日期行:根据表格中已有的日期信息,自动插入缺失的日期行,并填充相应的数值数据。

代码示例:

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class EM02 {

    public static void main(String[] args) {
        // 定义输入文件和输出文件的路径
        String inputFile = "input.xlsx";
        try (Workbook workbook = WorkbookFactory.create(new FileInputStream(inputFile))) { // 使用工作簿工厂创建 Excel 工作簿
            Sheet sheet = workbook.getSheet("P1"); // 获取 P1 工作表
            DecimalFormat df = new DecimalFormat("#.##"); // 创建 Decimal 格式化器,用于保留两位小数
            // 创建新的工作表
            String newSheetName = "P1-1";
            int sheetIndex = workbook.getSheetIndex(newSheetName);
            Sheet newDataSheet;
            if (sheetIndex >= 0) {
                workbook.removeSheetAt(sheetIndex);
            }
            newDataSheet = workbook.createSheet(newSheetName);
            workbook.setSheetName(workbook.getSheetIndex(newDataSheet), sheet.getSheetName()); // 设置新Sheet的名称
            // 复制原工作表的数据到新的工作表
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row oldRow = sheet.getRow(i);
                Row newRow = newDataSheet.createRow(i);
                if (oldRow != null) {
                    for (int j = 0; j < oldRow.getLastCellNum(); j++) {
                        Cell oldCell = oldRow.getCell(j);
                        Cell newCell = newRow.createCell(j);
                        if (oldCell != null) {
                            if(oldCell.getCellType() == CellType.STRING){
                                newCell.setCellValue(oldCell.getStringCellValue());
                            } else if(oldCell.getCellType() == CellType.NUMERIC){
                                newCell.setCellValue(oldCell.getNumericCellValue());
                            } else if(oldCell.getCellType() == CellType.BOOLEAN){
                                newCell.setCellValue(oldCell.getBooleanCellValue());
                            } else if(oldCell.getCellType() == CellType.FORMULA){
                                newCell.setCellValue(oldCell.getCellFormula());
                            } else if(oldCell.getCellType() == CellType.ERROR){
                                newCell.setCellValue(oldCell.getErrorCellValue());
                            }
                            // 复制原单元格的样式到新单元格
                            CellStyle oldCellStyle = oldCell.getCellStyle();
                            CellStyle newCellStyle = workbook.createCellStyle();
                            newCellStyle.cloneStyleFrom(oldCellStyle);
                            newCell.setCellStyle(newCellStyle);
                        }
                    }
                }
            }
            // 对每一行进行处理
            for (int i = 1; i <= newDataSheet.getLastRowNum(); i++) {
                Row row = newDataSheet.getRow(i); // 获取行对象
                if (row != null) {
                    Cell cell = row.getCell(1); // 获取第二列单元格
                    if (cell == null || cell.getCellType() == CellType.BLANK) { // 判断单元格是否为空或者空白
                        double avg = calculateEM(newDataSheet, i); // 计算EM算法填充的值
                        if (avg > 0) { // 如果填充的值大于 0
                            cell = row.createCell(1); // 创建新的单元格
                            cell.setCellValue(Double.parseDouble(df.format(avg))); // 将填充的值填入单元格
                            // 设置单元格的数据类型为数值类型
                            CellStyle cellStyle = workbook.createCellStyle();
                            DataFormat dataFormat = workbook.createDataFormat();
                            cellStyle.setDataFormat(dataFormat.getFormat("0.00"));
                            cell.setCellStyle(cellStyle);
                        }
                    }
                }
            }

            // 插入缺失的时间和数据
            insertMissingData(newDataSheet);

            // 将工作簿写入输入文件
            FileOutputStream outputStream = new FileOutputStream(inputFile);
            workbook.write(outputStream);
            outputStream.close();
            System.out.println("Data filling completed."); // 输出信息
        } catch (Exception e) { // 捕获异常
            e.printStackTrace();
        }
    }

    // 计算EM算法填充的值
    private static double calculateEM(Sheet sheet, int rowIndex) {
        Row row;
        double missingValue = 0; // 缺失值
        row = sheet.getRow(rowIndex); // 获取当前行对象
        if (row != null) {
            Cell cell = row.getCell(1); // 获取指定列的单元格
            if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                missingValue = cell.getNumericCellValue(); // 缺失值为单元格中的值
            } else if (cell != null && cell.getCellType() == CellType.STRING) {
                try {
                    missingValue = Double.parseDouble(cell.getStringCellValue()); // 转换为数字类型
                } catch (NumberFormatException e) {
                    missingValue = 0; // 转换失败则缺失值为 0
                }
            }
        }

        List<Double> data = new ArrayList<>(); // 存储数据
        for (int i = 0; i <= sheet.getLastRowNum(); i++) { // 对每一行进行处理
            row = sheet.getRow(i); // 获取行对象
            if (row != null) {
                Cell cell = row.getCell(1); // 获取指定列的单元格
                if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                    data.add(cell.getNumericCellValue()); // 将数据添加到列表中
                } else if (cell != null && cell.getCellType() == CellType.STRING) {
                    try {
                        double value = Double.parseDouble(cell.getStringCellValue()); // 转换为数字类型
                        data.add(value); // 将数据添加到列表中
                    } catch (NumberFormatException e) {
                        // 转换失败,忽略该值
                    }
                }
            }
        }

        if (data.size() > 0 && missingValue >= 0) { // 如果存在数据且缺失值大于 0
            double sum = 0; // 总和
            int count = 0; // 非缺失值个数
            for (double value : data) { // 遍历数据
                if (value >= 0) { // 非缺失值
                    sum += value; // 累加非缺失值
                    count++; // 非缺失值个数加一
                }
            }
            if (count > 0) { // 如果存在非缺失值
                double average = sum / count; // 计算平均值
                double variance = 0; // 方差
                for (double value : data) { // 遍历数据
                    if (value >= 0) { // 非缺失值
                        variance += Math.pow(value - average, 2); // 累加方差
                    }
                }
                variance /= count; // 计算方差
                double stdDeviation = Math.sqrt(variance); // 标准差
                return generateRandomValue(average, stdDeviation); // 返回填充的值
            }
        }
        return 0; // 如果不存在数据或缺失值小于等于0,返回 0
    }

    // 生成符合正态分布的随机值
    private static double generateRandomValue(double average, double stdDeviation) {
        double value;
        do {
            value = average + stdDeviation * Math.random(); // 生成随机值
        } while (value < 0); // 保证生成的值大于等于 0
        return value;
    }

    private static void insertMissingData(Sheet sheet) {
        Workbook workbook = sheet.getWorkbook(); // 获取工作簿对象
        List<Date> insertDates = new ArrayList<>(); // 保存插入的日期
        Sheet newDataSheet = workbook.createSheet("temp"); // 创建新的Sheet对象,用于保存插入的数据
        workbook.setSheetName(workbook.getSheetIndex(newDataSheet), sheet.getSheetName()); // 设置新Sheet的名称
        // 复制原有的数据到新的Sheet中
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            Row oldRow = sheet.getRow(i);
            Row newRow = newDataSheet.createRow(i);
            if (oldRow != null) {
                for (int j = 0; j < oldRow.getLastCellNum(); j++) {
                    Cell oldCell = oldRow.getCell(j);
                    Cell newCell = newRow.createCell(j);
                    if (oldCell != null) {
                        if(oldCell.getCellType() == CellType.STRING){
                            newCell.setCellValue(oldCell.getStringCellValue());
                        } else if(oldCell.getCellType() == CellType.NUMERIC){
                            newCell.setCellValue(oldCell.getNumericCellValue());
                        } else if(oldCell.getCellType() == CellType.BOOLEAN){
                            newCell.setCellValue(oldCell.getBooleanCellValue());
                        } else if(oldCell.getCellType() == CellType.FORMULA){
                            newCell.setCellValue(oldCell.getCellFormula());
                        } else if(oldCell.getCellType() == CellType.ERROR){
                            newCell.setCellValue(oldCell.getErrorCellValue());
                        }
                        // 复制原单元格的样式到新单元格
                        CellStyle oldCellStyle = oldCell.getCellStyle();
                        CellStyle newCellStyle = workbook.createCellStyle();
                        newCellStyle.cloneStyleFrom(oldCellStyle);
                        newCell.setCellStyle(newCellStyle);
                    }
                }
            }
        }

        // 对每一行进行处理
        for (int i = 1; i <= newDataSheet.getLastRowNum(); i++) {
            Row currentRow = newDataSheet.getRow(i);
            Row nextRow = newDataSheet.getRow(i + 1);
            if (currentRow != null && nextRow != null) {
                Cell currentDateCell = currentRow.getCell(0);
                Cell nextDateCell = nextRow.getCell(0);
                if (currentDateCell != null && nextDateCell != null && currentDateCell.getCellType() == CellType.NUMERIC && nextDateCell.getCellType() == CellType.NUMERIC) {
                    Date currentDate = currentDateCell.getDateCellValue();
                    Date nextDate = nextDateCell.getDateCellValue();
                    long diff = nextDate.getTime() - currentDate.getTime();
                    long diffDays = diff / (24 * 60 * 60 * 1000);
                    if (diffDays > 10) {
                        int insertCount = (int) (diffDays / 10);
                        for (int j = 1; j <= insertCount; j++) {
                            // 移动原有数据
                            newDataSheet.shiftRows(i + 1, newDataSheet.getLastRowNum(), 1, true, true);

                            // 插入新行
                            Row newRow = newDataSheet.createRow(i + 1);
                            Cell newDateCell = newRow.createCell(0);
                            Cell newDataCell = newRow.createCell(1);
                            Date insertDate = new Date(currentDate.getTime() + (long) j * 10 * 24 * 60 * 60 * 1000);
                            newDateCell.setCellValue(insertDate);
                            insertDates.add(insertDate); // 添加插入的日期到列表中

                            // 设置新单元格的格式与原单元格一致
                            CellStyle oldDateCellStyle = currentDateCell.getCellStyle();
                            CellStyle newDateCellStyle = workbook.createCellStyle();
                            newDateCellStyle.cloneStyleFrom(oldDateCellStyle);
                            newDateCell.setCellStyle(newDateCellStyle);

                            // 设置新数据单元格的格式与原单
                            CellStyle oldDataCellStyle = currentRow.getCell(1).getCellStyle();
                            CellStyle newDataCellStyle = workbook.createCellStyle();
                            newDataCellStyle.cloneStyleFrom(oldDataCellStyle);
                            newDataCell.setCellStyle(newDataCellStyle);

                            newDataCell.setCellValue(calculateEM(newDataSheet, i + j));

                            // 将原有数据填充到新行的相应位置上
                            Cell currentDataCell = currentRow.getCell(1);
                            CellStyle currentDataCellStyle = currentDataCell.getCellStyle();
                            Cell newDataCell2 = newRow.createCell(1);
                            newDataCell2.setCellStyle(currentDataCellStyle);
                            newDataCell2.setCellValue(currentDataCell.getNumericCellValue());
                        }
                        i += insertCount;
                    }
                }
            }
        }

        // 对插入的日期进行排序
        Collections.sort(insertDates);

        // 更新对应行的日期单元格的值
        for (int i = 0; i < insertDates.size(); i++) {
            Row row = newDataSheet.getRow(i + 1);
            Cell dateCell = row.getCell(0);
            dateCell.setCellValue(insertDates.get(i));
        }

        // 将新的Sheet复制回原有的Sheet中
        workbook.removeSheetAt(workbook.getSheetIndex(sheet));
        workbook.setSheetName(workbook.getSheetIndex(newDataSheet), sheet.getSheetName());
        workbook.setSheetOrder(sheet.getSheetName(), workbook.getSheetIndex(newDataSheet));
    }
}

使用方法:

  1. 将代码保存为EM02.java文件。
  2. 将需要修复的 Excel 文件命名为input.xlsx,并将其放置在与代码文件相同的目录下。
  3. 运行代码,程序会自动修复input.xlsx文件中的缺失数据,并覆盖原文件。

注意:

  • 此代码假设 Excel 文件中包含日期列和数据列,分别位于第一列和第二列。
  • EM 算法的填充效果取决于数据本身的分布情况,对于数据波动较大的情况,填充结果可能不太理想。
  • 程序会覆盖原文件,请确保备份重要数据。

代码解析:

  1. calculateEM()方法:该方法使用 EM 算法计算缺失值的估计值。方法首先获取指定行的数据,并计算所有非缺失值的平均值和标准差。然后,根据平均值和标准差生成一个符合正态分布的随机值作为缺失值的估计值。

  2. insertMissingData()方法:该方法根据已有的日期信息,插入缺失的日期行。方法首先创建新的 Sheet 对象,并将原有的数据复制到新的 Sheet 中。然后,遍历每一行,检查日期之间的间隔是否大于 10 天,如果大于 10 天,则在该行之后插入新的日期行,并填充相应的数据。

  3. main()方法:该方法是程序的入口点。方法首先创建工作簿对象,获取指定工作表,并创建新的工作表。然后,将原工作表的数据复制到新的工作表中,并使用 EM 算法填充缺失值。最后,插入缺失的日期行,并保存修改后的工作簿。

总结:

本程序使用 Java 和 Apache POI 库实现了 Excel 数据修复功能,通过 EM 算法填充缺失值,并根据已有日期插入缺失的日期行。该程序可以有效提高数据完整性,并为后续的数据分析提供更可靠的数据基础。

Excel 数据修复: 使用 EM 算法填充缺失值并插入缺失日期

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

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