Questions tagged [numpy-ndarray]

4

votes
2

answer
19

Views

Python NumPy - angled slice of 3D array

Working in NumPy, I understand how to slice 2D arrays from a 3D array using this article. Depending on the axis I'd want to slice in: array = [[[0 1 2] [3 4 5] [6 7 8]] [[9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] Slicing would give me: i_slice = array[0] [[0 1 2]...
Enger Bewza
8

votes
2

answer
210

Views

Fill Bounding Boxes in 2d array

I have a 2D numpy array which looks like array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0....
Abhijeet Parida
0

votes
0

answer
4

Views

Issue in shifting 3D array using numpy.roll()

I am having an issue while shifting 3D array using NumPy roll function. My array is of this dimension u((3, nx, ny, nz)). Problem is that np. roll function is not working in the same way as it supposes to be. Generally, in roll function: axis = 0 defines the shifting of rows and axis =1 defines shi...
krish
1

votes
1

answer
311

Views

Are Numpy arrays hashable?

I've read that numpy arrays are hashable which means it is immutable but I'm able to change it's values so what does it exactly mean by being hashable? c=pd.Series('a',index=range(6)) c Out[276]: 0 a 1 a 2 a 3 a 4 a 5 a dtype: object This doesn't give me error then why it gives er...
Bhavana bharti
0

votes
0

answer
4

Views

Numpy ordered subtraction of elements with full vectorization

Imagine a mxn array a, and a 1xn array b, we want to subtract b from a so that b is subtracted from the first element of a, then maximum of zero and b-a[0] is subtracted from a[1], and so on... So: x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) a = np.repeat(x, 100000).reshape(10, 100000) b = np.repe...
AI_p
1

votes
2

answer
40

Views

Comparing object ids of two numpy arrays

I have been using numpy for quite a while but I stumbled upon one thing that I didn't understand fully: a = np.ones(20) b = np.zeros(10) print(id(a)==id(b)) # prints False print(id(a), id(b)) # prints (4591424976, 4590843504) print(id(a[0])==id(b[0])) # prints True print(id(a[0...
mlRocks
1

votes
2

answer
44

Views

How to write a numpy ndarray to a textfile?

Say that I got this ndarray using numpy that I want to write to a text file. : [[1 2 3 4] [5 6 7 8] [9 10 11 12]] This is my code: width, height = matrix.shape with open('file.txt', 'w') as foo: for x in range(0, width): for y in range(0, height): foo.write(str(matrix[x, y])) foo...
singrium
1

votes
1

answer
42

Views

Why is the use of numpy.nonzero enormously faster than looping through a numpy array?

Let's create a numpy ndarray of 10 million bools with all values initialized to True n=10000000 sample = np.ones(n, dtype=bool) Next we'll set a few values to False sample[1] = sample[5] = sample[12] = sample[25] = sample[50] = False The number of True values is now n-5 = 9999995 We can count the...
hagbard7000
1

votes
1

answer
74

Views

finding nearest neighbor for python numpy.ndarray in 3d-space

I have a numpy.ndarray of 3d-points, i.e. the np.shape of it is (4350,3) and such a second numpy.ndarray of 3d-points of np.shape (10510,3). Now I am trying to find the right python-package to calculate the nearest neighbors in the second array of the points in the first array as quickly as possibl...
Studentu
1

votes
1

answer
18

Views

Python | cv2.imshow() loading arrays as BGR?

I have recorded some data as npy file. And I tried to diplay the image (data[0]) to check if it makes sense with the following code import numpy as np import cv2 train_data = np.load('c:/data/train_data.npy') for data in train_data: output = data[1] # only take the height, width and channels of the...
MrYouMath
0

votes
0

answer
7

Views

Why numpy mixed basic / advanced indexing depends on slice adjacency?

I know similar questions have been asked before (e.g.), but AFAIK nobody has answered my specific question... My question is about the numpy mixed advanced / basic indexing described here: ... Two cases of index combination need to be distinguished: The advanced indexes are separated by a slice, ell...
Jesse Knight
0

votes
0

answer
9

Views

Numpy array losing dimensions when applying mask of same shape

I have created a 2D mask appropriately named mask to have the same shape as the array data, the array I want to apply it upon. However when I do so, the data loses it's shape and becomes 1D. I thought that as each level for axis 0 is identical (shown with creation of mask using loop comprehension) t...
Max Collier
-3

votes
1

answer
46

Views

How to convert Numpy array to Panda DataFrame

I have a Numpy array that looks like this: [400.31865662] [401.18514808] [404.84015554] [405.14682194] [405.67735105] [273.90969447] [274.0894528] When I try to convert it to a Panda Dataframe with the following code y = pd.DataFrame(data) print(y) I get the following output when printing it. Why do...
Yannick
2

