Convert numpy array to MLlib Matrix

def to_matrix(np_array):
    """Convert numpy array to MLlib Matrix
    """
    if len(np_array.shape) == 2:
        return Matrices.dense(np_array.shape[0],
                              np_array.shape[1],
                              np_array.ravel())
    else:
        raise Exception("An MLLib Matrix can only be created from a two-dimensional " +
                        "numpy array, got {}".format(len(np_array.shape)))