Q:

How to invert only negative elements in numpy matrix?

I have a matrix containing positive and negative numbers like this:
>>> source_matrix
    array([[-4, -2,  0],
           [-5,  0,  4],
           [ 0,  6,  5]])

I'd like to had a copy of this matrix with inverted negatives:
>>> result
    array([[-0.25, -0.5,  0],
           [-0.2,  0,  4],
           [ 0,  6,  5]])

A:

Firstly, since your desired array is gonna contain float type you need to determine the array's dtype at creation time as float. The reason for that is because if you assign the float results of the inverted sub-array they'll automatically be casted to float. Secondly, you need to find the negative numbers in your array and then use a simple indexing in order to grab them and use np.true_divide() to perform the inversion.
In [25]: arr = np.array([[-4, -2,  0],
    ...:                 [-5,  0,  4],
    ...:                 [ 0,  6,  5]], dtype=np.float)
    ...:            
    ...:            

In [26]: mask = arr < 0

In [27]: arr[mask] = np.true_divide(1, arr[mask])

In [28]: arr
Out[28]: 
array([[-0.25, -0.5 ,  0.  ],
       [-0.2 ,  0.  ,  4.  ],
       [ 0.  ,  6.  ,  5.  ]])

