python - Why will numpy.round will not round my array?
问题描述
I am trying to round a numpy array that is outputted by the result of a Keras model prediction. However after executing numpy.round/numpy.around, there is no change.
The end goal here is for the array to get rounded down to 0 if below/equal 0.50 or rounded up if above 0.50.
The code is here:
from keras.models import load_model
import numpy
model = load_model('tried.h5')
data = numpy.loadtxt("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\indicatorout.csv", delimiter=",")
data = numpy.array([data])
print(data)
outdata = model.predict(data)
print(outdata)
numpy.around(outdata, 0)
print(outdata)
numpy.savetxt("AppData\Roaming\MetaQuotes\Terminal\94DDB309C90B408373EFC53AC730F336\MQL4\Files\modelout.txt", outdata)
The logs are also here:
Using TensorFlow backend.
[[1.19539070e+01 1.72686310e+01 2.24426384e+01 1.82771435e+01
2.23788052e+01 1.62105408e+01 1.44595184e+01 1.90179043e+01
1.71749554e+01 1.69194088e+01 1.89911938e+01 1.76701393e+01
5.19613740e-01 5.38522415e+01 9.64037247e+01 1.73570000e-04
4.35710000e-04 9.55710000e-04]]
[[0.4215713]]
[[0.4215713]]
Any help would be greatly appreciated, thank you.
解决方案
我假设您希望数组中的元素四舍五入到n
小数位。下面是这样做的说明:
# sample array to work with
In [21]: arr = np.random.randn(4)
In [22]: arr
Out[22]: array([-0.94817409, -1.61453252, 0.16566428, -0.53507549])
# round to 3 decimal places; note that `arr` is still unaffected.
In [23]: arr.round(decimals=3)
Out[23]: array([-0.948, -1.615, 0.166, -0.535])
# if you want to round it to nearest integer
In [24]: arr_rint = np.rint(arr)
In [25]: arr_rint
Out[25]: array([-1., -2., 0., -1.])
要使小数舍入就地工作,请指定out=
参数,如下所示:
In [26]: arr.round(decimals=3, out=arr)
Out[26]: array([-0.948, -1.615, 0.166, -0.535])
推荐阅读
- html - 如何在表格的 tr 下插入列表
- angular - 如何从另一个指令更改指令自定义属性的值
- wordpress - 如果自定义帖子类型有期限
- php - 从 WooCommerce 的变体下拉列表中消除隐藏或删除“服务”和“清除”
- python - 带有密度指示的 Pandas 散点图
- c - 如何使用 HAL 库读取和显示 STM32F4 的 ADC 值
- node.js - 如何仅允许从 Docker 中的前端访问后端
- android - 连接到 Firebase 失败
- javascript - 使用 vue.js 将 API 数据保存到数组中
- reporting-services - 远程链接服务器上的 SSRS 查询