def test_distributions_gaussian_kernel_random_sample(): d = GaussianKernelDensity([0, 4, 3, 5, 7, 4, 2]) x = numpy.array([5.367586, 2.574708, 2.114238, 2.170925, 4.596907]) assert_array_almost_equal(d.sample(5, random_state=5), x) assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
def plot_histogram(df, feature_name): d = GaussianKernelDensity(df[feature_name], bandwidth=2) d.plot( facecolor="c", edgecolor="w", bins=50, alpha=0.3, label="Gaussian Kernel Density", )
def test_gaussian_kernel(): d = GaussianKernelDensity([0, 4, 3, 5, 7, 4, 2]) assert_equal(round(d.log_probability(3.3), 4), -1.7042) d.fit([1, 6, 8, 3, 2, 4, 7, 2]) assert_equal(round(d.log_probability(1.2), 4), -2.0237) d.fit([1, 0, 108], weights=[2., 3., 278.]) assert_equal(round(d.log_probability(110), 4), -2.9368) assert_equal(round(d.log_probability(0), 4), -5.1262) d.summarize([1, 6, 8, 3]) d.summarize([2, 4, 7]) d.summarize([2]) d.from_summaries() assert_equal(round(d.log_probability(1.2), 4), -2.0237) d.summarize([1, 0, 108], weights=[2., 3., 278.]) d.from_summaries() assert_equal(round(d.log_probability(110), 4), -2.9368) assert_equal(round(d.log_probability(0), 4), -5.1262) d.freeze() d.fit([1, 3, 5, 4, 6, 7, 3, 4, 2]) assert_equal(round(d.log_probability(110), 4), -2.9368) assert_equal(round(d.log_probability(0), 4), -5.1262) e = Distribution.from_json(d.to_json()) assert_equal(e.name, "GaussianKernelDensity") assert_equal(round(e.log_probability(110), 4), -2.9368) assert_equal(round(e.log_probability(0), 4), -5.1262) f = pickle.loads(pickle.dumps(e)) assert_equal(f.name, "GaussianKernelDensity") assert_equal(round(f.log_probability(110), 4), -2.9368) assert_equal(round(f.log_probability(0), 4), -5.1262)
axes.legend(loc="best") fig.tight_layout() fig.show() fig.savefig("AE_result_.png", dpi=300) from pomegranate import NaiveBayes, GaussianKernelDensity data = np.array(list(mse_const_test) + list(mse_vars)) data = np.log(data) data = data.reshape(-1, 1) weights = np.ones(len(data)) weights[-len(mse_vars):] = len(mse_const_test) / float(len(mse_vars)) y = np.zeros(len(data)) y[-len(mse_vars):] = np.ones(len(mse_vars)) clf = NaiveBayes([ GaussianKernelDensity(bandwidth=0.1), GaussianKernelDensity(bandwidth=0.1) ]) clf.fit(data, y, weights=weights) pred = clf.predict_proba(data) x = np.array([np.arange(-7, -2, .01)]).T yy = clf.predict_proba(x) thresh = 0.5 plt.figure(figsize=(10, 5)) plt.hist(data[pred[:, 0] > thresh], color='blue', bins=30, alpha=0.5, normed=True, label="Constant unseen") plt.hist(data[pred[:, 1] > thresh],