11
11
import math
12
12
import numpy as np
13
13
14
- from scipy ._lib ._util import check_random_state , rng_integers
14
+ from scipy ._lib ._util import check_random_state , rng_integers , _transition_to_rng
15
15
from ._sputils import upcast , get_index_dtype , isscalarlike
16
16
17
17
from ._sparsetools import csr_hstack
@@ -1093,22 +1093,14 @@ def block_diag(mats, format=None, dtype=None):
1093
1093
dtype = dtype ).asformat (format )
1094
1094
1095
1095
1096
+ @_transition_to_rng ("random_state" )
1096
1097
def random_array (shape , * , density = 0.01 , format = 'coo' , dtype = None ,
1097
- random_state = None , data_sampler = None ):
1098
+ rng = None , data_sampler = None ):
1098
1099
"""Return a sparse array of uniformly random numbers in [0, 1)
1099
1100
1100
1101
Returns a sparse array with the given shape and density
1101
1102
where values are generated uniformly randomly in the range [0, 1).
1102
1103
1103
- .. warning::
1104
-
1105
- Since numpy 1.17, passing a ``np.random.Generator`` (e.g.
1106
- ``np.random.default_rng``) for ``random_state`` will lead to much
1107
- faster execution times.
1108
-
1109
- A much slower implementation is used by default for backwards
1110
- compatibility.
1111
-
1112
1104
Parameters
1113
1105
----------
1114
1106
shape : int or tuple of ints
@@ -1120,21 +1112,14 @@ def random_array(shape, *, density=0.01, format='coo', dtype=None,
1120
1112
sparse matrix format.
1121
1113
dtype : dtype, optional (default: np.float64)
1122
1114
type of the returned matrix values.
1123
- random_state : {None, int, `Generator`, `RandomState`}, optional
1124
- A random number generator to determine nonzero structure. We recommend using
1125
- a `numpy.random.Generator` manually provided for every call as it is much
1126
- faster than RandomState.
1127
-
1128
- - If `None` (or `np.random`), the `numpy.random.RandomState`
1129
- singleton is used.
1130
- - If an int, a new ``Generator`` instance is used,
1131
- seeded with the int.
1132
- - If a ``Generator`` or ``RandomState`` instance then
1133
- that instance is used.
1115
+ rng : `numpy.random.Generator`, optional
1116
+ Pseudorandom number generator state. When `rng` is None, a new
1117
+ `numpy.random.Generator` is created using entropy from the
1118
+ operating system. Types other than `numpy.random.Generator` are
1119
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
1134
1120
1135
1121
This random state will be used for sampling `indices` (the sparsity
1136
1122
structure), and by default for the data values too (see `data_sampler`).
1137
-
1138
1123
data_sampler : callable, optional (default depends on dtype)
1139
1124
Sampler of random data values with keyword arg `size`.
1140
1125
This function should take a single keyword argument `size` specifying
@@ -1143,7 +1128,7 @@ def random_array(shape, *, density=0.01, format='coo', dtype=None,
1143
1128
By default, uniform [0, 1) random values are used unless `dtype` is
1144
1129
an integer (default uniform integers from that dtype) or
1145
1130
complex (default uniform over the unit square in the complex plane).
1146
- For these, the `random_state` rng is used e.g. ``rng.uniform(size=size)``.
1131
+ For these, the `rng` is used e.g. ``rng.uniform(size=size)``.
1147
1132
1148
1133
Returns
1149
1134
-------
@@ -1160,13 +1145,13 @@ def random_array(shape, *, density=0.01, format='coo', dtype=None,
1160
1145
1161
1146
Default sampling uniformly from [0, 1):
1162
1147
1163
- >>> S = sp.sparse.random_array((3, 4), density=0.25, random_state =rng)
1148
+ >>> S = sp.sparse.random_array((3, 4), density=0.25, rng =rng)
1164
1149
1165
1150
Providing a sampler for the values:
1166
1151
1167
1152
>>> rvs = sp.stats.poisson(25, loc=10).rvs
1168
1153
>>> S = sp.sparse.random_array((3, 4), density=0.25,
1169
- ... random_state =rng, data_sampler=rvs)
1154
+ ... rng =rng, data_sampler=rvs)
1170
1155
>>> S.toarray()
1171
1156
array([[ 36., 0., 33., 0.], # random
1172
1157
[ 0., 0., 0., 0.],
@@ -1175,38 +1160,38 @@ def random_array(shape, *, density=0.01, format='coo', dtype=None,
1175
1160
Building a custom distribution.
1176
1161
This example builds a squared normal from np.random:
1177
1162
1178
- >>> def np_normal_squared(size=None, random_state =rng):
1179
- ... return random_state .standard_normal(size) ** 2
1180
- >>> S = sp.sparse.random_array((3, 4), density=0.25, random_state =rng,
1181
- ... data_sampler=np_normal_squared)
1163
+ >>> def np_normal_squared(size=None, rng =rng):
1164
+ ... return rng .standard_normal(size) ** 2
1165
+ >>> S = sp.sparse.random_array((3, 4), density=0.25, rng =rng,
1166
+ ... data_sampler=np_normal_squared)
1182
1167
1183
1168
Or we can build it from sp.stats style rvs functions:
1184
1169
1185
- >>> def sp_stats_normal_squared(size=None, random_state =rng):
1170
+ >>> def sp_stats_normal_squared(size=None, rng =rng):
1186
1171
... std_normal = sp.stats.distributions.norm_gen().rvs
1187
- ... return std_normal(size=size, random_state=random_state ) ** 2
1188
- >>> S = sp.sparse.random_array((3, 4), density=0.25, random_state =rng,
1189
- ... data_sampler=sp_stats_normal_squared)
1172
+ ... return std_normal(size=size, random_state=rng ) ** 2
1173
+ >>> S = sp.sparse.random_array((3, 4), density=0.25, rng =rng,
1174
+ ... data_sampler=sp_stats_normal_squared)
1190
1175
1191
1176
Or we can subclass sp.stats rv_continuous or rv_discrete:
1192
1177
1193
1178
>>> class NormalSquared(sp.stats.rv_continuous):
1194
1179
... def _rvs(self, size=None, random_state=rng):
1195
- ... return random_state .standard_normal(size) ** 2
1180
+ ... return rng .standard_normal(size) ** 2
1196
1181
>>> X = NormalSquared()
1197
1182
>>> Y = X().rvs
1198
1183
>>> S = sp.sparse.random_array((3, 4), density=0.25,
1199
- ... random_state =rng, data_sampler=Y)
1184
+ ... rng =rng, data_sampler=Y)
1200
1185
"""
1201
1186
# Use the more efficient RNG by default.
1202
- if random_state is None :
1203
- random_state = np .random .default_rng ()
1204
- data , ind = _random (shape , density , format , dtype , random_state , data_sampler )
1187
+ if rng is None :
1188
+ rng = np .random .default_rng ()
1189
+ data , ind = _random (shape , density , format , dtype , rng , data_sampler )
1205
1190
return coo_array ((data , ind ), shape = shape ).asformat (format )
1206
1191
1207
1192
1208
1193
def _random (shape , density = 0.01 , format = None , dtype = None ,
1209
- random_state = None , data_sampler = None ):
1194
+ rng = None , data_sampler = None ):
1210
1195
if density < 0 or density > 1 :
1211
1196
raise ValueError ("density expected to be 0 <= density <= 1" )
1212
1197
@@ -1215,7 +1200,7 @@ def _random(shape, density=0.01, format=None, dtype=None,
1215
1200
# Number of non zero values
1216
1201
size = int (round (density * tot_prod ))
1217
1202
1218
- rng = check_random_state (random_state )
1203
+ rng = check_random_state (rng )
1219
1204
1220
1205
if data_sampler is None :
1221
1206
if np .issubdtype (dtype , np .integer ):
@@ -1250,20 +1235,12 @@ def data_sampler(size):
1250
1235
return vals , ind
1251
1236
1252
1237
1238
+ @_transition_to_rng ("random_state" , position_num = 5 )
1253
1239
def random (m , n , density = 0.01 , format = 'coo' , dtype = None ,
1254
- random_state = None , data_rvs = None ):
1240
+ rng = None , data_rvs = None ):
1255
1241
"""Generate a sparse matrix of the given shape and density with randomly
1256
1242
distributed values.
1257
1243
1258
- .. warning::
1259
-
1260
- Since numpy 1.17, passing a ``np.random.Generator`` (e.g.
1261
- ``np.random.default_rng``) for ``random_state`` will lead to much
1262
- faster execution times.
1263
-
1264
- A much slower implementation is used by default for backwards
1265
- compatibility.
1266
-
1267
1244
.. warning::
1268
1245
1269
1246
This function returns a sparse matrix -- not a sparse array.
@@ -1281,15 +1258,11 @@ def random(m, n, density=0.01, format='coo', dtype=None,
1281
1258
sparse matrix format.
1282
1259
dtype : dtype, optional
1283
1260
type of the returned matrix values.
1284
- random_state : {None, int, `numpy.random.Generator`,
1285
- `numpy.random.RandomState`}, optional
1286
-
1287
- - If `seed` is None (or `np.random`), the `numpy.random.RandomState`
1288
- singleton is used.
1289
- - If `seed` is an int, a new ``RandomState`` instance is used,
1290
- seeded with `seed`.
1291
- - If `seed` is already a ``Generator`` or ``RandomState`` instance then
1292
- that instance is used.
1261
+ rng : `numpy.random.Generator`, optional
1262
+ Pseudorandom number generator state. When `rng` is None, a new
1263
+ `numpy.random.Generator` is created using entropy from the
1264
+ operating system. Types other than `numpy.random.Generator` are
1265
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
1293
1266
1294
1267
This random state will be used for sampling the sparsity structure, but
1295
1268
not necessarily for sampling the values of the structurally nonzero
@@ -1319,12 +1292,12 @@ def random(m, n, density=0.01, format='coo', dtype=None,
1319
1292
>>> import scipy as sp
1320
1293
>>> import numpy as np
1321
1294
>>> rng = np.random.default_rng()
1322
- >>> S = sp.sparse.random(3, 4, density=0.25, random_state =rng)
1295
+ >>> S = sp.sparse.random(3, 4, density=0.25, rng =rng)
1323
1296
1324
1297
Providing a sampler for the values:
1325
1298
1326
1299
>>> rvs = sp.stats.poisson(25, loc=10).rvs
1327
- >>> S = sp.sparse.random(3, 4, density=0.25, random_state =rng, data_rvs=rvs)
1300
+ >>> S = sp.sparse.random(3, 4, density=0.25, rng =rng, data_rvs=rvs)
1328
1301
>>> S.toarray()
1329
1302
array([[ 36., 0., 33., 0.], # random
1330
1303
[ 0., 0., 0., 0.],
@@ -1333,27 +1306,27 @@ def random(m, n, density=0.01, format='coo', dtype=None,
1333
1306
Building a custom distribution.
1334
1307
This example builds a squared normal from np.random:
1335
1308
1336
- >>> def np_normal_squared(size=None, random_state =rng):
1337
- ... return random_state .standard_normal(size) ** 2
1338
- >>> S = sp.sparse.random(3, 4, density=0.25, random_state =rng,
1309
+ >>> def np_normal_squared(size=None, rng =rng):
1310
+ ... return rng .standard_normal(size) ** 2
1311
+ >>> S = sp.sparse.random(3, 4, density=0.25, rng =rng,
1339
1312
... data_rvs=np_normal_squared)
1340
1313
1341
1314
Or we can build it from sp.stats style rvs functions:
1342
1315
1343
- >>> def sp_stats_normal_squared(size=None, random_state =rng):
1316
+ >>> def sp_stats_normal_squared(size=None, rng =rng):
1344
1317
... std_normal = sp.stats.distributions.norm_gen().rvs
1345
- ... return std_normal(size=size, random_state=random_state ) ** 2
1346
- >>> S = sp.sparse.random(3, 4, density=0.25, random_state =rng,
1318
+ ... return std_normal(size=size, random_state=rng ) ** 2
1319
+ >>> S = sp.sparse.random(3, 4, density=0.25, rng =rng,
1347
1320
... data_rvs=sp_stats_normal_squared)
1348
1321
1349
1322
Or we can subclass sp.stats rv_continuous or rv_discrete:
1350
1323
1351
1324
>>> class NormalSquared(sp.stats.rv_continuous):
1352
1325
... def _rvs(self, size=None, random_state=rng):
1353
- ... return random_state .standard_normal(size) ** 2
1326
+ ... return rng .standard_normal(size) ** 2
1354
1327
>>> X = NormalSquared()
1355
1328
>>> Y = X() # get a frozen version of the distribution
1356
- >>> S = sp.sparse.random(3, 4, density=0.25, random_state =rng, data_rvs=Y.rvs)
1329
+ >>> S = sp.sparse.random(3, 4, density=0.25, rng =rng, data_rvs=Y.rvs)
1357
1330
"""
1358
1331
if n is None :
1359
1332
n = m
@@ -1364,11 +1337,12 @@ def data_rvs_kw(size):
1364
1337
return data_rvs (size )
1365
1338
else :
1366
1339
data_rvs_kw = None
1367
- vals , ind = _random ((m , n ), density , format , dtype , random_state , data_rvs_kw )
1340
+ vals , ind = _random ((m , n ), density , format , dtype , rng , data_rvs_kw )
1368
1341
return coo_matrix ((vals , ind ), shape = (m , n )).asformat (format )
1369
1342
1370
1343
1371
- def rand (m , n , density = 0.01 , format = "coo" , dtype = None , random_state = None ):
1344
+ @_transition_to_rng ("random_state" , position_num = 5 )
1345
+ def rand (m , n , density = 0.01 , format = "coo" , dtype = None , rng = None ):
1372
1346
"""Generate a sparse matrix of the given shape and density with uniformly
1373
1347
distributed values.
1374
1348
@@ -1389,15 +1363,11 @@ def rand(m, n, density=0.01, format="coo", dtype=None, random_state=None):
1389
1363
sparse matrix format.
1390
1364
dtype : dtype, optional
1391
1365
type of the returned matrix values.
1392
- random_state : {None, int, `numpy.random.Generator`,
1393
- `numpy.random.RandomState`}, optional
1394
-
1395
- If `seed` is None (or `np.random`), the `numpy.random.RandomState`
1396
- singleton is used.
1397
- If `seed` is an int, a new ``RandomState`` instance is used,
1398
- seeded with `seed`.
1399
- If `seed` is already a ``Generator`` or ``RandomState`` instance then
1400
- that instance is used.
1366
+ rng : `numpy.random.Generator`, optional
1367
+ Pseudorandom number generator state. When `rng` is None, a new
1368
+ `numpy.random.Generator` is created using entropy from the
1369
+ operating system. Types other than `numpy.random.Generator` are
1370
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
1401
1371
1402
1372
Returns
1403
1373
-------
@@ -1415,7 +1385,7 @@ def rand(m, n, density=0.01, format="coo", dtype=None, random_state=None):
1415
1385
Examples
1416
1386
--------
1417
1387
>>> from scipy.sparse import rand
1418
- >>> matrix = rand(3, 4, density=0.25, format="csr", random_state =42)
1388
+ >>> matrix = rand(3, 4, density=0.25, format="csr", rng =42)
1419
1389
>>> matrix
1420
1390
<Compressed Sparse Row sparse matrix of dtype 'float64'
1421
1391
with 3 stored elements and shape (3, 4)>
@@ -1425,4 +1395,4 @@ def rand(m, n, density=0.01, format="coo", dtype=None, random_state=None):
1425
1395
[0. , 0. , 0. , 0. ]])
1426
1396
1427
1397
"""
1428
- return random (m , n , density , format , dtype , random_state )
1398
+ return random (m , n , density , format , dtype , rng )
0 commit comments