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())
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter