Java实现数据统计的常用算法,算法竞赛入门经典java版
作者:互联网
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
-
数据统计工具类
-
@author 胡湛霏
-
@since 2016-09-27
*/
public class DataStatisticsUtils {
/**
-
求和
-
@param arr
-
@return
*/
public static double getSum(double[] arr) {
double sum = 0;
for (double num : arr) {
sum += num;
}
return sum;
}
/**
-
求均值
-
@param arr
-
@return
*/
public static double getMean(double[] arr) {
return getSum(arr) / arr.length;
}
/**
-
求众数
-
@param arr
-
@return
*/
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;
}
/**
-
求中位数
-
@param arr
-
@return
*/
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)];
}
}
/**
-
求中列数
-
@param arr
-
@return
*/
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;
}
/**
-
求四分位数
-
@param arr
-
@return 存放三个四分位数的数组
*/
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;
}
/**
-
求极差
-
@param arr
-
@return
*/
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;
}
/**
-
求四分位数极差
-
@param arr
-
@return
*/
public static double getQuartilesRange(double[] arr) {
return getRange(getQuartiles(arr));
}
/**
-
求截断均值
-
@param arr 求值数组
-
@param p 截断量p,例如p的值为20,则截断20%(高10%,低10%)
-
@return
*/
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);
}
/**
-
求方差
-
@param arr
-
@return
*/
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;
}
/**
-
求绝对平均偏差(AAD)
-
@param arr
-
@return
*/
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;
}
/**
-
求中位数绝对偏差(MAD)
-
@param arr
-
@return
*/
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);
}
/**
-
求标准差
-
@param arr
-
@return
*/
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