.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/plot_pca_spec.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gettingstarted_examples_gallery_auto_examples_analysis_a_decomposition_plot_pca_spec.py: PCA analysis example -------------------- In this example, we perform the PCA dimensionality reduction of a spectra dataset .. GENERATED FROM PYTHON SOURCE LINES 16-17 Import the spectrochempy API package .. GENERATED FROM PYTHON SOURCE LINES 17-19 .. code-block:: default import spectrochempy as scp .. GENERATED FROM PYTHON SOURCE LINES 20-21 Load a dataset .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: default dataset = scp.read_omnic("irdata/nh4y-activation.spg")[::5] print(dataset) _ = dataset.plot() .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_001.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none NDDataset: [float64] a.u. (shape: (y:11, x:5549)) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Create a PCA object and fit the dataset so that the explained variance is greater or equal to 99.9% .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: default pca = scp.PCA(n_components=0.999) pca.fit(dataset) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-34 The number of fitted components is given by the n_components attribute (We obtain 23 components) .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: default pca.n_components .. rst-class:: sphx-glr-script-out .. code-block:: none 6 .. GENERATED FROM PYTHON SOURCE LINES 37-38 Transform the dataset to a lower dimensionality using all the fitted components .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: default scores = pca.transform() scores .. raw:: html
name nh4y-activation_PCA.transform
author runner@fv-az1333-513
created 2025-02-12 23:18:36+00:00
history
2025-02-12 23:18:36+00:00> Created using method PCA.transform
DATA
title
values
[[ 71.56 -16.87 ... -0.006776 0.216]
[ 50.39 8.205 ... 0.5566 -0.03804]
...
[ -26.25 -1.694 ... 0.1281 0.6077]
[ -25.46 -1.456 ... 4.175 -0.7883]]
shape (y:11, k:6)
DIMENSION `k`
size 6
title components
labels
[ #0 #1 #2 #3 #4 #5]
DIMENSION `y`
size 11
title acquisition timestamp (GMT)
coordinates
[1.468e+09 1.468e+09 ... 1.468e+09 1.468e+09] s
labels
[[ 2016-07-06 19:03:14+00:00 2016-07-06 19:53:14+00:00 ... 2016-07-07 02:43:15+00:00 2016-07-07 03:33:17+00:00]
[ vz0466.spa, Wed Jul 06 21:00:38 2016 (GMT+02:00) vz0471.spa, Wed Jul 06 21:50:37 2016 (GMT+02:00) ...
vz0512.spa, Thu Jul 07 04:40:39 2016 (GMT+02:00) vz0517.spa, Thu Jul 07 05:30:41 2016 (GMT+02:00)]]


.. GENERATED FROM PYTHON SOURCE LINES 42-44 Finally, display the results graphically ScreePlot .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: default _ = pca.screeplot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_002.png :alt: Scree plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_002.png :class: sphx-glr-multi-img * .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_003.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_003.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 47-48 Score Plot .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: default _ = pca.scoreplot(scores, 1, 2) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_004.png :alt: Score plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-52 Score Plot for 3 PC's in 3D .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: default _ = pca.scoreplot(scores, 1, 2, 3) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_005.png :alt: Score plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-56 Displays 4 loadings .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: default _ = pca.loadings[:4].plot(legend=True) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_006.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-60 Here we do a masking of the saturated region between 882 and 1280 cm^-1 .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: default dataset[ :, 882.0:1280.0 ] = scp.MASKED # remember: use float numbers for slicing (not integer) _ = dataset.plot() .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_007.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-67 Apply the PCA model .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: default pca = scp.PCA(n_components=0.999) pca.fit(dataset) pca.n_components .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 72-74 As seen above, now only 4 components instead of 23 are necessary to 99.9% of explained variance. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: default _ = pca.screeplot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_008.png :alt: Scree plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_008.png :class: sphx-glr-multi-img * .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_009.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_009.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 77-78 Displays the loadings .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: default _ = pca.loadings.plot(legend=True) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_010.png :alt: plot pca spec :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-82 Let's plot the scores .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default scores = pca.transform() _ = pca.scoreplot(scores, 1, 2) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_011.png :alt: Score plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-89 Labeling scoreplot with spectra labels Our dataset has already two columns of labels for the spectra but there are little too long for display on plots. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: default scores.y.labels .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 2016-07-06 19:03:14+00:00, vz0466.spa, Wed Jul 06 21:00:38 2016 (GMT+02:00)], [ 2016-07-06 19:53:14+00:00, vz0471.spa, Wed Jul 06 21:50:37 2016 (GMT+02:00)], ..., [ 2016-07-07 02:43:15+00:00, vz0512.spa, Thu Jul 07 04:40:39 2016 (GMT+02:00)], [ 2016-07-07 03:33:17+00:00, vz0517.spa, Thu Jul 07 05:30:41 2016 (GMT+02:00)]], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 92-93 So we define some short labels for each component, and add them as a third column: .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: default labels = [lab[:6] for lab in dataset.y.labels[:, 1]] scores.y.labels = labels # Note this does not replace previous labels, # but adds a column. .. GENERATED FROM PYTHON SOURCE LINES 98-99 now display thse .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: default _ = pca.scoreplot(scores, 1, 2, show_labels=True, labels_column=2) .. image-sg:: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_012.png :alt: Score plot :srcset: /gettingstarted/examples/gallery/auto_examples_analysis/a_decomposition/images/sphx_glr_plot_pca_spec_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-104 This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python .. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: default # scp.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.868 seconds) .. _sphx_glr_download_gettingstarted_examples_gallery_auto_examples_analysis_a_decomposition_plot_pca_spec.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pca_spec.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pca_spec.ipynb `