python-scipyHow do I use Python and SciPy to perform principal component analysis?
Principal Component Analysis (PCA) is a technique used to reduce the dimensionality of a dataset by transforming it into a new set of uncorrelated variables called principal components. To perform PCA with Python and SciPy, we can use the scipy.linalg.svd
function to calculate the singular value decomposition of the dataset. The singular values and vectors can then be used to calculate the principal components.
For example, consider the following dataset:
data = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
We can use the scipy.linalg.svd
function to calculate the singular value decomposition of this dataset:
U, S, V = scipy.linalg.svd(data)
The output of this function is three matrices: U
, S
, and V
. U
is a unitary matrix containing the left singular vectors, S
is a vector containing the singular values, and V
is a unitary matrix containing the right singular vectors. The principal components can then be calculated as follows:
principal_components = U @ np.diag(S)
The output of this code is:
array([[-2.44948974e+00, 0.00000000e+00, 0.00000000e+00],
[-4.89897948e+00, 0.00000000e+00, 0.00000000e+00],
[-7.34846923e+00, 0.00000000e+00, 0.00000000e+00]])
Code explanation
-
U, S, V = scipy.linalg.svd(data)
: This line of code calculates the singular value decomposition of the dataset and stores the results in three matricesU
,S
, andV
. -
principal_components = U @ np.diag(S)
: This line of code calculates the principal components from the singular value decomposition and stores them in the matrixprincipal_components
.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use the NumPy transpose function in Python?
- How do I create a numpy array of zeros using Python?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use the scipy ttest_ind function in Python?
- How do I use the trapz function in Python SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
See more codes...