Home > OS >  Python 3.9.9 - How to calculate the log of division of two items (t and t 1) from one array
Python 3.9.9 - How to calculate the log of division of two items (t and t 1) from one array

Time:01-21

I've stumbled onto a simple problem that blocks me from any further progress. I'd like to calculate the log of the division of two items in a loop and then save each result to a new array.

So I have my dataset that is already scaled with Sklearn MinMaxScaler, and the precise calculations I'd like to perform are these:

logs = np.empty(12530)
for i in data_prepared - 1:
    logs[i] = np.log10(data_prepared[i   1] / data_prepared[i])

But both math.log10() and np.log10 return Index Error:

IndexError: arrays used as indices must be of integer (or boolean) type

How can I perform the calculations I need?

EDIT: data_prepared dtype is float32

CodePudding user response:

If data_prepared is a numpy.array(), your loop will return the values of this list, not the indices. Your error hints that this is what is happening, since the values from data_prepared are not necessarily integers nor booleans.

for i in data_prepared - 1:

is the same as

for j in range(len(data_prepared)):
    i = data_prepared[j] - 1

So you should do

logs = np.empty(12530)
for i in range(len(data_prepared) - 1):
    logs[i] = np.log10(data_prepared[i   1] / data_prepared[i])

Or use dot products as pointed out in the comments.

  •  Tags:  
  • Related