2

I am trying to generate a random array of 0s and 1s, and I am getting the error: shape mismatch: objects cannot be broadcast to a single shape. The error seems to be occurring in the line randints = np.random.binomial(1,p,(n,n)). Here is the function:

import numpy as np

def rand(n,p):
    '''Generates a random array subject to parameters p and N.'''

    # Create an array using a random binomial with one trial and specified
    # parameters.
    randints = np.random.binomial(1,p,(n,n))

    # Use nested while loops to go through each element of the array
    # and assign True to 1 and False to 0.
    i = 0
    j = 0
    rand = np.empty(shape = (n,n),dtype = bool)
    while i < n:
        while j < n:
            if randints[i][j] == 0:
                rand[i][j] = False
            if randints[i][j] == 1:
                rand[i][j] = True
            j = j+1
        i = i +1
        j = 0

    # Return the new array.
    return rand

print rand

When I run it by itself, it returns <function rand at 0x1d00170>. What does this mean? How should I convert it to an array that can be worked with in other functions?

2
  • What versions of python and numpy are you running? Your code as posted above only has a misindentation in the i = i +1... Commented Apr 9, 2013 at 15:59
  • 3
    The reason it prints <function rand at 0x1d00170> when you run this program is because your line print rand is printing out the function object. You need to call the function instead. Try: print rand(4,2) instead. Commented Apr 9, 2013 at 16:07

2 Answers 2

4

You needn't go through all of that,

 randints = np.random.binomial(1,p,(n,n))

produces your array of 0 and 1 values,

 rand_true_false = randints == 1

will produce another array, just with the 1s replaced with True and 0s with False.

Sign up to request clarification or add additional context in comments.

1 Comment

Another way to convert to True/False is randints.astype(bool)
1

Obviously, the answer by @danodonovan is the most Pythonic, but if you really want something more similar to your looping code. Here is an example that fixes the name conflicts and loops more simply.

import numpy as np

def my_rand(n,p):
    '''Generates a random array subject to parameters p and N.'''

    # Create an array using a random binomial with one trial and specified
    # parameters.

    randInts = np.random.binomial(1,p,(n,n))

    # Use nested while loops to go through each element of the array
    # and assign True to 1 and False to 0.

    randBool = np.empty(shape = (n,n),dtype = bool)
    for i in range(n):
        for j in range(n):
             if randInts[i][j] == 0:
                randBool[i][j] = False
             else:
                randBool[i][j] = True

    return randBool


newRand = my_rand(5,0.3)
print(newRand)

1 Comment

Small meta-note: saying "the answer above" may not always be true as other answers are added. It's better to reference the answer by the poster, i.e. "the answer by @danodonovan". Also, it looks like your return statement should be indented into the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.