View Source Npy (npy v0.1.1)
Reading and writing array to Python npy/npz format file.
You can exchange matrix data - %Npy or %Nx.Tensor - with Python through npy/npz file.
examples
Examples
You make a uniform random tensor and save it to "random.npy" under Elixir.
[elixir]
iex(1)> t = Nx.random_uniform({5,5})
#Nx.Tensor<
f32[5][5]
[
[0.9286868572235107, 0.8993584513664246, 0.09174104034900665, 0.1891217827796936, 0.3033398985862732],
[0.6039875745773315, 0.1656373143196106, 0.6622694134712219, 0.4383099675178528, 0.2207845151424408],
[0.08031792938709259, 0.05638507753610611, 0.4931488037109375, 0.6378694772720337, 0.5468790531158447],
[0.6913296580314636, 0.5027941465377808, 0.05995653197169304, 0.3467581272125244, 0.8337613940238953],
[0.48116567730903625, 0.7345675826072693, 0.4312438666820526, 0.5565636157989502, 0.27805331349372864]
]
>
iex(2)> Npy.save("random.npy", t)
:ok
And then, you can read "random.npy" in Python.
[python]
>>> import numpy as np
>>> t = np.load("random.npy")
>>> print(t)
[[0.92868686 0.89935845 0.09174104 0.18912178 0.3033399 ]
[0.6039876 0.16563731 0.6622694 0.43830997 0.22078452]
[0.08031793 0.05638508 0.4931488 0.6378695 0.54687905]
[0.69132966 0.50279415 0.05995653 0.34675813 0.8337614 ]
[0.48116568 0.7345676 0.43124387 0.5565636 0.2780533 ]]
Link to this section Summary
Functions
Convert npy format binary to %Npy.
Convert a matrix list to %Npy{descr: 'descr', ...}.
Load array from npy/npz and convert it to %Npy or %Nx.Tensor.
Convert %Npy to %Nx.Tensor.
Save %Npy/%Nx.Tensor to npy file.
Save a %Npy to CSV file.
Save a list of %Npy/%Nx.Tensor to npz file.
Convert %Nx.Tensor to %Nx.
Convert %Npy/%Nx.Tensor to npy binary.
Convert %Npy to a matrix list.
Link to this section Functions
Convert npy format binary to %Npy.
examples
Examples
iex> Npy.from_bin!(npy_bin)
%Npy{...}
Convert a matrix list to %Npy{descr: 'descr', ...}.
parameters
Parameters
- list : matrix list
- descr : data type
"<f4"
- float 32bit"<i1"
- integer 8bit"<i4"
- integer 32bit
examples
Examples
iex> Npy.from_list([[[4.384970664978027, ...], ...], ...], "<f4")
%Npy{...}
Load array from npy/npz and convert it to %Npy or %Nx.Tensor.
parameters
Parameters
- fname : file name.
load/2
returns a list of %Npy/%Nx.Tensors for "xxx.npz". - mode : convertion mode
:npy
(default) - convert to %Npy{}:nx
- convert to %Nx.Tensor{}
examples
Examples
iex> Npy.load("sample.npy", :npy)
{:ok, %Npy{...}}
iex> Npy.load("sample.npy", :nx)
{:ok, #Nx.Tensor<...>}
iex> Npy.load("sample.npz", :npy)
{:ok, [%Npy{...}, %Npy{...}, ...]}
iex> Npy.load("sample.npz", :nx)
{:ok, [#Nx.Tensor<...>, #Nx.Tensor<...>}, ...]}
Convert %Npy to %Nx.Tensor.
examples
Examples
iex> Npy.npy2tensor(%Npy{...})
#Nx.Tensor<...>
Save %Npy/%Nx.Tensor to npy file.
examples
Examples
iex> Npy.save("sample.npy", %Npy{})
:ok
iex> Npy.save("sample.npy", %Nx.Tensor{})
:ok
Save a %Npy to CSV file.
For %Npy which has tow or one dimensonal shape.
examples
Examples
iex> Npy.savecsv("sample.csv", %Npy{shape: {100, 20}})
Save a list of %Npy/%Nx.Tensor to npz file.
examples
Examples
iex> Npy.savez("sample.npz", [%Npy{}, %Nx.Tensor{}, ...])
{:ok, "sample.npz"}
Convert %Nx.Tensor to %Nx.
examples
Examples
iex> Npy.tensor2npy(%Nx{...})
%Npy{...}
Convert %Npy/%Nx.Tensor to npy binary.
examples
Examples
iex> Npy.to_bin(%Npy{})
<<....>>
iex> Npy.to_bin(%Nx.Tensor{})
<<....>>
Convert %Npy to a matrix list.
examples
Examples
iex> Npy.to_list(%Npy{})
[
[
[4.384970664978027, ...],
...
],
...
]