numpy

Deleting numpy array elements from a 3d numpy array with given array of indices

六眼飞鱼酱① 提交于 2021-02-05 05:51:10
问题 I have a numpy array: arr = 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]]]) and an array of indices, ind = array([0, 1, 1]) What I would like to do is for the i th row in arr , delete the ind[i] th row in arr[i] using only numpy.delete. So in essence a more pythonic way to do this: x, y, z = arr.shape new_arr = np.empty((x, y - 1, z)) for i, j in enumerate(ind): new_arr[i] = np.delete(arr[i], j, 0) arr = new

Trouble with “ cv2.imshow ()” function

别等时光非礼了梦想. 提交于 2021-02-05 05:45:09
问题 I installed openCV and numpy libraries in python 2.7. I've tested them using commands import cv2 and import numpy and it compiled. But when I use the cv2.imshow('frame', ----) function it displays a window but not displaying the image. And it's showing " frame is Not Responding". So, I tried with matplotlib functions for displaying image and it worked. I inserted cv2.imshow function in the 2nd case and it worked. Versions [Python-2.7.10, OpenCV-2.4.11] Below is the code, Case 1: Not Working

Skip loop if a function is taking too long?

我是研究僧i 提交于 2021-02-05 05:19:08
问题 I have Python code which is taking too long, and I would like to stop and skip execution of this function if it takes longer than a few seconds. For example, the function I want to time is: batch_xs, batch_ys = train_loadbatch_from_lists(batch_size) In some instances this function call takes too long, and I would like to cancel it. I'm looking for something like this: if time for batch_xs, batch_ys = train_loadbatch_from_lists(batch_size) > 20 seconds: then skip with reference to this post. I

Boolean numpy arrays with Cython

∥☆過路亽.° 提交于 2021-02-05 04:01:13
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Boolean numpy arrays with Cython

夙愿已清 提交于 2021-02-05 03:55:50
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Boolean numpy arrays with Cython

亡梦爱人 提交于 2021-02-05 03:55:28
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Comparing numpy array of dtype object

天涯浪子 提交于 2021-02-05 01:56:53
问题 My question is "why?:" aa[0] array([[405, 162, 414, 0, array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'], dtype=object), 0, 0, 0]], dtype=object) aaa array([[405, 162, 414, 0, array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'], dtype=object), 0, 0, 0]], dtype=object) np.array_equal(aaa,aa[0]) False Those arrays are completly identical. My minimal example doesn't reproduce this: be=np.array([1],dtype=object) be array([1], dtype=object) ce=np.array([1],dtype=object) ce

How to speed up multiply and sum operations in numpy [duplicate]

北城余情 提交于 2021-02-04 21:31:08
问题 This question already has an answer here : Dot product between 2D and 3D numpy arrays (1 answer) Closed 5 months ago . I need to solve a Finite Element Method problem and have to calculate the following C from A and B with a large M ( M>1M ). For example, import numpy as np M=4000000 A=np.random.rand(4, M, 3) B=np.random.rand(M,3) C = (A * B).sum(axis = -1) # need to be optimized Could anyone come up with a code which is faster than (A * B).sum(axis = -1) ? You can reshape or re-arrange the

Outer product as string?

六月ゝ 毕业季﹏ 提交于 2021-02-04 20:57:58
问题 I am trying to do the following. The outer product of an array [a,b; c,d] with itself can be described as a 4x4 array of 'strings' of length 2. So in the upper left corner of the 4x4 matrix, the values are aa, ab, ac, ad. What's the best way to generate these strings in numpy/python or matlab? This is an example for just one outer product. The goal is to handle k successive outer products, that is the 4x4 matrix can be multiplied again by [a,b; c,d] and so on. 回答1: You can obtain @Jaime's

Outer product as string?

六月ゝ 毕业季﹏ 提交于 2021-02-04 20:57:52
问题 I am trying to do the following. The outer product of an array [a,b; c,d] with itself can be described as a 4x4 array of 'strings' of length 2. So in the upper left corner of the 4x4 matrix, the values are aa, ab, ac, ad. What's the best way to generate these strings in numpy/python or matlab? This is an example for just one outer product. The goal is to handle k successive outer products, that is the 4x4 matrix can be multiplied again by [a,b; c,d] and so on. 回答1: You can obtain @Jaime's