为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Java 中正确使用 Apache commons 数学库中的

如何在 Java 中正确使用 Apache commons 数学库中的

翻过高山走不出你 2024-01-05 16:43:58
我想基于遵循 Zipf 分布的单词(来自字典)创建数据源(用 Java 编写)。所以我来到了Apache commons 库的ZipfDistribution和NormalDistribution 。不幸的是,有关如何使用这些类的信息很少。我尝试做一些测试,但我不确定我是否以正确的方式使用它。我仅遵循每个构造函数的文档中所写的内容。但结果似乎并不“分布均匀”。import org.apache.commons.math3.distribution.NormalDistribution;import org.apache.commons.math3.distribution.ZipfDistribution;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;public class ZipfDistributionDataSource extends RichSourceFunction<String> {    private static final String DISTINCT_WORDS_URL = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt";    public static void main(String[] args) throws Exception {        ZipfDistributionDataSource zipfDistributionDataSource = new ZipfDistributionDataSource();        StringBuffer stringBuffer = new StringBuffer(zipfDistributionDataSource.readDataFromResource());        String[] words = stringBuffer.toString().split("\n");        System.out.println("size: " + words.length);        System.out.println("Normal Distribution");        NormalDistribution normalDistribution = new NormalDistribution(words.length / 2, 1);        for (int i = 0; i < 10; i++) {            int sample = (int) normalDistribution.sample();            System.out.print("sample[" + sample + "]: ");            System.out.println(words[sample]);        }        System.out.println();        System.out.println("Zipf Distribution");        ZipfDistribution zipfDistribution = new ZipfDistribution(words.length - 1, 1);        for (int i = 0; i < 10; i++) {            int sample = zipfDistribution.sample();            System.out.print("sample[" + sample + "]: ");            System.out.println(words[sample]);        }    }
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

从代码的角度来看,您使用它很好:) 问题在于假设源材料是按 Zipf 排序的,而它显然是按字母顺序排列的。使用的全部意义ZipfDistribution在于,words[0] 必须是最常见的单词(提示:它是“the”),并且大约是words[1] 频率的两倍)等。

查看完整回答
反对 回复 2024-01-05
  • 1 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信