Questions tagged [numpy-ndarray]

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy. Use this tag for questions related to this array type.

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

3757 questions
1
vote
2 answers

Broadcasting in numpy with multiple dimensions

I have an image with a black background that contains different shapes in different colors. I want to generate an image per shape, in which the shape is white and the background is black. I have been able to do this with numpy, but I would like to…
1
vote
4 answers

Explicit slicing across a particular dimension

I've got a 3D tensor x (e.g 4x4x100). I want to obtain a subset of this by explicitly choosing elements across the last dimension. This would have been easy if I was choosing the same elements across last dimension (e.g. x[:,:,30:50] but I want to…
Milad
  • 4,901
  • 5
  • 32
  • 43
1
vote
1 answer

Assigning elements to one array in a list of dictionaries

I have a list of dictionaries. Each dictionary contains a np.array as one of the values associated with a key. I am trying to change the values in a particular array of only one of the dictionaries in the list and am finding that each dictionary in…
1
vote
1 answer

Extracting specific columns in numpy array by condition

I have a homework assignment to extract a 2-dimensional numpy array out of another 2-dimensional np array by choosing specific columns by condition (not by range). So I have an array A with shape (3, 50000). I am trying to get a new array with shape…
Shaq
  • 303
  • 1
  • 10
1
vote
0 answers

how to read a complex data array

I am using Python to read a complex data structure from a .mat file. I have a data structure called complexData. If I use : print(str(complexData)) It shows array([[(array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0,…
djsg
  • 107
  • 1
  • 8
1
vote
3 answers

Numpy set all indexes to a value

I have a numpy ndarray a = np.ndarray((3,3)) and I want all of the indexes to start at the same value, e.g. 5: array([[5., 5., 5.], [5., 5., 5.], [5., 5., 5.]]) Note: I'm posting this Q&A style because every time I look this up I…
loganjones16
  • 492
  • 4
  • 19
1
vote
2 answers

Converting numpy array into pandas dataframe

I am getting error while converting the numpy array to pandas dataframe. suppose I am adding the following arrays a and b using np.vstack a=np.array((1,2,3,4)) b=np.array((11,22,33,44)) c=np.vstack((a,b)) pd.DataFrame(c) The last command give the…
lsr729
  • 752
  • 2
  • 11
  • 25
1
vote
2 answers

Numpy array - stack multiple columns into one using reshape

For a 2D array like this: table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]]) Is it possible to use np.reshape on table to get an array single_column where each column of table is stacked vertically? This can be accomplished by splitting…
rovyko
  • 4,068
  • 5
  • 32
  • 44
1
vote
2 answers

Get Diagonal indexes for a tall array

I have an array of size 5 x 3 x 3. I want to fill the diagonal of each 3 x 3 block with a number. How can I do this efficiently using numpy (A python library). My starting matrix is this: [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0…
1
vote
4 answers

Increasing the speed of finding an index of an item in list

I'm trying to speed up a program I've written, and after importing cProfile, I see that one function takes up a massive bit of computation time. It's this, which finds an numpy.ndarray in a list: def locate(arr, l ): for i in…
1
vote
1 answer

Creating a numpy array from a method using numpy array

I have a 2D numpy-array as input for a basic sigmoid-classifier. I would like the classifier to return an array with the probabilities. import numpy as np def sigmoid(x): sigm = 1 / (1 + np.exp(-x)) return sigm def p(D, w, b): …
Valentin Metz
  • 393
  • 6
  • 13
1
vote
1 answer

Rearranging the rows in the numpy and keeping track of each row

I have a 2000 numpy arrays of size 7 x 11. The rows are arranged in the following order of variables: pi, Tajima's D, Theta, dust kurtosis, J1/J2, J1, J2. That is first row always corresponds to pi, second row corresponds to Tajima's D and so…
Shafa Haider
  • 413
  • 2
  • 5
  • 13
1
vote
1 answer

How does NumPy ndarrary.view(...) work internally

I've used the following code to create a view but i want to understand how this view works under the hood, >>> x = np.array([(1, 2)], dtype=np.int8) >>> y = x.view(dtype=np.int16) where to find the source code for ndarray.view(...), I searched…
TheCodeCache
  • 820
  • 1
  • 7
  • 27
1
vote
1 answer

Is there a faster way to insert records to postgresql database while iterating over very large ndarray?

I am trying to loop over ndarray to record index and value of it to postgresql. Here is my code: for idx, val in enumerate(data): cur.execute("INSERT INTO public.spams(review_id, label, confidence_level, aoc, created_at) VALUES (%s, %s,…
Mert Koç
  • 45
  • 4
1
vote
1 answer

Numpy tensor implementation slower than loop

I have two functions that compute the same metric. One ends up using a list comprehension to cycle through a calculation, the other uses only numpy tensor operations. The functions take in a (N, 3) array, where N is the number of points in 3D space.…
Eden Trainor
  • 571
  • 5
  • 17
1 2 3
99
100