I already read some the question that I could find here or elsewhere. I wrote the code using other questions available on stackoverflow but still it doesn't work sometimes. There are three types of problems that might occur. I write the code here first and then will list the problems:
from matplotlib.mlab import PCA
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import pyplot as plt
fig=plt.figure()
ax=plt.subplot(111,projection='3d')
sample=np.asarray(sample)
result=PCA(sample)
hist, xedges, yedges =np.histogram2d(result.Y[:,0],result.Y[:,1])
x_data, y_data = np.meshgrid( np.arange(hist.shape[1]),np.arange(hist.shape[0]) )
x_data = x_data.flatten()
y_data = y_data.flatten()
z_data = hist.flatten()
ax.bar3d( x_data,y_data,np.zeros(len(z_data)),1, 1, z_data )
fig.tight_layout()
fig.savefig('3Dhist.eps')
plt.show()
1- First problem is the following error:
LinAlgError: SVD did not converge
97
98 def _raise_linalgerror_svd_nonconvergence(err, flag):
---> 99 raise LinAlgError("SVD did not converge")
100
101 def get_linalg_error_extobj(callback):
LinAlgError: SVD did not converge
And it rises when all of my vectors share one equal coordinate, i.e. they already lie on the same plane. Obviously a PCA is still possible with a unique answer but it gives the above error. To test it you can set sample to:
sample=[[ 0., 0.2, 0. ],
[ 0.4, 0.2, 0. ],
[ 0.4, 0.4, 0. ],
[ 0., 0.2, 0. ],
[ 0.6, 0.2, 0. ]]
2-(Semi-Solved: See the answers section.) The second problem is the following warning:
/Users/****/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:1673: RuntimeWarning: invalid value encountered in divide
for n in normals])
/Users/****/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/colors.py:394: RuntimeWarning: invalid value encountered in greater
if (c.ravel() > 1).any() or (c.ravel() < 0).any():
/Users/****/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/colors.py:394: RuntimeWarning: invalid value encountered in less
if (c.ravel() > 1).any() or (c.ravel() < 0).any():
I don't know the reason of this warning but this rises usually on large samples that I generate from my distribution.
3-3rd problem is that I cannot save it in any other format than .png
or otherwise it doesn't work. To clarify the array I am using is a numpy.float64
.