3,933 questions
-1
votes
1
answer
2k
views
Casting Numpy array to np.int_ throws RuntimeWarning after multiple repetitions
I came across a weird behavior with numpy, for which I do not have a good explanation.
In one function, I do some operations on a copied numpy array. There is no issue when I run this code once. ...
0
votes
1
answer
76
views
Python - Add the smallest value from each row of an ndarray to a list. If the smallest number is already in list , then add next smallest
Say I have a 4x4 array, each row already sorted by smallest value. I want to create a list with the minimum values from each array row without repeating a value. So if a minimum value is already in ...
0
votes
1
answer
445
views
Adding a constant value to a column within a 2D numpy array
I am working with nd arrays which contain coordinate data for multiple contours in [x, y, z] space.
I am trying to add x, y, and z offsets to the lines based on other conditions. The offsets will be ...
0
votes
1
answer
143
views
Mean absolute value along all columns of a 3D Numpy array
I always get find myself in state of confusion when dealing with a multi-dimensional array.
Imaging having the following array of arrays, where each array contains feature importance scores (3-...
1
vote
1
answer
113
views
How to compute the moving average over 3D array with a step size?
I need to calculate a moving average over a 3D array with a step size set by me. What I am doing right now is
img = np.ones(10,10,50)
img_new = bottleneck.move.move_mean(img, window=5, axis=2)
...
0
votes
2
answers
94
views
Replace numpy.array values with randomly selected values from other rows in the array only
I have the following array of shape (A, B) where A=4 and B=2 here:
[[1 1] [2 2] [3 3] [4 4]]
I want to modify the array so each row consists of randomly selected values from the other rows, with no ...
0
votes
0
answers
52
views
Python numpy array - address of view element
In below example x is view of y.
Why id(x[1,1]) and id(y[1,1]) are different?
I experimented with array views, but I don't understand how it works.
import numpy as np
x=np.array([[1,2,3],[4,5,6],[7,8,...
0
votes
1
answer
50
views
Is there an efficient way to use the content of one array as a lookup table to determine the content of a second array?
I have an array of points:
centers = array([0,0,0,0],
[0,1,0,0],
[0,0,0,1],
[1,0,0,0])
Using scikit.ndimage.distance_transform_edt(), the index ...
0
votes
1
answer
123
views
Numpy comparing nested arrays
I am considering arrays of shape (n,2) for arbitrary n. I want to check whether the two-element sub-arrays match. As an example:
import numpy as np
a=np.array([[1,0],[2,0]])
b=np.array([[1,0],[2,0]])
...
0
votes
1
answer
936
views
With a Huggingface dataset, how do you set specific features as numpy arrays, so they are ndarrays when loaded back?
When using load_dataset() to load a Mozilla Common Voice (v11) dataset, the resulting dataset (ds) has audio.arrays as numpy arrays. I don't know how to reproduce this.
How do you set just one feature ...
0
votes
2
answers
110
views
Ndarray dtype multidimensional array - how to choose the first element of each array element?
I have an multidimensional ndarray of tuples, which further consists of two elements - first is another array and second is a dict. I am after the first element of each of these tuples. Basically make ...
0
votes
1
answer
494
views
How to convert from cv2 findContours to np array when there are multiple arrays in the contours?
I have a picture of a simple black and white mandala. I'm using Python and cv2 and numpy. My goal is to get the contour as X, Y coordinates and save them in a gcode file. The problem is that I get as ...
0
votes
0
answers
72
views
Error while training a neural network model
Here is the 2 files. I am getting error in train_model.py file while using the fit method
Model.py
def alexnet(width,height,outputs,lr):
model = Sequential()
model.add(Conv2D(filters=64, ...
0
votes
1
answer
314
views
Scipy sosfilt ValueError ndarray is not C-contiguous
sosfilt from the scipy=1.9.3 library is giving me a strange ValueError when inputting a numpy=1.23.4 array.
MWE:
import numpy as np
from scipy.signal import sosfilt
fs=48000
rng = np.random....
0
votes
1
answer
225
views
psycopg2: Insert numpy Array with NaNs into Table
The following code works fine to insert multidimensional numpy arrays into a table
def adapt_numpy_array(numpy_array):
return AsIs(numpy_array.tolist())
register_adapter(np.ndarray, ...
3
votes
0
answers
349
views
Image clustering with extracted feature vectors on a large dataset
I've been trying to use a number of clustering techniques on an extremely large image dataset (around 1 million images). The process is quite standard - e.g., using a pretrained CNN (VGG19 in this ...
0
votes
2
answers
165
views
Indexing array by its value to its nearest non-integer step?
My apologies if I haven't phrased this properly. I have an array of ~10^7 monotonically increasing elements:
example_array = np.array([10, 10.5, 13, 15, 20, 35, 37.1, 40, 50])
I'd like to find the ...
1
vote
1
answer
138
views
Numpy's frompyfunc passed with array of shapes (2,4) and (2,1) executes 8(2*4) times, instead of 2 times
I have a function x as shown below that takes two numpy arrays as the input and I want to get back a boolean value upon some computation.
import numpy as np
def x(a,b):
print(a)
print(b)
#...
-1
votes
2
answers
163
views
Numpy - Conditional Indexing using index
So I have a 2D Numpy array i want to modify with classical conditional indexing:
array[array>threshold] = np.nan
But i want to implement a conditional that utilizes the index of the element, so as ...
0
votes
2
answers
56
views
Is there a pure NumPy way to create an array of ND indicies and values?
This script creates an array b of indices and values of given array a, which is useful for example in conversion of 2.5D height map to a point cloud:
import numpy as np
a = np.arange(12).reshape(3, 4)...
1
vote
1
answer
74
views
Assign consecutive numbers to numpy array locations satisfying certain condition
This Python script:
import numpy as np
a = np.random.rand(8, 8)
b = np.full_like(a, -1)
n = 0
for i, val in np.ndenumerate(a):
if val < 0.666:
b[i] = n
n += 1
creates array b with ...
0
votes
1
answer
529
views
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found object
I am trying to test batches in my training set. My training set is in a .tsv file with 3 columns: Quality( 1 indicates the two sentences are similar and 0 is the opposite), #1 String (1st String), #2 ...
-1
votes
1
answer
97
views
NumPy will not slice array column, only the row [closed]
I am having trouble slicing the below NumPy array. I want to pull out the first column. I have tried almost every combination and cannot slice the column I want; it always slices the row.
print(...
-1
votes
3
answers
115
views
Print elements when the difference is greater/lesser than a condition in numpy array
in a numpy array , I have to print out elements when the difference between elements is > or < 1
Basically it should print the element when the difference is > or < 1
Input : arr=np.array([...
0
votes
3
answers
80
views
Select elements from a matrix with numpy
I try to use numpy to fast text analysis. Exactly to collocations analysis.
Let's suppose I have the following string which I converted into a numpy array:
text = np.array(['a', 'b', 'c', 'd', 'e', 'b'...
1
vote
1
answer
339
views
Why does operating on a 0d Numpy array give a Numpy float?
Let's say I have a 0d Numpy array:
x = numpy.asarray(1.)
This has type numpy.ndarray. But now operate on x:
y = 1.*x
This now has type numpy.float64. The same thing is true when using all Numpy ...
1
vote
1
answer
167
views
Calculate RMSE in two matrix when they include Nan value in Python Code
I am a beginner at using Python code, and I have a problem with calculating the RMSE in two matrixes when they include Nan values.
For example, I have two matrixes, which include a couple of columns ...
0
votes
1
answer
72
views
Multiplying 2D matrices to get a 3D matrix
I have two matrices A and B of dimensions (n_m, n_u) and (n_m, n). I want a 3D matrix with dimensions (n, n_m, n_u) such that the first column of B is multiplied (element-wise) with every column of A ...
1
vote
3
answers
1k
views
Faster Alternative to numpy.unique
I'm trying to find unique pixels in a numpy ndarray of shape 400, 800, 3. I can get exactly what I want with np.unique(im.reshape(-1, 3), axis=0) however np.unique is too slow for my application ...
0
votes
3
answers
220
views
Given two arrays A and B, how to efficiently combine them so that the output is an array in which each element is a tuple (Aij,Bij)?)
Consider two arrays A and B, both of dimension NxN.
I wish to generate a new array NxN such that each new element is a tuple (or list, doesn't really matter) of the type (A_ij,B_ij).
I can do it by ...
1
vote
0
answers
37
views
Numpy slicing/indexing changes order of axis [duplicate]
I was working on a code and noticed that numpy indexing and slicing doesn't work as I expected. For example,
x = np.random.randn(1,5,3,4)
y = x[0, :, :, [1,2]]
y.shape
I expected y.shape to be (5,3,2)...
0
votes
1
answer
42
views
How to use a mask to limit broadcasted operations between two numpy arrays?
I have an array like so:
data = np.array([
[[10, 10, 10],
[10, 10, 10],
[10, 10, 10]],
[[20, 20, 20],
[20, 20, 20],
[20, 20, 20]],
[[30, 30, 30],
[30, 30, 30],
...
1
vote
1
answer
120
views
Numpy ndarray ValueError, dimensions don't match
I am having a Problem with the np.append and np.vstack
I am trying to combine a 48x48 grayscale face image with an array of the landmarks from the face.
So I can have an Array with the images and the ...
2
votes
3
answers
125
views
Why does np.arange(0.6, 0.68, 0.01) include 0.68? [duplicate]
I was testing a code in the console and got this:
>>> import numpy as np
>>> np.arange(0.6, 0.68, 0.01)
array([0.6 , 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68])
>>> np....
1
vote
1
answer
7k
views
TypeError: Dimensions of C (50, 49) should be one smaller than X(50) and Y(50) while using shading='flat' see help(pcolormesh)
My outputs T_results and c_results have values of 0 at the end, which I do not want so I sliced theses arrays excluding their last column and got their respective arrays T_results_cleaned = T_results[:...
1
vote
3
answers
471
views
Storing data not aligned to bytes without padding in Python (NumPy)
I have image data that is represented with 10 or 12-bit integers and I would like to save this data to disk without writing the unnecessary 6 or 4 zeros of padding when using 16-bit integer to ...
1
vote
0
answers
171
views
Is there a multicore option to compress a NumPy array?
I'm using np.savez_compressed() to compress and save a single large 4D NumPy array, but it uses only one CPU core. Is there an alternative, which can use many cores? Preferably something simple ...
0
votes
0
answers
42
views
View numpy array of pairs as tuples [duplicate]
I have an array of pairs of size (N,2), and I want to convert them to tuples to be able to perform an isin operation.
I tried using view, but it doesn't seem to work:
coo = np.array([[1,2],[1,6],[5,3],...
1
vote
0
answers
119
views
Save data from cpp into bytes that can be pickle loaded by Python and seen as numpy ndarray
PROBLEM
Let's say I have a python script that works with a numpy ndarray and then pickle.dumps it into bytes. Another python script would then pickle.loads those bytes and see the object as a numpy ...
3
votes
2
answers
412
views
Is there a numpy function similar to np.isin that allows for a tolerance instead of requiring that values be identical?
I have two arrays of different sizes and want to determine where elements of one array can be found in the other array. I would like to be able to allow for a tolerance between elements. The goal ...
1
vote
1
answer
103
views
Numpy Array: Efficiently find matching indices, but with possible missing corresponding values
I have a question very similar to this post. Essentially I start with 2 2d arrays(of possibly different width), with a bunch of rows where the leftmost column acts as an effective index and I would ...
1
vote
2
answers
45
views
Turn close proximity elements of a 2d NumPy array into that proximity average
What I have is an array of numpy arrays (a 2d numpy array).
my_array = [
[1, 2, 3, 4]
[1, 2, 3, 4]
[2, 3, 4, 5]
[2, 3, 4, 5]
]
and I want to turn it into:
...
4
votes
2
answers
59
views
How to find the position of same rows from one 2D array to another 2D array?
import numpy as np
# Create two sample dataframes
df1 = np.array([[0.000000,0.000000,0.000000],
[0.090000,0.000000,0.000000],
[0.190000,0.000000,0.000000],
[0.280000,0.000000,0.000000],
[0.380000,0....
-1
votes
1
answer
486
views
What does the 1.-np.arange() mean in numpy?
I have the following line of code given to me as part of an example in my python course
np.sqrt(1.-np.arange(0,1.01,0.01)**2)
But there have been no explanations given. I am confused as to what the ...
0
votes
1
answer
66
views
I would like to transform an Excel calculation into a Python program with the same results
A represents tasks that need to be processed on each machine. A total of 5 tasks are given T0 to T4.
B are the Setup values required to prepare a machine for processing a task that has been added ...
0
votes
1
answer
44
views
Appending a series of 2d numpy array to create a 3d numpy array
I thought this was an easy problem, but I have been struggling with it.
I have a dataframe with 4 columns (Open, High, Low, Close).
I need to iteratively select
for 100 times
a batch of 75 rows
each ...
1
vote
1
answer
69
views
Advanced indexing/subsetting
Using NumPy, given a and a smaller boolean array b:
a = np.array([False, True, True, False, True])
b = np.array([False, True, False])
how to obtain result c?
c = np.array([False, False, True, False, ...
0
votes
2
answers
230
views
Appending arrays to a list overwrites existing values
As a newbie in python I want to populate a list with images, in form of arrays. If I add elements, they overwrite the existing values. How can I add elements into a list?
Example code:
import numpy as ...
0
votes
2
answers
276
views
How to serialize a list of ndarray?
I have a script which reads a video frame by frame and accumulate them in a deque.
labeled_frame_dequeue = [numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]
I am trying to pass this list ...
1
vote
1
answer
165
views
Split a NumPy array of strings into 2 new NumPy arrays based on index positions provided by another NumPy array
This is my NumPy array:
og_arr = [['5mm', '45"', '300 mm WT', 'Nan'], ['50mm', '3/5"', 'Nan', 'Nan']]
I have written some logic that is able to identify at which index position the mm/inch ...