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

PCA, SVD, LDA on Iris Dataset

machine programs
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)
32 views3 pages

PCA, SVD, LDA on Iris Dataset

machine programs
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

15/10/2024, 16:13 week6 - Colab

import numpy as np
import pandas as pd
from [Link] import PCA
from [Link] import load_iris
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
X = [Link]
y = [Link]
feature_names = iris.feature_names

# Perform PCA
pca = PCA(n_components=2) # Reduce to 2 dimensions for visualization
X_pca = pca.fit_transform(X)

# Create a DataFrame for easier plotting


df_pca = [Link](data=X_pca, columns=['Principal Component 1', 'Principal Component 2'])
df_pca['Target'] = y

# Plot the results


[Link](figsize=(8, 6))
scatter = [Link](df_pca['Principal Component 1'], df_pca['Principal Component 2'], c=df_pca['Target'], cmap='viridis')
[Link](scatter, label='Target')
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]('PCA of Iris Dataset')
[Link]()

import numpy as np
import pandas as pd
from [Link] import load_iris
from [Link] import StandardScaler
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
X = [Link]
y = [Link]

# Standardize the data (mean=0, variance=1)


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Perform SVD
U, S, VT = [Link](X_scaled, full_matrices=False)

# Project data onto the first 2 principal components


X_svd = U[:, :2] @ [Link](S[:2])

[Link] 1/3
15/10/2024, 16:13 week6 - Colab

# Create a DataFrame for easier plotting


df_svd = [Link](data=X_svd, columns=['Singular Value 1', 'Singular Value 2'])
df_svd['Target'] = y

# Plot the results


[Link](figsize=(8, 6))
scatter = [Link](df_svd['Singular Value 1'], df_svd['Singular Value 2'], c=df_svd['Target'], cmap='viridis')
[Link](scatter, label='Target')
[Link]('Singular Value 1')
[Link]('Singular Value 2')
[Link]('SVD of Iris Dataset')
[Link]()

import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
X = [Link]
y = [Link]

# Perform LDA
lda = LinearDiscriminantAnalysis(n_components=2) # Reduce to 2 dimensions for visualization
X_lda = lda.fit_transform(X, y)

# Create a DataFrame for easier plotting


df_lda = [Link](data=X_lda, columns=['LD 1', 'LD 2'])
df_lda['Target'] = y

# Plot the results


[Link](figsize=(8, 6))
scatter = [Link](df_lda['LD 1'], df_lda['LD 2'], c=df_lda['Target'], cmap='viridis')
[Link](scatter, label='Target')
[Link]('LD 1')
[Link]('LD 2')
[Link]('LDA of Iris Dataset')
[Link]()

[Link] 2/3
15/10/2024, 16:13 week6 - Colab

[Link] 3/3

You might also like