Giter Club home page Giter Club logo

Comments (5)

frankie567 avatar frankie567 commented on August 17, 2024

There is a small nuance here.

If you manipulate numpy arrays with scalar values, like numbers, numpy.copy is enough and it will create a new array in memory with those values.

However, if you numpy array contains arbitrary Python objects (notice dtype=object), it won't copy them; it'll only pass their reference. IMO, you shouldn't worry too much about this, this is a very rare case (in general, you should probably avoid to store Python objects into a numpy array).

from building-data-science-applications-with-fastapi.

gitgithan avatar gitgithan commented on August 17, 2024

Sorry I left out the second half of that sentence, what I mean't to say was is the full sentence really correct?
The second half says you just have to use the copy method on the array.
I thought the copy method was a shallow copy, so this contradicts the first half of sentence saying it is a deep copy?

from building-data-science-applications-with-fastapi.

frankie567 avatar frankie567 commented on August 17, 2024

Well, maybe we could argue on the meaning of deep copy here. What I meant in this part is that if you just do:

v = m[::, 3:]

v here is a view (or shallow copy). It means that the underlying value are shared between v and m: if you change a value in v, it'll change in m also.

If you need to have a real copy of the values in the matrix (this is what I mean by deep copy here), you need to use the .copy method:

v = m[::, 3:].copy()

from building-data-science-applications-with-fastapi.

gitgithan avatar gitgithan commented on August 17, 2024

This was what I mean by copy() being shallow. I just discovered numpy.copy() behaves differently when type is int vs object

import copy 
import numpy as np

a_object_shallow = np.array([[1],[1,2]],dtype=object)
b_object_shallow = a_object_shallow.copy()
b_object_shallow[1][1] *= 10
print(repr(a_object_shallow))  # a_object_shallow[1][1] changed: array([list([1]), list([1, 20])], dtype=object)

a_object_deep = np.array([[1],[1,2]],dtype=object)
b_object_deep = copy.deepcopy(a_object_deep)
b_object_deep[1][1] *= 10
print(repr(a_object_deep))  # a_object_deep[1][1] no change: array([list([1]), list([1, 2])], dtype=object)

a_int = np.array([[1,2],[1,2]],dtype='int64')
b_int = a_int.copy()
b_int[1][1] *= 10
print(repr(a_int))  # a_int[1][1] no change: array([[1, 2],[1, 2]])

from building-data-science-applications-with-fastapi.

frankie567 avatar frankie567 commented on August 17, 2024

Yes, that's the point of the last example you mentioned in the numpy docs.

But as I said before, storing arbitrary objects in a numpy matrix is a quite rare case and should probably be avoided.

from building-data-science-applications-with-fastapi.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.