MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / _extract_labels

Function _extract_labels

neural_network/input_data.py:86–111  ·  view source on GitHub ↗

Extract the labels into a 1D uint8 numpy array [index]. Args: f: A file object that can be passed into a gzip reader. one_hot: Does one hot encoding for the result. num_classes: Number of classes for the one hot encoding. Returns: labels: a 1D uint8 numpy array.

(f, one_hot=False, num_classes=10)

Source from the content-addressed store, hash-verified

84
85@deprecated(None, "Please use tf.data to implement this functionality.")
86def _extract_labels(f, one_hot=False, num_classes=10):
87 """Extract the labels into a 1D uint8 numpy array [index].
88
89 Args:
90 f: A file object that can be passed into a gzip reader.
91 one_hot: Does one hot encoding for the result.
92 num_classes: Number of classes for the one hot encoding.
93
94 Returns:
95 labels: a 1D uint8 numpy array.
96
97 Raises:
98 ValueError: If the bystream doesn't start with 2049.
99 """
100 print("Extracting", f.name)
101 with gzip.GzipFile(fileobj=f) as bytestream:
102 magic = _read32(bytestream)
103 if magic != 2049:
104 msg = f"Invalid magic number {magic} in MNIST label file: {f.name}"
105 raise ValueError(msg)
106 num_items = _read32(bytestream)
107 buf = bytestream.read(num_items)
108 labels = np.frombuffer(buf, dtype=np.uint8)
109 if one_hot:
110 return _dense_to_one_hot(labels, num_classes)
111 return labels
112
113
114class _DataSet:

Callers 1

read_data_setsFunction · 0.85

Calls 2

_read32Function · 0.85
_dense_to_one_hotFunction · 0.85

Tested by

no test coverage detected