17

いわゆる「楕円体を囲む最小体積」を見つけるためのMATLABコードが存在します(たとえば、ここここでも)。便宜上、関連する部分を貼り付けます。

function [A , c] = MinVolEllipse(P, tolerance)
[d N] = size(P);

Q = zeros(d+1,N);
Q(1:d,:) = P(1:d,1:N);
Q(d+1,:) = ones(1,N);


count = 1;
err = 1;
u = (1/N) * ones(N,1);


while err > tolerance,
    X = Q * diag(u) * Q';
    M = diag(Q' * inv(X) * Q);
    [maximum j] = max(M);
    step_size = (maximum - d -1)/((d+1)*(maximum-1));
    new_u = (1 - step_size)*u ;
    new_u(j) = new_u(j) + step_size;
    count = count + 1;
    err = norm(new_u - u);
    u = new_u;
end

U = diag(u);
A = (1/d) * inv(P * U * P' - (P * u)*(P*u)' );
c = P * u;

以下にMATLABテストコードを示します。

points  = [[ 0.53135758, -0.25818091, -0.32382715], 
    [ 0.58368177, -0.3286576,  -0.23854156,], 
    [ 0.18741533,  0.03066228, -0.94294771], 
    [ 0.65685862, -0.09220681, -0.60347573],
    [ 0.63137604, -0.22978685, -0.27479238],
    [ 0.59683195, -0.15111101, -0.40536606],
    [ 0.68646128,  0.0046802,  -0.68407367],
    [ 0.62311759,  0.0101013,  -0.75863324]];

[A centroid] = minVolEllipse(points',0.001);
A
[~, D, V] = svd(A);

rx = 1/sqrt(D(1,1));
ry = 1/sqrt(D(2,2));
rz = 1/sqrt(D(3,3));

[u v] = meshgrid(linspace(0,2*pi,20),linspace(-pi/2,pi/2,10));

x = rx*cos(u').*cos(v');
y = ry*sin(u').*cos(v');
z = rz*sin(v');

for idx = 1:20,
    for idy = 1:10,
        point = [x(idx,idy) y(idx,idy) z(idx,idy)]';
        P = V * point;
        x(idx,idy) = P(1)+centroid(1);
        y(idx,idy) = P(2)+centroid(2);
        z(idx,idy) = P(3)+centroid(3);
    end
end

figure
plot3(points(:,1),points(:,2),points(:,3),'.');
hold on;
mesh(x,y,z);
axis square;
alpha 0;

これにより、共分散行列が生成されます。

A =
  47.3693 -116.0758  -79.1861
-116.0758  458.0874  280.0656
 -79.1861  280.0656  179.3886

MATLAB楕円体

さて、これがこのコードをPython(2.7)に移植する私の試みです:

from __future__ import division
import numpy as np
import numpy.linalg as la

def mvee(points,tol=0.001):
    N, d = points.shape

    Q = np.zeros([N,d+1])
    Q[:,0:d] = points[0:N,0:d]  
    Q[:,d] = np.ones([1,N])

    Q = np.transpose(Q)
    points = np.transpose(points)
    count = 1
    err = 1
    u = (1/N) * np.ones(shape = (N,))

    while err > tol:

        X = np.dot(np.dot(Q, np.diag(u)), np.transpose(Q))
        M = np.diag( np.dot(np.dot(np.transpose(Q), la.inv(X)),Q)) 
        jdx = np.argmax(M)
        step_size = (M[jdx] - d - 1)/((d+1)*(M[jdx] - 1))
        new_u = (1 - step_size)*u 
        new_u[jdx] = new_u[jdx] + step_size
        count = count + 1
        err = la.norm(new_u - u)       
        u = new_u

    U = np.diag(u)    
    c = np.dot(points,u)
    A = (1/d) * la.inv(np.dot(np.dot(points,U), np.transpose(points)) - np.dot(c,np.transpose(c)) )    
    return A, np.transpose(c)

対応するテストコード:

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay

#some random points
points = np.array([[ 0.53135758, -0.25818091, -0.32382715], 
[ 0.58368177, -0.3286576,  -0.23854156,], 
[ 0.18741533,  0.03066228, -0.94294771], 
[ 0.65685862, -0.09220681, -0.60347573],
[ 0.63137604, -0.22978685, -0.27479238],
[ 0.59683195, -0.15111101, -0.40536606],
[ 0.68646128,  0.0046802,  -0.68407367],
[ 0.62311759,  0.0101013,  -0.75863324]])

# compute mvee
A, centroid = mvee(points)
print A

# point it and some other stuff
U, D, V = la.svd(A)    

rx, ry, rz = [1/np.sqrt(d) for d in D]
u, v = np.mgrid[0:2*np.pi:20j,-np.pi/2:np.pi/2:10j]    

x=rx*np.cos(u)*np.cos(v)
y=ry*np.sin(u)*np.cos(v)
z=rz*np.sin(v)

for idx in xrange(x.shape[0]):
    for idy in xrange(y.shape[1]):
        x[idx,idy],y[idx,idy],z[idx,idy] = np.dot(np.transpose(V),np.array([x[idx,idy],y[idx,idy],z[idx,idy]])) + centroid


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:,0],points[:,1],points[:,2])    
ax.plot_surface(x, y, z, cstride = 1, rstride = 1, alpha = 0.1)
plt.show()

これを生成します:

[[ 0.84650504 -1.40006147  0.39857055]
[-1.40006147  2.60678264 -1.52583781]
[ 0.39857055 -1.52583781  1.04581752]]

ここに画像の説明を入力してください

明らかに異なります。何が得られますか?

4

1 に答える 1

15

Octaveを使用して、MinVolEllipseのwhileループが終了した後、

u =

   0.0053531
   0.2384227
   0.2476188
   0.0367063
   0.0257947
   0.2124423
   0.0838103
   0.1498518

uこれは、Python関数によって検出された結果と一致しますmvee。オクターブ側のより多くのデバッグ印刷ステートメントが生成されます

(P*u) = 

   0.50651
  -0.11166
  -0.57847

(P*u)*(P*u)' =

   0.256555  -0.056556  -0.293002
  -0.056556   0.012467   0.064590
  -0.293002   0.064590   0.334628

しかし、Python側では、

c = np.dot(points.T,u)
print(c)

収量

[ 0.50651212 -0.11165724 -0.57847018]

print(np.dot(c,np.transpose(c)))

収量

0.60364961984    # <-- This should equal (P*u)*(P*u)', a 3x3 matrix.

問題がわかれば、解決策は簡単です。(P*u)*(P*u)'次のように計算できます:

np.multiply.outer(c,c)

import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

pi = np.pi
sin = np.sin
cos = np.cos

def mvee(points, tol = 0.001):
    """
    Finds the ellipse equation in "center form"
    (x-c).T * A * (x-c) = 1
    """
    N, d = points.shape
    Q = np.column_stack((points, np.ones(N))).T
    err = tol+1.0
    u = np.ones(N)/N
    while err > tol:
        # assert u.sum() == 1 # invariant
        X = np.dot(np.dot(Q, np.diag(u)), Q.T)
        M = np.diag(np.dot(np.dot(Q.T, la.inv(X)), Q))
        jdx = np.argmax(M)
        step_size = (M[jdx]-d-1.0)/((d+1)*(M[jdx]-1.0))
        new_u = (1-step_size)*u
        new_u[jdx] += step_size
        err = la.norm(new_u-u)
        u = new_u
    c = np.dot(u,points)        
    A = la.inv(np.dot(np.dot(points.T, np.diag(u)), points)
               - np.multiply.outer(c,c))/d
    return A, c

#some random points
points = np.array([[ 0.53135758, -0.25818091, -0.32382715], 
                   [ 0.58368177, -0.3286576,  -0.23854156,], 
                   [ 0.18741533,  0.03066228, -0.94294771], 
                   [ 0.65685862, -0.09220681, -0.60347573],
                   [ 0.63137604, -0.22978685, -0.27479238],
                   [ 0.59683195, -0.15111101, -0.40536606],
                   [ 0.68646128,  0.0046802,  -0.68407367],
                   [ 0.62311759,  0.0101013,  -0.75863324]])

# Singular matrix error!
# points = np.eye(3)

A, centroid = mvee(points)    
U, D, V = la.svd(A)    
rx, ry, rz = 1./np.sqrt(D)
u, v = np.mgrid[0:2*pi:20j, -pi/2:pi/2:10j]

def ellipse(u,v):
    x = rx*cos(u)*cos(v)
    y = ry*sin(u)*cos(v)
    z = rz*sin(v)
    return x,y,z

E = np.dstack(ellipse(u,v))
E = np.dot(E,V) + centroid
x, y, z = np.rollaxis(E, axis = -1)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cstride = 1, rstride = 1, alpha = 0.05)
ax.scatter(points[:,0],points[:,1],points[:,2])

