def inference(self, x): """ x: 2d numpy array """ num = x.shape[0] latentVars = self._model.getInternalVariables() datacase = ContinuousDatacase.create(self.varNames) datacase.synchronize(self._model) ctp = NaturalCliqueTreePropagation(self._model) out = [None]*len(latentVars) datacase.putValues(x) evidence = datacase.getEvidence() ctp.use(evidence) ctp.propagate() for j in range(len(latentVars)): latent = np.exp(ctp.getMarginal(latentVars[j]).logprob) out[j] = latent return out
datacase = ContinuousDatacase.create(varNames) datacase.synchronize(net) ctp = NaturalCliqueTreePropagation(net) targetVar = None for v in net._variables.keys(): if v.name == "z": targetVar = v break print(targetVar) y_pred = [] datacase.putValues(X) evidence = datacase.getEvidence() ctp.use(evidence) ctp.propagate() latent = np.exp(ctp.getMarginal(targetVar).logprob) y_pred = np.argmax(latent, axis=1) print(y_pred) colors = np.array( list( islice( cycle([ '#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3', '#999999', '#e41a1c', '#dede00' ]), int(max(y_pred) + 1)))) plt.scatter(X[:, 0], X[:, 1], s=10, color=colors[y_pred]) plt.show()
# set up evidence datacase = ContinuousDatacase.create(varNames) datacase.synchronize(net) ctp = NaturalCliqueTreePropagation(net) targetVar = None for v in net._variables.keys(): if v.name == "z": targetVar = v break print(targetVar) y_pred = [] for i in range(len(X)): datacase.putValues(X[i]) evidence = datacase.getEvidence() ctp.use(evidence) ctp.propagate() latent = ctp.getMarginal(targetVar).parameter.prob y_pred.append(np.argmax(latent)) print(y_pred) colors = np.array( list( islice( cycle([ '#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3', '#999999', '#e41a1c', '#dede00' ]), int(max(y_pred) + 1)))) plt.scatter(X[:, 0], X[:, 1], s=10, color=colors[y_pred]) plt.show()