mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Enable ruff NPY002 rule (#11336)
This commit is contained in:
parent
39daaf8248
commit
f8a948914b
|
@ -61,7 +61,8 @@ def _create_spd_matrix(dimension: int) -> Any:
|
||||||
>>> _is_matrix_spd(spd_matrix)
|
>>> _is_matrix_spd(spd_matrix)
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
random_matrix = np.random.randn(dimension, dimension)
|
rng = np.random.default_rng()
|
||||||
|
random_matrix = rng.normal(size=(dimension, dimension))
|
||||||
spd_matrix = np.dot(random_matrix, random_matrix.T)
|
spd_matrix = np.dot(random_matrix, random_matrix.T)
|
||||||
assert _is_matrix_spd(spd_matrix)
|
assert _is_matrix_spd(spd_matrix)
|
||||||
return spd_matrix
|
return spd_matrix
|
||||||
|
@ -157,7 +158,8 @@ def test_conjugate_gradient() -> None:
|
||||||
# Create linear system with SPD matrix and known solution x_true.
|
# Create linear system with SPD matrix and known solution x_true.
|
||||||
dimension = 3
|
dimension = 3
|
||||||
spd_matrix = _create_spd_matrix(dimension)
|
spd_matrix = _create_spd_matrix(dimension)
|
||||||
x_true = np.random.randn(dimension, 1)
|
rng = np.random.default_rng()
|
||||||
|
x_true = rng.normal(size=(dimension, 1))
|
||||||
b = np.dot(spd_matrix, x_true)
|
b = np.dot(spd_matrix, x_true)
|
||||||
|
|
||||||
# Numpy solution.
|
# Numpy solution.
|
||||||
|
|
|
@ -187,7 +187,8 @@ def main():
|
||||||
tree = DecisionTree(depth=10, min_leaf_size=10)
|
tree = DecisionTree(depth=10, min_leaf_size=10)
|
||||||
tree.train(x, y)
|
tree.train(x, y)
|
||||||
|
|
||||||
test_cases = (np.random.rand(10) * 2) - 1
|
rng = np.random.default_rng()
|
||||||
|
test_cases = (rng.random(10) * 2) - 1
|
||||||
predictions = np.array([tree.predict(x) for x in test_cases])
|
predictions = np.array([tree.predict(x) for x in test_cases])
|
||||||
avg_error = np.mean((predictions - test_cases) ** 2)
|
avg_error = np.mean((predictions - test_cases) ** 2)
|
||||||
|
|
||||||
|
|
|
@ -55,12 +55,12 @@ TAG = "K-MEANS-CLUST/ "
|
||||||
|
|
||||||
def get_initial_centroids(data, k, seed=None):
|
def get_initial_centroids(data, k, seed=None):
|
||||||
"""Randomly choose k data points as initial centroids"""
|
"""Randomly choose k data points as initial centroids"""
|
||||||
if seed is not None: # useful for obtaining consistent results
|
# useful for obtaining consistent results
|
||||||
np.random.seed(seed)
|
rng = np.random.default_rng(seed)
|
||||||
n = data.shape[0] # number of data points
|
n = data.shape[0] # number of data points
|
||||||
|
|
||||||
# Pick K indices from range [0, N).
|
# Pick K indices from range [0, N).
|
||||||
rand_indices = np.random.randint(0, n, k)
|
rand_indices = rng.integers(0, n, k)
|
||||||
|
|
||||||
# Keep centroids as dense format, as many entries will be nonzero due to averaging.
|
# Keep centroids as dense format, as many entries will be nonzero due to averaging.
|
||||||
# As long as at least one document in a cluster contains a word,
|
# As long as at least one document in a cluster contains a word,
|
||||||
|
|
|
@ -289,12 +289,13 @@ class SmoSVM:
|
||||||
if cmd is None:
|
if cmd is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
for i2 in np.roll(self.unbound, np.random.choice(self.length)):
|
rng = np.random.default_rng()
|
||||||
|
for i2 in np.roll(self.unbound, rng.choice(self.length)):
|
||||||
cmd = yield i1, i2
|
cmd = yield i1, i2
|
||||||
if cmd is None:
|
if cmd is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
for i2 in np.roll(self._all_samples, np.random.choice(self.length)):
|
for i2 in np.roll(self._all_samples, rng.choice(self.length)):
|
||||||
cmd = yield i1, i2
|
cmd = yield i1, i2
|
||||||
if cmd is None:
|
if cmd is None:
|
||||||
return
|
return
|
||||||
|
|
|
@ -51,8 +51,9 @@ class DenseLayer:
|
||||||
self.is_input_layer = is_input_layer
|
self.is_input_layer = is_input_layer
|
||||||
|
|
||||||
def initializer(self, back_units):
|
def initializer(self, back_units):
|
||||||
self.weight = np.asmatrix(np.random.normal(0, 0.5, (self.units, back_units)))
|
rng = np.random.default_rng()
|
||||||
self.bias = np.asmatrix(np.random.normal(0, 0.5, self.units)).T
|
self.weight = np.asmatrix(rng.normal(0, 0.5, (self.units, back_units)))
|
||||||
|
self.bias = np.asmatrix(rng.normal(0, 0.5, self.units)).T
|
||||||
if self.activation is None:
|
if self.activation is None:
|
||||||
self.activation = sigmoid
|
self.activation = sigmoid
|
||||||
|
|
||||||
|
@ -174,7 +175,8 @@ class BPNN:
|
||||||
|
|
||||||
|
|
||||||
def example():
|
def example():
|
||||||
x = np.random.randn(10, 10)
|
rng = np.random.default_rng()
|
||||||
|
x = rng.normal(size=(10, 10))
|
||||||
y = np.asarray(
|
y = np.asarray(
|
||||||
[
|
[
|
||||||
[0.8, 0.4],
|
[0.8, 0.4],
|
||||||
|
|
|
@ -41,15 +41,16 @@ class CNN:
|
||||||
self.size_pooling1 = size_p1
|
self.size_pooling1 = size_p1
|
||||||
self.rate_weight = rate_w
|
self.rate_weight = rate_w
|
||||||
self.rate_thre = rate_t
|
self.rate_thre = rate_t
|
||||||
|
rng = np.random.default_rng()
|
||||||
self.w_conv1 = [
|
self.w_conv1 = [
|
||||||
np.asmatrix(-1 * np.random.rand(self.conv1[0], self.conv1[0]) + 0.5)
|
np.asmatrix(-1 * rng.random((self.conv1[0], self.conv1[0])) + 0.5)
|
||||||
for i in range(self.conv1[1])
|
for i in range(self.conv1[1])
|
||||||
]
|
]
|
||||||
self.wkj = np.asmatrix(-1 * np.random.rand(self.num_bp3, self.num_bp2) + 0.5)
|
self.wkj = np.asmatrix(-1 * rng.random((self.num_bp3, self.num_bp2)) + 0.5)
|
||||||
self.vji = np.asmatrix(-1 * np.random.rand(self.num_bp2, self.num_bp1) + 0.5)
|
self.vji = np.asmatrix(-1 * rng.random((self.num_bp2, self.num_bp1)) + 0.5)
|
||||||
self.thre_conv1 = -2 * np.random.rand(self.conv1[1]) + 1
|
self.thre_conv1 = -2 * rng.random(self.conv1[1]) + 1
|
||||||
self.thre_bp2 = -2 * np.random.rand(self.num_bp2) + 1
|
self.thre_bp2 = -2 * rng.random(self.num_bp2) + 1
|
||||||
self.thre_bp3 = -2 * np.random.rand(self.num_bp3) + 1
|
self.thre_bp3 = -2 * rng.random(self.num_bp3) + 1
|
||||||
|
|
||||||
def save_model(self, save_path):
|
def save_model(self, save_path):
|
||||||
# save model dict with pickle
|
# save model dict with pickle
|
||||||
|
|
|
@ -153,7 +153,7 @@ class _DataSet:
|
||||||
"""
|
"""
|
||||||
seed1, seed2 = random_seed.get_seed(seed)
|
seed1, seed2 = random_seed.get_seed(seed)
|
||||||
# If op level seed is not set, use whatever graph level seed is returned
|
# If op level seed is not set, use whatever graph level seed is returned
|
||||||
np.random.seed(seed1 if seed is None else seed2)
|
self._rng = np.random.default_rng(seed1 if seed is None else seed2)
|
||||||
dtype = dtypes.as_dtype(dtype).base_dtype
|
dtype = dtypes.as_dtype(dtype).base_dtype
|
||||||
if dtype not in (dtypes.uint8, dtypes.float32):
|
if dtype not in (dtypes.uint8, dtypes.float32):
|
||||||
raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype)
|
raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype)
|
||||||
|
@ -211,7 +211,7 @@ class _DataSet:
|
||||||
# Shuffle for the first epoch
|
# Shuffle for the first epoch
|
||||||
if self._epochs_completed == 0 and start == 0 and shuffle:
|
if self._epochs_completed == 0 and start == 0 and shuffle:
|
||||||
perm0 = np.arange(self._num_examples)
|
perm0 = np.arange(self._num_examples)
|
||||||
np.random.shuffle(perm0)
|
self._rng.shuffle(perm0)
|
||||||
self._images = self.images[perm0]
|
self._images = self.images[perm0]
|
||||||
self._labels = self.labels[perm0]
|
self._labels = self.labels[perm0]
|
||||||
# Go to the next epoch
|
# Go to the next epoch
|
||||||
|
@ -225,7 +225,7 @@ class _DataSet:
|
||||||
# Shuffle the data
|
# Shuffle the data
|
||||||
if shuffle:
|
if shuffle:
|
||||||
perm = np.arange(self._num_examples)
|
perm = np.arange(self._num_examples)
|
||||||
np.random.shuffle(perm)
|
self._rng.shuffle(perm)
|
||||||
self._images = self.images[perm]
|
self._images = self.images[perm]
|
||||||
self._labels = self.labels[perm]
|
self._labels = self.labels[perm]
|
||||||
# Start next epoch
|
# Start next epoch
|
||||||
|
|
|
@ -28,19 +28,20 @@ class TwoHiddenLayerNeuralNetwork:
|
||||||
# Random initial weights are assigned.
|
# Random initial weights are assigned.
|
||||||
# self.input_array.shape[1] is used to represent number of nodes in input layer.
|
# self.input_array.shape[1] is used to represent number of nodes in input layer.
|
||||||
# First hidden layer consists of 4 nodes.
|
# First hidden layer consists of 4 nodes.
|
||||||
self.input_layer_and_first_hidden_layer_weights = np.random.rand(
|
rng = np.random.default_rng()
|
||||||
self.input_array.shape[1], 4
|
self.input_layer_and_first_hidden_layer_weights = rng.random(
|
||||||
|
(self.input_array.shape[1], 4)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Random initial values for the first hidden layer.
|
# Random initial values for the first hidden layer.
|
||||||
# First hidden layer has 4 nodes.
|
# First hidden layer has 4 nodes.
|
||||||
# Second hidden layer has 3 nodes.
|
# Second hidden layer has 3 nodes.
|
||||||
self.first_hidden_layer_and_second_hidden_layer_weights = np.random.rand(4, 3)
|
self.first_hidden_layer_and_second_hidden_layer_weights = rng.random((4, 3))
|
||||||
|
|
||||||
# Random initial values for the second hidden layer.
|
# Random initial values for the second hidden layer.
|
||||||
# Second hidden layer has 3 nodes.
|
# Second hidden layer has 3 nodes.
|
||||||
# Output layer has 1 node.
|
# Output layer has 1 node.
|
||||||
self.second_hidden_layer_and_output_layer_weights = np.random.rand(3, 1)
|
self.second_hidden_layer_and_output_layer_weights = rng.random((3, 1))
|
||||||
|
|
||||||
# Real output values provided.
|
# Real output values provided.
|
||||||
self.output_array = output_array
|
self.output_array = output_array
|
||||||
|
|
|
@ -7,7 +7,6 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
|
||||||
"EXE001", # Shebang is present but file is not executable" -- FIX ME
|
"EXE001", # Shebang is present but file is not executable" -- FIX ME
|
||||||
"G004", # Logging statement uses f-string
|
"G004", # Logging statement uses f-string
|
||||||
"INP001", # File `x/y/z.py` is part of an implicit namespace package. Add an `__init__.py`. -- FIX ME
|
"INP001", # File `x/y/z.py` is part of an implicit namespace package. Add an `__init__.py`. -- FIX ME
|
||||||
"NPY002", # Replace legacy `np.random.choice` call with `np.random.Generator` -- FIX ME
|
|
||||||
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
|
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
|
||||||
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
|
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
|
||||||
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
|
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
|
||||||
|
|
Loading…
Reference in New Issue
Block a user