NumPy binary format reader and writer.

Reads and writes .npy (a single array) and .npz (an archive holding the feature matrix, the target, and the feature names together). This is the fastest and most faithful round trip for data that is already numeric, since nothing is parsed or re-typed.

Functions

Func

load_numpy

Line 16
load_numpy(filepath: Union[str, Path], target_key: str='y', data_key: str='X') -> Dataset

Load data from NumPy format (.npy or .npz).

For .npy files: assumes the file contains feature data only. For .npz files: looks for 'X' (features) and 'y' (target) arrays.

Parameters

filepath
str or Path
Path to .npy or .npz file
target_key
str
Key for target array in .npz file
data_key
str
Key for data array in .npz file

Returns

result
Dataset
Dataset object with X, y, feature_names
python
>>> from tuiml.datasets.loaders import load_numpy
>>> data = load_numpy('data.npz')
>>> data.X.shape, data.y.shape
>>> X, y = load_numpy('data.npz')  # Can unpack
Func

save_numpy

Line 69
save_numpy(filepath: Union[str, Path], data: np.ndarray, target: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None, compressed: bool=True)

Save data to NumPy's .npy or .npz format.

Writes a plain .npy array when only data is given. As soon as a target or feature names are supplied it writes an .npz archive instead, with the features under the X key and the target under y, so that load_numpy can restore everything in one call.

Parameters

filepath
str or Path
Output file path.
data
numpy.ndarray of shape (n_samples, n_features)
Feature matrix to write.
target
numpy.ndarray of shape (n_samples,) or None = None
Target values, stored under the y key.
feature_names
list of str or None = None
Feature names, stored alongside the arrays in the .npz.
compressed
bool = True
Whether to compress the archive. Ignored for plain .npy output.

Returns

None
The array or archive is written to filepath.
python
>>> from tuiml.datasets.loaders import save_numpy
>>> save_numpy('data.npz', X, target=y, feature_names=['a', 'b'])