plt.show()

ここに画像の説明を入力してください


ちなみに、この計算では多くの行列乗算を使用しますが、使用するnp.dotとかなり冗長に見えます。NumPy配列をNumPy行列に変換すると、行列の乗算は。で表すことができます*。例えば、

A = la.inv(np.dot(np.dot(points.T, np.diag(u)), points)
           - np.dot(c[:, np.newaxis], c[np.newaxis, :]))/d

になります

A = la.inv(points.T*np.diag(u)*points - c.T*c)/d    

読みやすさが重要なので、NumPy行列を使用してメインの計算を実行することをお勧めします。

def mvee(points, tol = 0.001):
    """
    Find the minimum volume ellipse.
    Return A, c where the equation for the ellipse given in "center form" is
    (x-c).T * A * (x-c) = 1
    """
    points = np.asmatrix(points)
    N, d = points.shape
    Q = np.column_stack((points, np.ones(N))).T
    err = tol+1.0
    u = np.ones(N)/N
    while err > tol:
        # assert u.sum() == 1 # invariant
        X = Q * np.diag(u) * Q.T
        M = np.diag(Q.T * la.inv(X) * Q)
        jdx = np.argmax(M)
        step_size = (M[jdx]-d-1.0)/((d+1)*(M[jdx]-1.0))
        new_u = (1-step_size)*u
        new_u[jdx] += step_size
        err = la.norm(new_u-u)
        u = new_u
    c = u*points
    A = la.inv(points.T*np.diag(u)*points - c.T*c)/d    
    return np.asarray(A), np.squeeze(np.asarray(c))
于 2012-12-24T19:42:40.040 に答える