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 VerticaPy, we’ll use the well-known iris dataset.

from verticapy.datasets import load_iris

iris = load_iris()
iris.head(100)
123
SepalLengthCm
Numeric(5,2)
100%
...
123
PetalWidthCm
Numeric(5,2)
100%
Abc
Species
Varchar(30)
100%
14.6...0.2Iris-setosa
24.7...0.2Iris-setosa
34.7...0.2Iris-setosa
44.8...0.1Iris-setosa
54.8...0.2Iris-setosa
64.8...0.2Iris-setosa
74.9...0.2Iris-setosa
84.9...0.1Iris-setosa
94.9...0.1Iris-setosa
104.9...0.1Iris-setosa
115.0...1.0Iris-versicolor
125.0...0.2Iris-setosa
135.1...0.2Iris-setosa
145.4...1.5Iris-versicolor
155.4...0.4Iris-setosa
165.4...0.4Iris-setosa
175.5...1.0Iris-versicolor
185.5...1.1Iris-versicolor
195.6...1.3Iris-versicolor
205.7...1.2Iris-versicolor
215.7...0.4Iris-setosa
225.8...2.4Iris-virginica
235.9...1.8Iris-versicolor
246.1...1.4Iris-versicolor
256.1...1.8Iris-virginica
266.3...1.5Iris-versicolor
276.3...1.6Iris-versicolor
286.3...2.5Iris-virginica
296.4...1.3Iris-versicolor
306.5...1.8Iris-virginica
316.5...2.2Iris-virginica
326.7...1.7Iris-versicolor
336.8...1.4Iris-versicolor
346.8...2.3Iris-virginica
357.0...1.4Iris-versicolor
367.1...2.1Iris-virginica
377.7...2.2Iris-virginica
384.4...0.2Iris-setosa
394.5...0.3Iris-setosa
404.8...0.2Iris-setosa
415.0...1.0Iris-versicolor
425.1...0.5Iris-setosa
435.1...0.2Iris-setosa
445.2...1.4Iris-versicolor
455.2...0.2Iris-setosa
465.2...0.1Iris-setosa
475.4...0.4Iris-setosa
485.5...0.2Iris-setosa
495.6...1.3Iris-versicolor
505.8...1.2Iris-versicolor
515.8...1.9Iris-virginica
525.8...1.9Iris-virginica
535.9...1.5Iris-versicolor
545.9...1.8Iris-virginica
556.0...1.6Iris-versicolor
566.0...1.5Iris-versicolor
576.1...1.2Iris-versicolor
586.2...1.8Iris-virginica
596.2...1.3Iris-versicolor
606.3...1.3Iris-versicolor
616.3...1.8Iris-virginica
626.4...2.3Iris-virginica
636.5...1.5Iris-versicolor
646.5...2.0Iris-virginica
656.5...2.0Iris-virginica
666.6...1.3Iris-versicolor
676.6...1.4Iris-versicolor
686.7...1.4Iris-versicolor
696.7...1.5Iris-versicolor
706.9...1.5Iris-versicolor
716.9...2.1Iris-virginica
726.9...2.3Iris-virginica
737.2...1.6Iris-virginica
747.2...1.8Iris-virginica
757.3...1.8Iris-virginica
767.7...2.3Iris-virginica
773.3...7.8Iris-setosa
783.3...7.8Iris-setosa
793.3...7.8Iris-setosa
803.3...7.8Iris-setosa
813.3...7.8Iris-setosa
823.3...7.8Iris-setosa
833.3...7.8Iris-setosa
843.3...7.8Iris-setosa
853.3...7.8Iris-setosa
863.3...7.8Iris-setosa
873.3...7.8Iris-setosa
883.3...7.8Iris-setosa
893.3...7.8Iris-setosa
903.3...7.8Iris-setosa
913.3...7.8Iris-setosa
923.3...7.8Iris-setosa
933.3...7.8Iris-setosa
943.3...7.8Iris-setosa
953.3...7.8Iris-setosa
963.3...7.8Iris-setosa
973.3...7.8Iris-setosa
983.3...7.8Iris-setosa
993.3...7.8Iris-setosa
1003.3...7.8Iris-setosa

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

iris.corr()

Let’s compute the PCA of the different elements.

from verticapy.machine_learning.vertica import PCA

model = PCA()

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



=======
columns
=======
index|    name     |  mean  |   sd   
-----+-------------+--------+--------
  1  |petallengthcm| 5.29520| 2.65046
  2  |sepalwidthcm | 3.67240| 0.83213
  3  |sepallengthcm| 5.02600| 1.23158
  4  |petalwidthcm | 2.63920| 2.66236


===============
singular_values
===============
index| value  |explained_variance|accumulated_explained_variance
-----+--------+------------------+------------------------------
  1  | 3.10953|      0.59239     |            0.59239           
  2  | 2.35110|      0.33866     |            0.93104           
  3  | 0.99380|      0.06051     |            0.99155           
  4  | 0.37133|      0.00845     |            1.00000           


====================
principal_components
====================
index|  PC1   |  PC2   |  PC3   |  PC4   
-----+--------+--------+--------+--------
  1  | 0.62676| 0.76262| 0.09839|-0.12608
  2  | 0.20444| 0.03229|-0.42096| 0.88315
  3  |-0.25445| 0.17577| 0.83700| 0.45143
  4  | 0.70755|-0.62167| 0.33548| 0.01884


========
counters
========
   counter_name   |counter_value
------------------+-------------
accepted_row_count|     250     
rejected_row_count|      0      
 iteration_count  |      1      


===========
call_string
===========
SELECT PCA('"public"."_verticapy_tmp_pca_v_mldb_43ed918e97bc11efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_443e79fa97bc11efa8720242ac120002_"', '"PetalLengthCm", "SepalWidthCm", "SepalLengthCm", "PetalWidthCm"'
USING PARAMETERS scale=false);

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_
Out[4]: array([0.59238864, 0.33865565, 0.06050821, 0.00844749])

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 vDataFrame.

model.transform(n_components = 2)
Abc
Species
Varchar(30)
100%
...
123
col1
Float(22)
100%
123
col2
Float(22)
100%
1Iris-setosa...-1.151989691196720.27622003481473
2Iris-setosa...-1.247423308776370.392295855356348
3Iris-setosa...-1.323757240110310.445025462456261
4Iris-setosa...-1.321835157863650.541843247223654
5Iris-setosa...-1.281525111072790.518058857623143
6Iris-setosa...-1.296527227123520.580475646448235
7Iris-setosa...-1.188403694479310.555939181390084
8Iris-setosa...-1.264159795748630.638911299240627
9Iris-setosa...-1.264159795748630.638911299240627
10Iris-setosa...-1.264159795748630.638911299240627
11Iris-versicolor...-1.186240260437260.446220218801964
12Iris-setosa...-1.069396517081090.662694415132234
13Iris-setosa...-0.9608318674101130.724609395749264
14Iris-versicolor...-0.7439871673224630.673959286317241
15Iris-setosa...-0.6771814553819140.843411194948978
16Iris-setosa...-0.524072475687260.824403426424333
17Iris-versicolor...-0.9541945597951710.901067700886617
18Iris-versicolor...-0.9088838076540760.856477715011076
19Iris-versicolor...-0.7451437081793780.888400452127056
20Iris-versicolor...-0.6918912213499841.03651661116969