I have a property of a Python object that returns an array. Now, I can set the setter of that property such that the whole array is settable. However, I’m missing how to make the elements by themselves settable through the property. I would expect from a user perspective (given an empty SomeClass class): Now, suppose that SomeClass.array is a
Tag: arrays
Test if an array is broadcastable to a shape?
What is the best way to test whether an array can be broadcast to a given shape? The “pythonic” approach of trying doesn’t work for my case, because the intent is to have lazy evaluation of the operation. I’m asking how to implement is_broadcastable below: or better yet: Answer I really think you guys are over thinking this, why not
numpy genfromtxt collapse recarray when data file has only one row
I am using genfromtxt function to read data from a csv file. Then I can access the array columns with e.g.: which then returns a 1-dimensional vector. Only when the source file has exactly one data row, the array is collapsed – its shape==() and therefore the vector returned by data[“My column name”] is not a vector but just a
Pop multiple items from the beginning and end of a list
Suppose I have a list of items like this: I want to pop two items from the left (i.e. a and b) and two items from the right (i.e. h,i). I want the most concise an clean way to do this. I could do it this way myself: Any other alternatives? Answer From a performance point of view: mylist =
All possible points of (latitude,longitude) from latitude/longitude coordinates in separate arrays
I have latitude and longitude coordinates in two separate arrays: How can I easily find all possible points made up of these coordinates? I’ve been messing around with itertools.combinations: but this doesn’t work for me because the points (71,75) and (43,42) are latitude/latitude and longitude/longitude pairs. What I would like to have is this: The a and b arrays will
Force NumPy ndarray to take ownership of its memory in Cython
Following this answer to “Can I force a numpy ndarray to take ownership of its memory?” I attempted to use the Python C API function PyArray_ENABLEFLAGS through Cython’s NumPy wrapper and found it is not exposed. The following attempt to expose it manually (this is just a minimum example reproducing the failure) fails with a compile error: My question: Is
Multidimensional arrays, using range, while simultaneously having a set start, stop, and step?
Oy mates, I am learning numpy on my own and getting a pretty good handle on it, a few concepts elude me even after reading the documentation though. I am trying to go through this matrix and make every second row have 10s all the way through it. The last line of code is incorrect. I know how to use
Remove rows in 3D numpy array
I need to remove some rows from a 3D numpy array. For exampe: and I want to remove the third row of both pages of the matrix in order to obtain something like: I have tried with but I can’t obtain what I need. Answer axis should 1.
elegant way of convert a numpy array containing datetime.timedelta into seconds in python 2.7
I have a numpy array called dt. Each element is of type datetime.timedelta. For example: how can I convert dt into the array dt_sec which contains only seconds without looping? my current solution (which works, but I don’t like it) is: I tried to use dt.total_seconds() but of course it didn’t work. any idea on how to avoid this loop?
unexpected result in numpy array slicing (view vs copy)
I’m trying to reduce the amount of copying in my code and I came across surprising behavior when dealing with numpy array slicing and views, as explained in: Scipy wiki page on copying numpy arrays I’ve stumbled across the following behavior, which is unexpected for me: Case 1.: As expected, this outputs: Case 2: When performing the slicing and addition