numpy-ndarray

Pytorch: Why is the memory occupied by the `tensor` variable so small?

谁都会走 提交于 2019-12-09 18:58:40
问题 In Pytorch 1.0.0, I found that a tensor variable occupies very small memory. I wonder how it stores so much data. Here's the code. a = np.random.randn(1, 1, 128, 256) b = torch.tensor(a, device=torch.device('cpu')) a_size = sys.getsizeof(a) b_size = sys.getsizeof(b) a_size is 262288. b_size is 72. 回答1: The answer is in two parts. From the documentation of sys.getsizeof, firstly All built-in objects will return correct results, but this does not have to hold true for third-party extensions as

Avoid overflow when adding numpy arrays

流过昼夜 提交于 2019-12-08 22:25:10
问题 I want to add numpy arrays with datatyp uint8. I know that the values in these arrays may be large enough for an overflow to happen. So I get something like: a = np.array([100, 200, 250], dtype=np.uint8) b = np.array([50, 50, 50], dtype=np.uint8) a += b Now, a is [150 250 44] . However, instead of an overflow I want values which are too large for uint8 to be the maximum allowed for uint8. So my desired result would be [150 250 255] . I could get this result with the following code: a = np

Intersect multiple 2D np arrays for determining zones

那年仲夏 提交于 2019-12-07 00:42:32
问题 Using this small reproducible example, I've so far been unable to generate a new integer array from 3 arrays that contains unique groupings across all three input arrays. The arrays are related to topographic properties: import numpy as np asp = np.array([8,1,1,2,7,8,2,3,7,6,4,3,6,5,5,4]).reshape((4,4)) #aspect slp = np.array([9,10,10,9,9,12,12,9,10,11,11,9,9,9,9,9]).reshape((4,4)) #slope elv = np.array([13,14,14,13,14,15,16,14,14,15,16,14,13,14,14,13]).reshape((4,4)) #elevation The idea is

Why does numpy mixed basic / advanced indexing depend on slice adjacency?

不羁岁月 提交于 2019-12-06 11:18:37
问题 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, ellipsis or newaxis. For example x[arr1,:,arr2] . The advanced indexes are all next to each other. For example x[...,arr1,arr2,:] but not x[arr1,:,1] since 1 is an advanced index in this regard. In

Constructing a ndarray of values from another ndarray containing dictionary keys

…衆ロ難τιáo~ 提交于 2019-12-06 09:48:43
I have a ndarray containing the dictionary keys arranged in a specific order. I want to create another ndarray containing the values of the respective keys. The order have to be maintained. Obvious approach is to iterate over the array containing the keys element by element but the problem is there is no way to know the shape of the array beforehand. Is it possible to flatten the ndarray of keys and iterate over it to generate flat ndarray of values and finally unravel it without harming the order? mydict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6} input_pattern = np.array([['a', 'f'], ['b',

Creating this numpy array in Python

假如想象 提交于 2019-12-06 06:51:04
I have the following numpy array import numpy as np a = np.array([1,2,6,8]) I want to create another numpy array from a such that it contains all the different possible sums of TWO elements of a . It's easy to show then that there are int(a.size*(a.size-1)/2) different possible sums, composed from: a[0] + a[1] a[0] + a[2] a[0] + a[3] a[1] + a[2] a[1] + a[3] a[2] + a[3] How can I construct a numpy array with the above sums as elements without using a double for loop (the only way I can think of it). For the above example, the output should be [3,7,9,8,10,14] MWE eff = int(a.size*(a.size-1)/2) c

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

非 Y 不嫁゛ 提交于 2019-12-05 19:43:59
问题 Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "<ipython-input-264-85078aa26398>", line 1, in <module> t3 = np.concatenate((t1,t2),axis=1) IndexError: axis 1 out of bounds [0, 1) why does it report that axis 1 is out of bounds? 回答1: Your title explains it - a 1d array does not have a 2nd axis! But having said that, on my system as on @Oliver W. s, it does not produce an error In

Perform numpy exp function in-place

元气小坏坏 提交于 2019-12-05 17:26:16
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? You can use the optional out argument of exp : a = np.array([3.4, 5]) res = np.exp(a, a) print(res is a) print(a) Output: True [ 29.96410005 148.4131591 ] exp(x[, out]) Calculate the exponential of all elements in the input array. Returns out : ndarray Output array, element-wise exponential of x . Here all elements of a will be replaced by the result of exp . The return value res is the same as a . No new array is created kmario23 Mike

Intersect multiple 2D np arrays for determining zones

纵然是瞬间 提交于 2019-12-05 05:02:08
Using this small reproducible example, I've so far been unable to generate a new integer array from 3 arrays that contains unique groupings across all three input arrays. The arrays are related to topographic properties: import numpy as np asp = np.array([8,1,1,2,7,8,2,3,7,6,4,3,6,5,5,4]).reshape((4,4)) #aspect slp = np.array([9,10,10,9,9,12,12,9,10,11,11,9,9,9,9,9]).reshape((4,4)) #slope elv = np.array([13,14,14,13,14,15,16,14,14,15,16,14,13,14,14,13]).reshape((4,4)) #elevation The idea is that the geographic contours are broken into 3 different properties using GIS routines: 1-8 for aspect

Why does numpy mixed basic / advanced indexing depend on slice adjacency?

大城市里の小女人 提交于 2019-12-04 17:23:05
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, ellipsis or newaxis. For example x[arr1,:,arr2] . The advanced indexes are all next to each other. For example x[...,arr1,arr2,:] but not x[arr1,:,1] since 1 is an advanced index in this regard. In the first case, the dimensions resulting from the advanced indexing operation come first in the result