Given a Pandas dataframe, what is the best way (readability OR execution speed) to convert to a cvxopt matrix or vice versa?
Currently I am doing:
cvxMat = matrix(pdObj.as_matrix())
pdObj[:]=np.array(cvxMat)
Also, is there a reasonably readable way of doing vector or matrix algebra using a mixture of cvxopt matrices and pandas dataframes without converting the objects?
The following is a vector dot product (pdObj & cvxMat are column vectors) that is far from readable:
(matrix(pdObj.as_matrix()).T*cvxMat)[0]
Any advice?
Follow-up to waitingkuo's answer:
Just for illustration with pandas dataframes:
>>> m1 = cvxopt.matrix([[1, 2, 3], [2, 3, 4]])
>>> m2 = pd.DataFrame(np.array(m1)).T
>>> m1
<3x2 matrix, tc='i'>
>>> m2.shape
(2, 3)
>>> np.dot(m1,m2)
array([[ 5, 8, 11],
[ 8, 13, 18],
[11, 18, 25]])
But note:
>>> m1 * m2
0 1 2
0 1 4 9
1 4 9 16
[2 rows x 3 columns]