为了是代码简短,方便阅读,去掉了很多健壮性检测的代码以及特殊处理。下面的代码实现的是:使用最基础GIS训练最大熵模型。GIS由于性能问题在实际中不适用,但是可以帮助我们理解最大熵训练到底在做什么。
import sys; import math; from collections import defaultdict class MaxEnt: def __init__(self): self._samples = []; self._Y = set([]); self._numXY = defaultdict(int); self._N = 0; self._n = 0; self._xyID = {}; self._C = 0; self._ep_ = []; self._ep = []; self._w = []; self._lastw = []; self._EPS = 0.01; def load_data(self, filename): for line in open(filename, "r"):
sample = line.strip().split("\t"); if len(sample) < 2: continue;
y = sample[0];
X = sample[1:];
self._samples.append(sample); self._Y.add(y); for x in set(X): self._numXY[(x, y)] += 1; def _initparams(self): self._N = len(self._samples);
self._n = len(self._numXY);
self._C = max([len(sample) - 1 for sample in self._samples]);
self._w = [0.0] * self._n;
self._lastw = self._w[:];
self._sample_ep(); def _convergence(self): for w, lw in zip(self._w, self._lastw): if math.fabs(w - lw) >= self._EPS: return False; return True; def _sample_ep(self): self._ep_ = [0.0] * self._n; for i, xy in enumerate(self._numXY):
self._ep_[i] = self._numXY[xy] * 1.0 / self._N;
self._xyID[xy] = i; def _zx(self, X): ZX = 0.0; for y in self._Y:
sum = 0.0; for x in X: if (x, y) in self._numXY:
sum += self._w[self._xyID[(x, y)]];
ZX += math.exp(sum); return ZX; def _pyx(self, X): ZX = self._zx(X);
results = []; for y in self._Y:
sum = 0.0; for x in X: if (x, y) in self._numXY: sum += self._w[self._xyID[(x, y)]];
pyx = 1.0 / ZX * math.exp(sum);
results.append((y, pyx)); return results; def _model_ep(self): self._ep = [0.0] * self._n; for sample in self._samples:
X = sample[1:];
pyx = self._pyx(X); for y, p in pyx: for x in X: if (x, y) in self._numXY:
self._ep[self._xyID[(x, y)]] += p * 1.0 / self._N; def train(self, maxiter = 1000): self._initparams(); for i in range(0, maxiter): print "Iter:%d..."%i;
self._lastw = self._w[:]; self._model_ep(); for i, w in enumerate(self._w): self._w[i] += 1.0 / self._C * math.log(self._ep_[i] / self._ep[i]); print self._w; if self._convergence(): break; def predict(self, input): X = input.strip().split("\t");
prob = self._pyx(X) return prob; if __name__ == "__main__":
maxent = MaxEnt();
maxent.load_data('data.txt');
maxent.train(); print maxent.predict("sunny\thot\thigh\tFALSE"); print maxent.predict("overcast\thot\thigh\tFALSE"); print maxent.predict("sunny\tcool\thigh\tTRUE");
sys.exit(0);
训练数据来自各种天气情况下是否打球的例子:data.txt
其中字段依次是:
play
|
outlook
|
temperature
|
humidity
|
windy
|
部分运行结果:
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。