float对象不能解释为整数

时间:2019-07-23 13:26:34

标签: python integer

# Convert array to float and rescale to voltage.
# Assume 3.3V / 12bits
# (we need calibration data to do a better job on this)
data = data.astype(np.float32) * (3.3 / 2**12)
if downsample > 1:  # if downsampling is requested, average N samples together
    data = data.reshape(num/downsample,downsample).mean(axis=1)
    num = data.shape[0]
    return np.linspace(0, (num-1)*1e-6*downsample, num), data, rate
else:
    return np.linspace(0, (num-1)*1e-6, num), data, rate`

在这一部分:data = data.reshape(num/downsample,downsample).mean(axis=1)中,我遇到此错误:

float object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:1)

Python3中的/符号等于浮点除法或“真”除法。因此结果永远是浮点数。

有两种方法可以解决此问题。但是,您首先应该确保可以将您的数据干净地划分为num*downsample(不带小数部分),否则仍然会导致错误:

data = data.reshape(num//downsample,downsample).mean(axis=1)

或:

data = data.reshape(int(num/downsample),downsample).mean(axis=1)

两个版本都获得结果编号的底版。因此,您要确保通过num/downsample获得的电话号码类似于“ x.0”,以便reshape不会抱怨。