How will it look?Variance is the expectation of the squared deviation of a random variable from its mean.
Informally, it measures the spread of a set of numbers from their mean.
The mathematical definition is:Covariance is a measure of the joint variability of two random variables.
In other words, how any 2 features vary from each other.
Using the covariance is very common when looking for patterns in data.
The mathematical definition is:From this definition, we can conclude that the covariance matrix will be symmetric.
This is important because it means that its eigenvectors will be real and non-negative, which makes it easier for us (we dare you to claim that working with complex numbers is easier than real ones!)After calculating the covariance matrix it will look like this:Covariance matrix visualizationAs you can see, the main diagonal is written as V (variance) and the rest is written as C (covariance), why is that?Because calculating the covariance of the same variable is basically calculating its variance (if you’re not sure why — please take a few minutes to understand what variance and covariance are).
Let’s calculate in Python the covariance matrix of the dataset using the following code:covarianceMatrix = pd.
DataFrame(data = np.
cov(dataset, rowvar = False), columns = irisModule.
feature_names, index = irisModule.
feature_names)The covariance matrix of the datasetWe’re not interested in the main diagonal, because they are the variance of the same variable.
Since we’re trying to find new patterns in the dataset, we’ll ignore the main diagonal.
Since the matrix is symmetric, covariance(a,b) = covariance(b,a), we will look only at the top values of the covariance matrix (above diagonal).
Something important to mention about covariance: if the covariance of variables a and b is positive, that means they vary in the same direction.
If the covariance of a and b is negative, they vary in different directions.
3) Calculate the eigenvalues and eigenvectors:As I mentioned at the beginning, eigenvalues and eigenvectors are the basic terms you must know in order to understand this step.
Therefore, I won’t explain it, but will rather move to compute them.
The eigenvector associated with the largest eigenvalue indicates the direction in which the data has the most variance.
Hence, using eigenvalues we will know what eigenvectors capture the most variability in our data.
eigenvalues, eigenvectors = np.
eig(covarianceMatrix)This is the vector of the eigenvalues, the first index at eigenvalues vector is associated with the first index at eigenvectors matrix.
The eigenvalues:Eigenvalues of the covariance matrixThe eigenvectors matrix:Eigenvectors matrix of the covariance matrix4) Choose the first K eigenvalues (K principal components/axises):The eigenvalues tells us the amount of variability in the direction of its corresponding eigenvector.
Therefore, the eigenvector with the largest eigenvalue is the direction with most variability.
We call this eigenvector the first principle component (or axis).
From this logic, the eigenvector with the second largest eigenvalue will be called the second principal component, and so on.
We see the following values:[4.
023]Let’s translate those values to percentages and visualize them.
We’ll take the percentage that each eigenvalue covers in the dataset.
totalSum = sum(eigenvalues)variablesExplained = [(i / totalSum) for i in sorted(eigenvalues, reverse = True)]As you can clearly see the first and eigenvalue takes 92.
5% and the second one takes 5.
3%, and the third and forth don’t cover much data from the total dataset.
Therefore we can easily decide to remain with only 2 variables, the first one and the second one.
featureVector = eigenvectors[:,:2]Let’s remove the third and fourth variables from the dataset.
Important to say that at this point we lose some information.
It is impossible to reduce dimensions without losing some information (under the assumption of general position).
PCA algorithm tells us the right way to reduce dimensions while keeping the maximum amount of information regarding our data.
And the remaining data set looks like this:Remaining eigenvectors after removal of two variables5) Build the new reduced dataset:We want to build a new reduced dataset from the K chosen principle components.
We’ll take the K chosen principles component (k=2 here) which gives us a matrix of size (4, 2), and we will take the original dataset which is a matrix of size (150, 4).
The matrices we need to work withWe’ll perform matrices multiplication in such a way:The first matrix we take is the matrix that contains the K component principles we’ve chosen and we transpose this matrix.
The second matrix we take is the original matrix and we transpose it.
At this point, we perform matrix multiplication between those two matrices.
After we perform matrix multiplication we transpose the result matrix.
Matrix multiplicationfeatureVectorTranspose = np.
transpose(featureVector)datasetTranspose = np.
transpose(dataset)newDatasetTranspose = np.
matmul(featureVectorTranspose, datasetTranspose)newDataset = np.
transpose(newDatasetTranspose)After performing the matrices multiplication and transposing the result matrix, these are the values we get for the new data which contains only the K principal components we’ve chosen.
ConclusionAs (we hope) you can now see, PCA is not that hard.
We’ve managed to reduce the dimensions of the dataset pretty easily using Python.
In our data set, we did not cause serious impact because we removed only 2 variables out of 4.
But let’s assume we have 200 variables in our data set, and we reduced from 200 variables to 3 variables — it’s already becoming more meaningful.
Hopefully, you’ve learned something new today.
Feel free to contact Chen Shani or Moshe Binieli on Linkedin for any questions.
.. More details