0% found this document useful (0 votes)
6 views3 pages

ML Exp11

The document describes implementing principal component analysis (PCA) for dimensionality reduction on sample data with 10 features. PCA is performed with 5 and 1 components, and regression models are fitted on the original and reduced data to compare mean squared errors, demonstrating PCA can reduce dimensions while maintaining accuracy.

Uploaded by

godizlatan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

ML Exp11

The document describes implementing principal component analysis (PCA) for dimensionality reduction on sample data with 10 features. PCA is performed with 5 and 1 components, and regression models are fitted on the original and reduced data to compare mean squared errors, demonstrating PCA can reduce dimensions while maintaining accuracy.

Uploaded by

godizlatan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 11

Aim: To implement Principal Component Analysis (PCA) algorithm for dimensionality reduction
Code / Output:

!pip install faker

from sklearn.decomposition import PCA


from faker import Faker
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Generate sample data with 100 samples and 10 features


fake = Faker()
data = []
for _ in range(100):
data.append([float(fake.pydecimal(positive=True)) for _ in
range(10)])

# Convert data to numpy array


import numpy as np
data = np.array(data)

# Define target variable (assuming the last feature is the target)


y = data[:, -1]
X = data[:, :-1] # All features except the last one

# Train regression model on original data


original_model = LinearRegression()
original_model.fit(X, y)

# Perform PCA with 5 components


pca = PCA(n_components=5)
pca.fit(X)

# Transform data to principal components


data_reduced = pca.transform(X)

# Train regression model on reduced data


reduced_model = LinearRegression()
reduced_model.fit(data_reduced, y)

# Make predictions on a small sample for comparison


sample_data = data[:5, :-1] # First 5 samples, excluding target
sample_reduced = pca.transform(sample_data)

original_predictions = original_model.predict(sample_data)
reduced_predictions = reduced_model.predict(sample_reduced)

# Calculate mean squared error for both models on the sample data
original_mse = mean_squared_error(data[:5, -1], original_predictions)
reduced_mse = mean_squared_error(data[:5, -1], reduced_predictions)

# Print results
print("Original Model MSE:", original_mse)
print("Reduced Model MSE:", reduced_mse)

# Print sample predictions for comparison (optional)


print("\nSample Predictions:")
print("Original:", original_predictions)
print("Reduced:", reduced_predictions)
# Perform PCA with 1 component
pca = PCA(n_components=1)
pca.fit(X)

# Transform data to principal component


data_reduced = pca.transform(X)

# Train regression model on reduced data


reduced_model = LinearRegression()
reduced_model.fit(data_reduced, y)

# Make predictions on a small sample for comparison


sample_data = data[:5, :-1] # First 5 samples, excluding target
sample_reduced = pca.transform(sample_data)

original_predictions = original_model.predict(sample_data)
reduced_predictions = reduced_model.predict(sample_reduced)

# Calculate mean squared error for both models on the sample data
original_mse = mean_squared_error(data[:5, -1], original_predictions)
reduced_mse = mean_squared_error(data[:5, -1], reduced_predictions)

# Print results
print("Original Model MSE:", original_mse)
print("Reduced Model MSE:", reduced_mse)

# Print sample predictions for comparison (optional)


print("\nSample Predictions:")
print("Original:", original_predictions)
print("Reduced:", reduced_predictions)

Conclusion: Thus, we have successfully implemented Principal Component Analysis (PCA) algorithm
and achieved dimensionality reduction.

You might also like