Loading...

Scaling Techniques

Normalizing data is crucial when using machine learning algorithms because of how sensitive most of them are to un-scaled data. For example, the neighbors-based and KMeans algorithms use the p-distance in their learning phase.

Normalization is the first step before using a linear regression due to Gauss-Markov assumptions.

Unscaled data can also create complications for the convergence of some ML algorithms. Normalization is also a way to encode the data and to retain the global distribution. When we know the estimators to use to scale the data, we can easily un-scaled the data and come back to the original distribution.

There are three main scaling techniques:

  • Z-Score: We reduce and center the feature values using the average and standard deviation. This scaling technique is sensitive to outliers.

  • Robust Z-Score: We reduce and center the feature values using the median and the median absolute deviation. This scaling technique is robust to outliers.

  • Min-Max: We reduce the feature values by using a bijection to [0,1]. The max will reach 1 and the min will reach 0. This scaling technique is robust to outliers.

To demonstrate data scaling in VerticaPy, we will use the well-known titanic dataset.

from verticapy.datasets import load_titanic

titanic = load_titanic()
titanic.head(100)
123
pclass
Int
100%
...
123
body
Int
9%
Abc
Varchar(100)
57%
11...22
21...[null]
31...[null]
41...[null]
51...[null]
61...[null]
71...[null]
81...62
91...[null]
101...[null]
111...[null]
121...[null]
131...110
141...[null]
151...[null]
161...38
171...[null]
181...126
191...292
201...175
211...[null]
221...122
231...166
241...[null]
251...207
261...[null]
271...232
281...[null]
291...[null]
301...[null]
311...[null]
321...[null]
331...46
341...169
351...[null]
361...[null]
371...[null]
381...[null]
391...[null]
401...[null]
411...[null]
421...[null]
431...[null]
441...[null]
451...[null]
461...[null]
471...[null]
481...[null]
491...[null]
501...[null]
511...[null]
521...[null]
531...[null]
541...[null]
551...[null]
561...[null]
571...[null]
581...[null]
591...[null]
601...[null]
611...[null]
621...[null]
631...[null]
641...[null]
651...[null]
661...[null]
671...[null]
681...[null]
691...[null]
701...[null]
711...[null]
721...[null]
731...[null]
742...[null]
752...[null]
762...[null]
772...[null]
782...[null]
792...[null]
802...[null]
812...[null]
822...[null]
832...236
842...[null]
852...[null]
862...[null]
872...155
882...[null]
892...75
902...35
912...[null]
922...[null]
932...[null]
942...[null]
952...[null]
962...[null]
972...[null]
982...165
992...[null]
1002...[null]

Let’s look at the fare and age of the passengers.

titanic.select(["age", "fare"])
123
age
Numeric(8)
80%
123
fare
Numeric(12)
99%
171.049.5042
245.035.5
3[null]31.0
417.047.1
527.0136.7792
637.083.1583
731.052.0
850.0106.425
936.031.6792
1037.053.1
1124.079.2
1245.083.475
1340.00.0
1442.042.5
15[null]51.8625
1642.052.0
17[null]30.6958
1829.030.0
1946.075.2417
2054.051.8625

These lie in different numerical intervals so it’s probably a good idea to scale them. To scale data in VerticaPy, we can use the scale() method.

help(titanic["age"].scale)
Help on function scale in module verticapy.core.vdataframe._scaler:

scale(method: Literal['zscore', 'robust_zscore', 'minmax'] = 'zscore', by: Optional[Annotated[Union[str, list[str]], 'STRING representing one column or a list of columns']] = None, return_trans: bool = False) -> 'vDataFrame'

Scales the input vDataColumns using the input method.

Parameters
----------
method: str, optional
    Method used to scale the data.
     - zscore:
        Normalization using the Z-Score.

        .. math::

            Z_{score}(x) = (x - x_{avg}) / x_{std}

     - robust_zscore:
        Normalization using the Robust Z-Score.

        .. math::

            Z_{rscore}(x) = (x - x_{med}) / (1.4826 * x_{mad})

     - minmax:
        Normalization using the MinMax.

        .. math::

            Z_{minmax}(x) = (x - x_{min}) / (x_{max} - x_{min})
by: SQLColumns, optional
    vDataColumns used in the partition.
return_trans: bool, optimal
    If  set to True,  the method  returns the  transformation
    used instead of the parent vDataFrame. This parameter is used
    for testing purposes.

Returns
-------
vDataFrame
    self._parent

The three main scaling techniques are available. Let’s scale the fare and the age using the MinMax method.

titanic["age"].scale(method = "minmax")
titanic["fare"].scale(method = "minmax")
titanic.select(["age", "fare"])
123
age
Numeric(22)
80%
123
fare
Numeric(26)
99%
10.8870340153131670.096625763278767
20.5606878373289820.069291385304605
3[null]0.060507970265993
40.2092381071921680.091933077404138
50.3347558679553160.266975218277623
60.4602736287184640.162314191734533
70.3849629722605750.101497240446182
80.6234467177105560.207727765663171
90.4477218526421490.061833680375821
100.4602736287184640.10364429745562
110.2971005397263710.154588104679569
120.5606878373289820.162932348966251
130.4979289569474080.0
140.5230325091000380.082954475364668
15[null]0.101228858320002
160.5230325091000380.101497240446182
17[null]0.059914211409383
180.3598594201079450.058556100257413
190.5732396134052970.146862017624605
200.6736538220158150.101228858320002

Both of the features now scale in [0,1]. It is also possible to scale by a specific partition with the by parameter.