Finding a Mean of an Attribute for a Specific Class in Python

Vicki

I'm trying to write a function to find the mean of an attribute for values that only fall within a specific class.

Below is my code:

`mean=0
total=0
count=0
for i in range(len(training_data)):
    if (training_data[i,334])==0:
        if training_data[i,2]<>None:
            total+=training_data[i,2]
            count+=1
    mean=total/count`

However, my attribute has some null values in it. I am working with numpy, and the null values are being coded as "NaN". In my function above, even though I am specifically specifying that the value cannot be equal to "None", which is Python's equivalent to null, my "total" attribute continues to show up as 'nan'. I have tried many different equivalents to "None" and have not been able to get a value for the total variable other than 'nan'. Is there something obvious I'm missing? Thank you in advance!

Andrey Sobolev

With the power of numpy your code can be trimmed to 2 lines:

idx = training_data[:,334] == 0
mean = np.nanmean(training_data[idx, 2])

idx here is the boolean array which is True for the indices of rows falling into specific class, and np.nanmean calculates the mean value ignoring NaNs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Finding specific digit pattern with regex in python

From Dev

Why is BeautifulSoup not finding a specific table class?

From Dev

finding a specific split string in CSV file [Python]

From Dev

LINQ to XML: Finding an element with a specific attribute

From Dev

Finding all users with specific attribute in a through relation

From Dev

Python crawler not finding specific Xpath

From Dev

Finding an equation that results in a specific integer, Python

From Dev

Add attribute to python class

From Dev

How to check if element contains specific class attribute

From Dev

trigger click an element with a specific class and attribute

From Dev

finding a specific value from list of dictionary in python

From Dev

Python: Finding nearest integer value to specific integer

From Dev

Class attribute shadowing in Python class

From Dev

Jupyter not finding Python class

From Dev

Traversing up and finding a child element with attribute equal to a specific value

From Dev

Python: Sub attribute in a class

From Dev

Finding mean of a part of list in python and then modifying the list

From Dev

Jquery Find a specific class with a specific attribute value

From Dev

Python - Finding and multiplying an integer after specific word

From Dev

Python Class and Attribute Error

From Dev

Finding file path for a specific java class

From Dev

Python Code for Finding Specific Parameters

From Dev

LINQ to XML: Finding an element with a specific attribute

From Dev

How to Add and remove attribute if found specific class

From Dev

Class attribute shadowing in Python class

From Dev

Stop hover and click on element with not specific class attribute

From Dev

Python class - attribute inheritance

From Dev

Finding element closest to mean in python list

From Dev

Finding descendants of div with specific class

Related Related

  1. 1

    Finding specific digit pattern with regex in python

  2. 2

    Why is BeautifulSoup not finding a specific table class?

  3. 3

    finding a specific split string in CSV file [Python]

  4. 4

    LINQ to XML: Finding an element with a specific attribute

  5. 5

    Finding all users with specific attribute in a through relation

  6. 6

    Python crawler not finding specific Xpath

  7. 7

    Finding an equation that results in a specific integer, Python

  8. 8

    Add attribute to python class

  9. 9

    How to check if element contains specific class attribute

  10. 10

    trigger click an element with a specific class and attribute

  11. 11

    finding a specific value from list of dictionary in python

  12. 12

    Python: Finding nearest integer value to specific integer

  13. 13

    Class attribute shadowing in Python class

  14. 14

    Jupyter not finding Python class

  15. 15

    Traversing up and finding a child element with attribute equal to a specific value

  16. 16

    Python: Sub attribute in a class

  17. 17

    Finding mean of a part of list in python and then modifying the list

  18. 18

    Jquery Find a specific class with a specific attribute value

  19. 19

    Python - Finding and multiplying an integer after specific word

  20. 20

    Python Class and Attribute Error

  21. 21

    Finding file path for a specific java class

  22. 22

    Python Code for Finding Specific Parameters

  23. 23

    LINQ to XML: Finding an element with a specific attribute

  24. 24

    How to Add and remove attribute if found specific class

  25. 25

    Class attribute shadowing in Python class

  26. 26

    Stop hover and click on element with not specific class attribute

  27. 27

    Python class - attribute inheritance

  28. 28

    Finding element closest to mean in python list

  29. 29

    Finding descendants of div with specific class

HotTag

Archive