Principal component analysis (PCA) is a technique for reducing the dimensionality of data by projecting it onto a lower-dimensional subspace that preserves the most variance in the data. To perform PCA on a NumPy array without using other libraries, you can follow these steps:

Standardize the data by subtracting the mean and dividing by the standard deviation of each column. This ensures that each feature has zero mean and unit variance.
Compute the covariance matrix of the standardized data. This is a square matrix that measures how each feature varies with respect to each other.
Compute the eigenvalues and eigenvectors of the covariance matrix. These are scalar and vector quantities that represent the magnitude and direction of the principal components, respectively.
Sort the eigenvalues in descending order and select the top k eigenvalues and their corresponding eigenvectors, where k is the number of principal components you want to keep. The larger the eigenvalue, the more variance it explains.
Transform the standardized data by multiplying it with the matrix of eigenvectors. This projects the data onto the new subspace spanned by the principal components.
Alternatively, you can use singular value decomposition (SVD) to compute the principal components directly from the standardized data, without computing the covariance matrix or the eigenvalues and eigenvectors. SVD decomposes a matrix into three matrices: U, S, and V, where U contains the left singular vectors, S contains the singular values, and V contains the right singular vectors. The right singular vectors are equivalent to the eigenvectors of the covariance matrix, and the singular values are related to the eigenvalues by a scaling factor. Therefore, you can use SVD to obtain the principal components by selecting the top k columns of V and multiplying them with the standardized data.

To calculate the fraction of variance explained by each principal component, you can divide each eigenvalue (or squared singular value) by the sum of all eigenvalues (or squared singular values). This gives you a measure of how much information is retained by each component.

Here is some pseudocode for performing PCA using NumPy:

# Import NumPy
import numpy as np

# Define your data as a NumPy array
data = np.array(...)

# Standardize the data
mean = np.mean(data, axis=0) # calculate the mean of each column
std = np.std(data, axis=0) # calculate the standard deviation of each column
data_std = (data - mean) / std # standardize each column

# Method 1: Using covariance matrix and eigenvalues/eigenvectors

# Compute the covariance matrix
cov_matrix = np.cov(data_std.T) # transpose the data before passing it to np.cov

# Compute the eigenvalues and eigenvectors
eig_values, eig_vectors = np.linalg.eig(cov_matrix)

# Sort the eigenvalues and eigenvectors in descending order
idx = np.argsort(eig_values)[::-1] # get the indices of sorted eigenvalues
eig_values = eig_values[idx] # sort the eigenvalues
eig_vectors = eig_vectors[:, idx] # sort the eigenvectors accordingly

# Select the top k eigenvalues and eigenvectors
k = ... # specify your desired number of components
eig_values = eig_values[:k] # select the top k eigenvalues
eig_vectors = eig_vectors[:, :k] # select the top k eigenvectors

# Transform the data
data_pca = np.dot(data_std, eig_vectors) # multiply the standardized data with eigenvectors

# Method 2: Using singular value decomposition (SVD)

# Compute SVD
u, s, v = np.linalg.svd(data_std)

# Select the top k columns of v
k = ... # specify your desired number of components
v = v[:k] # select the top k columns of v

# Transform the data
data_pca = np.dot(data_std, v.T) # multiply the standardized data with transpose of v

# Calculate fraction of variance explained by each component
var_exp = (s**2 / np.sum(s**2))[:k] # divide each squared singular value by their sum and select top k values
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence