[英]Python defining multimodal function
I need no create a function that can tell if:我不需要创建一个函数来判断:
I got the first 2 points cover:我得到了前 2 分的封面:
lista = [1,2,2,3,3,4]
contador = {}
for i in lista:
cuenta = lista.count(i)
contador[i] = cuenta
maximo =(0)
moda = [0]
for i in contador:
if(contador[i]>maximo):
maximo = contador[i]
moda = i
freq = contador[i]
if maximo == 1:
print("There is no mode")
else:
print ("Mode is: %d, with a frequency of: %d" % (moda, freq))
But I´m struggling to find a way to define if a list is multimodal.但是我正在努力寻找一种方法来定义列表是否是多模式的。 I thought of first defining which frequency is the highest and then check
contador
to take out all frequencies below but it doesn´t seem to work:我想首先定义哪个频率最高,然后检查
contador
以取出下面的所有频率,但它似乎不起作用:
for i in contador:
if contador[i] < max:
delete = [i]
del contador[delete]
Any ideas on how to do it?关于如何做到这一点的任何想法?
Thank you谢谢
well for multimodial you need to check the max frequency and count the frequency count.对于多模态,您需要检查最大频率并计算频率计数。 if it is 1 then mean no mode, more then 1 and exactly 1 then there is 1 mode and i frequency count is more than 1 and has multiple value has same count then it is multimodial
如果它是 1 则表示没有模式,超过 1 并且正好是 1 则有 1 种模式,并且 i 频率计数大于 1 并且有多个值具有相同的计数那么它是多模态的
lista = [1,2,3,3,4]
res = {}
for i in lista:
if i not in res.keys():
res[i]=1
else:
res[i]+=1
freq = res.values()
max_freq = max(freq)
if max_freq==1:
print('no mode')
else:
key = ''
if list(freq).count(max_freq)==1:
for k, v in res.items():
if v==max_freq:
print('mode is {}, frequency is {}'.format(k, max_freq))
break
else:
print('multimodeal')
for k, v in res.items():
if v==max_freq:
print('mode is {}, frequency is {}'.format(k, max_freq))
The simplest modification to your existing code would be something like:对现有代码的最简单修改如下:
maximo = 0
moda = []
for i in contador:
if(contador[i] > maximo):
maximo = i
freq = contador[i]
moda = []
if(contador[i]==freq):
moda.append(i)
And change the final print
to:并将最终
print
更改为:
print ("Mode is: %s, with a frequency of: %d" % (moda, freq))
The whole thing simplified with library functions:使用库函数简化了整个过程:
from collections import Counter
lista = [1, 2, 2, 3, 3, 4]
contador = Counter(lista)
# get the second part (the count) from the first element of the first most common element
freq = contador.most_common(1)[0][1]
# get all the x's for which the count is freq
moda = [x for x, c in contador.items() if c == freq]
if freq == 1:
print("There is no mode")
else:
print("Mode is: %s, with a frequency of: %d" % (moda, freq))
def moda(x):
input_string = input("Lista de numeros separados por un espacio: ")
lista = input_string.split()
contador = {}
for i in lista:
cuenta = lista.count(i)
contador[i] = cuenta
maximo =(0)
for i in contador:
if(contador[i]>maximo):
maximo = contador[i]
modas = {}
for i in contador:
if contador[i] == maximo:
modas[i] = contador[i]
if maximo == 1:
return("No hay modas")
elif len(modas)>1:
return ("Las modas y sus frecuencias son:")
return (modas)
else:
return ("la moda y su frecuencia es:")
return (modas)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.