编程语言
首页 > 编程语言> > Java实现数据统计的常用算法,算法竞赛入门经典java版

Java实现数据统计的常用算法,算法竞赛入门经典java版

作者:互联网

import java.util.Arrays;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

/**

*/

public class DataStatisticsUtils {

/**

*/

public static double getSum(double[] arr) {

double sum = 0;

for (double num : arr) {

sum += num;

}

return sum;

}

/**

*/

public static double getMean(double[] arr) {

return getSum(arr) / arr.length;

}

/**

*/

public static double getMode(double[] arr) {

Map<Double, Integer> map = new HashMap<Double, Integer>();

for (int i = 0; i < arr.length; i++) {

if (map.containsKey(arr[i]

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

)) {

map.put(arr[i], map.get(arr[i]) + 1);

} else {

map.put(arr[i], 1);

}

}

int maxCount = 0;

double mode = -1;

Iterator iter = map.keySet().iterator();

while (iter.hasNext()) {

double num = iter.next();

int count = map.get(num);

if (count > maxCount) {

maxCount = count;

mode = num;

}

}

return mode;

}

/**

*/

public static double getMedian(double[] arr) {

double[] tempArr = Arrays.copyOf(arr, arr.length);

Arrays.sort(tempArr);

if (tempArr.length % 2 == 0) {

return (tempArr[tempArr.length >> 1] + tempArr[(tempArr.length >> 1) - 1]) / 2;

} else {

return tempArr[(tempArr.length >> 1)];

}

}

/**

*/

public static double getMidrange(double[] arr) {

double max = arr[0], min = arr[0];

for (int i = 0; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];

}

if (arr[i] < min) {

min = arr[i];

}

}

return (min + max) / 2;

}

/**

*/

public static double[] getQuartiles(double[] arr) {

double[] tempArr = Arrays.copyOf(arr, arr.length);

Arrays.sort(tempArr);

double[] quartiles = new double[3];

// 第二四分位数(中位数)

quartiles[1] = getMedian(tempArr);

// 求另外两个四分位数

if (tempArr.length % 2 == 0) {

quartiles[0] = getMedian(Arrays.copyOfRange(tempArr, 0, tempArr.length / 2));

quartiles[2] = getMedian(Arrays.copyOfRange(tempArr, tempArr.length / 2, tempArr.length));

} else {

quartiles[0] = getMedian(Arrays.copyOfRange(tempArr, 0, tempArr.length / 2));

quartiles[2] = getMedian(Arrays.copyOfRange(tempArr, tempArr.length / 2 + 1, tempArr.length));

}

return quartiles;

}

/**

*/

public static double getRange(double[] arr) {

double max = arr[0], min = arr[0];

for (int i = 0; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];

}

if (arr[i] < min) {

min = arr[i];

}

}

return max - min;

}

/**

*/

public static double getQuartilesRange(double[] arr) {

return getRange(getQuartiles(arr));

}

/**

*/

public static double getTrimmedMean(double[] arr, int p) {

int tmp = arr.length * p / 100;

double[] tempArr = Arrays.copyOfRange(arr, tmp, arr.length + 1 - tmp);

return getMean(tempArr);

}

/**

*/

public static double getVariance(double[] arr) {

double variance = 0;

double sum = 0, sum2 = 0;

for (int i = 0; i < arr.length; i++) {

sum += arr[i];

sum2 += arr[i] * arr[i];

}

variance = sum2 / arr.length - (sum / arr.length) * (sum / arr.length);

return variance;

}

/**

*/

public static double getAbsoluteAverageDeviation(double[] arr) {

double sum = 0;

double mean = getMean(arr);

for (int i = 0; i < arr.length; i++) {

sum += Math.abs(arr[i] - mean);

}

return sum / arr.length;

}

/**

*/

public static double getMedianAbsoluteDeviation(double[] arr) {

double[] tempArr = new double[arr.length];

double median = getMedian(arr);

for (int i = 0; i < arr.length; i++) {

tempArr[i] = Math.abs(arr[i] - median);

}

return getMedian(tempArr);

}

/**

*/

public static double getStandardDevition(double[] arr) {

double sum = 0;

double mean = getMean(arr);

for (int i = 0; i < arr.length; i++) {

sum += Math.sqrt((arr[i] - mean) * (arr[i] - mean));

}

return (sum / (arr.length - 1));

}

}

数据规范化的代码整理:

标签:tempArr,arr,java,double,算法,param,return,length,Java
来源: https://blog.csdn.net/m0_63174529/article/details/121278454