小编She*_*don的帖子

将 numpy 数组写入文本文件的速度

我需要将一个非常“高”的两列数组写入文本文件,而且速度非常慢。我发现如果我将数组重塑为更宽的数组,写入速度会快得多。例如

import time
import numpy as np
dataMat1 = np.random.rand(1000,1000)
dataMat2 = np.random.rand(2,500000)
dataMat3 = np.random.rand(500000,2)
start = time.perf_counter()
with open('test1.txt','w') as f:
    np.savetxt(f,dataMat1,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)

start = time.perf_counter()
with open('test2.txt','w') as f:
    np.savetxt(f,dataMat2,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)

start = time.perf_counter()
with open('test3.txt','w') as f:
    np.savetxt(f,dataMat3,fmt='%g',delimiter=' ')
end = time.perf_counter()
print(end-start)
Run Code Online (Sandbox Code Playgroud)

在三个数据矩阵中元素数量相同的情况下,为什么最后一个比其他两个更耗时?有没有办法加快写入“高”数据数组的速度?

python performance numpy

6
推荐指数
1
解决办法
2087
查看次数

如何使用 ESRI ASCII 栅格格式文件(如 Matlab 中的 mapshow)在 Python 中绘制栅格地图?

例如,我有一个 ESRI ASCII Raster 格式的 DEM 文件,如下所示:

ncols 480
nrows 450
xllcorner 378923
yllcorner 4072345
cellsize 30
nodata_value -32768
43 2 45 7 3 56 2 5 23 65 34 6 32 54 57 34 2 2 54 6 
35 45 65 34 2 6 78 4 2 6 89 3 2 7 45 23 5 8 4 1 62 ...
Run Code Online (Sandbox Code Playgroud)

我想绘制一个栅格图来显示地形。我知道它可以通过Matlab中的mapshow来实现

[Z,R] = arcgridread(filename);
mapshow(Z,R,'DisplayType','Surface')
Run Code Online (Sandbox Code Playgroud)

但如何在Python中做到这一点呢?如果坐标系是英国国家网格,是否可以在Python中的栅格地图上添加shapefile图层(例如多边形文件)?

python

5
推荐指数
1
解决办法
1859
查看次数

标签 统计

python ×2

numpy ×1

performance ×1