Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 意外的numpy.unique行为_Python_Numpy_Unique - Fatal编程技术网

Python 意外的numpy.unique行为

Python 意外的numpy.unique行为,python,numpy,unique,Python,Numpy,Unique,我正在使用numpy.unique获取已使用numpy.ravel展平的蒙版数组的值、索引和计数,并获得意外结果 如果我按照以下步骤手动测试它,它将正常工作: >>> import numpy as np >>> a = np.array([[1,2,3],[1,0,0],[2,1,5]]) >>> a array([[1, 2, 3], [1, 0, 0], [2, 1, 5]]) >>> sr

我正在使用numpy.unique获取已使用numpy.ravel展平的蒙版数组的值、索引和计数,并获得意外结果

如果我按照以下步骤手动测试它,它将正常工作:

>>> import numpy as np
>>> a = np.array([[1,2,3],[1,0,0],[2,1,5]])
>>> a
array([[1, 2, 3],
       [1, 0, 0],
       [2, 1, 5]])
>>> src = np.ma.masked_equal(a, 0)
>>> src
masked_array(data =
 [[1 2 3]
 [1 -- --]
 [2 1 5]],
             mask =
 [[False False False]
 [False  True  True]
 [False False False]],
       fill_value = 0)

>>> src = src.ravel()
>>> src
masked_array(data = [1 2 3 1 -- -- 2 1 5],
             mask = [False False False False  True  True False False False],
       fill_value = 0)

>>> s_values, s_idx, s_counts = np.unique(src, return_inverse=True, return_counts=True)

>>> s_values
masked_array(data = [1 2 3 5 --],
             mask = [False False False False  True],
       fill_value = 0)
>>> s_counts
array([3, 2, 1, 1, 2])
但是,当我从图像文件将相同的逻辑应用于uint8阵列时,我得到以下结果:

>>> src_ds = '/Users/histo/S2_10_T_DN_2016_7_27_0_4328_repro.tif'
>>> src_ds = gdal.Open(src_ds)
>>> src = src_ds.GetRasterBand(1).ReadAsArray()
>>> src = np.ma.masked_equal(src, 0)
>>> src = src.ravel()
>>> s_values, s_idx, s_counts = np.unique(src, return_index=True, return_inverse=True)
>>> s_values
masked_array(data = [3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 55 56 57 58 59 60 61 62 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 --
 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63
 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 -- 63 64 65 66
 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255],
             mask = [False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
 False  True False  True False  True False  True False  True False  True
...
由于某些原因,63和mask作为唯一值一次又一次地重复,这当然会抛出计数和索引,并使结果无法用于任何后续分析

我曾尝试用ndimage打开数据,结果与其他图像相同。再次,我得到重复值掩码值。。。作为独特的价值观

很奇怪。有人见过这个吗?

TL;博士 通过更改遮罩阵列的默认填充值来解决此问题:

import numpy as np

x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8')
y = np.ma.masked_equal(x, 0)
v, i, c = np.unique(y, return_index=True, return_counts=True)
print(v)
# [1 2 3 -- 63 -- 63 -- 64]

np.ma.core.default_filler['u'] = 0  # fill value for unsigned integers
np.ma.core.default_filler['i'] = 0  # fill value for unsigned integers

v, i, c = np.unique(y, return_index=True, return_counts=True)
print(v)
# [-- 1 2 3 63 64]

细节 此问题是可再现的,并不特别适用于图像(请注意,仅当
return\u index=True
时才会发生此问题):

显然,这与数字63有关。63有什么特别之处?二进制全1(在8位表示的情况下有两个前导零)

但是,我不知道为什么这个特定的数字(例如127个作品)会在
np.unique
中与掩码组合时导致奇怪的行为

这与数组的排序有关。没有
return\u索引
np.unique内部使用
np.sort
,否则使用
np.argsort

x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8')
y = np.ma.masked_equal(x, 0)
print(np.sort(y))
# [1 1 2 2 3 63 63 63 64 -- -- -- -- -- --]
print(y[np.argsort(y)])
# [1 1 2 2 3 -- 63 63 -- -- -- -- 63 -- 64]
默认情况下,在排序期间使用该函数替换屏蔽值。
int
(也用于
uint8
)的默认填充值为999999。这是二进制0b11110100001000111111-最低8位等于63!因此,对于排序算法,掩码值和63是等效的,它们混杂在一起

可以更改默认填充值,从而解决问题:

np.ma.core.default_filler['u'] = 0  # fill value for unsigned integers
np.ma.core.default_filler['i'] = 0  # fill value for unsigned integers

作为一种解决方法,您可以将数据转换为另一种数据类型,例如16位浮点整数:

x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8')
y = np.ma.masked_equal(x.astype('int16'), 9)
v, i, c = np.unique(y, return_index=True, return_counts=True)
print(v)
# [1 2 3 63 64 --]

对于特定用例的另一种选择是根本不使用屏蔽数组。由于只有0被掩码替换,因此很容易忽略结果中的0。

从这里开始很难。什么是类型(src)?>>>src.dtype-dtype('uint8')原因是
np.int8(999999)==63
供参考:哇。谢谢@kazemakase找到这个。魔法63。我会尝试你建议的解决办法。@JustinHX我找到了问题的根源。如果您想继续使用
uint8
数据类型,请查看更新的答案。谢谢。我将测试uint8解决方案。仅供参考,该文件已提交给SciPy,并已标记为错误。
np.ma.core.default_filler['u'] = 0  # fill value for unsigned integers
np.ma.core.default_filler['i'] = 0  # fill value for unsigned integers
x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8')
y = np.ma.masked_equal(x.astype('int16'), 9)
v, i, c = np.unique(y, return_index=True, return_counts=True)
print(v)
# [1 2 3 63 64 --]