始める無料で始める

パーセンタイルの計算

コーヒー販売サンプルデータの取引金額について、平均値・最小値・最大値をすでに計算しました。次に、取引金額の中央値、25パーセンタイル、75パーセンタイルを小数点以下2桁で求めましょう。

date coffee_name payment_method quantity amount
3/1/24 Latte cash 1 $6.26
3/5/24 Hot Chocolate card 3 $32.51
3/6/24 Americano card 2 $18.70

LocalDateArraysListDescriptiveStatistics などの必要なパッケージはすでにインポートされています。

この演習はコースの一部です

Java によるデータクリーニング

コースを見る

演習の手順

  • コーヒー販売データの取引金額の中央値(50パーセンタイル)を計算しましょう。
  • 取引金額の25パーセンタイルと75パーセンタイルを計算しましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

public class CoffeeSalesExample {
    private record CoffeeSales(LocalDate date, String coffeeName,
            String paymentMethod, int quantity, double amount) {
    }

    public static void main(String[] args) {
        List sales = Arrays.asList(
                new CoffeeSales(LocalDate.of(2024, 3, 1), "Latte", "cash", 1, 6.26),
                new CoffeeSales(LocalDate.of(2024, 3, 5), "Hot Chocolate", "card", 3, 32.51),
                new CoffeeSales(LocalDate.of(2024, 3, 6), "Americano", "card", 2, 18.70));

        DescriptiveStatistics stats = new DescriptiveStatistics();
        sales.forEach(sale -> stats.addValue(sale.amount()));

        System.out.printf("\nSales analyzed: %d%n", sales.size());
        System.out.printf("Average amount: $%.2f%n", stats.getMean());
        System.out.printf("Amount range: $%.2f - $%.2f%n",
                stats.getMin(), stats.getMax());
	    
        // Compute the median transaction amount
        System.out.printf("Median amount: $%.2f%n", stats.____(____));
        
        // Compute the 25th and 75th percentiles of the amounts
        System.out.printf("Normal range: $%.2f - $%.2f%n",
                stats.____(____), stats.____(____));
    }
}
コードを編集して実行