Question

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]
Was it helpful?

Solution

You can get the numpy array from pandas by pdObj.values

You can do matrix multiplication between the cvxopt matrix and numpy matrix directly

In [90]: m1 = cvxopt.matrix([[1, 2, 3], [2, 3, 4]])

In [91]: m2 = np.matrix([[1, 2, 3], [2, 3, 4]])

In [92]: m1
Out[92]: <3x2 matrix, tc='i'>

In [94]: m2.shape
Out[94]: (2, 3)

In [95]: m1 * m2
Out[95]: 
matrix([[ 5,  8, 11],
        [ 8, 13, 18],
        [11, 18, 25]]) 

OTHER TIPS

An alternative to messing with cvxopt __init__ is to define your own dot;
A or B can be numpy arrays, or array-like, or anything with .value or .values:

def dot( A, B ):
    """ np.dot .value or .values if they exist """
    for val in "value values" .split():
        A = getattr( A, val, A )  # A.val or A
        B = getattr( B, val, B )
    A = np.asanyarray( A )
    B = np.asanyarray( B )
    try: 
        np.dot( A, B )
    except ValueError:
        print >>sys.stderr, "error: can't dot shapes %s x %s" % (A.shape, B.shape)
        raise

(Bytheway I avoid matrices, stick to numpy arrays and vecs -- a separate issue.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top