Loading...

Decomposition

Decomposition is the process of using an orthogonal transformation to convert a set of observations of possibly-correlated variables (with numerical values) into a set of values of linearly-uncorrelated variables called principal components.

Since some algorithms are sensitive to correlated predictors, it can be a good idea to use the PCA (Principal Component Analysis: Decomposition Technique) before applying the algorithm. Since some algorithms are also sensitive to the number of predictors, we’ll have to be picky with which variables we include.

To demonstrate data decomposition in vastorbit, we’ll use the well-known iris dataset.

from vastorbit.datasets import load_iris

iris = load_iris()
iris.head(100)
123
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
15.13.51.40.2Iris-setosa
24.93.01.40.2Iris-setosa
34.73.21.30.2Iris-setosa
44.63.11.50.2Iris-setosa
55.03.61.40.2Iris-setosa
65.43.91.70.4Iris-setosa
74.63.41.40.3Iris-setosa
85.03.41.50.2Iris-setosa
94.42.91.40.2Iris-setosa
104.93.11.50.1Iris-setosa
115.43.71.50.2Iris-setosa
124.83.41.60.2Iris-setosa
134.83.01.40.1Iris-setosa
144.33.01.10.1Iris-setosa
155.84.01.20.2Iris-setosa
165.74.41.50.4Iris-setosa
175.43.91.30.4Iris-setosa
185.13.51.40.3Iris-setosa
195.73.81.70.3Iris-setosa
205.13.81.50.3Iris-setosa
215.43.41.70.2Iris-setosa
225.13.71.50.4Iris-setosa
234.63.61.00.2Iris-setosa
245.13.31.70.5Iris-setosa
254.83.41.90.2Iris-setosa
265.03.01.60.2Iris-setosa
275.03.41.60.4Iris-setosa
285.23.51.50.2Iris-setosa
295.23.41.40.2Iris-setosa
304.73.21.60.2Iris-setosa
314.83.11.60.2Iris-setosa
325.43.41.50.4Iris-setosa
335.24.11.50.1Iris-setosa
345.54.21.40.2Iris-setosa
354.93.11.50.1Iris-setosa
365.03.21.20.2Iris-setosa
375.53.51.30.2Iris-setosa
384.93.11.50.1Iris-setosa
394.43.01.30.2Iris-setosa
405.13.41.50.2Iris-setosa
415.03.51.30.3Iris-setosa
424.52.31.30.3Iris-setosa
434.43.21.30.2Iris-setosa
445.03.51.60.6Iris-setosa
455.13.81.90.4Iris-setosa
464.83.01.40.3Iris-setosa
475.13.81.60.2Iris-setosa
484.63.21.40.2Iris-setosa
495.33.71.50.2Iris-setosa
505.03.31.40.2Iris-setosa
517.03.24.71.4Iris-versicolor
526.43.24.51.5Iris-versicolor
536.93.14.91.5Iris-versicolor
545.52.34.01.3Iris-versicolor
556.52.84.61.5Iris-versicolor
565.72.84.51.3Iris-versicolor
576.33.34.71.6Iris-versicolor
584.92.43.31.0Iris-versicolor
596.62.94.61.3Iris-versicolor
605.22.73.91.4Iris-versicolor
615.02.03.51.0Iris-versicolor
625.93.04.21.5Iris-versicolor
636.02.24.01.0Iris-versicolor
646.12.94.71.4Iris-versicolor
655.62.93.61.3Iris-versicolor
666.73.14.41.4Iris-versicolor
675.63.04.51.5Iris-versicolor
685.82.74.11.0Iris-versicolor
696.22.24.51.5Iris-versicolor
705.62.53.91.1Iris-versicolor
715.93.24.81.8Iris-versicolor
726.12.84.01.3Iris-versicolor
736.32.54.91.5Iris-versicolor
746.12.84.71.2Iris-versicolor
756.42.94.31.3Iris-versicolor
766.63.04.41.4Iris-versicolor
776.82.84.81.4Iris-versicolor
786.73.05.01.7Iris-versicolor
796.02.94.51.5Iris-versicolor
805.72.63.51.0Iris-versicolor
815.52.43.81.1Iris-versicolor
825.52.43.71.0Iris-versicolor
835.82.73.91.2Iris-versicolor
846.02.75.11.6Iris-versicolor
855.43.04.51.5Iris-versicolor
866.03.44.51.6Iris-versicolor
876.73.14.71.5Iris-versicolor
886.32.34.41.3Iris-versicolor
895.63.04.11.3Iris-versicolor
905.52.54.01.3Iris-versicolor
915.52.64.41.2Iris-versicolor
926.13.04.61.4Iris-versicolor
935.82.64.01.2Iris-versicolor
945.02.33.31.0Iris-versicolor
955.62.74.21.3Iris-versicolor
965.73.04.21.2Iris-versicolor
975.72.94.21.3Iris-versicolor
986.22.94.31.3Iris-versicolor
995.12.53.01.1Iris-versicolor
1005.72.84.11.3Iris-versicolor

Notice that all the predictors are well-correlated with each other.

iris.corr()

Let’s compute the PCA of the different elements.

from vastorbit.machine_learning.vast import PCA

model = PCA()
model.fit(
    iris,
    [
        "PetalLengthCm",
        "SepalWidthCm",
        "SepalLengthCm",
        "PetalWidthCm",
    ],
)

Let’s compute the correlation matrix of the result of the PCA.

model.transform().corr()

Notice that the predictors are now independant and combined together and they have the exact same amount of information than the previous variables. Let’s look at the accumulated explained variance of the PCA components.

model.explained_variance_ratio_

Most of the information is in the first two components with more than 97.7% of explained variance. We can export this result to a VastFrame.

model.transform(n_components = 2)
123
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
123
col0
Decimal(38,33)
100%
123
col1
Decimal(38,33)
100%
15.13.51.40.2Iris-setosa-2.68420712510395280.32660731476437427
24.93.01.40.2Iris-setosa-2.7153906156341328-0.1695568475560382
34.73.21.30.2Iris-setosa-2.8898195396179185-0.1373456096050428
44.63.11.50.2Iris-setosa-2.746437197308736-0.3111243157520073
55.03.61.40.2Iris-setosa-2.72859298183131930.3339245635684391
65.43.91.70.4Iris-setosa-2.2798973610096040.7477827132251201
74.63.41.40.3Iris-setosa-2.8208906821806323-0.0821045110246971
85.03.41.50.2Iris-setosa-2.6264819933238230.17040534896027557
94.42.91.40.2Iris-setosa-2.8879585653356346-0.5707980263316074
104.93.11.50.1Iris-setosa-2.6738446867191237-0.10669170375275287
115.43.71.50.2Iris-setosa-2.50652678933890940.6519350136725583
124.83.41.60.2Iris-setosa-2.61314271827105940.021520631960241694
134.83.01.40.1Iris-setosa-2.787433975997099-0.22774018887112002
144.33.01.10.1Iris-setosa-3.225200446274981-0.503279909485443
155.84.01.20.2Iris-setosa-2.64354321694115461.1861948994134386
165.74.41.50.4Iris-setosa-2.3838693237993851.3447543445598482
175.43.91.30.4Iris-setosa-2.62252620312581540.8180896745965832
185.13.51.40.3Iris-setosa-2.6483227324791310.319136667750872
195.73.81.70.3Iris-setosa-2.199077961430770.8792440880917256
205.13.81.50.3Iris-setosa-2.58734618891774340.5204736388059529