web123456

Java performs statistics analysis

  • //1.Summon
  • public static double getSum(double[] arr){
  • double sum = 0;
  • for(int i =0;i < ;i++){
  • sum += arr[i];
  • }
  • return sum;
  • }
  • //2. Find the average
  • public static double getMean(double[] arr){
  • return getSum(arr) / ;
  • }
  • //3. Find the mode
  • public static double getMuch(double[] arr){
  • /*Method 1
  • //Construct the mapping of corresponding relationships
  • Map<Double,Integer> map = new HashMap<Double,Integer>();
  • for(int i = 0;i < ;i++){
  • if((arr[i])){
  • //containsKey(Object key) Returns true if this map contains the mapping relationship of the specified key.
  • //get(Object key) returns the value mapped by the specified key; if this map does not contain the mapping relationship of the key, return null.
  • //put(K key, V value) associates the specified value with the specified key in this map (optional action).
  • (arr[i], (arr[i])+1);
  • }else{
  • (arr[i], 1);
  • }
  • }
  • int maxCount = 0;//Number of initialization occurrences
  • double much = -1;//Initialize mode
  • //Iterator
  • Iterator<Double> iter = ().iterator();
  • while(()){
  • //next() returns the next element of the iteration
  • double num = ();
  • int count = (num);
  • if(count > maxCount){
  • maxCount = count;
  • much = num;
  • }
  • }
  • return much;
  • */
  • //Method 2
  • if( == 1){
  • return arr[0];
  • }
  • int count = 0;
  • int max = 0;
  • double much = 0;
  • (arr);
  • for(int i = 0;i < - 1;i ++){
  • if(arr[i] == arr[i+1]){
  • count ++ ;
  • }else{
  • count = 0;
  • }
  • if(count > max){
  • max = count;
  • much = arr[i];
  • }
  • }
  • return much;
  • }
  • //4. Find the median number
  • public static double getMedian(double[] arr){
  • if( % 2 == 0){
  • //If the original array is an even number, the median is the average of the two in the middle
  • return ((arr[/2]+arr[/2-1])/2);
  • }else{
  • //If the original array is an odd number, the median is the middle number
  • return arr[/2];
  • }
  • }
  • //5. Seek extreme differences
  • public static double getRange(double[] arr){
  • double max = arr[0],min = arr[0];
  • for(int i = 0;i<;i++){
  • if(arr[i]>max){
  • max = arr[i];
  • }
  • if(arr[i]<min){
  • min = arr[i];
  • }
  • }
  • return max-min;
  • }
  • //6. Find the variance
  • public static double getVariance(double[] arr){
  • double variance = 0 ;
  • double mean = getMean(arr);
  • double sum = 0;
  • for(int i=0;i<;i++){
  • sum += (arr[i]-mean)*(arr[i]-mean);
  • }
  • variance = sum/;
  • return variance;
  • }
  • //7. Find the standard deviation
  • public static double getStandardDevition(double[] arr){
  • return (getVariance(arr));
  • }