简体   繁体   English

python加法概率

[英]python additive probability

I've created a python 2.7 tkinter module which uses scale widget data (aquatic or terrestrial) to influence a tuple which selects between animals. 我创建了一个python 2.7 tkinter模块,该模块使用比例小部件数据(水生或陆地)来影响在动物之间进行选择的元组。 The module sorts and displays the three animals (deer, eel, turtles) in ranked descending order upon clicking 'Submit' and activating the associated command. 单击“提交”并激活关联的命令后,该模块将以降序排列并显示三种动物(鹿,鳗鱼,乌龟)。

from Tkinter import (N, S, E, W, BOTH, BOTTOM, END, FLAT, HORIZONTAL, LEFT, NO, RAISED, RIGHT, TOP, YES, Button, Entry, Frame, Grid, Label, Pack, Scale, Text, Tk)

from operator import mul

root = Tk()
root.title('Example')

class Environment:
    def __init__(self, parent):

        # layout
        self.myParent = parent

        self.main_frame = Frame(parent, background="light blue")
        self.main_frame.pack(expand=YES, fill=BOTH)

        self.main_left_frame = Frame(self.main_frame, background="light blue")
        self.main_left_frame.pack(side=LEFT, expand=YES, fill=BOTH)

        self.main_right_frame = Frame(self.main_frame, background="light blue")
        self.main_right_frame.pack(side=RIGHT, expand=YES, fill=BOTH)

        self.water = Scale(self.main_right_frame, from_=0.01, to=1.00, orient=HORIZONTAL, bd=0, label="Aquatic",
        background="white", troughcolor="cyan", length=50, width=10, sliderlength=10, resolution=0.01)
        self.water.pack()
        self.water.set(1.00)

        self.soil = Scale(self.main_right_frame, from_=0.01, to=1.00, orient=HORIZONTAL, bd=0, label="Terrestrial",
        background="white", troughcolor="saddle brown", length=50, width=10, sliderlength=10, resolution=0.01)
        self.soil.pack()
        self.soil.set(1.00)

        self.id_frame = Frame(self.main_left_frame, background="white")
        self.id_frame.pack(side=BOTTOM)

        # submit button
        self.submitbutton = Button(self.main_left_frame,text="Submit", background="black", foreground="white",
        width=6, padx="2m", pady="1m")
        self.submitbutton.pack(side=TOP)
        self.submitbutton.bind("<Button-1>", self.submitbuttonclick)
        self.submitbutton.bind("<Return>", self.submitbuttonclick)

        #Animal Matrix
        self.animal = [
        ('Odocoileous virginiana','White-tailed Deer',self.soil.get,0.99,0.01,0.99),
        ('Anguilla anguilla','American Eel',self.water.get,0.99,0.01,0.99),
        ('Trachemys scripta','Red-eared Slider',lambda:self.soil.get()*self.water.get(),0.99,0.01,0.99)]

    def submitbuttonclick(self, event):
        self.id_frame.destroy()
        self.id_frame = Frame(self.main_left_frame, background="white")
        self.id_frame.pack(side=BOTTOM)

        A=self.animal

        #equation
        sigma = float(sum(reduce(mul,item[3:]) for item in A))
        B = [(item[0], "%.2f" % (item[2]()*reduce(mul, item[3:])/sigma)) for item in A]
        C = sorted(B, key=lambda item: item[1], reverse=True)  

        Label(self.id_frame, text = C[0], background = "white").pack(side=TOP, anchor = W)
        Label(self.id_frame, text = C[1], background = "white").pack(side=TOP, anchor = W)
        Label(self.id_frame, text = C[2], background = "white").pack(side=TOP, anchor = W)

environment = Environment(root)       
root.mainloop()

In theory, sliding the aquatic scale to 0.01 should eliminate the eel (Eel: 0.00) and sliding the terrestrial scale to 0.01 should eliminate the deer (Deer: 0.00). 从理论上讲,将水生尺度降低到0.01应该消除鳗鱼(Eel:0.00),而将水生尺度降低到0.01应该消除鹿(鹿:0.00)。 This works fine. 这很好。

However, when dealing with an animal that is both aquatic and terrestrial, the code falls apart. 但是,在处理既有水生又有陆生动物的动物时,代码会分崩离析。 The turtle should not hit zero so long as either the aquatic or terrestrial scale is above 0.01. 只要水生或陆地规模大于0.01,乌龟就不应打零。 Additive probability doesn't work. 加性概率不起作用。

lambda:self.soil.get()*self.water.get()

How do I express this so that both are considered but neither can cancel out the other? 我如何表达这一点,以便将两者都考虑在内,但又不能互相抵消呢?

Probabilities are funny things. 概率是有趣的事情。 To combine these probabilities you need to look at the probability that it is not terrestial and it is not aquatic. 要结合这些概率,你需要看的概率,这是不是陆栖,它不是水产品。 Then take 1 minus that combined probability. 然后取1减去该组合概率。 The code for the turtle would look like this. 乌龟的代码如下所示。

('Trachemys scripta','Red-eared Slider',lambda:1-(1-self.soil.get())*(1-self.water.get()),0.99,0.01,0.99)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM