3,933 questions
4
votes
1
answer
118
views
Numpy vectorising n-variable function by index
Given a closed-form function f(x,y,z), what is the most efficient way to fill up a 3D ndarray arr[z][y][x] with the values of f(x,y,z)?
For example, if I wanted a 3D ndarray arr[z][y][x] with each ...
4
votes
1
answer
172
views
Weird behavior in large complex128 NumPy arrays, imaginary part only [closed]
I'm working on numerical simulations. I ran into an issue with large NumPy arrays (~ 26 GB) on Linux with 128 GB of RAM. The arrays are of type complex128.
Arrays are instantiated without errors (if ...
2
votes
2
answers
142
views
Making a random grid that doesn't vary 'too much'
I'm making a meshgrid to represent background radiation levels, but I'd like it to be less noisy. My implementation is trivial, I've plotted
import numpy as np
x = np.linspace(-2, 2, 100)
y = np....
1
vote
1
answer
100
views
Numpy __array__ not working when return array with single item
Because plain dict is not adequate for inheritance, I design the following MyDict with UserDict in python standard libraries:
import numpy as np
from collections import UserDict
class MyUserDict(...
1
vote
1
answer
59
views
numpy.ndarray of seconds of a day to datetime format
I have a Python numpy.ndarray of seconds of the day with a bunch of decimal seconds:
import numpy as np
sec = [40389.66574375, 40390.12063928, 40391.32714992, 40392.64457077, 40393.48519607, 40394....
2
votes
2
answers
147
views
issue with sympy lambdify
I just want numerically integrate the Lorenz' famous 3 equations
I create this code that seems perfectly work:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import ...
3
votes
0
answers
217
views
Numpy IO seems to have an 2GB overhead in StorNex (cvfs) File System
TL;DR:
Initally I thought the numpy load functions doubles memory usage at peak
after some additional tests it seems like the underlying file system (StorNex [cfvs]) leads to a size-independent 2GB ...
1
vote
0
answers
111
views
PySide6 QImage conversion to PyQtGraph ndarray
I cannot generate a QImage with some text on it, keep it in memory, and then successfully display it on a pyqtgraph.ImageItem which needs it as an np.ndarray
import sys
import numpy as np
import ...
0
votes
2
answers
61
views
Identify identical vectors as part of a multidimensional dot product
I am wanting to identify identical vectors after a dot product calc. The below works for a single dimension but not multi-dimensionally.
Single Dimension
a = np.array([0.8,0.5])
b = np.array([0.8,0.5])...
5
votes
1
answer
143
views
Is there a Numpy method or function to split an array of uint64 into two arrays of uint32
Say I have an array as follows:
arr = np.asarray([1, 2, 3, 4294967296, 100], dtype=np.uint64)
I now want two arrays, one array with the lower 32 bits of every element, and one with the upper 32 bits ...
10
votes
1
answer
660
views
Arrays of size 0 in NumPy
I need to work with arrays that can have zeros in their shapes. However, I am encountering an issue. Here's an example:
import numpy as np
arr = np.array([[]])
assert arr.shape == (1,0)
arr.reshape((...
1
vote
0
answers
80
views
Python Numpy Crash without error or warning with exit code -1073740791 due to _multiarray_umath.cp312-win_amd64.pyd
Numpy library crash without console error or warning constantly
In Python Console see notifications either Process finished with exit code -1073741819 (0xC0000005) or Process finished with exit code -...
0
votes
1
answer
122
views
NumPy Stride Tricks: Is it possible to add the windows back into the original array size at the same location without for loops?
I'm currently trying to implement my own version of 2D Pooling (with channels included) in Python using only NumPy, but I've run into a bit of a roadblock with vectorizing the backpropagation process. ...
-1
votes
1
answer
49
views
Sort columns of numpy unitary matrix such that highest element of each column is on the diagonal
Take a unitary matrix U. I want to swap the columns such that the largest element of each column (in absolute value) is on the diagonal (modulo ties). What is the best way to do this in numpy?
0
votes
1
answer
75
views
Removing rows from numpy 3D array based on last element
What I'm trying to do is essentially removing all rows h,s in a 3D numpy array a if a[h,s,v] = some value for all v
More specifically, I have a loaded image from cv2 which contains some transparent ...
3
votes
2
answers
68
views
Project one index from another array
I have an array a of size (M, N, K).
And I have an array b of size (M, N) with integer values of [0, K-1].
How do I get the array... c of size (M, N), where
c[i, j] == a[i, j, b[i, j]]
in the simplest ...
1
vote
3
answers
68
views
Can I create a multidimensional array containing a unit matrix without nested loops? [closed]
Suppose I have a Numpy array n indices, where the first n-2 represents some counting indices and the last 2 indices represent a square MxM matrix. I want to initialize this structure so it will ...
0
votes
0
answers
38
views
np.zeros becomes faster with a very large array size [duplicate]
%timeit -n 1000 -r 7 np.zeros(100000000, dtype=bool)
gives 4.22 ms
%timeit -n 1000 -r 7 np.zeros(1000000000, dtype=bool) # 1 more zero = 10 times longer array
gives 3.13 μs (not even milliseconds, ...
1
vote
3
answers
81
views
Does Numpy return view or copy when combining slicing and advanced indexing?
The following snippet:
import numpy as np
x = np.arange(25).reshape(5, 5)
print(x.base)
y = x[:2, [0, 2]]
print(y.base)
outputs
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
2
votes
1
answer
151
views
How can I convert the datatype of a numpy array sourced from an awkward array
I have a numpy array I converted from awkward array by to_numpy() function, and the resulting array has the datatype: dtype=[('phi', '<f8'), ('eta', '<f8')]). I want to make it a regular tuple ...
4
votes
2
answers
168
views
How to Mark Repeated Entries as True Starting from the Second Occurrence Using NumPy?
Problem
I have a NumPy array and need to identify repeated elements, marking the second occurrence and beyond as True, while keeping the first occurrence as False.
For example, given the following ...
0
votes
1
answer
103
views
I'm trying to define NN model with Pytorch but there's a type error when I wanted to to convert y-train to y-train-tensor What should I do to fix it?
Here is my code. I import Wisconsin breast cancer database from kaggle to vscode. All dataset variables all numeric and float 64.
There is this type error:
(Exception has occurred: TypeError
can't ...
0
votes
1
answer
40
views
Index a batch of numpy vectors with a batch of numpy index matrices [duplicate]
If I have a vector vec, I can index it with a matrix as follows:
import numpy as np
vec = np.asarray([1,2,3,4]) # Shape (4,)
mat = np.asarray([[0,2],
[3,1]]) # Shape (2,2)
result =...
1
vote
2
answers
52
views
How to write a function for numpy array input whose action depends on the input's numerical value
The function has this mathematical form
f(x) = 1, if x<1
= g(x), for 1<=x<10
= 0, for x >=10
where g(x) is a simple function.
It is straightforward to write such a function if ...
3
votes
1
answer
63
views
Modify numpy array of arrays
I have a numpy array of numpy arrays and I need to add a leading zero to each of the inner arrays:
a = [[1 2] [3 4] [5 6]] --> b = [[0 1 2] [0 3 4] [0 5 6]]
Looping through like this:
for item in a:...