Somachr
Somachr

Reputation: 464

NUMPY create, fill with random binary data

I need to create an 2D array.

    import numpy as np
    self.col = 10
    self.row = 5

    ...

    matrix = np.array(self.row, self.col) # NOT WORKING

What is the right syntax please

i also need to fill it with random binary data

Upvotes: 5

Views: 3184

Answers (2)

alfred_kok
alfred_kok

Reputation: 1

import numpy as np

def toBit(x):
        if x<= 0:
            x = 0               
        else:
            x = 1
        return x

VtoBit = np.vectorize(toBit)

arr1 = np.random.randn(6,10)
arr2 = VtoBit(arr1)

print(arr2)

Upvotes: 0

YS-L
YS-L

Reputation: 14738

Generate a random matrix with binary values:

import numpy as np
row, col = 10, 5
matrix = np.random.randint(2, size=(row,col))

Upvotes: 10

Related Questions