Home > Net >  Numpy array not changing the array itself
Numpy array not changing the array itself

Time:04-06

Im not sure if im missing something out because i'm new to Python, but according to a few threads and docs that I've read like this one, from what i understand the numpy should change the array since im passing it by parameter. Thats not what is occurring in my code though.

class Graph(object):
def __init__(self):
    self.arr = self.createArray()


def insertVertice(self):
    ##SHAPE = (1,1) - (2,2)
    np.insert(self.arr, 0, 0, axis=1)

if I let the code like this and print "arr", the bidimensional array stays the same [[0 0] [0 0]] but if I do it like:

self.arr = np.insert(self.arr, 0, 0, axis=1)

It changes...[[0 0 0] [0 0 0]]

Does anyone have a clue on what am I missing here?

CodePudding user response:

In the doc you linked, it is clearly specified :

Return value: out [ndarray] A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

That means you have to use the return value as you are doing in the second example, the operation is not on the input array directly.

  • Related