Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python 3.9.9 – How to calculate the log of division of two items (t and t+1) from one array

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading