Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random sparse matrix filled with only values 0 or 1

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?

like image 849
myles Avatar asked Apr 20 '15 10:04

myles


3 Answers

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.

like image 114
ali_m Avatar answered Oct 18 '22 22:10

ali_m


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.

like image 4
Markus Strauss Avatar answered Oct 18 '22 22:10

Markus Strauss


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
like image 1
moldovean Avatar answered Oct 18 '22 20:10

moldovean