Greck9
Greck9

Reputation: 29

How to calculate mean of array with type numpy64float?

I have a very big cycle in which I need to calculate mean. In this cycle I generate 1000 possible oddsratio. Just look at the end of my code:

oddsratio=(cross_tab[1][1]*cross_tab[0][0])/(cross_tab[1][0]*cross_tab[0][1])
lst = [oddsratio]
np_lst = np.array(oddsratio)
new_array = np_lst.astype(type('float', (float,), {}))
mean_arr = np.mean(new_array)
print(sum(new_array)/len(new_array))

the output (many numbers) of oddsratio is numpyfloat64 and I got error using np.mean() or statistics.mean() I tried to convert it in np.array, but got error again: 'float' object is not iterable If i just run print(mean_arr) it gives me all numbers of oddsratio

Initially, I tried to convert in list, but output created for every value in list with additional '[]', for example I had lst = [[0.9][0.7][0.6]....]. I tried to remove inner list, but it does not work also.

Output of oddsrato

I never worked with numpy previously,and the mistake is not understandable for me. Please could anybody give me advice how to fix it?

Upvotes: 0

Views: 108

Answers (2)

folen gateis
folen gateis

Reputation: 2012

the numpy reference for np.mean says that np.mean return value is an ndarray, but if you don't specify the axis argument it will return a single float. that's why sum(mean_arr) throws a TypeError (object not iterable).

i don't understand when you say that print(mean_arr) gives you all numbers of oddsratio: the screen you posted shows print(oddsratio). mean_arr is just a float. either you're always printing oddratio, or you're not showing us the entirety of your code.

Upvotes: 1

Joseph Bradshaw
Joseph Bradshaw

Reputation: 149

Have you tried to call mean() on your array?

np_lst.mean()

Upvotes: 0

Related Questions