Reputation: 1671
I'm trying to generate a random csr_matrix using SciPy but I need it to only be filled with values 0 or 1.
So far I'm trying to use:
rand(1000, 10, density=0.2, format='csr', random_state=np.random.randint(0, 2))
and I get the correct structure and density I want, but the values filling it are floats between 0 and 1.
Is there a way to generate this structure with just floats of 0 or 1?
Upvotes: 9
Views: 9098
Reputation: 1032
How about
import scipy.sparse as ss
data = ss.random(1000, 10, density=.2, format='csr',
data_rvs=np.ones, # fill with ones
dtype='f' # use float32 first
).astype('int8') # then convert to int8
ss.random
only supports float types of which float32
is the smallest, whereas int8
is the smallest integer type available.
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.random.html for more information.
Upvotes: 4
Reputation: 3261
np.random.randint(0,2,1000)
will generate 1000 random variables between 0 and 1 inclusive. Then, it's up to you what kind of container you want to use for the matrix
my_v = np.random.randint(0,5,1000)
my_v[my_v>1]=1
Upvotes: 1
Reputation: 74152
You could simply replace the non-zero values in your random matrix with ones:
from scipy.sparse import rand
x = rand(1000, 10, density=0.2, format='csr')
x.data[:] = 1
print(np.unique(x.todense().flat))
# [ 0. 1.]
I don't think that the random_state=
kwarg does what you think it does - it simply allows you to either specify the seed for the random number generator, or to explicitly pass an np.random.RandomState
instance to serve as the RNG.
Upvotes: 12