def __init__(self, pdata, qdata, alpha, beta, s): """ Constructor. Takes in pdata -- data drawn from p qdata -- data drawn from q alpha -- exponent for p beta -- exponent for q s -- smoothness (which we require) of the two densities. """ self.Kp = kde.KDE(pdata, s) self.Kq = kde.KDE(qdata, s) self.dim = pdata.shape[1] self.alpha = alpha self.beta = beta self.s = s
def kde_rate(D, ns, ps, iters=10): """ Compute the kde_rate of convergence on the density D as a function on n. The rate will be computed in ell_p^p norm for each p """ ms = [[] for p in ps] vs = [[] for p in ps] for n in ns: sub_scores = [[] for p in ps] for i in range(iters): data = D.sample(n) K = kde.KDE(data, D.s) [ sub_scores[i].append(K.kde_error(D, ps[i])) for i in range(len(ps)) ] print "n = %d " % n + " ".join([ str(ps[i]) + " = %0.2f" % np.mean(sub_scores[i]) for i in range(len(ps)) ]) [ms[i].append(np.mean(sub_scores[i])) for i in range(len(ps))] [vs[i].append(np.std(sub_scores[i])) for i in range(len(ps))] return (ns, ms, vs)
def test_gamma(self, axis): data = numpy.array([1,1,2,2,3,3,3,4,6,8], dtype=float) weights = numpy.array([5,8,9,4,5,2,3,1,1,2], dtype=float) weights /= weights.sum() pdf = kde.KDE(data, weights=weights, kernel='gamma') xs = numpy.linspace(0,15,num=500) ys = pdf.evaluate(xs) axis.plot(xs, ys, color='black')
def createMap(self, trips, cell_size, gaussian_blur, init_graphdb_filename, fclass, graphdb_filename, isinitMap=0): k = kde.KDE() k.create_kde_with_trips(trips, cell_size, gaussian_blur) # (2) skeleton.py input_kde = imread(self.input_filename) s = skeleton.GrayscaleSkeleton() skeleton_s = s.skeletonize(input_kde) toimage(skeleton_s, cmin=0, cmax=255).save(self.skeleton_filename) # (3) graph_extract.py bounding_box_file = open(self.bounding_box_filename, 'r') bounding_box_values = bounding_box_file.readline().strip("\n").split( " ") bounding_box_file.close() skeleton_g = imread(self.skeleton_filename) graph_extract.min_lat, graph_extract.min_lon, graph_extract.max_lat, graph_extract.max_lon = float( bounding_box_values[0]), float(bounding_box_values[1]), float( bounding_box_values[2]), float(bounding_box_values[3]) graph_extract.height = len(skeleton_g) graph_extract.width = len(skeleton_g[0]) graph_extract.yscale = graph_extract.height / (graph_extract.max_lat - graph_extract.min_lat) graph_extract.xscale = graph_extract.width / (graph_extract.max_lon - graph_extract.min_lon) g = graph_extract.Graph() # 当前的轨迹生成的.db文件 # self.graphdb_filename = prefix+"db/updatedb/1m/skeleton_map_1m.db" g.extract2( skeleton_g.astype(np.bool).astype(np.int), skeleton_g, graphdb_filename, fclass) if (isinitMap): # self.init_graphdb_filename=prefix+"db/initdb/skeleton_map_1m.db" # 这里用当前的轨迹生成的数据去 更新 init.db文件 print "log:src.pipeline.run.createMap:执行" g.extract( skeleton_g.astype(np.bool).astype(np.int), skeleton_g, init_graphdb_filename, fclass)
def __init__(self): matplotlib.rcParams['font.size'] = 6 fig = pyplot.gcf() ax = pyplot.gca() data = [(2,3), (2,3), (4,1), (7,1), (2,2), (5,4)] data = numpy.array(data) grid = numpy.linspace grid = numpy.mgrid[0:10:101j,0:10:101j].reshape(2,-1).T x = numpy.linspace(0,10,101) y = numpy.linspace(0,10,101) pdf = kde.KDE(data).evaluate(grid).reshape(101,101)[:,:].T ax.pcolormesh(x,y,pdf,cmap='magma') ax.plot(data[:,0], data[:,1], 'o', markersize=2, color='white') pyplot.savefig('2d.pdf')
def main(): training_data = [0] estimator = kde.KDE(training_data) query_points = numpy.linspace(-5, 5, 101) print(estimator.evaluate(query_points, cuda=True).shape)