Java实现KNN算法填充Excel缺失值
package org.example;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory;
public class knn {
public static void main(String[] args) {
String inputFile = 'input.xlsx';
String outputFile = 'output1.xlsx';
try (Workbook workbook = WorkbookFactory.create(new FileInputStream(inputFile));
FileOutputStream outputStream = new FileOutputStream(outputFile)) {
Sheet sheet = workbook.getSheetAt(0);
DecimalFormat df = new DecimalFormat('#.##');
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row != null) {
Cell cell = row.getCell(1);
if (cell != null && cell.getCellType() != CellType.BLANK) {
// 如果单元格不为空,则跳过
} else {
double avg = calculateKNN(sheet, i, 1);
if (avg > 0) {
if(cell == null){
cell = row.createCell(1);
}
cell.setCellValue(Double.parseDouble(df.format(avg)));
}
}
}
}
workbook.write(outputStream);
System.out.println('Data filling completed.');
} catch (Exception e) {
e.printStackTrace();
}
}
private 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;
} 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;
}
}
}
if (missingValue > 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++) {
double value = data.get(distances.indexOf(distances.get(i)));
sum += value;
count++;
}
if (count > 0) {
return sum / count;
} else {
return 0;
}
}
} else {
return 0;
}
}
private static Date parseDate(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy/MM/dd HH:mm');
try {
return dateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}

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