Python的数学模块包含像floor&这样的便捷函数ceil.这些函数采用浮点数并返回低于或高于它的最接近的整数.但是,这些函数将答案作为浮点数返回.例如:
import math
f=math.floor(2.3)
Run Code Online (Sandbox Code Playgroud)
现在f返回:
2.0
Run Code Online (Sandbox Code Playgroud)
从这个浮点数中获取整数的最安全的方法是什么,而不存在舍入错误的风险(例如,如果浮点数相当于1.99999),或者我应该完全使用另一个函数?
我需要找到一个独特的行numpy.array.
例如:
>>> a # I have
array([[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0]])
>>> new_a # I want to get to
array([[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)
我知道我可以在阵列上创建一个集合并循环,但我正在寻找一个有效的纯numpy解决方案.我相信有一种方法可以将数据类型设置为void然后我可以使用numpy.unique,但我无法弄清楚如何使其工作.
我偶然发现了np.float32或np.float64的分区结果,我不明白
我在Python 3.6.7中使用numpy 1.15.4
>>> import numpy as np
>>> np.float32(0)//1
-0.0
Run Code Online (Sandbox Code Playgroud)
我知道,我可以通过abs()等解决方法,但为什么我首先得到"-0.0"而不是"0.0"?
在xarray中使用DataArray对象,找到具有值的所有单元格的最佳方法是!= 0.
例如在熊猫中我会这样做
df.loc[df.col1 > 0]
Run Code Online (Sandbox Code Playgroud)
我的具体例子我正在试着看三维脑成像数据.
first_image_xarray.shape
(140, 140, 96)
dims = ['x','y','z']
Run Code Online (Sandbox Code Playgroud)
查看xarray.DataArray.where的文档,看起来我想要这样的东西:
first_image_xarray.where(first_image_xarray.y + first_image_xarray.x > 0,drop = True)[:,0,0]
Run Code Online (Sandbox Code Playgroud)
但我仍然得到零的数组.
<xarray.DataArray (x: 140)>
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., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., …Run Code Online (Sandbox Code Playgroud) python ×4
numpy ×2
arrays ×1
integer ×1
math ×1
pandas ×1
python-2.x ×1
python-3.x ×1
unique ×1