当前位置: 首页>>代码示例>>Python>>正文


Python NearestNeighbors.radius_neighbors方法代码示例

本文整理汇总了Python中sklearn.neighbors.NearestNeighbors.radius_neighbors方法的典型用法代码示例。如果您正苦于以下问题:Python NearestNeighbors.radius_neighbors方法的具体用法?Python NearestNeighbors.radius_neighbors怎么用?Python NearestNeighbors.radius_neighbors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.neighbors.NearestNeighbors的用法示例。


在下文中一共展示了NearestNeighbors.radius_neighbors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_radius_neighbors_boundary_handling

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def test_radius_neighbors_boundary_handling():
    X = [[0.999, 0.001], [0.5, 0.5], [0, 1.], [-1., 0.001]]
    n_points = len(X)

    # Build an exact nearest neighbors model as reference model to ensure
    # consistency between exact and approximate methods
    nnbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X)

    # Build a LSHForest model with hyperparameter values that always guarantee
    # exact results on this toy dataset.
    lsfh = ignore_warnings(LSHForest, category=DeprecationWarning)(
        min_hash_match=0, n_candidates=n_points, random_state=42).fit(X)

    # define a query aligned with the first axis
    query = [[1., 0.]]

    # Compute the exact cosine distances of the query to the four points of
    # the dataset
    dists = pairwise_distances(query, X, metric='cosine').ravel()

    # The first point is almost aligned with the query (very small angle),
    # the cosine distance should therefore be almost null:
    assert_almost_equal(dists[0], 0, decimal=5)

    # The second point form an angle of 45 degrees to the query vector
    assert_almost_equal(dists[1], 1 - np.cos(np.pi / 4))

    # The third point is orthogonal from the query vector hence at a distance
    # exactly one:
    assert_almost_equal(dists[2], 1)

    # The last point is almost colinear but with opposite sign to the query
    # therefore it has a cosine 'distance' very close to the maximum possible
    # value of 2.
    assert_almost_equal(dists[3], 2, decimal=5)

    # If we query with a radius of one, all the samples except the last sample
    # should be included in the results. This means that the third sample
    # is lying on the boundary of the radius query:
    exact_dists, exact_idx = nnbrs.radius_neighbors(query, radius=1)
    approx_dists, approx_idx = lsfh.radius_neighbors(query, radius=1)

    assert_array_equal(np.sort(exact_idx[0]), [0, 1, 2])
    assert_array_equal(np.sort(approx_idx[0]), [0, 1, 2])
    assert_array_almost_equal(np.sort(exact_dists[0]), dists[:-1])
    assert_array_almost_equal(np.sort(approx_dists[0]), dists[:-1])

    # If we perform the same query with a slightly lower radius, the third
    # point of the dataset that lay on the boundary of the previous query
    # is now rejected:
    eps = np.finfo(np.float64).eps
    exact_dists, exact_idx = nnbrs.radius_neighbors(query, radius=1 - eps)
    approx_dists, approx_idx = lsfh.radius_neighbors(query, radius=1 - eps)

    assert_array_equal(np.sort(exact_idx[0]), [0, 1])
    assert_array_equal(np.sort(approx_idx[0]), [0, 1])
    assert_array_almost_equal(np.sort(exact_dists[0]), dists[:-2])
    assert_array_almost_equal(np.sort(approx_dists[0]), dists[:-2])
开发者ID:NelleV,项目名称:scikit-learn,代码行数:60,代码来源:test_approximate.py

示例2: __init__

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
class NNLR:

    def __init__(self, k=5, rad=2, mode='k', feat_names=None):
        self.mode = mode
        self.k = k
        self.NN = NearestNeighbors(k, radius=rad)
        
    def fit(self, X, Y):
        self.X = X
        self.Y = Y
        self.NN.fit(X)
        self.active=defaultdict(int)
    def nn_lin(self, testX, neighbors):
        l = DecisionTreeRegressor()
        return np.mean(self.Y[neighbors])
        l.fit(self.X[neighbors], self.Y[neighbors])
        # for idx in np.where(l.coef_)[0]:
            # self.active[idx]+=1
        return l.predict([testX])[0]

    def predict(self, X):
        if self.mode == 'k':
            neighbors = self.NN.kneighbors(X)[1]
        elif self.mode == 'rad':
            neighbors = self.NN.radius_neighbors(X)[1]
        return np.array([self.nn_lin(Xtst, nbr) for (Xtst, nbr) in zip(X, neighbors)])
