我正在从事一个主要基于逻辑编程的项目。我预定义了相关规则和事实,程序使用这些规则和事实来计算概率,然后将这些概率附加到数据中,并馈入进一步的机器学习模型。例如,可以在prolog中轻松定义计算概率的程序如下:
has_lot_work(daniel, 8). %number of lets say urgent tasks
has_lot_work(david, 3).
stress(X, P) :- has_lot_work(X, P2), P is P2 / 100.
to_smoke(X, Prob) :- stress(X, P1), friends(Y, X), influences(Y, X, P2), smokes(Y), Prob is P1 + P2.
to_have_asthma(X, 0.3) :- smokes(X). %30 percent of current smokers get asthma
to_have_asthma(X, Prob) :- to_smoke(X, P2), Prob is P2 * 0.25. %25 percent of smokers-to-be will get asthma
friends(X, Y) :- friend(X, Y).
friends(X, Y) :- friend(Y, X).
influences(X, Y, 0.4) :- friends(X, Y). %friends influence each other by 40 percent
friend(peter, david).
friend(peter, rebecca).
friend(daniel, rebecca).
smokes(peter).
smokes(rebecca).
在这个例子中,我对计算某人吸烟的概率(to_smoke(Who, Prob))和患哮喘的概率(to_have_asthma(Who, Prob))感兴趣。我使用Python来获取和清理数据,并用于之后的ML模型,因此我希望也能在Python中应用这种逻辑。但是我找不到一种方法来进行这种逻辑计算,也找不到一种合适的方法来连接Python与Prolog,避免出现错误和问题。