Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to calculate median, variance, standard deviation and mode

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Java to calculate median, variance, standard deviation, mode". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Java to calculate median, variance, standard deviation, and mode.

Java calculates median, variance, standard deviation, mode

Import java.text.DecimalFormat;import java.util.*;/** * Mathematical algorithm (Mathematical algorithm (Variance, Standard deviation, median, Mod) * @ author * / public class MathAlgorithm {private final static double dmax = 999 maximum of the Double.MAX_VALUE;//Double type. If the value is too large, the multiplication will reach infinity private final static double dmin = Double.MIN_VALUE. / / minimum value of Double type private final static int n = 100 for / suppose to find the variance and standard deviation of 100 doubl numbers public static void main (String [] args) {Random random = new Random (); double [] x = new double [n]; for (int I = 0; I)

< n; i++) {// 随机生成n个double数 x[i] = Double.valueOf(Math.floor(random.nextDouble() * (dmax - dmin))); System.out.println(x[i]); } // 设置doubl字符串输出格式,不以科学计数法输出 DecimalFormat df = new DecimalFormat("#,##0.00");// 格式化设置 // 计算方差 double dV = getVariance(x); System.out.println("方差=" + df.format(dV)); // 计算标准差 double dS = getStandardDiviation(x); System.out.println("标准差=" + df.format(dS)); int[] intArr={5,10,15,8,6}; System.out.println(Arrays.toString(intArr)+" 中位数:"+median(intArr)); int[] intArr2={5,10,15,8,6,7}; System.out.println(Arrays.toString(intArr2)+" 中位数:"+median(intArr2)); int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 1, 2, 2, 3, 4, 5}; List modalNums = getModalNums(arr); System.out.println("众数:"+modalNums); float[] arr2 = {0.1f, 1.1f, 2.1f, 3.1f, 4.1f, 5.1f, 6.1f, 7.1f, 8.1f, 9.1f, 10.1f, 1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 4.1f, 5.1f}; List modalNums2 = getModalNums(arr2); System.out.println("众数:"+modalNums2); } /** * 方差s^2=[(x1-x)^2 +...(xn-x)^2]/n * @param x * @return */ public static double getVariance(double[] x) { int m = x.length; double sum = 0; for (int i = 0; i < m; i++) {// 求和 sum += x[i]; } double dAve = sum / m;// 求平均值 double dVar = 0; for (int i = 0; i < m; i++) {// 求方差 dVar += (x[i] - dAve) * (x[i] - dAve); } return dVar / m; } /** * 标准差σ=sqrt(s^2) * @param x * @return */ public static double getStandardDiviation(double[] x) { int m = x.length; double sum = 0; for (int i = 0; i < m; i++) {// 求和 sum += x[i]; } double dAve = sum / m;// 求平均值 double dVar = 0; for (int i = 0; i < m; i++) {// 求方差 dVar += (x[i] - dAve) * (x[i] - dAve); } return Math.sqrt(dVar / m); } /** * 中位数(int) * @param nums: A list of integers. * @return: An integer denotes the middle number of the array. */ public static int median(int []nums){ if(nums.length==0) return 0; int start=0; int end=nums.length-1; int index=partition(nums, start, end); if(nums.length%2==0){ while(index!=nums.length/2-1){ if(index>

Nums.length/2-1) {index=partition (nums, start, index-1);} else {index=partition (nums, index+1, end) } else {while (indexless nums. Indexer 2) {if (index > nums.length/2) {index=partition (nums, start, index-1) } else {index=partition (nums, index+1, end);} return nums [index] } private static int partition (int nums [], int start, int end) {int left=start; int right=end; int pivot=nums [left]; while (left

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report