1. np.bincount(x, weights=None, minlength=None)


解释的很清楚。这里我简单解释一下

作用:求array中值得个数。适用于求分割prediction中各类的像素数。比如三分类,那么在prediction中的值就为0,1,2。

该函数可以求出0有多少像素,1有多少像素,2有多少像素。

示例:

import numpy as np
seg = np.ones((512, 512, 512))
seg[:50, :50, 6] = 0
seg[69: 87, 90:200, 112: 234] = 2

np.unique(seg) #  >>>  array([0., 1., 2.])

seg.dtype
Out[11]: dtype('float64')

flatten = seg.flatten()
bincount = np.bincount(flatten)  # 用一维来做
TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'

# np.bincount 不接受float类型
flatten = seg.flatten().astype('int64')
bincount = np.bincount(flatten)
Out[14]: array([     2500, 133973668,    241560])