- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Python 以二维数组的形式生成一些初始数据,例如“X”,然后使用 Fortran 对它们进行一些计算。最初,当数组大小约为 10,000 x 10,000 时,np.savetxt 在速度方面运行良好。但是一旦我开始增加数组的维度,savetxt 的速度就会显着降低。所以我尝试了 np.save ,这导致保存速度更快,但文件以 .npy 格式保存。我将如何在 Fortran 中读取这样的文件以重建原始数组?根据我的阅读,二进制通常会导致最低的空间消耗和最快的速度。
在 Fortran 90 中,
open(10,file='/home/X.npy')
read(10,*)X
我收到以下错误:Fortran 运行时错误:列表输入的第 1 项中的实数错误
编辑:在 python 中我这样做,
import numpy as np
x = np.linspace(-50,50,10)
y = np.linspace(-50,50,10)
X,Y = np.meshgrid(x,y)
np.save('X',X)
然后在 Fortran 中我这样做,
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'X.npy'
integer, parameter :: N = 10
real(DPR), allocatable, dimension(:, :) :: X
allocate(X(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) X
close(40)
write(*,*) X
end program read_unformatted_binary_from_python
输出以 1.8758506894003703E-309 1.1711999948023422E+171 5.2274167985502976E-037 8.4474009688929314E+252 2.651412321034566210E+180E+252 2.651412321034566210E+180 73E+247 2.1620996769994603E+233 7.5805790251297605E-096 3.4756671925988047E-152 6.5549091408576423E-260 -50.000000000000000 - 38.888888888888886 -27.777777777777779 等等
使用 .bin 格式时不会发生此错误,仅当使用 np.save 时会导致 npy。
最佳答案
Vladimir F 是正确的,您希望在 Fortran 中对“原始二进制”文件进行“流式”访问。这是一个 MWE:
python
import numpy as np
A = np.random.rand(10000, 10000)
print(A.sum())
A.tofile('data.bin')
中文
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'data.bin'
integer, parameter :: N = 10000
real(DPR), allocatable, dimension(:, :) :: dat
allocate(dat(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) dat
close(40)
write(*,*) sum(dat)
end program read_unformatted_binary_from_python
我的 Fortran 示例可能比必要的要长一些,因为我使用许多不同的系统和编译器套件,并且也不喜欢大型静态数组(毕竟我是 Fortran 用户)。
我在 MacBook Pro 上使用 Python 2.7.x、Numpy 13.x 和来自 Homebrew GCC 6.3.0_1 的 gfortran 快速编写了代码,但这应该适用于所有系统。
更新:这里需要特别注意阵列的形状和大小。如果分配的 dat
大于文件中的内容,则流式 read
应该尝试填充整个数组,点击 EOF
符号,并发出错误。在 Python 中,np.fromfile()
方法将读取到 EOF
然后返回具有适当长度的一维数组,即使 A
是最初是多维的。这是因为原始二进制文件没有元数据,只是来自 RAM 的连续字节串。
因此,Python 中的以下行生成相同的文件:
A = np.random.rand(10000, 10000)
A.tofile('file.whatever')
A.ravel().tofile('file.whatever')
A.reshape((100, 1000, 1000)).tofile('file.whatever')
并且该文件可以读入并 reshape 为:
B = np.fromfile('file.whatever').reshape(A.shape)
B = np.fromfile('file.whatever').reshape((100, 1000, 100, 10))
# or something like
B = np.fromfile('file.whatever') # just a 1D array
B.resize(A.shape) # resized in-place
在 Fortran 中,使用流访问很容易读取整个原始文件,而无需提前知道其大小,尽管您显然需要某种用户输入来 reshape 数据:
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'data.bin'
integer :: N = 10000, bytes, reals, M
real(DPR), allocatable :: A(:,:), D(:, :), zeros(:)
real(DPR), allocatable, target :: B(:)
real(DPR), pointer :: C(:, :)
allocate(A(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) A
write(*,*) 'sum of A', sum(A)
inquire(unit=40, size=bytes)
reals = bytes/8
allocate(B(reals))
read(40, pos=1) B
write(*,*) 'sum of B', sum(B)
! now reshape B in-place assuming the user wants the first dimension
! (which would be the outer dimension in Python) to be length 100
N = 100
if(mod(reals, N) == 0) then
M = reals/N
call C_F_POINTER (C_LOC(B), C, [N, M])
write(*, *) 'sum of C', sum(C)
write(*, *) 'shape of C', shape(C)
else
write(*,*) 'file size is not divisible by N!, did not point C to B'
end if
! now reshape B out-of-place with first dimension length 9900, and
! pad the result so that there is no size mismatch error
N = 9900
M = reals/N
if(mod(reals, N) > 0) M=M+1
allocate(D(N, M))
allocate(zeros(N), source=real(0.0, DPR))
D = reshape(B, [N, M], pad=zeros)
write(*,*) 'sum of D', sum(D)
write(*,*) 'shape of D', shape(D)
! obviously you can also work with shape(A) in fortran the same way you
! would use A.shape() in Python, if you already knew that A was the
! correct shape of the data
deallocate(D)
allocate(D, mold=A)
D = reshape(B, shape(A))
write(*,*) 'sum of D', sum(D)
write(*,*) 'shape of D', shape(D)
! or, just directly read it in, skipping the whole reshape B part
read(40, pos=1) D
write(*,*) 'sum of D', sum(D)
close(40)
end program read_unformatted_binary_from_python
关于python - 从 .npy 文件中读取数组到 Fortran 90,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46318178/
我是处理大型数据集的新手。我想用我的训练数据生成一个 .npy 文件。我有大约 1.7GB 的 PNG 图像要加载,但是由于内存错误,我将这个设置分成了 block ,我现在试图将大约 389.5 M
如何在 python 中打开 .NPY 文件以便读取它们?我一直在尝试运行我找到的一些代码,但它以 .NPY 文件输出,所以我不知道它是否工作。 最佳答案 *.npy 文件是存储 numpy 数组的二
我有大约 2000 个 .npy 文件,每个文件代表一个一维 float 向量,其中包含 100,000 到 1,000,000 个条目(这两个数字将来都会大幅增长)。对于每个文件,我想要它包含的向量
我有一个 dict,并且我将一个数组与每个键相关联(键本身是一个数字)。 小例子: import numpy as np data = {} data[2.5] = np.array([np.arra
我一直在尝试输出npy文件的内容,当print(np.load('/home/ugwz/fcn/vgg16.npy', encoding='latin1')),部分输出如下所示,有点难以阅读。 然后我
是否存在任何 *.npy 文件阅读器/可视化器? (适用于 Windows) 最佳答案 有科学python调用的IDE spyder .它能够将 .npy 文件中的所有数据导入工作区。工作区中的所有变
我有一个 .npy 文件,我基本上知道其中的所有内容(大小、元素数量、元素类型等),我希望有一种方法可以在不加载数组的情况下检索特定值。目标是使用尽可能少的内存。 我正在寻找类似的东西 def ext
如何查看以 .npy 扩展名存储的图像并以该格式保存我自己的文件? 最佳答案 .npy 是 numpy 数组的文件扩展名 - 您可以使用 numpy.load 读取它们: import numpy a
我正在尝试加载我之前保存的稀疏数组。保存稀疏数组很容易。尝试阅读它虽然很痛苦。 scipy.load 在我的稀疏数组周围返回一个 0d 数组。 import scipy as sp A = sp.lo
我正在使用 numpy.save 函数保存 NumPy 数组。我希望其他开发人员能够使用 C 语言从这些文件中读取数据。所以我需要知道,numpy 如何在文件中组织二进制数据。好的,当我保存“i4”数
Numpy能够读写磁盘上的文本数据或二进制数据。 将数组以二进制格式保存到磁盘 np.load和np.save是读写磁盘数组数据的两个主要函数,默认情况下,数组是以未压缩的原始二进制格式保存在扩
Tensorflow似乎缺少“ .npy”文件的阅读器。 如何将我的数据文件读入新的tensorflow.data.Dataset点线中? 我的数据不适合存储在内存中。 每个对象都保存在单独的“ .n
我想知道是否有任何方法可以将数组作为 numpy 文件(.npy)直接写入 AWS S3 存储桶。我可以用 np.save将文件保存在本地,如下所示。但我正在寻找一种直接将其写入 S3 的解决方案,而
我正在尝试使用 np.save() 将数据帧和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误: File "/Users/sofiafarina/opt/ana
我正在开发一个 SMS 网关,我在考虑我应该在其中实现的功能。因此,我查看了一些 SMS 网关(SMSNOW、SMS studio 和 Ozeki)作为示例。 我几乎完成了实现,但我遇到了几乎所有 S
在我的启动时运行的主类中,它尝试将一些数据放入 HashMap 中。但它说 HashMap 为 null,并且无法添加数据。 public class COD extends JavaPlugin{
我正在尝试创建一个函数,它将采用 numpy dstr 名称作为参数,并绘制该分布中的随机数据点的直方图。 如果它仅适用于需要 1 个参数的 npy 发行版,那就没问题。只是真的坚持尝试创建 np.r
我使用 python 创建了一个平均图像文件并将其保存到 numpy 文件中。我想知道如何将此 .npy 文件转换为 .binaryproto 文件。我正在使用此文件使用 GoogLeNet 进行训练
是否可以在不先在内存中分配相应数组的情况下创建 .npy 文件? 我需要创建和使用一个大型 numpy 数组,该数组太大而无法在内存中创建。 Numpy 支持内存映射,但据我所知,我的选择是: 使用
Numpy提供了几种数据保存的方法。 以3*4数组a为例: 1. a.tofile("filename.bin") 这种方法只能保存为二进制文件,且不能保存当前数据的行列信
我是一名优秀的程序员,十分优秀!