python - Not able to generate a random array of categorical labels -


i have random state fixed seed using make predictive results replicable:

rng = np.random.randomstate(101) len(finaltestsentences) = 500 

i trying use seed generate array of random categorical variables using unique list of possibilities. here's example (i used set on training labels generate unique labels have):

unique_train_labels = set([u'bla', u'blee', u'blu', u'ma']) 

i do:

categorical_random = np.array[rng.choice(unique_train_labels, len(finaltestsentences))] 

but getting:

categorical_random = np.array[rng.choice(unique_train_labels, len(finaltestsentences))]   file "mtrand.pyx", line 1072, in mtrand.randomstate.choice (numpy/random/mtrand/mtrand.c:11545) valueerror: must 1-dimensional or integer 

note, workaround here

what going wrong , how can achieve aim?

if @ definition of np.random.choice, you'll see a, population, converted numpy array by

a = np.array(a, copy=false) 

if set, conversion little different. example, np.array([1, 2, 3]) gives array 3 elements: array([1, 2, 3]) np.array({1, 2, 3}) gives array single element element set. because of dimension problem, numpy raises valueerror.

to avoid can either convert set list with:

rng.choice(list(unique_train_labels), len(finaltestsentences)) 

or directly use np.unique unique labels:

rng.choice(np.unique([u'bla', u'blee', u'blu', u'ma']), len(finaltestsentences)) 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -