Thursday, December 27, 2018

Data Normalization using min-max technique

Data Normalization is simply a pre-processing method in which data is made usable according to the situation or need. There are many techniques used for data normalization. One of which is min-max.
Min-Max Technique
This is a technique used to normalize data according to the need. This technique sets the numerical values or list of values between the maximum and minimum values using a predefined range. For example, the range can be from 0 to 1 or it can be from -1 to +1 etc.
The following formula is used to find the resulting range of values.

Where
   is the new value
          is the original value
    is the attribute minimum value
    is the attribute maximum value
    is the new minimum value from selected / desired range
    is the new maximum value from selected / desired range

For example, I have a range of values from Iris data set. 5.1 , 3.5 , 1.4 , 0.2    Iris-Satosa
Putting these values in the formula using the range of “from 0 to 1”



I have written a program using the same formula in Visual Studio Forms using C# language.
 Here is the detail
In this application I used 2 methods
1.       For single set of values
2.      For complete data set saved as a .txt file
In first case 4 boxes are used to take individual values from the user.
The other option opens a .txt file using browse button and then takes each row of values, normalize, and then saves to another .txt file.
Here is the code:


if (pathTxt.Text != string.Empty)
            {
                File.WriteAllText(Application.StartupPath + "\\IrisDataNew.txt""Normalized Data \r\n");
                string line = "";
                using (StreamReader file = new StreamReader(@pathTxt.Text.ToString()))
                {                   
                    while ((line = file.ReadLine()) != null)
                    {
                        string[] irisData = line.Split(',');                      
                        forloopstr(irisData,0);
                    }
                    MessageBox.Show("Data Normalized Successfully! \r\n \r\n Note: can check result in debug folder.");
                }
            }
            else
                if (val1Txt.Text == string.Empty)
                    MessageBox.Show("Enter 1st value plz!");
                else if (val2Txt.Text == string.Empty)
                    MessageBox.Show("Enter 2nd value plz!");
                else if (val3Txt.Text == string.Empty)
                    MessageBox.Show("Enter 3rd value plz!");
                else if (val4Txt.Text == string.Empty)
                    MessageBox.Show("Enter 4th value plz!");
                else
                {
                    string[] iData = { val1Txt.Text.ToString(), val2Txt.Text.ToString(), val3Txt.Text.ToString(), val4Txt.Text.ToString(),"class" };
                    forloopstr(iData, 1);
                }
                        }
public string calc(Decimal x, Decimal y0, Decimal z0, int zn, int yn)
        {
            string x1 = "";
            x1 = decimal.Round((((x - y0) / (z0 - y0)) * (zn - yn) + yn),2).ToString();
            return x1;
        }

public void forloopstr(string[] irisdata, int opt)
        {
            string res = "";
            int counter = 0;
            for (int i = 0; i <= 3; i++)
            {
                string[] irisD = { irisdata[0], irisdata[1], irisdata[2], irisdata[3] };
                decimal x = Convert.ToDecimal(irisD[i]);
                decimal y0 = Convert.ToDecimal(irisD.Min().ToString());
                decimal z0 = Convert.ToDecimal(irisD.Max().ToString());
                int yn = Convert.ToInt32(rng1Txt.Text.ToString());
                int zn = Convert.ToInt32(rng2Txt.Text.ToString());
                //for live values
                if(opt == 1)
                {
                    string res1 = calc(x, y0, z0, zn, yn);
                    if (i == 0)
                        res1Lbl.Text = res1;
                    else if (i == 1)
                        res2Lbl.Text = res1;
                    else if (i == 2)
                        res3Lbl.Text = res1;
                    else if (i == 3)
                        res4Lbl.Text = res1;
                }
                //for whole file
                else if (opt == 0)
                {
                    if (res != null)
                        res = res + calc(x, y0, z0, zn, yn) + ", ";
                    else
                        res = calc(x, y0, z0, zn, yn) + ", ";
                }
            }
            counter++;
            res = res + irisdata[4] + "\r\n";
            File.AppendAllText(Application.StartupPath + "\\IrisDataNew.txt", res);                      


        }

Popular Posts