2
import numpy as np 
x = ([1,2,3,3])
y = ([1,2,3])
z = ([6,6,1,2,9,9])

(only positive values) In each array i need to return the most common value, or, if values come up the same amount of times - return the minimum. This is home assignment and I can't use anything but numpy.

outputs:

f(x) = 3,
f(y) = 1,
f(z) = 6
1
  • You need a numpy solution? Commented Jul 23, 2018 at 9:23

5 Answers 5

2

for a numpy exclusive solution something like this will work:

occurances = np.bincount(x)
print (np.argmax(occurances))

The above mentioned method won't work if there is a negative number in the list. So in order to account for such an occurrence kindly use:

not_required, counts = np.unique(x, return_counts=True)
x=np.array(x)
if (x >= 0).all():
    print(not_required[np.argmax(counts)])
else:    
    print(not_required[np.argmax(counts)])
Sign up to request clarification or add additional context in comments.

5 Comments

@SergejFomin will post a workaround negative numbers soon
@SergejFomin do checkout the updated answer, as it turns out not required is actually required :P
sorry i messed up kindly check out the updated answer
it still has one issue it will not tell if there are two elements with same number of counts, will fix that as well in a while
@SergejFomin I am posting a new answer. I tried to come up with something pretty and small but something that can be used universally can only be like this. Kindly check the other answer.
2

It's called a mode function. See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mode.html

1 Comment

@jdehesa: yep, just noticed it is homework, will edit
0

Without numpy

n_dict = {}
for k in x:
    try:
        n_dict[k] += 1
    except KeyError:
        n_dict[k] = 1

rev_n_dict = {}
for k in n_dict:
    if n_dict[k] not in rev_n_dict:
        rev_n_dict[n_dict[k]] = [k]
    else:
        rev_n_dict[n_dict[k]].append(k)

local_max = 0
for k in rev_n_dict:
    if k > local_max:
        local_max = k

if len(rev_n_dict[local_max]) > 0:      
    print (min(rev_n_dict[local_max]))
else:
    print (rev_n_dict[local_max])

Comments

0

To add to the previous results, you could use a collections.Counter object:

 my_array =  [3,24,543,3,1,6,7,8,....,223213,13213]
 from collections import Counter
 my_counter = Counter( my_array)
 most_common_value = my_counter.most_common(1)[0][0]

Comments

0

It is quite simple but certainly not pretty. I have used variable names that will be self explanatory along with the comments. Feel free to ask if there is a doubt.

import numpy as np
x=([6,6,1,2,9,9])

def tester(x): 
    not_required, counts = np.unique(x, return_counts=True)
    x=np.array(x)
    if (x >= 0).all():
        highest_occurance=[not_required[np.argmax(counts)]]
        number_of_counts=np.max(counts)

    else:    
        highest_occurance=not_required[np.argmax(counts)]
        number_of_counts=np.max(counts)

    return highest_occurance,number_of_counts

most_abundant,first_test_counts=(tester(x))

new_x=[vals for vals in x if vals not in most_abundant]

second_most_abundant,second_test_counts=(tester(new_x))

if second_test_counts==first_test_counts:
    print("Atleast two elements have the same number of counts",most_abundant," and", second_most_abundant, "have %s"%first_test_counts,"occurances")
else:
    print("%s occurrs for the max of %s times"%(most_abundant,first_test_counts))

we can also loop it to check if there are more than two elements with the same occurrence, instead of using an if else for a specific case of only looking at two elements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.