Mark sent a recent email complaining the previous biplot code not working. Though I was not able to replicate his errors,but from the error message figured the error was due to the PCA numbers supplied.

That reminded me of the simplification task that I intended to do on the previous version to make it work like it works in matlab.

No dependency on PCA data structure, send to variables, scores and coefficient and plot the biplot.

So here’s simplification,it works like matlab.

# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import PCA
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import PCA
def biplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n=coeff.shape[0]
scalex = 1.0/(xs.max()- xs.min())
scaley = 1.0/(ys.max()- ys.min())
plt.scatter(xs*scalex,ys*scaley)
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color='r',alpha=0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color='g', ha='center', va='center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color='g', ha='center', va='center')
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
# test
data=np.random.rand(100,10)*10
pca=PCA(scores)
biplot(pca.Y[:,0:2],pca.Wt[:,0:2])
plt.show()

### Like this:

Like Loading...

*Related*

Could you please explain what is score? Thanks

LikeLike

Hi Camille

The results of a PCA are usually discussed in terms of component scores, sometimes called factor scores (the transformed variable values corresponding to a particular data point), and loadings (the weight by which each standardized original variable should be multiplied to get the component score).

The biology essentially plots two scores and the arrows are the components.

Hope this helps. Please look at PCA on Wikipedia to understand better.

Thanks

LikeLike