Skip to content

Commit 1899ec4

Browse files
committed
Added npz example and credited Aalto
1 parent 63156da commit 1899ec4

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

content/scientific-data.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ An overview of common data formats
226226
- 🟨 : Ok / depends on a case
227227
- ❌ : Bad
228228

229+
Adapted from Aalto university's `Python for scientific computing <https://aaltoscicomp.github.io/python-for-scicomp/work-with-data/#what-is-a-data-format>`__.
230+
229231
Some of these formats (e.g. JSON and CSV) are saved as text files (ASCII), thus they are
230232
human-readable. This makes them easier to visually check them (e.g. for format errors) and
231233
are supported out of the box by many tools. However, they tend to be slower during I/O and

content/stack.rst

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,44 @@ Views and copies of arrays
359359
I/O with NumPy
360360
^^^^^^^^^^^^^^
361361

362-
- Numpy provides functions for reading data from file and for writing data
363-
into the files
364-
- Simple text files
365-
366-
- :meth:`numpy.loadtxt`
367-
- :meth:`numpy.savetxt`
368-
- Data in regular column layout
369-
- Can deal with comments and different column delimiters
362+
Numpy provides functions for reading from/writing to files. Both ASCII and binary
363+
formats are supported with the CSV and npy/npz formats:
364+
365+
.. tabs::
366+
367+
.. tab:: CSV
368+
369+
The ``numpy.loadtxt()`` and ``numpy.savetxt()`` functions can be used. They
370+
save in a regular column layout and can deal with different delimiters,
371+
column titles and numerical representations.
372+
373+
.. code-block:: python
374+
375+
a = np.array([1, 2, 3, 4])
376+
np.savetxt("my_array.csv", a)
377+
b = np.loadtxt("my_array.csv")
378+
a == b
379+
# True
380+
381+
.. tab:: Binary
382+
383+
The npy format is a binary format used to dump arrays of any
384+
shape. Several arrays can be saved into a single npz file, which is
385+
simply a zipped collection of different npy files. All the arrays to
386+
be saved into a npz file can be passed as kwargs to the ``numpy.savez()``
387+
function. The data can then be recovered using the ``numpy.load()`` method,
388+
which returns a dictionary-like object in which each key points to one of the arrays:
389+
390+
.. code-block:: python
391+
392+
a = np.array([1, 2, 3, 4])
393+
b = np.array([5, 6, 7, 8])
394+
395+
np.savez("my_arrays.npz", array_1=a, array_2=b)
396+
data = np.load("my_arrays.npz")
397+
data['array_1'] == a
398+
data['array_2'] == b
399+
# Both are true
370400
371401
372402
Random numbers

0 commit comments

Comments
 (0)