Principal component analysisΒΆ

try pytraj online:

http://mybinder.org/badge.svg

load trajectory

In [1]:
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
# load pytraj
import pytraj as pt

# load trajectory to memory
traj = pt.load('tz2.nc', 'tz2.parm7')

Get the data

In [2]:
# compute pca
data = pt.pca(traj, mask='!@H=', n_vecs=2)

print('projection values of each frame to first mode = {} \n'.format(data[0][0]))
print('projection values of each frame to second mode = {} \n'.format(data[0][1]))
print('eigvenvalues of first two modes', data[1][0])
print("")
print('eigvenvectors of first two modes: \n', data[1][1])
projection values of each frame to first mode = [  4.93425131  13.80002308  20.61605835 ..., -57.92280579 -61.25728607
 -52.85142136] 

projection values of each frame to second mode = [  4.03333616  -6.9132452  -14.53991318 ...,  -6.757936     2.1086719
  -3.60922861] 

eigvenvalues of first two modes [ 1399.36472919   240.42342439]

eigvenvectors of first two modes: 
 [[ 0.00251526  0.0405033  -0.0084492  ...,  0.04534061 -0.09515649
  -0.0475896 ]
 [ 0.0313375   0.12882763 -0.02338202 ..., -0.00380469 -0.04104856
  -0.04230317]]

plot

In [3]:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'  # high resolution
import matplotlib

projection_data = data[0]
from matplotlib import pyplot as plt

plt.scatter(projection_data[0], projection_data[1], marker='o', c=range(traj.n_frames), alpha=0.5)
plt.xlabel('PC1')
plt.ylabel('PC2')
cbar = plt.colorbar()
cbar.set_label('frame #')

(pca.ipynb; pca_evaluated.ipynb; pca.py)