1
$\begingroup$

I have just followed this tutorial in order to try to understand PCA.

https://towardsdatascience.com/pca-using-python-scikit-learn-e653f8989e60

However I used a different dataset (Water potability).

ph  Hardness    Solids  Chloramines Sulfate Conductivity    Organic_carbon  Trihalomethanes Turbidity   Potability
0   NaN 204.890455  20791.318981    7.300212    368.516441  564.308654  10.379783   86.990970   2.963135    0
1   3.716080    129.422921  18630.057858    6.635246    NaN 592.885359  15.180013   56.329076   4.500656    0
2   8.099124    224.236259  19909.541732    9.275884    NaN 418.606213  16.868637   66.420093   3.055934    0
3   8.316766    214.373394  22018.417441    8.059332    356.886136  363.266516  18.436524   100.341674  4.628771    0
4   9.092223    181.101509  17978.986339    6.546600    310.135738  398.410813  11.558279   31.997993   4.075075    0

I normalized the data:

from sklearn.preprocessing import StandardScaler
features = ['ph', 'Hardness', 'Solids', 'Chloramines', 'Sulfate','Conductivity','Organic_carbon','Trihalomethanes','Turbidity']
# Separating out the features
x = df.loc[:, features].values
# Separating out the target
y = df.loc[:,['Potability']].values
# Standardizing the features
x = StandardScaler().fit_transform(x)

and then I did PCA:

import pandas as pd 
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents, columns = ['principal component 1', 'principal component 2'])

principalDf.head(5)

When I tried to plot, I dont see a clear separation on the scatter plot

enter image description here

And then I checked the variance, it sums 50% only

pca.explained_variance_ratio_

array([0.25580228, 0.2538827 ])

THe question is:

  1. What does this means? I mean its not closed to 90%, so it means features are not correlated at all to the target potability field?

  2. What could you conclude from the plot here?

Or maybe I used PCA incorrectly

$\endgroup$

1 Answer 1

2
$\begingroup$

You probably use PCA correctly. However, you need more than 2 components to explain the variability of your data. You should try more components or other dimension reduction techniques. You may see other here.

You may try other preprocessing techniques too and then use PCA again.

And, other option is understand if all variables before reduction is sufficient important to your model. After this, you may drop the component and then try again to reduce the components.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.