开发者ID:edgelord,项目名称:Social-Outcome-Modeling,代码行数:28,代码来源:preprocess.py

示例3: _wpca_analysis

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def _wpca_analysis(L, C, intensities):
    """
    Determine the eccentricity of each cluster using weighted PCA (See
    Jolliffe 2002, 14.2.1). The smallest normalized explained variance
    is small for flat of filiform objects.

    - L is a numpy matrix (one point on each row)
    - intensities are gray levels of each point

    No cluster assignment is used here: a ball of radius 10 around each
    center is used to find the cloud of points.
    """
    np.set_printoptions(threshold=50000)
    n_points, n_features = L.shape
    tee.log('WPCA - Fitting NearestNeighbors on', n_points, 'points')
    nbrs = NearestNeighbors(radius=10.0).fit(L)
    for i, c in enumerate(C):
        array_c = np.array([c.x, c.y, c.z])
        i_nbrs = nbrs.radius_neighbors([array_c], 10.0, return_distance=False)[0]
        points_within = L[i_nbrs]
        if len(points_within) < 64:  # too small set, there is no point in running PCA
            c.EVR = [0.499, 0.499, 0.002]
            c.last_variance = c.EVR[2]
        else:
            w = np.sqrt(intensities[i_nbrs]/255.0)
            wX = np.dot(np.diag(w), points_within)
            pca = sklearn.decomposition.PCA(n_components=3)
            X_r = pca.fit(wX).transform(wX)
            c.EVR = pca.explained_variance_ratio_
            c.last_variance = c.EVR[2]
        print('WPCA done on', i, '/', len(C), 'name=', c.name, 'EVR=', c.EVR)
开发者ID:paolo-f,项目名称:bcfind,代码行数:33,代码来源:mscd.py

示例4: test_radius_neighbors

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def test_radius_neighbors():
    # Checks whether Returned distances are less than `radius`
    # At least one point should be returned when the `radius` is set
    # to mean distance from the considering point to other points in
    # the database.
    # Moreover, this test compares the radius neighbors of LSHForest
    # with the `sklearn.neighbors.NearestNeighbors`.
    n_samples = 12
    n_features = 2
    n_iter = 10
    rng = np.random.RandomState(42)
    X = rng.rand(n_samples, n_features)

    lshf = ignore_warnings(LSHForest, category=DeprecationWarning)()
    # Test unfitted estimator
    assert_raises(ValueError, lshf.radius_neighbors, X[0])

    ignore_warnings(lshf.fit)(X)

    for i in range(n_iter):
        # Select a random point in the dataset as the query
        query = X[rng.randint(0, n_samples)].reshape(1, -1)

        # At least one neighbor should be returned when the radius is the
        # mean distance from the query to the points of the dataset.
        mean_dist = np.mean(pairwise_distances(query, X, metric='cosine'))
        neighbors = lshf.radius_neighbors(query, radius=mean_dist,
                                          return_distance=False)

        assert_equal(neighbors.shape, (1,))
        assert_equal(neighbors.dtype, object)
        assert_greater(neighbors[0].shape[0], 0)
        # All distances to points in the results of the radius query should
        # be less than mean_dist
        distances, neighbors = lshf.radius_neighbors(query,
                                                     radius=mean_dist,
                                                     return_distance=True)
        assert_array_less(distances[0], mean_dist)

    # Multiple points
    n_queries = 5
    queries = X[rng.randint(0, n_samples, n_queries)]
    distances, neighbors = lshf.radius_neighbors(queries,
                                                 return_distance=True)

    # dists and inds should not be 1D arrays or arrays of variable lengths
    # hence the use of the object dtype.
    assert_equal(distances.shape, (n_queries,))
    assert_equal(distances.dtype, object)
    assert_equal(neighbors.shape, (n_queries,))
    assert_equal(neighbors.dtype, object)

    # Compare with exact neighbor search
    query = X[rng.randint(0, n_samples)].reshape(1, -1)
    mean_dist = np.mean(pairwise_distances(query, X, metric='cosine'))
    nbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X)

    distances_exact, _ = nbrs.radius_neighbors(query, radius=mean_dist)
    distances_approx, _ = lshf.radius_neighbors(query, radius=mean_dist)
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:61,代码来源:test_approximate.py

