PCA plot parameters

Viewed 143

I am a beginner in python trying to create a 2 component PCA plot, using pandas, sklearn.preprocessing, sklearn.decomposition, and Matplotlib.pyplot.

My data frame is very large, relating to the characteristics of different species of plant, with many variables (>100 columns), and I would like to compare the effect of one of the characteristics/columns (stem length) on the variance of the data. The column for stem length consists of floats, ranging in size from 0 to around 75cm.

I would like to plot a PCA comparing the variance of characteristics when stem length >40cm and stem length <40cm. However I have no idea how to proceed with this.

I have been using the following website as a guide for the PCA plot.

I have already written the following code:

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

df = pd.read_csv("plant_data.csv")

x = StandardScaler().fit_transform(x)

plt.style.use("seaborn-darkgrid")

pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents, 
                           columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, df[['stem_length']]], axis = 1)

How do I set the conditions for the parameters to be stem_length >40 and stem_length <40?

1 Answers

The given dataset in the question link is called the "Iris Dataset". Considering that, and your working example with 2-principal components, you now have finalDF with three features (or dimensions, or columns - in excel).

Now, you need to define a feature, which can be acheived as:

finalDF['stem_length_gt_40'] = finalDF['stem_length'].apply(lambda x: 1 if x > 40 else 0)

The code creates another column named stem_length_gt_40 whose value is 1 if stem_length > 40 else 0.

Considering this, now you can perhaps plot PCA-1 vs. PCA-2 and colour them differently based on stem_length_gt_40 using seaborn.scatterplot as below:

import seaborn as sns
import matplotlib.pyplot as plt

# plt.style.use("seaborn-darkgrid")

sns.scatterplot(x = 'principal component 1', y = 'principal component 2', data = finalDF, hue = 'stem_length_gt_40')

You can learn more about sns.scatterpolt over here.

Related