1 votes

(Error) TypeError: tuple indices must be integers or slices, not numpy.float64

I am making a program for a project and I need to do an average between a sine and a cosine:

def simul(pres, maxi, ctrl):
    prom = []
    segundos = range(ctrl, 1000+1)
    print("Resultado:")
    for s in segundos:
        print(f"{s} segundos")
        print(f"Presion: {pres} mmHg.\n")
        prom.append(pres)
        if pres < maxi:
            test1=np.sin(pres)
            test2=np.cos(pres)
            pres += 4
            prom = np.mean(test1, test2)
        else:
            test1 = np.sin(pres)
            test2 = np.cos(pres)
            pres -= 15
            prom = np.mean(test1, test2)

It is a function and I use this input data:

simul(10, 90, 0)

I get this error:

TypeError: tuple indices must be integers or slices, not numpy.float64

I don't know what to do.

0 votes

The error is due to incorrect use of numpy.mean should be in any case prom = np.mean((test1, test2)) . However, in prom = np.mean() you make prom is a scalar, but you define it as a list originally..... I don't quite know what you want to do with this, in the following iteration prom.append will fail...

0 votes

@FJSevilla it is supposed to be a graph and I wanted to make an average between the sine and cosine graph but it is my first program working with python but I will investigate more to solve my mistake thanks.

0 votes

Shouldn't you do prom.append(np.mean((test1, test2))) instead of prom.append(pres) ? The error that you originally commented is solved as I said, when doing np.mean(test1, test2) you really do np.mean(a=test1, axis=test2) This causes the failure by passing as axis a float and not an integer. It is solved by putting both values in an iterable, like a tuple. np.mean((test1, test2)) which is equivalent to np.mean(a=(test1, test2)) or a NumPy array.

1voto

FJSevilla Points 29084

The error is because you are incorrectly passing your values to numpy.mean. The method signature is:

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)

Where a is the array or an iterable object that can be converted into an array. This array contains the values whose average we want to calculate. The second argument is the axis (in case we have a multidimensional array) on which we want to apply the operation (average by rows, by columns, etc).

When in your code you do:

prom = np.mean(test1, test2)

you really do:

prom = np.mean(a=test1, axis=test2)

when trying to get the elements belonging to the specified axis, NumPy does a.shape[axis] If you have passed as axis test2 which is a float, you have the error shown. Anyway, even without the error, this would calculate the average of test1 (a float), which is as expected. test1 ...

The solution is to simply pass a NumPy array or other container (such as a tuple) with both values as the first argument:

np.mean((test1, test2))

On the other hand, a prom (which is a list), you add as a new item the value of pres in each iteration, when you should add in principle the value of the average. Moreover, when doing prom = np.mean(test1, test2) you reassign to the variable prom a scalar (the average), so from that moment on it stops pointing to a list and in the next iteration the call to prom.append will end in an exception.

from matplotlib import pyplot as plt
import numpy as np

def simul(pres, maxi, ctrl):
    promedios = []
    segundos = range(ctrl, 1000 + 1)
    print("Resultado:")

    for s in segundos:
        sinv = np.sin(pres * 2)
        cosv = np.cos(pres)
        promedio = np.mean((sinv, cosv))
        promedios.append(promedio)

        print(f"{s} segundos")
        print(f"Presion: {pres} mmHg.\n")
        print(f"Sin-Cos promedio: {promedio}")

        if pres < maxi:
            pres += 4
        else:
            pres -= 15

    plt.rcParams["toolbar"] = "None"
    plt.plot(segundos, promedios)
    plt.title("Aumento de PIC")
    plt.ylabel("PIC") 
    plt.show()

simul(10, 90, 0)

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X