示例5: mean_shift

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def mean_shift(X, bandwidth, n_seeds, kernel_function='gaussian', max_iterations=100, proximity_thresh=5):
    '''
    ---Parameters---
    X : data in form (samples, dims)
    bandwidth : radius of nearest neighbors
    n_seeds : 
    kernel_update_function : can be "gaussian" or "flat" or your own kernel
    proximity_thresh : minimum distance (in pixels) a new cluster must be away from previous ones

    ---Returns---
    cluster_centers : 
    cluster_counts : how many pixels are with the neighborhood of each cluster
    '''

    import numpy as np
    from sklearn.neighbors import BallTree, NearestNeighbors
    from sklearn.utils import extmath
    from sklearn.metrics.pairwise import euclidean_distances
    from collections import defaultdict 

    if kernel_function == 'gaussian':
        kernel_update_function = gaussian_kernel
    elif kernel_function == 'flat':
        kernel_update_function = flat_kernel
    else:
        kernel_update_function = kernel_function


    n_points, n_features = X.shape
    stop_thresh = 1e-2 * bandwidth # when mean has converged                                                                                                               
    cluster_centers = []
    cluster_counts = [] 
    # ball_tree = BallTree(X)# to efficiently look up nearby points
    neighbors = NearestNeighbors(radius=bandwidth).fit(X)

    seeds = X[(np.random.uniform(0,X.shape[0], n_seeds)).astype(np.int)]
 
    # For each seed, climb gradient until convergence or max_iterations                                                                                                     
    for weighted_mean in seeds:
         completed_iterations = 0
         while True:
             points_within = X[neighbors.radius_neighbors([weighted_mean], bandwidth, return_distance=False)[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:
                # Only add cluster if it's different enough from other centers
                if len(cluster_centers) > 0:
                    diff_from_prev = [np.linalg.norm(weighted_mean-cluster_centers[i], 2) for i in range(len(cluster_centers))]
                    if np.min(diff_from_prev) > proximity_thresh:
                        cluster_centers.append(weighted_mean)
                        cluster_counts.append(points_within.shape[0])
                else:
                    cluster_centers.append(weighted_mean)
                    cluster_counts.append(points_within.shape[0])
                break
             completed_iterations += 1
 
    return cluster_centers, cluster_counts
开发者ID:MerDane,项目名称:pyKinectTools,代码行数:61,代码来源:MeanShift.py

示例6: test_radius_neighbors

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def test_radius_neighbors():
    """Checks whether Returned distances are less than `radius`

    At least one point should be returned when the `radius` is set
    to mean distance from the considering point to other points in
    the database.
    Moreover, this test compares the radius neighbors of LSHForest
    with the `sklearn.neighbors.NearestNeighbors`.
    """
    n_samples = 12
    n_features = 2
    n_iter = 10
    rng = np.random.RandomState(42)
    X = rng.rand(n_samples, n_features)

    lshf = LSHForest()
    # Test unfitted estimator
    assert_raises(ValueError, lshf.radius_neighbors, X[0])

    lshf.fit(X)

    for i in range(n_iter):
        query = X[rng.randint(0, n_samples)]
        mean_dist = np.mean(pairwise_distances(query, X, metric='cosine'))
        neighbors = lshf.radius_neighbors(query, radius=mean_dist,
                                          return_distance=False)
        # At least one neighbor should be returned.
        assert_greater(neighbors.shape[0], 0)
        # All distances should be less than mean_dist
        distances, neighbors = lshf.radius_neighbors(query,
                                                     radius=mean_dist,
                                                     return_distance=True)
        assert_array_less(distances[0], mean_dist)

    # Multiple points
    n_queries = 5
    queries = X[rng.randint(0, n_samples, n_queries)]
    distances, neighbors = lshf.radius_neighbors(queries,
                                                 return_distance=True)
    assert_equal(neighbors.shape[0], n_queries)
    assert_equal(distances.shape[0], n_queries)
    # dists and inds should not be 2D arrays
    assert_equal(distances.ndim, 1)
    assert_equal(neighbors.ndim, 1)

    # Compare with exact neighbor search
    query = X[rng.randint(0, n_samples)]
    mean_dist = np.mean(pairwise_distances(query, X, metric='cosine'))
    nbrs = NearestNeighbors(algorithm='brute', metric='cosine')
    nbrs.fit(X)

    distances_approx, _ = lshf.radius_neighbors(query, radius=mean_dist)
    distances_exact, _ = nbrs.radius_neighbors(query, radius=mean_dist)
    # Distances of exact neighbors is less than or equal to approximate
    assert_true(np.all(np.less_equal(np.sort(distances_exact[0]),
                                     np.sort(distances_approx[0]))))
开发者ID:CC-Fu-CC,项目名称:scikit-learn,代码行数:58,代码来源:test_approximate.py

示例7: compute

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
 def compute(self):
     nn = NearestNeighbors(radius=self.eps, algorithm='auto', metric=self.metric).fit(self.x)
     self.distances, self.indices = nn.radius_neighbors(self.x, self.eps)
     print self.distances.shape, self.indices.shape
     for i in xrange(self.n):
         if not self.processed[i]:
             self.expand_cluster_order(i)
     assert self.ordered_file_index == self.n
     # print self.ordered_file
     self.draw_reachability_plot()
     return self.ordered_file
开发者ID:arssher,项目名称:sphere_dm,代码行数:13,代码来源:clusterization.py

示例8: build

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
    def build(self,tweets,minimalTermPerTweet=5, remove_noise_with_poisson_Law=False) :
        """
        Return an upper sparse triangular matrix of similarity j>i
        """
        timeThreshold=float(self.timeThreshold)
        distanceThreshold=float(self.distanceThreshold)
        useOnlyHashtags=self.useOnlyHashtags
        numberOfTweets=len(tweets)

        M=dok_matrix((numberOfTweets, numberOfTweets),dtype=np.float)
        print "      Calculating TF-IDF vectors ..."
        TFIDFVectors,TweetPerTermMap=getTweetsTFIDFVectorAndNorm(tweets, minimalTermPerTweet=minimalTermPerTweet, remove_noise_with_poisson_Law=remove_noise_with_poisson_Law,useOnlyHashtags=useOnlyHashtags)
        print "      Constructing similarity matrix ..."

        distanceThresholdInDegree=distanceThreshold/DEG_LATITUDE_IN_METER
        spatialIndex=NearestNeighbors(radius=distanceThresholdInDegree, algorithm='auto')
        spatialIndex.fit(np.array([(tweet.position.latitude,tweet.position.longitude) for tweet in tweets]))

        SHOW_RATE=100

        for i in range(numberOfTweets) :
            if (i%SHOW_RATE==0) : print "\t",i,";"
            
            tweetI,TFIDFVectorI=tweets[i],TFIDFVectors[i]
            neighboors=set()
            
            #Recuperation des voisins par mots (les tweets ayant au moins un term en commun)
            TFIDFVectorIKeySet=set(TFIDFVectorI)
            for term in TFIDFVectorIKeySet : neighboors|=TweetPerTermMap[term]

            #Recuperation des voisins en espace (les tweets dans le voisinage self.distanceThreshold)
            position=np.array([tweetI.position.latitude,tweetI.position.longitude]).reshape(-1,2)
            neighboors&=set(spatialIndex.radius_neighbors(position)[1][0])

            for j in neighboors :
                tweetJ=tweets[j]

                """
                Ignorer les tweets qui ne sont pas apres le tweetI
                Ignorer les tweets qui ne sont pas dans le voisinage temporelle du tweetI
                """
                if (j<=i or tweetJ.delay(tweetI)>self.timeThreshold) : continue
                
                TFIDFVectorJ=TFIDFVectors[j]
                TFIDFVectorJKeySet=set(TFIDFVectorJ)
                keysIntersection=TFIDFVectorIKeySet & TFIDFVectorJKeySet
                similarity=0
                for term in keysIntersection : similarity+=TFIDFVectorI[term]*TFIDFVectorJ[term]
                M[i,j]=similarity

        return coo_matrix(M)
开发者ID:AnesBendimerad,项目名称:EventDetectionFromTwitter,代码行数:53,代码来源:LEDSimilarityMatrixBuilder.py

示例9: getSim_dense

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def getSim_dense(day, centroids, dataset, thred_radius_dist, vdw, rel_dw):
    print "## Begin calculating centroid dataset sim.", len(centroids), dataset.shape
    dataset_vdw = dataset[range(vdw[0], vdw[1]),:]

    if 1:
        nnModel = NearestNeighbors(radius=thred_radius_dist, algorithm='brute', metric='minkowski', p=2, n_jobs=1)
        num_centroids = len(centroids)
        #allData = np.append(centroids, dataset, axis=0)
        nnModel.fit(dataset)
        ngIdxArray = nnModel.radius_neighbors(centroids, thred_radius_dist, return_distance=False)
    if 0:
        ngIdxArray = []
        for vecId, vec in enumerate(centroids):#.reshape(1, -1).tolist()
            distArr = euclidean_distances(np.array([vec]), dataset_vdw)
            nn_keys = [i+vdw[0] for i, eu in enumerate(distArr[0]) if eu <= thred_radius_dist]
            ngIdxArray.append(np.asarray(nn_keys, dtype=np.int32))
        ngIdxArray = np.asarray(ngIdxArray)

    print "## nn cal completed", time.asctime()
    return ngIdxArray
开发者ID:qolina,项目名称:DBED,代码行数:22,代码来源:getSim.py

示例10: _finalize_masses

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def _finalize_masses(X, C, intensities):
    """
    Regardless of the parameters of the algorithm, place a ball of
    radius 10 around each center and compute the mass in the ball.
    Rationale: thresholding for discriminating between centers and non
    centers should not depend on parameters used to seek the centers.
    This mass will be later used for the recall-precision curve.
    Hopefully wild variations of performance across different
    substacks will be reduced this way.
    """
    n_points, n_features = X.shape
    tee.log('Finalizing masses - Fitting NearestNeighbors on', n_points, 'points')
    nbrs = NearestNeighbors(radius=10.0).fit(X)

    for c in C:
        array_c = np.array([c.x, c.y, c.z])
        i_nbrs = nbrs.radius_neighbors([array_c], 10.0, return_distance=False)[0]
        points_within = X[i_nbrs]
        if len(points_within) == 0:
            break
        c.mass = sum(intensities[i_nbrs])
开发者ID:paolo-f,项目名称:bcfind,代码行数:23,代码来源:mscd.py

示例11: _mi_dc

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
def _mi_dc(x, y, k):
    """
    Calculates the mututal information between a continuous vector x and a
    disrete class vector y.

    This implementation can calculate the MI between the joint distribution of
    one or more continuous variables (X[:, 1:3]) with a discrete variable (y).

    Thanks to Adam Pocock, the author of the FEAST package for the idea.

    Brian C. Ross, 2014, PLOS ONE
    Mutual Information between Discrete and Continuous Data Sets
    """

    y = y.flatten()
    n = x.shape[0]
    classes = np.unique(y)
    knn = NearestNeighbors(n_neighbors=k)
    # distance to kth in-class neighbour
    d2k = np.empty(n)
    # number of points within each point's class
    Nx = []
    for yi in y:
        Nx.append(np.sum(y == yi))

    # find the distance of the kth in-class point
    for c in classes:
        mask = np.where(y == c)[0]
        knn.fit(x[mask, :])
        d2k[mask] = knn.kneighbors()[0][:, -1]

    # find the number of points within the distance of the kth in-class point
    knn.fit(x)
    m = knn.radius_neighbors(radius=d2k, return_distance=False)
    m = [i.shape[0] for i in m]

    # calculate MI based on Equation 2 in Ross 2014
    MI = psi(n) - np.mean(psi(Nx)) + psi(k) - np.mean(psi(m))
    return MI
开发者ID:bacalfa,项目名称:mifs,代码行数:41,代码来源:mi.py

示例12: NNR

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
class NNR(object):
    def __init__(self, r=1.0, k=10, def_mean=0, def_sd=float('inf'), off=0.1):
        self.reg = KRadiusNeighborsRegressor(
            n_neighbors=k, radius=r, defval=def_mean, weights=self.comp_weights)
        # Regresses squared error
        self.err_reg = NearestNeighbors(n_neighbors=k, radius=r)
        self.off = off
        self.def_sd = float(def_sd)

    def comp_weights(self, dists):
        return 1.0 / (dists + self.off)

    def fit(self, X, y):
        self.reg.fit(X, y)
        errs = y - self.reg.predict(X)
        self.sse = errs * errs
        self.err_reg.fit(X)

    def predict(self, X, return_std=False):
        if not return_std:
            return self.reg.predict(X)
        else:
            mean_val = self.reg.predict(X)

            sds = []
            dists, inds = self.err_reg.radius_neighbors(
                X, return_distance=True)
            for d, i in zip(dists, inds):
                if len(d) < 2:
                    sds.append(self.def_sd)
                    continue
                else:
                    errs = self.sse[i]
                    weights = self.comp_weights(d)
                    sd = np.average(errs, axis=0, weights=weights) / len(d)
                    sds.append(sd)

            return mean_val, np.array(sds)
开发者ID:Humhu,项目名称:percepto,代码行数:40,代码来源:test_modal.py

示例13: GlobalKMeans

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]

#.........这里部分代码省略.........
        centroids.shape = (1, X.shape[1])

        # compute the distance of the examples to the centroids
        mindist = np.zeros(X.shape[0])
        for i in range(X.shape[0]):
            mindist[i] = \
            euclidean_distances(X[i].reshape(1, -1), centroids[assignments[i]].reshape(1, -1), squared=True)[0]

        for k in range(2, self.n_clusters + 1):
            newCentroid = self._compute_next_centroid(X, centroids, assignments, mindist)
            centroids = np.vstack((centroids, newCentroid))
            km = KMeans(n_clusters=k, init=centroids, n_init=1)
            km.fit(X)
            assignments = km.labels_
            for i in range(X.shape[0]):
                mindist[i] = \
                euclidean_distances(X[i].reshape(1, -1), centroids[assignments[i]].reshape(1, -1), squared=True)[0]

        return km.cluster_centers_, km.labels_, km.inertia_

    def _compute_next_centroid(self, X, centroids, assignments, mindist):
        """
        Computes the candidate for the next centroid

        :param X:
        :param centroids:
        :return:
        """
        minsum = np.infty
        candCentroid = None

        # Compute the first candidate to new centroid
        for i in range(X.shape[0]):
            distance = euclidean_distances(X[i].reshape(1, -1), centroids[assignments[i]].reshape(1, -1))[0]
            S2 = self._neighbors.radius_neighbors(X[i].reshape(1, -1), radius=distance, return_distance=False)[0]
            S2centroid = np.sum(X[S2], axis=0) / len(S2)
            S2centroid.shape = (1, X.shape[1])
            cost = self._compute_fk(X, mindist, S2centroid)

            if cost < minsum:
                minsum = cost
                candCentroid = S2centroid

        # Compute examples for the new centroid
        S2 = []
        newDist = euclidean_distances(X, candCentroid.reshape(1, -1), squared=True)
        for i in range(X.shape[0]):
            if newDist[i] < mindist[i]:
                S2.append(i)

        newCentroid = sum(X[S2]) / len(S2)
        newCentroid.shape = (1, X.shape[1])

        while not (candCentroid == newCentroid).all():
            candCentroid = newCentroid
            S2 = []
            newDist = euclidean_distances(X, candCentroid.reshape(1, -1), squared=True)
            for i in range(X.shape[0]):
                if newDist[i] < mindist[i]:
                    S2.append(i)

            newCentroid = np.sum(X[S2], axis=0) / len(S2)
            newCentroid.shape = (1, X.shape[1])

        return candCentroid

    def _compute_fk(self, X, mindist, ccentroid):
        """
        Computes the cost function

        :param X:
        :param mindist:
        :param ccentroid:
        :return:
        """

        # Distances among the examples and the candidate centroid
        centdist = euclidean_distances(X, ccentroid.reshape(1, -1), squared=True)

        fk = 0
        for i in range(X.shape[0]):
            fk = fk + min(mindist[i], centdist[i][0])

        return fk

    @staticmethod
    def _find_nearest_cluster(examp, centers):
        """
        Finds the nearest cluster for an example
        :param examp:
        :param centers:
        :return:
        """

        dist = euclidean_distances(centers, examp.reshape(1, -1))

        pmin = np.argmin(dist)
        vmin = np.min(dist)

        return pmin, vmin
开发者ID:allinox,项目名称:kemlglearn,代码行数:104,代码来源:GlobalKMeans.py

示例14: plan_cached_rrt

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]

