2017-12-05 12 views
0

私は重心クラスタリングをやっています。なぜこのエラー?行列の乗算を行うときに何か悪いことが起きたように見えます。放送のエラー:最適な転送ライブラリ

import numpy as np 
import pandas as pd 
import ot 

def initialize_clusters(points, k): 
    return points[np.random.randint(points.shape[0], size=k)] 

def get_distances(centroid, points): 
    return np.linalg.norm(points - centroid, axis=1) 

if __name__ == "__main__": 
    X = pd.read_csv('./csv/inst_clust2.csv',encoding='utf-8')[['lat','lng']] 
    M = ot.dist(X,metric='euclidean') 
    X = X.as_matrix() 
    k = 3 
    maxiter = 50 
    centroids = initialize_clusters(X,k) 
    classes = np.zeros(X.shape[0],dtype=np.float64) 
    distances = np.zeros([X.shape[0],k],dtype=np.float64) 
    for i in range(maxiter): 
     for i, c in enumerate(centroids): 
      distances[:,i] = get_distances(c, X) 
     classes = np.argmin(distances,axis=1) 
     for c in range(k): 
      print(X) 
      print(X[classes==c]) 
      centroids[c]=ot.barycenter(X[classes==c],M,1e-3) 

は、メッセージが間違った形の配列に何かを代入しようとする試みによって引き起こされるエラー

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ot/bregman.py:801: RuntimeWarning: invalid value encountered in log 
    return np.exp(np.mean(np.log(alldistribT), axis=1)) 
Traceback (most recent call last): 
    File "/Users/Chu/Documents/dssg2018/bc.py", line 25, in <module> 
    centroids[c]=ot.bregman.barycenter(X[classes==c],M,1e-3) 
ValueError: could not broadcast input array from shape (605) into shape (2) 

答えて

0

を与えます。

>>> f = np.zeros((2,2)) 
>>> f[0] = np.zeros((605,)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: could not broadcast input array from shape (605) into shape (2) 

ot.barycenterのドキュメントを読むと、最初のパラメータと一致する形状の配列が返されます。それはX[classes==c]あなたはot.barycenter(X[classes==c][0],M,1e-3)を試してみました以上の2の長さを有することを意味しますか?ここで

は、関連するドキュメントです:

barycenter(A, M, reg, weights=None, numItermax=1000, stopThr=0.0001, verbose=False, log=False) 
    Compute the entropic regularized wasserstein barycenter of distributions A 

    ... snip ... 

    Parameters 
    ---------- 
    A : np.ndarray (d,n) 
     n training distributions of size d 

    ... snip ... 

    Returns 
    ------- 
    a : (d,) ndarray