Python定义多模态函数
我不需要创建一个函数来判断: 没有模式 有1种模式 如果是多式联运清单 我得到了前2个积分:Python定义多模态函数,python,python-3.x,Python,Python 3.x,我不需要创建一个函数来判断: 没有模式 有1种模式 如果是多式联运清单 我得到了前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 = conta
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))
但我正在努力找到一种方法来定义列表是否是多模态的。我想先定义哪个频率是最高的,然后检查contador以删除下面的所有频率,但它似乎不起作用:
for i in contador:
if contador[i] < max:
delete = [i]
del contador[delete]
有什么办法吗
谢谢对于多式联运,您需要检查最大频率并计算频率计数。如果为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))
对于多式联运,您需要检查最大频率并计算频率计数。如果为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))
对现有代码最简单的修改如下:
maximo = 0
moda = []
for i in contador:
if(contador[i] > maximo):
maximo = i
freq = contador[i]
moda = []
if(contador[i]==freq):
moda.append(i)
并将最终打印更改为:
整个过程通过库函数简化:
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))
对现有代码最简单的修改如下:
maximo = 0
moda = []
for i in contador:
if(contador[i] > maximo):
maximo = i
freq = contador[i]
moda = []
if(contador[i]==freq):
moda.append(i)
并将最终打印更改为:
整个过程通过库函数简化:
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))