Min-Max Normalization on a Data Set in Java

Hey Guys,

In this article, we will learn about Min-Max Normalization and implement it in Java for a given data set.

What is data normalization?

Data normalization is a technique to organize data in such a way that the data appears similar across all fields and records. It helps in removing unnecessary anomalies and ease the analysis of data. Another goal of normalizing is to logically group data together that are related to each other.

What is Min-Max Normalization?

Min-max normalization is one of the common methods to normalize data. ItÂ performs a linear transformation on the original data set. Suppose that min and max are the minimum and maximum values of a data set respectively, min-max normalization maps every value vi of the set to vi in the range [new_min, new_max] by computing

Let’s take an example,

Suppose we have to normalize the following data set, 200, 300, 400, 600, 1000 to a new range [0, 1], then using min-max normalization

min = 200, max = 1000, new_min = 0, new_max = 1

The normalized data set is: 0, 0.125, 0.25, 0.5, 1

Let’s write code to implement this in Java.

Code:

```// Min-Max Normalization on a Data Set
import java.util.*;
class minmax{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the Data Set: ");
int size = sc.nextInt();
System.out.println("Enter the values of the Data Set: ");
float set[] = new float[size];
for(int i = 0; i<size; i++) set[i] = sc.nextFloat();
System.out.println("Enter the new minimum: ");
int new_min = sc.nextInt();
System.out.println("Enter the new maximum: ");
int new_max = sc.nextInt();

normalize(new_min, new_max, set);
}

// Method to find minimum value from the data set
static float minValue(float arr[]){
float min = arr[0];
for(int i = 1; i<arr.length; i++)
if(arr[i]<min) min = arr[i];
return min;
}

// Method to find maximum value from the data set
static float maxValue(float arr[]){
float max = arr[0];
for(int i = 1; i<arr.length; i++)
if(arr[i]>max) max = arr[i];
return max;
}

// Method to find the normalized values of the data set
static void normalize(int new_min, int new_max, float arr[]){
System.out.println("The Data Set after Normalization: ");
for(int i = 0 ; i<arr.length ; i++){
float v = ((arr[i] - minValue(arr))/(maxValue(arr) - minValue(arr)))*(new_max - new_min) + new_min;
System.out.printf("%.3f ",v);
}
}
}```

Output:

```//Output 1
Enter the size of the Data Set:
5
Enter the values of the Data Set:
200 300 400 600 1000
Enter the new minimum:
0
Enter the new maximum:
1
The Data Set after Normalization:
0.000 0.125 0.250 0.500 1.000

// Output 2
Enter the size of the Data Set:
4
Enter the values of the Data Set:
40 70 50 15
Enter the new minimum:
5
Enter the new maximum:
10
The Data Set after Normalization:
7.273 10.000 8.182 5.000```