我正在使用numpy.mean来计算numpy数组的平均值,但我收到一个错误(zip参数#2必须支持迭代)和NAN值。
valueList最初是一个包含unicode字符串的列表 - 从python复制: valueList = {list} [u'273',u'275',u'255',u'250',u'296',u'282',u'259'....]
使用列表推导转换为浮点数之后,它在python中显示如下: values = {list} [273.0,275.0,255.0,250.0,296.0,282.0,259.0 ....]
我做错了什么?
import numpy as np
# valueList is bult from pulling apart a JSON object containing strings
# Looks like this:
# valueList = {list}[u'273', u'275', u'255', u'250', u'296', u'282', u'259',....]
# Turning all members of the valueList into float objects
valueList = [float(value) for value in valueList]
# valueList now looks like:
# valueList = {list}[273.0, 275.0, 255.0, 250.0, 296.0, 282.0, 259.0,....]
# converting the list into a numpy like array
values = np.asarray(valueList)
# values = {ndarray}[ 273. 275. 255. ..., 192. 180. 176.]
# taking the log of the passed value - **Works fine!** - but I do get this warning:
# RuntimeWarning: invalid value encountered in log, lnValues = np.log(values)
lnValues = np.log(values)
# lnValues = {ndarray}[ 5.6094718 5.6167711 5.54126355 ..., 5.25749537 5.19295685 5.170484 ]
# calculating the mean and sd of the lnValues - **Doesnt work!?!**
lnValuesMean = np.mean(lnValues)
# lnValuesMean = {flost64} nan
# and this gives this error:
# zip argument #2 must support iteration
lnValuesSD = np.std(lnValues)
编辑:
首先,我为添加然后删除内容而道歉 - 我无法确定要包含多少信息,并且通常无法包含准确的代码示例/代码段。
因此,假设存在NAN值,我将代码更改为包括:
# converting the list into a numpy like array
values = np.asarray(valueList)
# values = {ndarray}[ 273. 275. 255. ..., 192. 180. 176.]
# removing NANs
values = values[np.logical_not(np.isnan(values))]
但那仍然没有用,所以我假设有负值或零值,所以我也补充说:
# converting the list into a numpy like array
values = np.asarray(valueList)
# values = {ndarray}[ 273. 275. 255. ..., 192. 180. 176.]
# removing NANs
values = values[np.logical_not(np.isnan(values))]
# removing possible negative values and zero values
values = values[values>0.0]
似乎最终过滤,删除可能的'0'值,是有效的。在我的数据集中不应该有任何零值(在~3500中总共有6个),但是很好。
答案 0 :(得分:0)
因此,假设存在NAN值,我将代码更改为包括:
# converting the list into a numpy like array
values = np.asarray(valueList)
# values = {ndarray}[ 273. 275. 255. ..., 192. 180. 176.]
# removing NANs
values = values[np.logical_not(np.isnan(values))]
但那仍然没有用,所以我假设有负值或零值,所以我也补充说:
# converting the list into a numpy like array
values = np.asarray(valueList)
# values = {ndarray}[ 273. 275. 255. ..., 192. 180. 176.]
# removing NANs
values = values[np.logical_not(np.isnan(values))]
# removing possible negative values and zero values
values = values[values>0.0]
似乎最终过滤,删除可能的'0'值,是有效的。在我的数据集中不应该有任何零值(在~3500中总共有6个),但是很好。