Experiment No 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

EXPERIMENT NO .

8
Aim : To Implement Principal Component Analysis using python.
Roll No : 09

Program:
import numpy as np
import matplotlib.pyplot as plt

# Create a random dataset for demonstration purposes


np.random.seed(0)
data = np.random.rand(100, 2) # 100 data points with 2 features

# Standardize the data (optional but recommended for PCA)


mean = np.mean(data, axis=0)
std_dev = np.std(data, axis=0)
standardized_data = (data - mean) / std_dev

# Compute the covariance matrix


cov_matrix = np.cov(standardized_data, rowvar=False)

# Perform PCA to obtain eigenvalues and eigenvectors


eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)

# Sort eigenvalues and eigenvectors in descending order


sorted_indices = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[sorted_indices]
eigenvectors = eigenvectors[:, sorted_indices]

# Choose the number of principal components to retain


k=1

# Project the data onto the selected principal components


projected_data = standardized_data.dot(eigenvectors[:, :k])

# Plot the original data and projected data


plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.scatter(standardized_data[:, 0], standardized_data[:, 1], c='b', marker='o', label='Original
Data')
plt.title('Original Data')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.subplot(1, 2, 2)
plt.scatter(projected_data, np.zeros_like(projected_data), c='r', marker='o', label='Projected
Data')
plt.title(f'Projected Data (k={k})')
plt.xlabel('Principal Component 1')
plt.legend()

plt.tight_layout()
plt.show()

Output:

You might also like