2.7. Array Export

2.7.1. SetUp

>>> import numpy as np

2.7.2. np.savetxt()

Save integers:

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.savetxt('myfile.csv', a, delimiter=',')  
1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00
4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00
>>>
>>> np.savetxt('myfile.csv', a, delimiter=',', fmt='%d')  
1,2,3
4,5,6

Save floats:

>>> a = np.array([[5.4, 3.9, 1.3, 0.4],
...               [5.9, 3. , 5.1, 1.8],
...               [6. , 3.4, 4.5, 1.6],
...               [7.3, 2.9, 6.3, 1.8],
...               [5.6, 2.5, 3.9, 1.1]])
>>>
>>> np.savetxt('myfile.csv', a, delimiter=',')  
5.400000000000000355e+00,3.899999999999999911e+00,1.300000000000000044e+00,4.000000000000000222e-01
5.900000000000000355e+00,3.000000000000000000e+00,5.099999999999999645e+00,1.800000000000000044e+00
6.000000000000000000e+00,3.399999999999999911e+00,4.500000000000000000e+00,1.600000000000000089e+00
7.299999999999999822e+00,2.899999999999999911e+00,6.299999999999999822e+00,1.800000000000000044e+00
5.599999999999999645e+00,2.500000000000000000e+00,3.899999999999999911e+00,1.100000000000000089e+00
>>>
>>> np.savetxt('myfile.csv', a, delimiter=',', fmt='%.1f')  
5.4,3.9,1.3,0.4
5.9,3.0,5.1,1.8
6.0,3.4,4.5,1.6
7.3,2.9,6.3,1.8
5.6,2.5,3.9,1.1
>>>
>>> np.savetxt('myfile.csv', a, delimiter=',', fmt='%.2f')  
5.40,3.90,1.30,0.40
5.90,3.00,5.10,1.80
6.00,3.40,4.50,1.60
7.30,2.90,6.30,1.80
5.60,2.50,3.90,1.10

2.7.3. Other

Table 2.9. NumPy Export methods

Method

Data Type

Format

Description

np.savetxt()

Text

.csv, .txt, .dat

Save in text format, such as CSV

np.save()

Binary

.npy

Save in NumPy native format

np.savez()

Binary

.npz

Save multiple arrays to native format

np.savez_compressed()

Compressed

.npz

Save multiple arrays to compressed native format

>>> 
... data = np.loadtxt('myfile.csv', delimiter=',', usecols=1, skiprows=1, dtype=np.float16)
...
... small = (data < 1)
... medium = (data < 1) & (data < 2.0)
... large = (data < 2)
...
... np.save('/tmp/small', data[small])
... np.save('/tmp/medium', data[medium])
... np.save('/tmp/large', data[large])

2.7.4. Assignments

Code 2.43. Solution
"""
* Assignment: Numpy Loadtext
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Load text from `DATA`
    2. Define variables:
        a. `species: np.ndarray[str]` - first row, columns 2, 3, 4
        b. `features: np.ndarray[float]` - all rows except the first one, columns 0, 1, 2, 3
        c. `labels: np.ndarray[int]` - all rows except the first one, column 4
    3. Run doctests - all must succeed

Polish:
    1. Wczytaj tekst z `DATA`
    2. Zdefiniuj zmienne:
        a. `species: np.ndarray[str]` - pierwszy wiersz, kolumny 2, 3, 4
        b. `features: np.ndarray[float]` - wszystkie wiersze poza pierwszym, kolumny 0, 1, 2, 3
        c. `labels: np.ndarray[int]` - wszystkie wiersze poza pierwszym, kolumna 4
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert species is not Ellipsis, \
    'Assign result to variable: `species`'
    >>> assert labels is not Ellipsis, \
    'Assign result to variable: `labels`'
    >>> assert features is not Ellipsis, \
    'Assign result to variable: `features`'

    >>> assert type(species) is np.ndarray, \
    'Variable `species` has invalid type, expected: np.ndarray'
    >>> assert type(features) is np.ndarray, \
    'Variable `features` has invalid type, expected: np.ndarray'
    >>> assert type(labels) is np.ndarray, \
    'Variable `labels` has invalid type, expected: np.ndarray'

    >>> assert species.dtype == np.dtype('<U10'), \
    'Variable `species` has invalid type, expected: str'
    >>> assert features.dtype is np.dtype('float64'), \
    'Variable `features` has invalid type, expected: float'
    >>> assert labels.dtype is np.dtype('int64'), \
    'Variable `labels` has invalid type, expected: int'

    >>> assert len(species) == 3, \
    'Variable `species` length should be 3'
    >>> assert len(features) == 151, \
    'Variable `features` length should be 151'
    >>> assert len(labels) == 151, \
    'Variable `labels` length should be 151'

    >>> species
    array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

    >>> features[:3]
    array([[5.4, 3.9, 1.3, 0.4],
           [5.9, 3. , 5.1, 1.8],
           [6. , 3.4, 4.5, 1.6]])

    >>> features[-3:]
    array([[4.9, 2.5, 4.5, 1.7],
           [6.3, 2.8, 5.1, 1.5],
           [6.8, 3.2, 5.9, 2.3]])

    >>> labels
    array([0, 2, 1, 2, 1, 0, 1, 1, 0, 2, 2, 0, 0, 2, 2, 1, 2, 2, 2, 1, 0, 1,
           1, 0, 0, 0, 2, 2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2, 1, 1, 1, 2, 2,
           0, 1, 1, 1, 1, 1, 2, 0, 2, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 2, 0, 0,
           0, 0, 0, 0, 1, 0, 2, 0, 0, 1, 1, 2, 2, 1, 0, 2, 1, 0, 1, 0, 2, 1,
           0, 2, 0, 2, 1, 0, 2, 1, 1, 0, 0, 1, 2, 2, 2, 1, 0, 1, 1, 1, 2, 2,
           0, 2, 2, 0, 2, 1, 2, 0, 0, 1, 0, 2, 0, 2, 1, 2, 2, 2, 1, 0, 2, 1,
           0, 0, 2, 0, 2, 1, 1, 1, 0, 1, 1, 2, 0, 1, 1, 0, 2, 2, 2])
"""

import numpy as np


DATA = 'https://python3.info/_static/iris-dirty.csv'

species = ...
features = ...
labels = ...