python - 计算 numpy memmap Infinity 输出的均值和方差

标签 python numpy memory

创建内存映射数组:

  out = np.memmap('my_array.mmap', dtype=np.float16, mode='w+', shape=(num_axis1, num_axis2))
  for index,row in enumerate(temp_train_data):
        __,cd_i=pywt.dwt(X_train[index:index+1001].ravel(),'haar')
        out[index]=(cd_i)
(Pdb) out.shape
(1421392L, 3504L)

现在,我只是对这个数组进行特征缩放。减去均值并除以方差。

np.mean(out[:,1])
memmap(inf, dtype=float16)

输出是 内存映射(inf, dtype=float16) 我不明白为什么!

可重现的例子:

import numpy as np
ut = np.memmap('my_array.mmap', dtype=np.float16, mode='w+',\
  shape=(140000, 3504))
for index,row in enumerate(ut):
    ut[index]=np.random.rand(1,3504)*10
print np.max(ut[:,1])
print np.mean(ut[:,1],axis=0)

10.0

inf

最佳答案

According to Wikipediafloat16 数据类型无法处理大于 65520 的整数。您集合中所有值的总和可能大于该值,因此在计算平均值时它会四舍五入为无穷大。

考虑使用具有更高上限的数据类型。例如,float32

ut = np.memmap('my_array.mmap', dtype=np.float32, mode='w+',\
  shape=(140000, 3504))

当我进行此更改时,我得到了一些不错的非无限输出:

9.99996471405
4.9927

或者,如果您确实需要一个float16 数组,您可以指定用于计算平均值的类型:

print np.mean(ut[:,1],axis=0,dtype=np.float32)

关于python - 计算 numpy memmap Infinity 输出的均值和方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227847/

相关文章:

python - 按行填充 numpy 数组

python - django aws s3图像在上传时调整大小并访问各种调整大小的图像

python - ElementTree中fromstring函数的上限

numpy - 如何让 NumPy 在 Ubuntu 中使用 OpenBlas?

python - 为什么 np.argwhere 的结果形状与其输入不匹配?

c - 访问冲突 strcpy

c++ - malloc 的不常见使用

python - bool 值的大列表和小列表以及字节数组的相对性能

python - 在 Windows 上为 Numpy 和 friend 使用哪个 Python?

python - 〜./zshrc : command not found: $