# About Feature Scaling
Scale down or scale up the data in order to standardise the range of features/ variable


## Sections

- [Min Max normalization](#Min-Max-normalization)
- [Z Score normalization](#Z-Score-normalization)
- [Decimal Scaling normalization](#Z-Score-normalization)


Let us define a list with random numbers as our independent variable


```python
import random
random.seed(332)

data = []

for x in range(100):
  data.append(random.randint(1,101))

print (data)
```

## Min-Max-scaling-normalization
Min-Max normalization is a simple technique where the technique can specifically fit the data in a pre-defined boundary.

\begin{equation} X_{norm} = \frac{X - X_{min}}{X_{max}-X_{min}} \end{equation}


In the cell below we will use the *data* list and use the min-max scaling normalisation to normalise it
- *min_max_normal* is the list of scaled output
- *X_min* is the minimum value of the data
- *x_max* is the maximum value of the data
- *D* upper boundry of the predefined range
- *C* lower boundry of the predefined range 


```python
min_max_normal = []
X_min = min(data)
X_max = max(data)
D = 1
C = 0

for element in data:
    X_norm = (float(element - X_min)/(X_max - X_min))*(D - C) + C
    min_max_normal.append(X_norm)

print (min_max_normal)
```

## Z-Score-normalization
We scale the feature so that transformed features are with an mean of zero and standard deviation of one.

\begin{equation} z = \frac{x - \mu}{\sigma}\end{equation} 

In the cell below we will use the *data* list and use the min-max scaling normalisation to normalise it
- *z_score_normal* is the list of scaled output
- *mean* is the minimum value of the data
- *std* is the maximum value of the data


```python
import math 

def mean(column):
    """
    takes input the list of variables from the data
    returns mean of the variables in the list
    """
    sum_ = 0
    for element in column:
        sum_ = sum_ + element
        
    return float(sum_)/len(column)

def std(column):
    """
    takes input the list of variables from the data
    returns standard deviation of the variables in the list
    """
    if len(column) <= 1:
        return 0.0

    mean_data, sd = mean(column), 0.0

    # calculate stan. dev.
    for el in column:
        sd += (float(el) - mean_data)**2
    sd = math.sqrt(sd / float(len(column)-1))

    return sd
```


```python
z_normal = []

mean_data = mean(data)
std_data = std(data)

for element in data:
    z_norm = float(element - mean_data)/std_data
    z_normal.append(z_norm)

print (z_normal)
```

## Decimal Scaling Normalisation

We normalize by moving the decimal point of values of features. The number of decimal points moved depends on the maximum absolute value in the features, it provides the range between -1 and 1

- Take the maximum number of digits. For eg. 3031, then maximum digits is 4
- Calculate power of 10. 10^4 = 10000.
- Divide each number by 10000.


```python
ds_normal =[]
max_digits = 10**len(str(max(data)))

for element in data:
    ds_norm = float(element)/max_digits
    ds_normal.append(ds_norm)

print (ds_normal)
```


```python

```

87a4062ebed336cbe2820f10a98195acbb16b476
ipynb
Jupyter Notebook
DataPreperation/4_Normalization.ipynb
sumandeepb/A10_DataPreperationAndVisualization
ee3192169d0e8f06a0a3c7598d29a1ca9d72bba8
Apache-2.0
DataPreperation/4_Normalization.ipynb
sumandeepb/A10_DataPreperationAndVisualization
ee3192169d0e8f06a0a3c7598d29a1ca9d72bba8
Apache-2.0
DataPreperation/4_Normalization.ipynb
sumandeepb/A10_DataPreperationAndVisualization
ee3192169d0e8f06a0a3c7598d29a1ca9d72bba8
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES