0

I have a Python numpy array I want to change the values of it.

Here is my array:

[[  0   0   0 ...,   0   0   0]
 [  0   0   0 ...,   0   0   0]
 [  0   0   0 ...,   0   0   0]
 ..., 
 [  0   0   0 ..., 174 152 178]
 [  0   0   0 ..., 193 157 172]
 [  0   0   0 ..., 199 173 166]]

How can I change all values which are greater than 0 to 1?

gboffi
  • 22,939
  • 8
  • 54
  • 85
Shiuli Pervin
  • 125
  • 2
  • 8

2 Answers2

0

Suppose a is a numpy array. Then, you make use of Boolean indexing and do a[a > 0] = 1 to change all values greater than 0 to 1. Take a look at http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing for more examples.

edwinksl
  • 7,005
  • 4
  • 30
  • 36
0

It worked. I did like this:

a=[[0,0,0,0,0]
   [0,0,0,0,0]
   [0,0,34,34,35]
   [0,0,11,34,67]]
a[a>0]=1
Shiuli Pervin
  • 125
  • 2
  • 8