#.........这里部分代码省略.........
            pmin_idx = pnearest_idx

            cum_c = self.integrate_costs(edge_C,parents,pnearest_idx)
            min_edge_c = self.cost_manager.path_cost(path_new,goal_xy[:2])
            cmin = cum_c +min_edge_c
            cumulative_costs = {}
            Pnear_fwd = cached["Pnear_forward"]
            for num,p_idx in enumerate(Pnear_fwd):
                p = V_xy[p_idx]
                p_xyz = V[p_idx]
                cum_cost = self.integrate_costs(edge_C,parents,p_idx)
                cumulative_costs[p_idx] = cum_cost
                p_idx_path = cached["pnear_pnew"][num]["path"]
                reached = cached["pnear_pnew"][num]["reached"]
                safe = cached["pnear_pnew"][num]["safe"]
                if reached is True and safe is True:
                    path_c = self.cost_manager.path_cost(p_idx_path,goal_xy[:2])
                else:
                    path_c = 0
                #reached = False
                c = cum_cost + path_c
                if (safe is True and
                    reached is True and c < cmin):
                    cmin = c
                    min_edge_c = path_c
                    pmin_idx = p_idx      

            if E.has_key(pmin_idx):
                E[pmin_idx].add(len(V))
            else:
                E[pmin_idx] = set([len(V)])   
            edge_C[pmin_idx,len(V)] = min_edge_c  
            cumulative_last = cmin     
            pnew_idx = len(V)
            V.append(pnew)
            p4dist = np.zeros(4)
            p4dist[:2] = pnew[:2];p4dist[2] = np.cos(pnew[2]);p4dist[3] = np.sin(pnew[2])
            V_xy.append(p4dist)
            parents[pnew_idx] = pmin_idx
            """
            Re-wire the tree
            """
            unsafe = 0
            Pnear_bwd = cached["Pnear_backward"]
            for en,p_idx in enumerate(Pnear_bwd):
                if parents.has_key(p_idx):
                    if not cumulative_costs.has_key(p_idx):
                        cumulative_costs[p_idx] = self.integrate_costs(edge_C,parents,p_idx)
                    p_xyz = V[p_idx]
                    rewire_path = cached["pnew_pnear"][en]["path"]
                    rewire_reached = cached["pnew_pnear"][en]["reached"]
                    rewire_safe = cached["pnew_pnear"][en]["safe"]
                    #rewire_path,rewire_reached = posq.simulate(pnew,p_xyz,steps = int(stp))
                    if rewire_reached is True and rewire_safe is True :
                        rewire_path_c = self.cost_manager.path_cost(rewire_path,goal_xy[:2])
                    else:
                        rewire_path_c = 0
                    c = cumulative_last + rewire_path_c
                    if (rewire_safe is True and c < cumulative_costs[p_idx] and rewire_reached is True):
                        E[parents[p_idx]].remove(p_idx)
                        edge_C.pop(parents[p_idx],p_idx)
                        edge_C[pnew_idx,p_idx] = rewire_path_c
                        parents[p_idx] = pnew_idx
                        if E.has_key(pnew_idx):
                            E[pnew_idx].add(p_idx)
                        else:
                            E[pnew_idx] = set([p_idx])
            rrt_iter +=1
            if rrt_iter==len(cached_points):
                planning_done=True
                nbrs.fit(V_xy)
                p4dist = np.zeros(4)
                p4dist[:2] = goal_xy[:2];p4dist[2] = np.cos(goal_xy[2]);p4dist[3] = np.sin(goal_xy[2])
                points_near_goal = []
                add = 0
                while len(points_near_goal)==0:
                    dist,points_near_goal = nbrs.radius_neighbors(p4dist, self.goal_tolerance+add, return_distance = True)
                    points_near_goal = points_near_goal[0]
                    add +=0.1
                print "DONE PLANNING"
                print "TIME TAKEN",time.time()-t1
                print "POINTS NEAR GOAL",points_near_goal

        #self.samp_point_pub.publish(marker_points)
        """
        Find best path:
        """
        min_cost = None;
        for i in points_near_goal:
            c_path = self.integrate_costs(edge_C,parents,i)
            if c_path < min_cost or min_cost==None:
                m = i
                min_cost = c_path
        self.publish_rrt(V,E)   
        print "MINIMUM PATH COST RRT",min_cost
        path = self.get_path(parents,V,m)
        pt = path_to_pose(path)            
        print 'total time: ', time.time()-t1 
        self._path_pub.publish(pt)
        return pt,path
