C++ program to perform data transformation Min-max and Z score Normalization
Simple C++ program to perform data transformation using min-max normalization, z-score normalization and decimal scaling operation.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n;
double sum=0;
cout<<"Enter The Number of values:";
cin>>n;
int a[n],r1,r2;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter starting and ending range for normalization:";
cin>>r1>>r2;
double max = a[0];
int min = a[0];
for (int i = 0; i <n; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
float v[n];double temp;
for(int i=0;i<n;i++)
{
temp=double(double(double(a[i]-min)*(r2-r1))/(max-min))+ r1;
v[i]=temp;
}
cout<<"normalization by Min-max"<<endl;
cout<<"Values are:"<<endl;
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
cout<<"normalization by Z-score"<<endl;
double mean;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=(sum/n);double temp1=0,std_dev1;
for(int i=0;i<n;i++)
{
temp1=temp1+pow(a[i]-mean,2);
}
std_dev1=sqrt(temp1/n);
for(int i=0;i<n;i++)
{
temp=double(a[i]-mean)/std_dev1;
v[i]=temp;
}
cout<<"Values are:"<<endl;
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
int j=0;
cout<<"normalization by decimal scaling"<<endl;
while(max>1)
{
max=max/10;
j=j+1;
}
for(int i=0;i<n;i++)
{
temp=a[i]/pow(10,j);
v[i]=temp;
}
cout<<"Values are:"<<endl;
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
return 0;
}
Comments
Post a Comment