与 Matlab 相比,Numpy 加载 csv 太慢 [英] Numpy loading csv TOO slow compared to Matlab
问题描述
我发布这个问题是因为我想知道我是否做了什么非常错误的事情才能得到这个结果.
我有一个中等大小的 csv 文件,我尝试使用 numpy 加载它.为了说明,我使用 python 制作了文件:
导入时间将 numpy 导入为 npmy_data = np.random.rand(1500000, 3)*10np.savetxt('./test.csv', my_data, delimiter=',', fmt='%.2f')
然后,我尝试了两种方法:numpy.genfromtxt、numpy.loadtxt
setup_stmt = 'import numpy as np'stmt1 = """my_data = np.genfromtxt('./test.csv', delimiter=',')"""stmt2 = """my_data = np.loadtxt('./test.csv', delimiter=',')"""t1 = timeit.timeit(stmt=stmt1, setup=setup_stmt, number=3)t2 = timeit.timeit(stmt=stmt2, setup=setup_stmt, number=3)
结果显示t1 = 32.159652940464184, t2 = 52.00093725634724.
但是,当我尝试使用 matlab 时:
tic对于 i = 1:3my_data = dlmread('./test.csv');结尾目录
结果显示:经过的时间是3.196465秒.
我知道加载速度可能会有一些差异,但是:
- 这比我预期的要多得多;
- 是不是 np.loadtxt 应该比 np.genfromtxt 更快?
- 我还没有尝试过 python csv 模块,因为加载 csv 文件是我经常做的事情,而且使用 csv 模块,编码有点冗长......但如果是这样的话,我很乐意尝试唯一的办法.目前我更关心是不是我做错了什么.
任何输入将不胜感激.非常感谢提前!
是的,将 csv
文件读入 numpy
非常慢.沿着代码路径有很多纯 Python.这些天,即使我使用纯 numpy
我仍然使用 pandas
进行 IO:
或者,在这样一个足够简单的情况下,您可以使用类似 Joe Kington 写的内容此处:
<预><代码>>>>%time data = iter_loadtxt("test.csv")CPU 时间:用户 2.84 秒,系统:24 毫秒,总计:2.86 秒挂壁时间:2.86 秒还有 Warren Weckesser 的 textreader 库,以防 pandas
太重一个依赖:
I posted this question because I was wondering whether I did something terribly wrong to get this result.
I have a medium-size csv file and I tried to use numpy to load it. For illustration, I made the file using python:
import timeit
import numpy as np
my_data = np.random.rand(1500000, 3)*10
np.savetxt('./test.csv', my_data, delimiter=',', fmt='%.2f')
And then, I tried two methods: numpy.genfromtxt, numpy.loadtxt
setup_stmt = 'import numpy as np'
stmt1 = """
my_data = np.genfromtxt('./test.csv', delimiter=',')
"""
stmt2 = """
my_data = np.loadtxt('./test.csv', delimiter=',')
"""
t1 = timeit.timeit(stmt=stmt1, setup=setup_stmt, number=3)
t2 = timeit.timeit(stmt=stmt2, setup=setup_stmt, number=3)
And the result shows that t1 = 32.159652940464184, t2 = 52.00093725634724.
However, When I tried using matlab:
tic
for i = 1:3
my_data = dlmread('./test.csv');
end
toc
The result shows: Elapsed time is 3.196465 seconds.
I understand that there may be some differences in the loading speed, but:
- This is much more than I expected;
- Isn't it that np.loadtxt should be faster than np.genfromtxt?
- I haven't tried python csv module yet because loading csv file is a really frequent thing I do and with the csv module, the coding is a little bit verbose... But I'd be happy to try it if that's the only way. Currently I am more concerned about whether it's me doing something wrong.
Any input would be appreciated. Thanks a lot in advance!
Yeah, reading csv
files into numpy
is pretty slow. There's a lot of pure Python along the code path. These days, even when I'm using pure numpy
I still use pandas
for IO:
>>> import numpy as np, pandas as pd
>>> %time d = np.genfromtxt("./test.csv", delimiter=",")
CPU times: user 14.5 s, sys: 396 ms, total: 14.9 s
Wall time: 14.9 s
>>> %time d = np.loadtxt("./test.csv", delimiter=",")
CPU times: user 25.7 s, sys: 28 ms, total: 25.8 s
Wall time: 25.8 s
>>> %time d = pd.read_csv("./test.csv", delimiter=",").values
CPU times: user 740 ms, sys: 36 ms, total: 776 ms
Wall time: 780 ms
Alternatively, in a simple enough case like this one, you could use something like what Joe Kington wrote here:
>>> %time data = iter_loadtxt("test.csv")
CPU times: user 2.84 s, sys: 24 ms, total: 2.86 s
Wall time: 2.86 s
There's also Warren Weckesser's textreader library, in case pandas
is too heavy a dependency:
>>> import textreader
>>> %time d = textreader.readrows("test.csv", float, ",")
readrows: numrows = 1500000
CPU times: user 1.3 s, sys: 40 ms, total: 1.34 s
Wall time: 1.34 s
这篇关于与 Matlab 相比,Numpy 加载 csv 太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!