1

I have volume data from which I want to build an octave scale: 2^n : 1,2,4,8,16,32,64...etc n = 0,1,2,3,4,5,6...

The volume data:

Biovolume (µm³)   
0.238873
1.05251
2.82718

My code is:

import pandas as pd
import numpy as np

#data file
data = pd.read_csv("data.csv")

#define and sort biovolume
Biovolume = data['Biovolume'].as_matrix()
Biovolume = np.sort(Biovolume)

#octave scale:
min_scale = np.floor(np.log2(np.min(Biovolume))).astype('int')
max_scale = np.ceil(np.log2(np.max(Biovolume))).astype('int')

The minimum of the scale for the volume data is -3 and the maximum 2. Next step is to actually build the octave scale for the data:

octave_scale = np.power(2, range(min_scale, max_scale+1))

However, I get this error: ValueError: Integers to negative integer powers are not allowed.

I guess this means that it isn't possible to do 2^-3, 2^-2 and 2^-1. Why is this? Is there a solution?

Olga
  • 65
  • 2
  • 10
  • 1
    Possible duplicate of [Why can't i raise to a negative power in numpy?](https://stackoverflow.com/questions/43287311/why-cant-i-raise-to-a-negative-power-in-numpy) – Morse Jun 12 '18 at 16:37

1 Answers1

3

See this answer for an explanation of why np.power doesn't handle negative ints.

One solution is to use np.float_power, which is specifically designed to handle negative powers. From the docs:

The intent is that the function will return a usable result for negative powers and seldom overflow for positive powers.

example:

>>> np.power(5,-2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Integers to negative integer powers are not allowed.
>>> np.float_power(5, -2)
0.040000000000000001
sacuL
  • 49,704
  • 8
  • 81
  • 106