gpt4 book ai didi

java - 从排序数据中获取中值

转载 作者:行者123 更新时间:2023-12-01 12:17:32 27 4
gpt4 key购买 nike

我有一组按以下格式排序(升序)的数据:

| Category | Value | S.D. |
| A | 0.1 | 0.1 |
| A | 0.2 | 0.05 |
| A | 1.3 | 0.08 |
| B | 0.1 | 0.01 |
| B | 0.2 | 0.08 |
| B | 0.6 | 0.9 |
| B | 0.7 | 0.01 |
| B | 0.9 | 0.05 |
| B | 1.1 | 0.6 |
| C | 0.5 | 0.3 |
| C | 0.9 | 0.04 |
| C | 1.0 | 0.14 |
| C | 2.1 | 0.1 | etc...

大约有 300 行。我已从 csv 导入此内容并已排序为 List。例如,data.get(1).getCategory() 结果为“A”,data.get(2).getValue() 结果为“0.2”(它是一个 String 因为我正在使用一个库。)

数据可能会发生变化。我需要计算每个类别的中值,并打印每个中值及其类别名称。如果条目数为偶数,则取 S.D 最小的中间值。应该使用。例如,使用上面的数据:

"A: 0.2"
"B: 0.7"
"C: 0.9"

最佳答案

这是对排序列表解决方案的单次传递:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Medians {
public static void printMedians(List<Row> rows) {
if (rows.size() == 0) return;
Collections.sort(rows);
int currentCategoryIndex = 0;
String currentCategory = rows.get(0).category;
for (int i = 0; i < rows.size(); i++) {
if (i == rows.size() - 1
|| !currentCategory.equals(rows.get(i + 1).category)) {
int categorySize = i + 1 - currentCategoryIndex;
int medianIndex = currentCategoryIndex + categorySize / 2;
double median;

if (categorySize % 2 == 0) {
median = rows.get(medianIndex - 1).stdDev < rows.get(medianIndex).stdDev
? rows.get(medianIndex - 1).value
: rows.get(medianIndex).value;
} else {
median = rows.get(medianIndex).value;
}

System.out.printf("%s: %.1f%n", currentCategory, median);

if (i < rows.size() - 1) {
currentCategory = rows.get(i + 1).category;
currentCategoryIndex = i + 1;
}
}
}
}

private static class Row implements Comparable<Row> {
private final String category;
private final double value;
private final double stdDev;

public Row(String category, double value, double standardDeviation) {
this.category = category;
this.value = value;
this.stdDev = standardDeviation;
}

@Override
public int compareTo(Row o) {
if (category.equals(o.category)) {
return value == o.value ? 0 : value > o.value ? 1 : - 1;
}
return category.compareTo(o.category);
}
}

public static void main(String[] args) {
List<Row> rows = new ArrayList<>();
rows.add(new Row("A", 0.2, 0.05));
rows.add(new Row("A", 1.3, 0.08));
rows.add(new Row("A", 0.1, 0.1));

rows.add(new Row("B", 0.6, 0.9));
rows.add(new Row("B", 1.1, 0.6));
rows.add(new Row("B", 0.7, 0.01));
rows.add(new Row("B", 0.9, 0.05));
rows.add(new Row("B", 0.1, 0.01));
rows.add(new Row("B", 0.2, 0.08));

rows.add(new Row("C", 0.5, 0.3));
rows.add(new Row("C", 1.0, 0.14));
rows.add(new Row("C", 2.1, 0.1));
rows.add(new Row("C", 0.9, 0.04));
printMedians(rows);
}
}

但我更喜欢这个:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class CategoryMedianCalculator {
private final Map<String, List<Row>> categories = new HashMap<>();

public void addRow(String category, double value, double stdDev) {
List<Row> rows = categories.get(category);
if (rows == null) {
rows = new ArrayList<>();
categories.put(category, rows);
}
rows.add(new Row(category, value, stdDev));
}

public Map<String, Double> getMedians() {
Map<String, Double> result = new TreeMap<>();
for (Map.Entry<String, List<Row>> entry: categories.entrySet()) {
result.put(entry.getKey(), getMedian(entry.getValue()));
}
return result;
}

private static double getMedian(List<Row> rows) {
Collections.sort(rows);
int index = rows.size() / 2;
if (rows.size() % 2 == 0) {
return rows.get(index - 1).stdDev < rows.get(index).stdDev
? rows.get(index - 1).value
: rows.get(index).value;
} else {
return rows.get(index).value;
}
}

private static class Row implements Comparable<Row> {
private final String category;
private final double value;
private final double stdDev;

public Row(String category, double value, double stdDev) {
this.category = category;
this.value = value;
this.stdDev = stdDev;
}

@Override
public int compareTo(Row o) {
return value == o.value ? 0 : value > o.value ? 1 : - 1;
}
}

public static void main(String[] args) {
CategoryMedianCalculator calc = new CategoryMedianCalculator();
calc.addRow("A", 0.2, 0.05);
calc.addRow("A", 1.3, 0.08);
calc.addRow("A", 0.1, 0.1);

calc.addRow("B", 0.6, 0.9);
calc.addRow("B", 1.1, 0.6);
calc.addRow("B", 0.7, 0.01);
calc.addRow("B", 0.9, 0.05);
calc.addRow("B", 0.1, 0.01);
calc.addRow("B", 0.2, 0.08);

calc.addRow("C", 0.5, 0.3);
calc.addRow("C", 1.0, 0.14);
calc.addRow("C", 2.1, 0.1);
calc.addRow("C", 0.9, 0.04);

for (Map.Entry<String, Double> median : calc.getMedians().entrySet()) {
System.out.printf("%s: %.1f%n", median.getKey(), median.getValue());
}
}
}

关于java - 从排序数据中获取中值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com