【问题标题】:broadcast error : optimal transport library广播错误:最佳传输库
【发布时间】:2017-12-05 15:47:19
【问题描述】:

我正在做一个重心聚类。但为什么会出现这个错误?看起来在做矩阵乘法时发生了一些不好的事情。

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)

【问题讨论】:

    标签: python numpy machine-learning


    【解决方案1】:

    该消息是由于尝试将某些内容分配给错误形状的数组而引起的。

    >>> 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)
    

    我对@9​​87654322@ 文档的阅读是,它将返回与其第一个参数匹配的形状数组。这意味着X[classes==c] 的长度大于2。你试过ot.barycenter(X[classes==c][0],M,1e-3) 吗?

    以下是相关文档:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2018-09-22
      • 2017-12-26
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多