mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Enable ruff ICN001 rule (#11329)
* Enable ruff ICN001 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -22,7 +22,7 @@ import os
|
||||
import typing
|
||||
import urllib
|
||||
|
||||
import numpy
|
||||
import numpy as np
|
||||
from tensorflow.python.framework import dtypes, random_seed
|
||||
from tensorflow.python.platform import gfile
|
||||
from tensorflow.python.util.deprecation import deprecated
|
||||
@ -39,8 +39,8 @@ DEFAULT_SOURCE_URL = "https://storage.googleapis.com/cvdf-datasets/mnist/"
|
||||
|
||||
|
||||
def _read32(bytestream):
|
||||
dt = numpy.dtype(numpy.uint32).newbyteorder(">")
|
||||
return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
|
||||
dt = np.dtype(np.uint32).newbyteorder(">")
|
||||
return np.frombuffer(bytestream.read(4), dtype=dt)[0]
|
||||
|
||||
|
||||
@deprecated(None, "Please use tf.data to implement this functionality.")
|
||||
@ -68,7 +68,7 @@ def _extract_images(f):
|
||||
rows = _read32(bytestream)
|
||||
cols = _read32(bytestream)
|
||||
buf = bytestream.read(rows * cols * num_images)
|
||||
data = numpy.frombuffer(buf, dtype=numpy.uint8)
|
||||
data = np.frombuffer(buf, dtype=np.uint8)
|
||||
data = data.reshape(num_images, rows, cols, 1)
|
||||
return data
|
||||
|
||||
@ -77,8 +77,8 @@ def _extract_images(f):
|
||||
def _dense_to_one_hot(labels_dense, num_classes):
|
||||
"""Convert class labels from scalars to one-hot vectors."""
|
||||
num_labels = labels_dense.shape[0]
|
||||
index_offset = numpy.arange(num_labels) * num_classes
|
||||
labels_one_hot = numpy.zeros((num_labels, num_classes))
|
||||
index_offset = np.arange(num_labels) * num_classes
|
||||
labels_one_hot = np.zeros((num_labels, num_classes))
|
||||
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
|
||||
return labels_one_hot
|
||||
|
||||
@ -107,7 +107,7 @@ def _extract_labels(f, one_hot=False, num_classes=10):
|
||||
)
|
||||
num_items = _read32(bytestream)
|
||||
buf = bytestream.read(num_items)
|
||||
labels = numpy.frombuffer(buf, dtype=numpy.uint8)
|
||||
labels = np.frombuffer(buf, dtype=np.uint8)
|
||||
if one_hot:
|
||||
return _dense_to_one_hot(labels, num_classes)
|
||||
return labels
|
||||
@ -153,7 +153,7 @@ class _DataSet:
|
||||
"""
|
||||
seed1, seed2 = random_seed.get_seed(seed)
|
||||
# If op level seed is not set, use whatever graph level seed is returned
|
||||
numpy.random.seed(seed1 if seed is None else seed2)
|
||||
np.random.seed(seed1 if seed is None else seed2)
|
||||
dtype = dtypes.as_dtype(dtype).base_dtype
|
||||
if dtype not in (dtypes.uint8, dtypes.float32):
|
||||
raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype)
|
||||
@ -175,8 +175,8 @@ class _DataSet:
|
||||
)
|
||||
if dtype == dtypes.float32:
|
||||
# Convert from [0, 255] -> [0.0, 1.0].
|
||||
images = images.astype(numpy.float32)
|
||||
images = numpy.multiply(images, 1.0 / 255.0)
|
||||
images = images.astype(np.float32)
|
||||
images = np.multiply(images, 1.0 / 255.0)
|
||||
self._images = images
|
||||
self._labels = labels
|
||||
self._epochs_completed = 0
|
||||
@ -210,8 +210,8 @@ class _DataSet:
|
||||
start = self._index_in_epoch
|
||||
# Shuffle for the first epoch
|
||||
if self._epochs_completed == 0 and start == 0 and shuffle:
|
||||
perm0 = numpy.arange(self._num_examples)
|
||||
numpy.random.shuffle(perm0)
|
||||
perm0 = np.arange(self._num_examples)
|
||||
np.random.shuffle(perm0)
|
||||
self._images = self.images[perm0]
|
||||
self._labels = self.labels[perm0]
|
||||
# Go to the next epoch
|
||||
@ -224,8 +224,8 @@ class _DataSet:
|
||||
labels_rest_part = self._labels[start : self._num_examples]
|
||||
# Shuffle the data
|
||||
if shuffle:
|
||||
perm = numpy.arange(self._num_examples)
|
||||
numpy.random.shuffle(perm)
|
||||
perm = np.arange(self._num_examples)
|
||||
np.random.shuffle(perm)
|
||||
self._images = self.images[perm]
|
||||
self._labels = self.labels[perm]
|
||||
# Start next epoch
|
||||
@ -235,8 +235,8 @@ class _DataSet:
|
||||
images_new_part = self._images[start:end]
|
||||
labels_new_part = self._labels[start:end]
|
||||
return (
|
||||
numpy.concatenate((images_rest_part, images_new_part), axis=0),
|
||||
numpy.concatenate((labels_rest_part, labels_new_part), axis=0),
|
||||
np.concatenate((images_rest_part, images_new_part), axis=0),
|
||||
np.concatenate((labels_rest_part, labels_new_part), axis=0),
|
||||
)
|
||||
else:
|
||||
self._index_in_epoch += batch_size
|
||||
|
Reference in New Issue
Block a user