本文整理汇总了Python中sklearn.neighbors.BallTree.query_radius方法的典型用法代码示例。如果您正苦于以下问题:Python BallTree.query_radius方法的具体用法?Python BallTree.query_radius怎么用?Python BallTree.query_radius使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.neighbors.BallTree
的用法示例。
在下文中一共展示了BallTree.query_radius方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _calc_tree
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def _calc_tree(xx, yy, radius):
X = np.zeros((len(xx), 2), dtype='float')
X[:, 0] = xx[:]
X[:, 1] = yy[:]
tree = BallTree(X, metric='euclidean')
ind = tree.query_radius(X, r=radius)
ind_sw = tree.query_radius(X, r=VARIANCE_RADIUS_SW)
return ind, ind_sw
示例2: __init__
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
class BallTreeANN:
def __init__(self):
"""
Constructor
"""
self.nbrs = None
def build_index(self, dataset, leaf_size):
self.nbrs = BallTree(dataset, leaf_size=leaf_size, metric="euclidean")
return self.nbrs
def build_store_index(self, dataset, path, leaf_size):
self.build_index(dataset, leaf_size)
self.store_index(path)
def store_index(self, path):
with open(path, "wb") as output1:
pickle.dump(self.nbrs, output1, pickle.HIGHEST_PROTOCOL)
def load_index(self, path):
with open(path, "rb") as input1:
self.nbrs = pickle.load(input1)
def search_in_radious(self, vector, radious=2):
distances, indices = self.nbrs.query_radius(vector, r=radious, return_distance=True)
return distances, indices
def search_neighbors(self, vector, num_neighbors):
distances, indices = self.nbrs.query(vector, k=num_neighbors)
return distances, indices
示例3: eval
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def eval(self, X):
"""Evaluate the kernel density estimation
Parameters
----------
X : array_like
array of points at which to evaluate the KDE. Shape is
(n_points, n_dim), where n_dim matches the dimension of
the training points.
Returns
-------
dens : ndarray
array of shape (n_points,) giving the density at each point.
The density will be normalized for metric='gaussian' or
metric='tophat', and will be unnormalized otherwise.
"""
X = np.atleast_2d(X)
if X.ndim != 2:
raise ValueError('X must be two-dimensional')
if X.shape[1] != self.X_.shape[1]:
raise ValueError('dimensions of X do not match training dimension')
if self.metric == 'gaussian':
# wrangle gaussian into scikit-learn's 'rbf' kernel
gamma = 0.5 / self.h / self.h
D = pairwise_kernels(X, self.X_, metric='rbf', gamma=gamma)
D /= np.sqrt(2 * np.pi * self.h ** (2 * X.shape[1]))
dens = D.sum(1)
elif self.metric == 'tophat':
# use Ball Tree to efficiently count neighbors
bt = BallTree(self.X_)
counts = bt.query_radius(X, self.h,
count_only=True)
dens = counts / n_volume(self.h, X.shape[1])
elif self.metric == 'exponential':
D = pairwise_distances(X, self.X_)
dens = np.exp(-abs(D) / self.h)
dens = dens.sum(1)
dens /= n_volume(self.h, X.shape[1]) * special.gamma(X.shape[1])
elif self.metric == 'quadratic':
D = pairwise_distances(X, self.X_)
dens = (1 - (D / self.h) ** 2)
dens[D > self.h] = 0
dens = dens.sum(1)
dens /= 2. * n_volume(self.h, X.shape[1]) / (X.shape[1] + 2)
else:
D = pairwise_kernels(X, self.X_, metric=self.metric, **self.kwargs)
dens = D.sum(1)
return dens
示例4: BallTree
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
class BallTree():
def __init__(self,walkers):
self.walkers = walkers
self.tree = BT(walkers.getWalkersLocation())
def getEdges(self, radius):
results = []
for i,neighbors in enumerate(self.tree.query_radius(self.walkers.getWalkersLocation(),radius)):
if len(neighbors) > 0:
for n in neighbors:
results.append((self.walkers[i],self.walkers[n]))
return results
示例5: avgdigamma
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def avgdigamma(data, dvec, leaf_size=16):
"""Convenience function for finding expectation value of <psi(nx)> given
some number of neighbors in some radius in a marginal space.
Parameters
----------
points : numpy.ndarray
dvec : array_like (n_points,)
Returns
-------
avgdigamma : float
expectation value of <psi(nx)>
"""
tree = BallTree(data, leaf_size=leaf_size, p=float('inf'))
n_points = tree.query_radius(data, dvec - EPS, count_only=True)
return digamma(n_points).mean()
示例6: estimate_bayes_factor
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def estimate_bayes_factor(traces, logp, r=0.05, return_list=False):
"""From astroml, estimates the bayes factor using the local density of points"""
D, N = traces.shape
# compute volume of a D-dimensional sphere of radius r
Vr = np.pi ** (0.5 * D) / scipy.special.gamma(0.5 * D + 1) * (r ** D)
# use neighbor count within r as a density estimator
bt = BallTree(traces.T)
count = bt.query_radius(traces.T, r=r, count_only=True)
BF = logp + np.log(N) + np.log(Vr) - np.log(count)
if return_list:
return BF
else:
p25, p50, p75 = np.percentile(BF, [25, 50, 75])
return p50, 0.7413 * (p75 - p25)
示例7: mean_shift
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def mean_shift(X, bandwidth, seeds, kernel_update_function, max_iterations=10):
n_points, n_features = X.shape
stop_thresh = 1e-3 * bandwidth # when mean has converged
cluster_centers = []
ball_tree = BallTree(X) # to efficiently look up nearby points
# For each seed, climb gradient until convergence or max_iterations
for weighted_mean in seeds:
completed_iterations = 0
while True:
points_within = X[ball_tree.query_radius([weighted_mean], bandwidth*3)[0]]
old_mean = weighted_mean # save the old mean
weighted_mean = kernel_update_function(old_mean, points_within, bandwidth)
converged = extmath.norm(weighted_mean - old_mean) < stop_thresh
if converged or completed_iterations == max_iterations:
cluster_centers.append(weighted_mean)
break
completed_iterations += 1
return cluster_centers
示例8: mean_shift_clustering
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def mean_shift_clustering(points, bandwidth, max_iterations=500):
stop_thresh = 1e-3 * bandwidth
cluster_centers = []
points_labels = []
ball_tree = BallTree(points)
for weighted_mean in points:
iter = 0
while True:
points_within = points[ball_tree.query_radius([weighted_mean],
bandwidth*3)[0]]
old_mean = weighted_mean
weighted_mean = mean_shift(old_mean, points_within, bandwidth)
converged = euclid_dist(weighted_mean, old_mean) < stop_thresh
if converged or iter == max_iterations:
cluster_centers, points_labels = assign_cluster(weighted_mean,
cluster_centers,
points_labels)
break
iter += 1
return np.asarray(cluster_centers), np.asarray(points_labels)
示例9: int
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
train = int(total * TRAIN_PERCENTAGE)
test = total - train
distances = []
per_stops_popu = collections.defaultdict(int)
distance = dist(lat, lon, stop_lat, stop_lon)
if cnt < train:
per_stops_popu[actual_stop] += 1
distances.append(distance)
else:
if cnt == train:
idx = int(len(distances) * DISTANCE_FACTOR)
if idx >= int(len(distances)):
idx = -1
per_radius = sorted(distances)[idx]
# global
stops = tree.query_radius([lat, lon], r=RADIUS)[0]
l = [(id_to_stop_id[s][0], dist(lat, lon, id_to_stop_id[s][1], id_to_stop_id[s][2]), id_to_stop_id[s][3], id_to_stop_id[s][4]) for s in stops]
if actual_stop in get_largest_n(l, [1], LIST_SIZE):
global_nearest += 1
if actual_stop in get_largest_n(l, [2], LIST_SIZE):
global_route += 1
if actual_stop in get_largest_n(l, [3], LIST_SIZE):
global_popu += 1
if distance > RADIUS:
global_lost += 1
# personalized
stops = tree.query_radius([lat, lon], r=per_radius)[0]
l = [(id_to_stop_id[s][0], dist(lat, lon, id_to_stop_id[s][1], id_to_stop_id[s][2]), id_to_stop_id[s][3], id_to_stop_id[s][4], per_stops_popu[s]) for s in stops]
if actual_stop in get_largest_n(l, [1], LIST_SIZE):
per_nearest += 1
if actual_stop in get_largest_n(l, [2], LIST_SIZE):
示例10: BallTree
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
xyz = np.zeros((5903, 3))
xyz[:, 0] = x[:, 0]
xyz[:, 1] = y[:, 0]
xyz[:, 2] = z[:, 0]
Xtrain = import_train["Xtrain"]
scaler = preprocessing.StandardScaler().fit(Xtrain)
Xtrain = scaler.transform(Xtrain)
from sklearn.neighbors import kneighbors_graph, BallTree
from sklearn.feature_extraction.image import grid_to_graph
xyz_balltree = BallTree(xyz)
print xyz_balltree
print xyz_balltree.query_radius(xyz[0], r=0.04)
# connectivity = kneighbors_graph(xyz_balltree, 2, include_self=True,mode='connectivity')
# connectivity = grid_to_graph(n_x =x, n_y = y, n_z = z )
# agglo = cluster.FeatureAgglomeration(n_clusters = 590)
# agglo.fit(Xtrain)
# Xtrain_reduced = agglo.transform(Xtrain)
"""
#k_fold = cross_validation.KFold(len(X_train), 5)
Y_kf = Ytrain.ravel()
k_fold = StratifiedKFold(Y_kf, n_folds=10)
示例11: tract_smooth
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
def tract_smooth(tractography, var, file_output):
from sklearn.neighbors import BallTree
var = float(var)
std = var ** 2
points = tractography.original_tracts()
all_points = numpy.vstack(points)
bt = BallTree(all_points)
N = len(all_points) / 3
I = numpy.eye(3)[None, ...]
for i, tract in enumerate(tractography.original_tracts()):
# all_points = numpy.vstack(points[:i] + points[i + 1:])
# bt = BallTree(all_points)
diff = numpy.diff(tract, axis=0)
diff = numpy.vstack((diff, diff[-1]))
lengths = numpy.sqrt((diff ** 2).sum(1))
# cum_lengths = numpy.cumsum(lengths)
diff_norm = diff / lengths[:, None]
tangent_lines = diff_norm[:, None, :] * diff_norm[:, :, None]
normal_planes = I - tangent_lines
# weight_matrices = normal_planes + 1e10 * tangent_lines
N = max(len(d) for d in bt.query_radius(tract, var * 3))
close_point_distances, close_point_indices = bt.query(
tract, N
)
close_points = all_points[close_point_indices]
difference_vectors = close_points - tract[:, None, :]
projected_vectors = (
normal_planes[:, None, :] *
difference_vectors[..., None]
).sum(-2)
projected_points = projected_vectors + tract[:, None, :]
# projected_distances2 = (projected_vectors**2).sum(-1)
# projected_weights = numpy.exp(- .5 * projected_distances2 / std)
# projected_weights /= projected_weights.sum(-1)[:, None]
weights = numpy.exp(
-.5 * close_point_distances ** 2 / std
)[..., None]
weights /= weights.sum(-2)[..., None]
# tract += (weights * projected_vectors).sum(-2)
# weighted_distances = (
# weight_matrices[:, None, :] *
# difference_vectors[..., None]
# ).sum(-2)
# weighted_distances *= difference_vectors
# weighted_distances = weighted_distances.sum(-1) ** .5
# weighted_points = (projected_points * weights).sum(1)
weighted_points = (projected_points * weights).sum(1)
tract[:] = weighted_points
# tract /= norm_term
return Tractography(
tractography.original_tracts(),
tractography.original_tracts_data(),
**tractography.extra_args
)
示例12: BallTree
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
# 2.4.2) sum this minimum distance to tot
# tot is the final distance between s0 and s2
# the s2 with minimum distance is the desired streamline
np.random.seed(0)
prototypes_id = np.random.permutation(dm.shape[0])[:200]
dp = dm[:,prototypes_id] # dissimilarity projection
kdt = BallTree(dp) # KDTree(dp)
radius = 100
k = 10
sid = 9
idx1 = kdt.query_radius(dp[sid], radius)[0]
# idx1 = kdt.query(dp[sid], k)[1][0]
dm_small1 = dm[idx1][:,idx1]
e1 = dm_small1[np.triu_indices(dm_small1.shape[0],1)]
spgk = np.zeros(dm.shape[0])
for i in range(dm.shape[0]):
idx2 = kdt.query_radius(dp[i], radius)[0]
# idx2 = kdt.query(dp[i], k)[1][0]
dm_small2 = dm[idx2][:,idx2]
e2 = dm_small2[np.triu_indices(dm_small2.shape[0],1)]
spgk[i] = np.multiply.outer(np.exp(-e1), np.exp(-e2)).sum()
print i, spgk[i]
示例13: two_point
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
#.........这里部分代码省略.........
random_tree : BallTree (optional)
the ball tree used to calculate distances between objects
quickly in the randomly generated set. only returned if
return_trees == True
RR : ndarray (optional)
the RR counts may be returned (if return_RR==True) and used
again without recomputing if the theta bins and the random
sample is exactly the same
DD : ndarray (optional)
the DD pair counts, returned if return_DD==True
"""
data = np.asarray(data)
bins = np.asarray(bins)
rng = check_random_state(random_state)
if method not in ['standard', 'landy-szalay']:
raise ValueError("method must be 'standard' or 'landy-szalay'")
if bins.ndim != 1:
raise ValueError("bins must be a 1D array")
if data.ndim == 1:
data = data[:, np.newaxis]
elif data.ndim != 2:
raise ValueError("data should be 1D or 2D")
n_samples, n_features = data.shape
Nbins = len(bins) - 1
# shuffle all but one axis to get background distribution
if data_R is None:
print "two_point says: generating random sample"
data_R = data.copy()
for i in range(n_features - 1):
rng.shuffle(data_R[:, i])
else:
data_R = np.asarray(data_R)
if (data_R.ndim != 2) or (data_R.shape[-1] != n_features):
raise ValueError('data_R must have same n_features as data')
factor = len(data_R) * 1. / len(data)
if BT_D is None:
if verbose:
print "two_point says: computing BallTree for data"
BT_D = BallTree(data)
if BT_R is None:
if verbose:
print "two_point says: computing BallTree for random sample"
BT_R = BallTree(data_R)
counts_DD = np.zeros(Nbins + 1)
counts_RR = np.zeros(Nbins + 1)
if verbose:
print "two_point says: working through the CF calc. This could take a while"
for i in range(Nbins + 1):
counts_DD[i] = np.sum(BT_D.query_radius(data, bins[i],
count_only=True))
if RR is None:
counts_RR[i] = np.sum(BT_R.query_radius(data_R, bins[i],
count_only=True))
if verbose:
print "two_point says: binning done!"
DD = np.diff(counts_DD)
if RR is None:
RR = np.diff(counts_RR)
# check for zero in the denominator
RR_zero = (RR == 0)
RR[RR_zero] = 1
if method == 'standard':
corr = factor**2 * DD / RR - 1
elif method == 'landy-szalay':
counts_DR = np.zeros(Nbins + 1)
for i in range(Nbins + 1):
counts_DR[i] = np.sum(BT_R.query_radius(data, bins[i],
count_only=True))
DR = np.diff(counts_DR)
corr = (factor ** 2 * DD - 2 * factor * DR + RR) / RR
corr[RR_zero] = np.nan
to_return=corr
if return_trees:
to_return=[to_return]
to_return.append(BT_D)
to_return.append(BT_R)
if return_RR:
if not return_trees:
to_return=[to_return]
to_return.append(RR)
if return_DD:
if (not return_trees) and (not return_RR):
to_return=[to_return]
to_return.append(DD)
return to_return
示例14: Learner
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
#.........这里部分代码省略.........
# print list(zip(*_board.getMoveList(AI_COLOR))[1])
j = list(zip(*_board.getMoveList(AI_COLOR))[1]).index(_move)
self.weights_list[i][j] *= factor
else:
self.state_list.append(state)
self.weights_list.append([1] * len(_board.getMoveList(AI_COLOR)))
# print zip(*_board.getMoveList(AI_COLOR))[1]
j = list(zip(*_board.getMoveList(AI_COLOR))[1]).index(_move)
self.weights_list[-1][j] *= factor
elif _move.color == PLAYER_COLOR:
_move = _move.getInverse()
state = _board.getInverse().getArray().tolist()
if state in self.state_list:
i = self.state_list.index(state)
# j = self.state_list[i].find(move)
j = list(zip(*_board.getInverse().getMoveList(AI_COLOR))[1]).index(_move)
self.weights_list[i][j] *= (1.0 / factor)
else:
self.state_list.append(state)
self.weights_list.append([1] * len(_board.getInverse().getMoveList(AI_COLOR)))
j = list(zip(*_board.getInverse().getMoveList(AI_COLOR))[1]).index(_move)
self.weights_list[-1][j] *= (1.0 / factor)
self.X = np.array(self.state_list)
self._tree = BallTree(self.X, metric='manhattan')
def getAiHistory(self):
return cp.deepcopy(self._ai_history)
def _getMinimax(self, current_board):
# return random.choice([bd[1] for bd in current_board.getMoveList(AI_COLOR)])
(bestBoard, bestVal) = minMax2(current_board, 6)
# print("bestVal", bestVal)
# bestBoard[0].printBoard()
return bestBoard[1]
def _getNearestNeighbors(self, current_board):
#dist, ind = self._tree.query(current_board.getArray(), k=3)
if self._tree is None:
return None
ind = self._tree.query_radius(current_board.getArray(), r = self._threshold).tolist()
ind = ind[0].tolist()
if len(ind) > 0:
pass
# print "neighbors found"
#cur_moves = current_board.getMoveList(AI_COLOR)
moves = []
weights = []
# print ind
for i in ind:
_board = Board(new_array = self.state_list[i])
assert(len(_board.getMoveList(AI_COLOR)) == len(self.weights_list[i]))
for j, (board, move) in enumerate(_board.getMoveList(AI_COLOR)):
# move.printMove()
# current_board.printBoard()
if current_board.verifyMove(AI_COLOR, move = move):
# print "move found"
# move.printMove()
if move not in moves:
moves.append(move)
weights.append(self.weights_list[i][j])
else:
weights[moves.index(move)] *= self.weights_list[i][j]
if len(moves) == 0:
# raise Exception()
# print "aborted neighbors"
return None
else:
assert(len(moves) == len(weights))
zipped = zip(moves, weights)
moves = [mv[0] for mv in zipped if mv[1] >= 1]
weights = [mv[1] for mv in zipped if mv[1] >= 1]
if len(moves) < 1: return None
return np.random.choice(moves, 1, weights)[0]
#neighbor_moves = [move for move in neighbor_moves if move in cur_moves]
def _featureTransform(self):
#replace weights with a Gaussian at some point
#or come up with a better feature transform
weights = [1, 2, 3, 4, 4, 3, 2, 1]
transformed_list = []
for state in self.state_list:
assert(len(state) == 32)
new_state = []
for i in range(32):
new_state.append(state[i] * weights[i / 4])
transformed_list.append(new_state)
self.X = np.array(transformed_list)
示例15: AngularCatalog
# 需要导入模块: from sklearn.neighbors import BallTree [as 别名]
# 或者: from sklearn.neighbors.BallTree import query_radius [as 别名]
#.........这里部分代码省略.........
if set_nbins is None:
#Get our theta bin info from the CF if we can. Error if we can't
if self._theta_bins is None:
raise ValueError("AngularCatalog.generate_rr says: I need"
" either a set number of bins (set_nbins=N)"
" or thetas from a CF to extrapolate. "
" You have given me neither.")
centers, edges = self._cfs[name].get_thetas(unit='degrees')
nbins= np.ceil( len(centers) * 2. * max_sep/edges.max())
else:
nbins=set_nbins
#Make the bins
rr_theta_bins = binclass.ThetaBins(min_sep, max_sep, nbins,
unit='d', logbins=logbins)
use_centers, use_theta_bins = rr_theta_bins.get_thetas(unit='degrees')
#Do the loop
G_p=np.zeros(nbins)
rr_counts=np.zeros(nbins)
for n_i in np.arange(n_chunks):
print "doing chunk #", n_i
#Remake the random sample so we're sure we have the right oversample factor
self.generate_random_sample(masked=True, make_exactly=number_needed)
#Code snippet shamelessly copied from astroML.correlations
xyz_data = corr.ra_dec_to_xyz(self._ra_random,
self._dec_random)
data_R = np.asarray(xyz_data, order='F').T
bins = corr.angular_dist_to_euclidean_dist(use_theta_bins)
Nbins = len(bins) - 1
counts_RR = np.zeros(Nbins + 1)
for i in range(Nbins + 1):
counts_RR[i] = np.sum(self._random_tree.query_radius(data_R, bins[i],
count_only=True))
rr = np.diff(counts_RR)
#Landy and Szalay define G_p(theta) as <N_p(theta)>/(n(n-1)/2)
G_p += rr/(number_needed*(number_needed-1))
rr_counts += rr
print "Dividing out the theta bin sizes and number of chunks"
#I divide out the bin width because just using the method
#that L&S detail gives you a {G_p,i} with the property that
#Sum[G_p,i]=1. This is not equivalent to Integral[G_p d(theta)]=1,
#which is what they assume everywhere else.
#Dividing out the bin width gives you that and lets you pretend
#G_p is a continuous but chunky-looking function.
G_p /= np.diff(use_theta_bins)
G_p /= n_chunks
self._rr_ngals=[total_number, n_chunks]
self._Gp = gpclass.Gp(min_sep, max_sep, nbins, G_p, total_number,
n_chunks, logbins=logbins, unit='d',
RR=rr_counts)
if save_to is not None:
self.save_gp(save_to)
#------------------------------------------------------------------------------------------
#-------------------------------------#
#- Read in previously calculated CFs -#
#-------------------------------------#
def load_cf(self, filen, overwrite_existing=False, name_prefix=''):
#Load in a CF from a file or set of files