【问题标题】:Get median values from sorted data从排序数据中获取中值
【发布时间】:2014-11-13 03:26:14
【问题描述】:

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

| 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"

【问题讨论】:

  • 到目前为止你有什么代码?告诉我们你做了什么。
  • 本身还没有代码,但我的总体想法是遍历列表,注意类别变化的位置。然后再次迭代以找到适当的中值。它应该可以工作,但看起来不必要的混乱。
  • 使用列表进行存储是硬性要求吗?另外,当您说“数据可能会发生变化”时,是否意味着当另一行附加到排序列表时,您的算法仍然需要产生正确的结果?
  • 我正在获取一个具有相同标题但数据不同的通用 csv 文件,然后将我的程序应用于它。因此,它不应该特定于一组数据。永远不会创建新行,因为列表仅从输入的 csv 生成。
  • 我说 300 行,因为目前这是我正在处理的大小。未来可能会达到数千甚至更多,但这不应该对其产生不利影响。

标签: java median


【解决方案1】:

这是对排序列表解决方案的一次遍历:

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());
    }
  }
}

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多