Comments (5)
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.
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.
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.
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.
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)
- What is the use of fastapi.params? HOT 1
- Refering to python built-in `id` instead of `comment.post_id` in HTTPException? HOT 2
- [Chapter 6] comment argument in create_comment() as CommentCreate instance HOT 1
- Chapter 9 external api test is missing the test function HOT 1
- Delete irrelevant files from the project
- Discussion: consider merging `with` statements HOT 2
- Too much data for declared Content-Length error in DELETE post (Chapter 3) HOT 1
- excute(update_query) return influenced rows HOT 6
- Chapter 4 `PostPartialUpdate` model errors after meeting `response_model=PostPublic` HOT 3
- Chapter3_headers_cookies_02.py calling from automatic docs forces user-agent entry that is ignored HOT 1
- How does the way the app is run affect whether book code works? HOT 4
- Directory problems in Chapter 6 Alembic section HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from building-data-science-applications-with-fastapi.