有没有类似于numpy.loadtxt的Julia函数?或者说有没有类似功能的其他函数?

3
我正在尝试将我的一些Python程序转换到Julia上,其中一个需要从txt文件中以矩阵的形式读入值,然后使用该矩阵进行乘法等运算。
那么,除了逐行或逐字符迭代外,是否有更好的方法在Julia中从文件输入并加载矩阵?
例如,文本文件可能如下所示:
5 9
10 3

所以我的矩阵就会是这样的。
[[5,9],
 [10,3]]

然后我会用它来乘以其他矩阵等等。

我刚开始学习Julia,所以我仍在尽力查看库和MIT源代码。到目前为止,我的最佳想法(假设没有相当于numpy.loadtxt的东西)是逐行加载到数组中,然后稍后重新整形,但我希望尽可能地高效,并且那似乎是一种缓慢而不干净的导入方式。

2个回答

5
尝试使用Readdlm()。更多详情请查看文章
julia> file="23 12 13 22
       15 61 17 10
       1 0 11 12"

您可以读取并将其转换为数组readdlm(IOBuffer(file)),也可以通过以下方式将数组的元素强制转换为整数 readdlm(IOBuffer(file),int)

julia> readdlm(IOBuffer(file))
3x4 Array{Float64,2}:
 23.0  12.0  13.0  22.0
 15.0  61.0  17.0  10.0
 1.0   0.0   11.0  12.0

0

CSV.read()函数也可以用于读取分隔文件。与readdlm()相比,CSV.read()的优点是对于大文件来说速度更快。例如,要读取以下内容:

julia> file = 
       """
       23 12 13 22
       15 61 17 10
       1 0 11 12
       """

代码将会是:

julia> CSV.read(IOBuffer(file),delim=" ",ignorerepeated=true,header=false)
3×4 DataFrames.DataFrame
│ Row │ Column1 │ Column2 │ Column3 │ Column4 │
│     │ Int64   │ Int64   │ Int64   │ Int64   │
├─────┼─────────┼─────────┼─────────┼─────────┤
│ 1   │ 23      │ 12      │ 13      │ 22      │
│ 2   │ 15      │ 61      │ 17      │ 10      │
│ 3   │ 1       │ 0       │ 11      │ 12      │

对于小文件,readdlm() 更快。

julia> using BenchmarkTools

julia> @btime CSV.read(IOBuffer(file),delim=" ",ignorerepeated=true,header=false);
  68.197 μs (185 allocations: 14.80 KiB)

julia> @btime readdlm(IOBuffer(file));
  2.465 μs (20 allocations: 40.70 KiB)

但对于较大的文件,CSV.read() 更快、更高效。

julia> @btime CSV.read(IOBuffer(file^100000),delim=" ",ignorerepeated=true,header=false);
  32.027 ms (230 allocations: 3.26 MiB)

julia> @btime readdlm(IOBuffer(file^100000));
  142.187 ms (3600025 allocations: 116.44 MiB)

它可以转换为数组,如下所示:

julia> dffile = CSV.read(IOBuffer(file),delim=" ",ignorerepeated=true,header=false);

julia> convert(Matrix, dffile)
3×4 Array{Int64,2}:
 23  12  13  22
 15  61  17  10
  1   0  11  12

许多其他个性化设置可以用于读取不同类型的文件,并在文档中详细说明。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接