1.Implement Dimensionality reduction by Principal Component Analysis and analyze the results of both methods. Consider petrol _consumption.cs dataset. Also write the program to visualize insights of the dataset.
Sun Nov 03 2024 12:53:58 GMT+0000 (Coordinated Universal Time)
Saved by
@varuntej
#python
import pandas as pd
# Load the dataset
df = pd.read_csv('petrol_consumption.csv')
# Display basic information about the dataset
print(df.head())
print(df.describe())
print(df.info())
from sklearn.preprocessing import StandardScaler
# Separate the features and target variable if there's one
X = df.drop(columns=['Petrol_Consumption']) # Assuming 'Petrol_Consumption' is the target variable
y = df['Petrol_Consumption']
# Standardize the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
from sklearn.decomposition import PCA
# Apply PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
# Check the explained variance
print("Explained variance by each component:", pca.explained_variance_ratio_)
print("Cumulative explained variance:", pca.explained_variance_ratio_.cumsum())
content_copyCOPY
Comments