No Unique Mode; Found 2 Equally Common Values

No unique mode; found 2 equally common values

For example:

lst = [1, 1, 2, 2, 3]

# test for count lst elements
lst_count = [[x, lst.count(x)] for x in set(lst)]
print lst_count
# [[1, 2], [2, 2], [3, 1]]

# remove count <= 1
lst_count = [x for x in set(lst) if lst.count(x) > 1]
print lst_count
# [1, 2]

# get 1 or 2 by index
print lst_count[0], lst_count[1]
# 1 2

Another way:

from collections import Counter

# change lst elements to str, for more readable
lst = ['a', 'a', 'b', 'b', 'c']

# get a dict, key is the elements in lst, value is count of the element
d_mem_count = Counter(lst)
print d_mem_count
# Counter({'a': 2, 'b': 2, 'c': 1})

for k in d_mem_count.keys():
if d_mem_count[k] > 1:
print k

# output as below
# a
# b

Can't find the mode for multiple common values

You may just use mode from pandas

df4.mode()

How to error check Python Statistics mode function?

Check with a set:

if len(set(nums)) == len(nums) 

If you have dups the length of the set will be shorter than the length of your list.

if len(set(nums)) != len(nums): # check different lengths, if different we have dups 
print ("The mode is ", Most)
Most = mode(nums)
else: # else both are the same size so no dups, just print
print("No duplicates in nums")

Set cannot have duplicate items:

In [1]: nums =[1,2,3,4,1]

In [2]: nums
Out[2]: [1, 2, 3, 4, 1]

In [3]: set(nums)
Out[3]: {1, 2, 3, 4} # sets cannot have duplicate items

In [4]: len(set(nums)) == len(nums) # set = len 4, nums = len 5
Out[4]: False

How to take the mode across elements in multiple numpy arrays of 1-d

You can find the mode using scipy.stats.mode. You can also concatenate your multiple numpy arrays into a single array, and then feed that to mode.

import numpy as np
import scipy.stats

arrays = [np.array([0,2,3,4,0]), np.array([1,2,9,4,5])]

result = scipy.stats.mode(np.concatenate(arrays))
# ModeResult(mode=array([0]), count=array([2]))

result.mode
# array([0])

The return value of scipy.stats.mode is a namedtuple ModeResult, which includes the mode and the number of times the value(s) appear.

To find the mode per column, you can stack your arrays into a 2D array, and then find the mode along the first axis.

arrays = [
np.array([0, 2, 3, 4, 0]),
np.array([1, 2, 9, 4, 5]),
np.array([0, 9, 9, 4, 1])]
result = scipy.stats.mode(np.stack(arrays), axis=0)
result.mode
# array([[0, 2, 9, 4, 0]])

How would I use a try/except loop to find output a message based on a specific error from a module?

Try this.This will give you the exact detailed error which you want i.e

userNumbers = [1,3,5,7,10]

def mode(list1):
try:
print("The Mode = ",statistics.mode(userNumbers))
except statistics.StatisticsError as e:
print("There was an error with the statistics module")
print e

mode(userNumbers)

Output

C:\Python27\python.exe 
C:/Users/kapil/PycharmProjects/KapilsucksatPython/py/O.py
There was an error with the statistics module
no unique mode; found 5 equally common values

Process finished with exit code 0

Note the usage of raising the exception as a alias "e" here and then
printing e which prints the exact problem that lies with the module
statistics



Related Topics



Leave a reply



Submit