How to save an NxNxN array (or Matrix) into a file in Julia (or Python)?

丶灬走出姿态 提交于 2020-01-01 08:34:47

问题


I'm working on a Jupyter notebook and currently using Julia

I'm trying to save a 3x3x3 Array into a textfile so when I include it in another notebook, the array is a 3x3x3 Array too.

Any suggestions? Thanks in advance.


回答1:


You could use the JLD.jl (Julia Data) package:

Pkg.add("JLD")
using JLD
r = rand(3, 3, 3)
save("data.jld", "data", r)
load("data.jld")["data"]

The advantage of the JLD package is that it preserves the exact type information of each variable.




回答2:


Okay I admit that I am a python lover, though Julia is starting to grow on me. So as an old python user there is a Julia package that can convert arrays into numpy npz files and then read them as well. Example:

    x = reshape(1:27, 3,3,3)
    Pkg.add("NPZ")
    using NPZ
    npzwrite("TEST.npz",x)

And now I can later load this file (so long as I am using the NPZ package):

    y = npzread("TEST.npz")


来源:https://stackoverflow.com/questions/30926050/how-to-save-an-nxnxn-array-or-matrix-into-a-file-in-julia-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!