Issue
I have two lists that have been combined using zip, and I need to find the percent of one of these variables.
I have zipped these two lists so the proper values are associated. Next I need to single out one specific variable, give it a name, and find it's percent.
In: np.asarray((unique_elements, counts_elements))
Out:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 84430, 23984, 107355, 91459, 80237, 179391, 69367]])
geologyzip = zip(unique_elements, counts_elements)
geologytotal=np.sum(geologyzip)
I don't know if that last bit was correct, but I need to total these values and find the percent of #4 which I also need to associate with the name madison
I need something to say madison=x% but I'm not sure where to go from here.
Solution
Here's something you can look at to see why numpy is a powerful source of one-liners.
import numpy as np
unique_elements, counts_elements = np.array([[ 0, 1, 2, 3, 4, 5, 6],
[ 84430, 23984, 107355, 91459, 80237, 179391, 69367]])
value = 4
percent = counts_elements[unique_elements==value].sum()/counts_elements.sum()*100.0
This says sum all elements in counts_elements
corresponding to unique_elements
being equal to the value
you specify, then divide it by the total count counts_elements.sum()
. This is slightly overkill. You could study it to figure out why.
Answered By - kevinkayaks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.