votes
2

answer
71

Views

Numpy array to Tensor

I am changing the code created by numpy to tensorflow code. However, tensorflow does not support specifying each element, (eg x [i] = 7), boolean (eg.var [x
cgjung
0

votes
2

answer
15

Views

array is not callable in python “'numpy.ndarray' object is not callable”

I am working on a neural network and when i try to shuffle the two numpy.ndarray i get this error. I tried rechecking the shuffle function format and cannot find any faults with that. Please help train_images,train_labels = shuffle(train_images,train_labels) TypeError...
Charan Karthikeyan
2

votes
0

answer
26

Views

Why numpy.ndarray.data is toggle? [duplicate]

This question already has an answer here: Behavior of ndarray.data for views in numpy 2 answers Python numpy data pointer addresses change without modification 2 answers I'm using python 3.6.7 create a 1-D array, and print its data attribute, and found it is toggle. But why? >>> a = np.arange(12)...
FarmerLi
2

votes
2

answer
33

Views

Numpy arithmetic

I have the following code : import numpy as np x=np.array([[3, 5, 1]]) print(x.shape) #get (1,3) np.multiply(x.shape, 8) #get [ 8, 24] print(*x.shape) # get 1 3 np.array((np.multiply(*x.shape), 8)) #get [3, 8] Please explain why/how np.multiply(*x.shape, 8) get [3, 8] ?
ngBeginner
19

votes
4

answer
7.1k

Views

How does the axis parameter from NumPy work?

Can someone explain exactly what the axis parameter in NumPy does? I am terribly confused. I'm trying to use the function myArray.sum(axis=num) At first I thought if the array is itself 3 dimensions, axis=0 will return three elements, consisting of the sum of all nested items in that same position....
CodyBugstein
5

votes
1

answer
65

Views

3-D Matrix Multiplication in Numpy

I have to multiply two 2-D matrices, bob and tim, in Numpy Python 3.x bob.shape gives (2,4) tim.shape gives (7,4) This piece of code gives a 3-D matrix with a shape of (2,7,4) np.array([foo*tim for foo in bob]) It gives the output I want. But, I was wondering if there was a more elegant/faster way t...
3

votes
2

answer
41

Views

When is the size of an ndarray not fixed?

The numpy.ndarray documentation states that: An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. I'm surprised by the adjective usually here. I thought an ndarray is always of a fixed size. When is the size of an ndarray not fixed?
gerrit
1

votes
2

answer
205

Views

Group by with numpy.mean

How do I calculate the mean for each of the below workerid's? Below is my sample NumPy ndarray. Column 0 is the workerid, column 1 is the latitude, and column 2 is the longitude. I want to calculate the mean latitude and longitude for each workerid. I want to keep this all using NumPy (ndarray),...
salvationishere
3

votes
1

answer
33

Views

How to split a numpy array using a binary list?

If I have a numpy array say A = [[1,2],[3,4],[5,6],[7,8]] and a list L = [1,0,1,1] Is there a way to split A down axis0 based off of if they are a 1/0 in L? This would be my desired result: A1 = [[1,2],[5,6],[7,8]] A2 = [[3,4]]
Ammastaro
2

votes
1

answer
20

Views

ndarray rows are not displayed each one per line

I have this code: import numpy as np M = np.matrix([[-4.41991030e-05,-9.27712599e-04,3.75797779e-04,4.11804326e-04,1.08815444e-04], [-3.58432112e-04,-6.11583291e-04,1.18565910e-03,4.10337098e-04,9.96854953e-05], [-1.36865905e-03,1.19013259e-03,1.62785645e-03,1.85052363e-04,6.73256050e-05], [-0.00292...
singrium
2

votes
1

answer
34

Views

How can I add to each element of a numpy ndarray the sum of all its index values with python ?

I have a numpy ndarray, let's take an example (but it can be totally different): [[[0 0 0] [1 1 1] [0 0 0]] [[1 0 1] [1 0 1] [1 0 1]]] [[1 0 0] [1 1 0] [1 1 1]]] I want to add to each element the sum of its indexes in all dimensions. The result here would be: [[[ 0 1 2] [ 4 5 6] [ 6 7 8]] [[10...
Cépagrave
2

votes
3

answer
44

Views

How do I in a sense “juggle” elements in ndarrays?

What I mean by this is imagine you have an ndarray a with shape (2,3,4). I want to define another ndarray b with shape (3,2,4) such that b[i][j][k] = a[j][i][k] Matrix operations only apply to the last 2 index places. If there is a way to make matrix operations act on any 2 chosen index places then...
Ziad Fakhoury
2

votes
2

answer
30

Views

Merge 2 numpy Arrays with timestamps

I have two numpy ndarrays - each with their own timestamp-dimension. I want to merge them together. However the interval of their timestamps is not necessarily the same. Here's an example of what I mean: Array 1: names = ['timestamp', 'value'] a1 = [(1531000000, 0), (1532000000, 1), (1533000000, 2)...
RazorHail
2

votes
2

answer
32

Views

overriding partially an numpy array does not work

I tried to override a numpy array partially does anyone know how to do that in such comfort indexing way? Thanks!
malocho
4

votes
2

answer
47

Views

numpy conditional list membership element wise

I have a 2D numpy array: a = np.array([[0,1], [2,3]]) I have a list of values to keep: vals_keep = [1,2] I want to test for list membership for each element in the array. Something like: mask = a in vals_keep The result I want: array([[False, True], [True, False]])
svh160
6

votes
0

answer
79

Views

Numpy: strange different behavior of inplace and explicit operation

I want to operate on numpy arrays to use their indexing, and I want to include the 0-dimensional case. Now I came across a strange situation, where a type conversion appears, if I don't use in-place multiplication: In [1]: import numpy as np In [2]: x = 1.*np.array(1.) In [3]: y = np.array(1.) In [4...
greeeeeeen
4

votes
2

answer
819

Views

Perform numpy exp function in-place

As in title, I need to perform numpy.exp on a very large ndarray, let's say ar, and store the result in ar itself. Can this operation be performed in-place?
AreTor
2

votes
3

answer
1k

Views

Python Numpy repeating an arange array

so say I do this x = np.arange(0, 3) which gives array([0, 1, 2]) but what can I do like x = np.arange(0, 3)*repeat(N=3)times to get array([0, 1, 2, 0, 1, 2, 0, 1, 2])
Runner Bean
2

votes
3

answer
651

Views

How to delete a row based on a condition from a numpy array?

From the following array : test = np.array([[1,2,'a'],[4,5,6],[7,'a',9],[10,11,12]]) How can I delete the rows that contain 'a' ? Expected result : array([[ 4, 5, 6], [10, 11, 12]])
G F
7

votes
2

answer
7.3k

Views

Numpy zip function

If I have two numpy 1D arrays, for example x=np.array([1,2,3]) y=np.array([11,22,33]) How can I zip these into Numpy 2D coordinates arrays? If I do: x1,x2,x3=zip(*(x,y)) The results are of type list, not Numpy arrays. So I have do x1=np.asarray(x1) and so on.. Is there a simpler method, where I do n...
Håkon Hægland
4

votes
4

answer
12.9k

Views

Transforming a row vector into a column vector in Numpy

Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy?
MY_G
4

votes
4

answer
1.9k

Views

Compare a matrix against a column vector

Arrays 'A' and vector 'B' below are part of pandas dataframe. I have a large array A of form: 28 39 52 77 80 66 7 18 24 9 97 68 I have a vector B of form: 32 5 42 17 How do I compare pythonically each column of A against B. I am trying to get True/False values for A < B comparison to get t...
Zanam
3

votes
0

answer
71

Views

Partitioning the rows of a numpy array into buckets

I am writing code to process a large point cloud. I have been able to successfully vectorize most of my code to make it efficient, however, I cannot think of a good way of achieving this: I have an nx3 numpy array that represents points in 3d (x,y,z) with z >= 0. I want to create a list of length k,...
Amay Saxena
0

votes
2

answer
26

Views

Slicing a column from a 2D array

I have the following code : import numpy as np a = np.array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]]) a[:, 2:3] #get [[3],[9]) a[:, 2] #get [3, 9] The last line returns a 1D array. The only way to get a 2D back is do a[:, 2:3]. Intuitively, I'd think slicing 1 column out of a 2D array ge...
ngBeginner
3

votes
3

answer
71

Views

NumPy ufuncs are 2x faster in one axis over the other

I was doing some computation, and measured the performance of ufuncs like np.cumsum over different axes, to make the code more performant. In [51]: arr = np.arange(int(1E6)).reshape(int(1E3), -1) In [52]: %timeit arr.cumsum(axis=1) 2.27 ms ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops...
kmario23
3

votes
2

answer
91

Views

Why NumPy creates a view for x[[slice(None), 1, 2]]

In NumPy documentation for advanced indexing, it is mentioned that Also recognize that x[[1, 2, 3]] will trigger advanced indexing, whereas x[[1, 2, slice(None)]] will trigger basic slicing. A matrix is stored sequentially into the memory. I understand that it makes sense to make a view of x[[1, 2,...
Yousof
3

votes
2

answer
69

Views

Numpy […,None]

I have found myself needing to add features to existing numpy arrays which has led to a question around what the last portion of the following code is actually doing: np.ones(shape=feature_set.shape)[...,None] Set-up As an example, let's say I wish to solve for linear regression parameter estimates...
EntryLevelR

View additional questions