numpy-dtype

Why do numpy array turns int into float

吃可爱长大的小学妹 提交于 2021-02-02 10:00:25
问题 I'm trying to fill an array with integers, but it seems like numpy array keep turning the integers into floats. Why is this happening and how do I stop this? arr = np.empty(9) arr[3] = 7 print(arr[3]) >>>7.0 回答1: NumPy arrays, unlike Python lists, can contain only a single type, which (as far as I know) is set at creation time. Everything you put into the array gets converted to that type. By default, the data type is assumed to be float . To set another type, you can pass dtype to the empty

Why is the dtype shown (even if it's the native one) when using floor division with NumPy?

大城市里の小女人 提交于 2020-07-15 01:49:24
问题 Normally the dtype is hidden when it's equivalent to the native type: >>> import numpy as np >>> np.arange(5) array([0, 1, 2, 3, 4]) >>> np.arange(5).dtype dtype('int32') >>> np.arange(5) + 3 array([3, 4, 5, 6, 7]) But somehow that doesn't apply to floor division or modulo: >>> np.arange(5) // 3 array([0, 0, 0, 1, 1], dtype=int32) >>> np.arange(5) % 3 array([0, 1, 2, 0, 1], dtype=int32) Why is there a difference? Python 3.5.4, NumPy 1.13.1, Windows 64bit 回答1: You actually have multiple

Why is the dtype shown (even if it's the native one) when using floor division with NumPy?

大兔子大兔子 提交于 2020-07-15 01:49:06
问题 Normally the dtype is hidden when it's equivalent to the native type: >>> import numpy as np >>> np.arange(5) array([0, 1, 2, 3, 4]) >>> np.arange(5).dtype dtype('int32') >>> np.arange(5) + 3 array([3, 4, 5, 6, 7]) But somehow that doesn't apply to floor division or modulo: >>> np.arange(5) // 3 array([0, 0, 0, 1, 1], dtype=int32) >>> np.arange(5) % 3 array([0, 1, 2, 0, 1], dtype=int32) Why is there a difference? Python 3.5.4, NumPy 1.13.1, Windows 64bit 回答1: You actually have multiple

Why is the dtype shown (even if it's the native one) when using floor division with NumPy?

浪子不回头ぞ 提交于 2020-07-15 01:48:27
问题 Normally the dtype is hidden when it's equivalent to the native type: >>> import numpy as np >>> np.arange(5) array([0, 1, 2, 3, 4]) >>> np.arange(5).dtype dtype('int32') >>> np.arange(5) + 3 array([3, 4, 5, 6, 7]) But somehow that doesn't apply to floor division or modulo: >>> np.arange(5) // 3 array([0, 0, 0, 1, 1], dtype=int32) >>> np.arange(5) % 3 array([0, 1, 2, 0, 1], dtype=int32) Why is there a difference? Python 3.5.4, NumPy 1.13.1, Windows 64bit 回答1: You actually have multiple

Why numpy.asarray return an array full of boolean

坚强是说给别人听的谎言 提交于 2020-01-06 05:24:13
问题 If I want to have an array filled whith 0 or 1 depending of the pixels value in a image, I write this : image = "example.jpg" imageOpen = Image.open(image) bwImage = imageOpen.convert("1", dither=Image.NONE) bw_np = numpy.asarray(bwImage) print(type(bw_np[0, 0])) Result : <class 'numpy.bool_'> Because of the .convert bilevel mode "1" , the array must be full of 1 and 0 . https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.convert When I try something simpler : bw_np =

Why numpy.asarray return an array full of boolean

江枫思渺然 提交于 2020-01-06 05:24:05
问题 If I want to have an array filled whith 0 or 1 depending of the pixels value in a image, I write this : image = "example.jpg" imageOpen = Image.open(image) bwImage = imageOpen.convert("1", dither=Image.NONE) bw_np = numpy.asarray(bwImage) print(type(bw_np[0, 0])) Result : <class 'numpy.bool_'> Because of the .convert bilevel mode "1" , the array must be full of 1 and 0 . https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.convert When I try something simpler : bw_np =

Pandas DataFrame - Aggregate on column whos dtype=='category' leads to slow performance

风格不统一 提交于 2020-01-03 13:09:37
问题 I work with big dataframes with high memory usage and I read that if I change the dtype on repeated values columns I can save big amount of memory. I tried it and indeed it dropped the memory usage by 25% but then I bumped into a performance slowness which I could not understand. I do group-by aggregation on the dtype 'category' columns and before I changed the dtype it took about 1 second and after the change it took about 1 minute. This code demonstrates the performance degradation by

What is the default dtype for str like input in numpy?

隐身守侯 提交于 2019-12-22 09:14:07
问题 I just wanted to confirm if the default data type for string is unicode while creating a ndarray . I could not find any reference which states this clearly. May be it is too obvious and doesn't need stating. When dtype is specified: >>> import numpy as np >>> g = np.array([['a', 'b'],['c', 'd']], dtype='S') >>> g array([[b'a', b'b'], [b'c', b'd']], dtype='|S1') Without specifying the dtype: >>> g = np.array([['a', 'b'],['c', 'd']]) >>> g array([['a', 'b'], ['c', 'd']], dtype='<U1') Also, what

Constructing np.array with overlapping fields in dtype

不问归期 提交于 2019-12-13 16:53:31
问题 I have a dtype as follows: pose_dtype = np.dtype([('x', np.float64), ('y', np.float64), ('theta', np.float64)]) Right now, I can write: pose = np.array((1, 2, np.pi), dtype=pose_dtype) I'd like to add an xy field to make this easier to work with. I can do this with: pose_dtype = np.dtype(dict( names=['x', 'y', 'theta', 'xy'], formats=[np.float64, np.float64, np.float64, (np.float64, 2)], offsets=[0, 8, 16, 0] )) However, now I can no longer construct the array using my previous method, and

How does numpy determine the array data type when it contains multiple dtypes?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 02:12:14
I am trying to do hands on the numpy, i cam across following datatype when used inbuilt method dtype.Following the few results i have got. Can you please explain what it means by u11 a1 = np.array([3,5,'p']) print(a1.dtype) o/p = >U11 Numpy's array objects that are PyArrayObject types have a NPY_PRIORITY attribute that denotes the priority of the type in which should be considered as the array's dtype in cases that it contains items with heterogeneous data types. You can access to this priority using PyArray_GetPriority API which Returns the __array_priority__ attribute (converted to a double)