NpyArray
#struct NpyArrayOne multi dimensional NumPy array. The data is always held in memory as 64 bit floats.
| Name | Type | Description |
|---|---|---|
shape | Vector<int> | The size of each dimension, such as [2, 3]. |
dtype | int | The data type in the file; one of the NPY_ constants. |
data | Vector<f64> | The elements in row order. |
import npy
import matrix
func main throw:
mut m := matrix.Matrix { rows = 2, cols = 3 }
repeat 6 in i:
m.a.Push(i as f64)
end
mut a := npy.FromMatrix(m)
println a.shape, a.Ndim(), a.Count(), a.dtype == npy.NPY_F64
bytes := npy.WriteNpy(a)
println bytes.len > 64
mut back := try npy.ReadNpy(bytes.data, bytes.len)
println back.shape, back.data
m2 := try npy.ToMatrix(back)
println m2.at(1, 2)
println npy.WriteNpyFile(a, "a.npy")
mut from_file := try npy.ReadNpyFile("a.npy")
println from_file.Count()
mut keys := Vector {} as Vector<str>
keys.Push("x")
keys.Push("y")
mut arrays := Vector {} as Vector<npy.NpyArray>
arrays.Push(a)
arrays.Push(back)
println npy.NpzWrite("ab.npz", keys, arrays)
println try npy.NpzList("ab.npz")
mut y := try npy.NpzRead("ab.npz", "y")
println y.shape
try:
npy.NpzRead("ab.npz", "z")
catch e:
println e.code
end
println npy.NPY_F32,
npy.NPY_I8,
npy.NPY_I16,
npy.NPY_I32,
npy.NPY_I64,
npy.NPY_U8,
npy.NPY_U16,
npy.NPY_U32,
npy.NPY_BOOL
y.Free()
from_file.Free()
back.Free()
a.Free()
end[2, 3] 2 6 true true [2, 3] [0, 1, 2, 3, 4, 5] 5 true 6 true ["x", "y"] [2, 3] npy.missing_key 1 2 3 4 5 6 7 8 9