MATLAB:fix() ゼロ方向への丸め > Numpy:実装 > 正: floor(), 負: ceil()


動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)

MATLAB

>> xs = [3.14 -2.718]

xs =

    3.1400   -2.7180

>> fix(xs)

ans =

     3    -2

Numpy

上記に対して以下のように実装した。

for実装

https://ideone.com/H8xPlf (Python3.5)

import numpy as np


def get_fix(xs):
    res = []
    for elem in xs:
        if elem >= 0.0:
            res += [np.floor(elem)]
        else:
            res += [np.ceil(elem)]
    return res

xs = [3.14, -2.718]
res = get_fix(xs)
print(res)
run
[3.0, -2.0]

内包表記実装

https://ideone.com/YjJwOh (Python3.5)

import numpy as np


def get_fix(xs):
    res = [np.floor(e) if e >= 0 else np.ceil(e) for e in xs]
    return res

xs = [3.14, -2.718]
res = get_fix(xs)
print(res)
run
[3.0, -2.0]