示例#1
0
    def fit(self, X):
        n, d = X.shape
        k = self.k
        self.mu = np.mean(X, 0)
        X = X - self.mu

        # Randomly initial Z, W
        z = np.random.randn(n * k)
        w = np.random.randn(k * d)

        squared_term = (np.dot(z.reshape(n, k), w.reshape(k, d)) - X)**2
        f = np.sum(np.sqrt(squared_term + self.epsilon))

        for i in range(50):
            f_old = f
            z = find_min(self._fun_obj_z, z, 10, False, w, X, k)
            w = find_min(self._fun_obj_w, w, 10, False, z, X, k)
            # f = np.sum(np.dot(z.reshape(n,k),w.reshape(k,d))-X)
            squared_term = (np.dot(z.reshape(n, k), w.reshape(k, d)) - X)**2
            f = np.sum(np.sqrt(squared_term + self.epsilon))
            print('Iteration {:2d}, loss = {}'.format(i, f))
            if f_old - f < 1e-4:
                break

        self.W = w.reshape(k, d)
        return self
示例#2
0
    def compress(self, X):
        n = X.shape[0]
        # nearest_neighbours = np.zeros((n, self.nn))

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.

        adjacency_matrix = np.zeros((n, n))
        nearest_neighbours = self.knn(X)
        for i, j in enumerate(nearest_neighbours):
            for neighbour in j:
                adjacency_matrix[i, neighbour] = D[i, neighbour]
                adjacency_matrix[neighbour, i] = D[neighbour, i]

        dijkstra = utils.dijkstra(adjacency_matrix)

        dijkstra[np.isinf(dijkstra)] = dijkstra[~np.isinf(dijkstra)].max()
        # Initialize low-dimensional representation with PCA
        Z = PCA(self.k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, dijkstra)
        Z = z.reshape(n, self.k)
        return Z
示例#3
0
def Graham(points, show, save):

    # on definit une variable global essentiel pour pouvoir choisir notre "pivot"
    # avec lequel on pour voir faire nos calculs d'angles pour faire le balayages de Graham
    global point_pivot

    p0 = find_min(points)
    # notre point_pivot est essentiel pour faire le scan de Graham
    # on a choisi d'utiliser le point le plus bas. On fait tous les calculs par rapport à celui-ci
    # on l'a choisi car on sait que ce point sera toujours dans l'enveloppe convexe !!
    point_pivot = points[points.index(p0)]

    trie_points = quicksort(points)
    del trie_points[trie_points.index(p0)]
    # hull est la liste contenant les points de l'enveloppe convexe
    hull = [p0, trie_points[0]]

    for s in trie_points[1:]:
        while determinant(hull[-2], hull[-1], s) <= 0:
            del hull[-1]
            if len(hull) < 2:
                break

        hull.append(s)
    return hull
示例#4
0
文件: manifold.py 项目: jaysc96/CS340
    def compress(self, X):
        n = X.shape[0]
        k = self.k
        K = self.K

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)
        nbrs = np.argsort(D, axis=1)[:, 1:K + 1]
        G = np.zeros((n, n))

        for i in range(n):
            for j in nbrs[i]:
                G[i, j] = D[i, j]
                G[j, i] = D[j, i]

        D = utils.dijkstra(G)
        D[D == np.inf] = -np.inf
        max = np.max(D)
        D[D == -np.inf] = max

        # Initialize low-dimensional representation with PCA
        Z = PCA(k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
示例#5
0
def Shamos_bis(E):
    n = len(E)
    if n < 4:
        return polar_quicksort(E, find_min(E))
    else:
        E1, E2 = Division(E)
        return Fusion(Shamos_bis(E1), Shamos_bis(E2))
示例#6
0
def Jarvis(points, show, save):

    # on prend le point le plus en bas pour trier on sait qu'il sera
    # forcement dans l'enveloppe convexe

    # la fonction pour trouver le min est en O(n)
    a = find_min(points)
    # fonction index tres pratiquep our recuperer la postition
    #d'un element dans une liste
    index = points.index(a)

    l = index
    # la liste de l'enveloppe convexe
    result = []
    result.append(a)

    while (True):
        q = (l + 1) % len(points)
        for i in range(len(points)):
            if i == l:
                continue

            #si c'est colinéaire, on prend le point le plus loin
            d = determinant(points[l], points[i], points[q])
            if d > 0 or (d == 0 and distance(points[i], points[l]) > distance(
                    points[q], points[l])):
                #print("on rentre dans la boucle")
                q = i
        l = q
        if l == index:
            break
        result.append(points[q])

    return result
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # Construct nearest neighbour graph
        G = np.zeros([n, n])
        for i in range(n):
            neighbours = np.argsort(D[i])[:self.nn + 1]
            for j in neighbours:
                G[i, j] = D[i, j]
                G[j, i] = D[j, i]

        # Compute ISOMAP distances
        D = utils.dijkstra(G)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        Z = PCA(self.k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, self.k)
        return Z
示例#8
0
    def compress(self, X, n_components, n_neighbours):
        n = X.shape[0]
        k = self.k
        numNeighbours = self.numNeighbours

        # find the distances to every other point
        euclD = utils.euclidean_dist_squared(X, X)
        euclD = np.sqrt(euclD)

        knnD = np.zeros((n, n))

        # get the KNN of point i
        for i in range(n):
            # finds numNeighbours smallest distances from obj_i
            # +1 because it will always select itself as (distance of 0), and distances are non-negative
            minIndexes = np.argsort(euclD[i])[:numNeighbours + 1]

            for index in minIndexes:
                # add distances of KNN_i to the distance matrix
                knnD[i, index] = euclD[i, index]

        D = np.zeros((n, n))
        # get distance of every other path using only KNN
        for i in range(n):
            for j in range(n):
                if i != j:
                    D[i, j] = utils.dijkstra(knnD, i, j)

        Z = AlternativePCA(k).fit(X).compress(X)
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
示例#9
0
 def compress(self, X):
     n, d = X.shape
     k = self.k
     X = X - self.mu
     # We didn't enforce that W was orthogonal so we need to optimize to find Z
     z = np.zeros(n * k)
     z = find_min(self._fun_obj_z, z, 100, False, self.W.flatten(), X, k)
     Z = z.reshape(n, k)
     return Z
示例#10
0
    def compress(self, X):
        n = X.shape[0]
        k = self.k

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # Initialize low-dimensional representation with PCA
        Z = PCA(k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
示例#11
0
from utils import find_max
from utils import find_min

numbers = [12, 89, 78, 45, 34, 19, 6, 9, 56, 2]
max_num = find_max(numbers)
minimum = find_min(numbers)
print(max_num)
print(minimum)
示例#12
0
from utils import find_min
numbers = [
    10, 3, 6, 2, 1000, 23, 56, 111111, 23, 45, 78, 7, 7, 5, 7, 8, 7, 5, 4
]
min = find_min(numbers)
print(min)
示例#13
0
import utils
numbers = [245,512,51]
maximum = utils.find_max(numbers)
minimum = utils.find_min(numbers)
ave = utils.average(numbers)
print(ave)