开发者ID:KyriacosShiarli,项目名称:rrt_learn_catkin,代码行数:104,代码来源:motion_planner_posq.py

示例15: make_cached_rrt

# 需要导入模块: from sklearn.neighbors import NearestNeighbors [as 别名]
# 或者: from sklearn.neighbors.NearestNeighbors import radius_neighbors [as 别名]
    def make_cached_rrt(self,sample_fn,points_to_cache = 4500,bias=0.02):
        """
        CAching the RRT
        """
        print "NOW CACHING RRT ---"
        marker_points = MarkerArray()
        vol_freecells = len(self._freecells)*self._navmap.info.resolution**2
        gamma_rrg = 2*sqrt(1.5*vol_freecells/pi)
        probot = np.array([self._robot_pose.pose.position.x,self._robot_pose.pose.position.y,2*np.arccos(self._robot_pose.pose.orientation.w)])

        # V is a list of edges. E is a disctionary where the key is connected with its values.
        # parents is  a disctionary where parent of key is value. Since is a key, each node has only one parent
        V = [probot]
        p4dist = np.zeros(4)
        p4dist[:2] = probot[:2];p4dist[2] = np.cos(probot[2]);p4dist[3] = np.sin(probot[2])
        V_xy = [p4dist]

        sampled_points = []
        Dist = [0.0]
        # C stores the cost at vertex idx which is hte sum of the edges going to it.
        goal_xy = np.array([self._goal.pose.position.x,self._goal.pose.position.y,2*np.arccos(self._goal.pose.orientation.w)])
        edge_C = {}
        planning_time=self.planning_time
        nbrs = NearestNeighbors(n_neighbors=1,algorithm="kd_tree",leaf_size = 30)
        lowest_cost_idx = None
        nbrs.fit(V_xy)
        t1 = time.time()
        planning_done = False
        rrt_iter = 0
        bias = 0.02
        stp = 8
        while not planning_done:
            cached_nbrs ={}
            t2 = time.time()
            #bias/=1.001 # gradually reduce the bias for the target
            """
            Sampling new point
            """
            reached = False
            samp_count = 0
            alternative = True
            while reached ==False:
                    
                prand,g_s = sample_fn(goal_xy,bias = bias)
                p4dist = np.zeros(4)
                p4dist[:2] = prand[:2];p4dist[2] = np.cos(prand[2]);p4dist[3] = np.sin(prand[2])
                #(dist, idx) = nbrs.kneighbors(prand.reshape(1, -1))
                (dist, idx) = nbrs.kneighbors(p4dist.reshape(1, -1))
                pnearest_idx = idx.flatten(1)[0]
                pnearest = V[pnearest_idx]

                """
                Turning new point into reachable point
                """


                stp = 50
                path_new,reached,stp = posq.simulate(pnearest,prand,steps = stp,return_steps=True)
                pnew = path_new[-1]
                if alternative == True:
                    d = prand[:2] - pnearest[:2]
                    ang = np.arctan2(d[1],d[0])

                    add = np.array([self._rrt_eta*np.cos(ang),self._rrt_eta*np.sin(ang),ang])
                    pnew =np.zeros(3)
                    pnew[:2] = pnearest[:2]+add[:2]
                    pnew[2] = ang
                
                #self.publish_local_path(pub_path)
                #pnew = [pnearest[0]+ self._rrt_eta*np.cos(pnearest[2]),pnearest[1]+ self._rrt_eta*np.sin(pnearest[2]),pnearest[2]]
                if reached == True:
                    pnew = prand
                elif reached == False:
                    stp = 400
                    path_new,reached,stp = posq.simulate(pnearest,pnew,steps = stp,return_steps = True,eps=0.1)
                    pnew = path_new[-1]
                """
                Checking if segment is valid and updating graph
                """
                #stp = 40
                
                #stp = 30

            """
            Checking if segment is valid and updating graph
            """
            if self.path_safe(path_new) is True:

                r = np.min([gamma_rrg*sqrt(log(len(V))/float(len(V))),self._rrt_eta])
                p4dist = np.zeros(4)
                p4dist[:2] = pnew[:2];p4dist[2] = np.cos(pnew[2]);p4dist[3] = np.sin(pnew[2])
                #Pnear_idx = nbrs.radius_neighbors(pnew.reshape(1, -1)[:,:2], r, return_distance = False)
                Pnear_idx = nbrs.radius_neighbors(p4dist.reshape(1, -1), r, return_distance = False)
                Pnear_idx = Pnear_idx[0]
                cached_nbrs["prand"] = prand
                cached_nbrs["pnearest_idx"] = pnearest_idx
                cached_nbrs["path_new"] = path_new
                cached_nbrs["pnew"] = pnew
                cached_nbrs["path_new"] = path_new
                cached_nbrs["Pnear_idx"] = Pnear_idx
#.........这里部分代码省略.........
开发者ID:KyriacosShiarli,项目名称:rrt_learn_catkin,代码行数:103,代码来源:motion_planner_posq.py


注:本文中的sklearn.neighbors.NearestNeighbors.radius_neighbors方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。