Machine Learning: Apply PCA on Datasets and Related

FactorAnalyzer
https://github.com/EducationalTestingService/factor_analyzer

sruti-jain/Marketing-Analysis-for-Hotel-Chain-website
https://github.com/sruti-jain/Marketing-Analysis-for-Hotel-Chain-website

Understanding PCA (Principal Component Analysis) with Python
https://towardsdatascience.com/dive-into-pca-principal-component-analysis-with-python-43ded13ead21

The code for the most part will work though it used an earlier version of Python
You will need module: StandardScaler
Otherwise you might find the code below to be useful:
[# ref: https://python-for-multivariate-analysis.readthedocs.io/a_little_book_of_python_for_multivariate_analysis.html]

import sklearn
from sklearn import preprocessing

standardisedX = sklearn.preprocessing.scale(cancer.data)
standardisedX = pd.DataFrame(standardisedX) #, index=cancer.data.index, columns=cancer.data.columns)
standardisedX.apply(np.mean)

X_scaled = standardisedX

from sklearn import decomposition
pca = decomposition.PCA(n_components=3).fit(standardisedX)
pca = decomposition.PCA(n_components=3).fit_transform(standardisedX)
X_pca = pca

ex_variance=np.var(X_pca,axis=0)
ex_variance_ratio = ex_variance/np.sum(ex_variance)
ex_variance_ratio

Xax=X_pca[:,0]
Yax=X_pca[:,1]
labels=cancer.target
cdict={0:’red’,1:’green’}
labl={0:’Malignant’,1:’Benign’}
marker={0:’*’,1:’o’}
alpha={0:.3, 1:.5}
fig,ax=plt.subplots(figsize=(7,5))
fig.patch.set_facecolor(‘white’)
for l in np.unique(labels):
ix=np.where(labels==l)
ax.scatter(Xax[ix],Yax[ix],c=cdict[l],s=40,
label=labl[l],marker=marker[l],alpha=alpha[l])

# for loop ends
plt.xlabel("First Principal Component",fontsize=14)
plt.ylabel("Second Principal Component",fontsize=14)
plt.legend()
plt.show()

Sayed Ahmed

Linkedin: https://ca.linkedin.com/in/sayedjustetc

Blog: http://sitestree.com, http://bangla.salearningschool.com