示例#1
0
 def fit(self, X, y):
     t = time()  # get labels for test data
     # build the graph result is the affinity matrix
     if self.kernel is 'dbscan' or self.kernel is None:
         affinity_matrix = self.dbscan(X, self.eps, self.minPts)
     # it is possible to use other kernels -> as parameter
     elif self.kernel is 'rbf':
         affinity_matrix = rbf_kernel(X, X, gamma=self.gamma)
     elif self.kernel is 'knn':
         affinity_matrix = NearestNeighbors(self.naighbors).fit(X).kneighbors_graph(X, self.naighbors).toarray()
     else:
         raise
     print( "praph(%s) time %2.3fms"%(self.kernel, (time() - t) *1000))
     if affinity_matrix.max() == 0 :
         print("no affinity matrix found")
         return y
     
     degree_martix   = np.diag(affinity_matrix.sum(axis=0))
     affinity_matrix = np.matrix(affinity_matrix)
     
     try:
         inserve_degree_matrix = np.linalg.inv(degree_martix)
     except np.linalg.linalg.LinAlgError as err:
         if 'Singular matrix' in err.args:
             # use a pseudo inverse if it's not possible to make a normal of the degree matrix
             inserve_degree_matrix =  np.linalg.pinv(degree_martix)
         else:
             raise
         
     matrix = inserve_degree_matrix * affinity_matrix
     # split labels in different vectors to calculate the propagation for the separate label
     labels = np.unique(y)
     labels = [x for x in labels if x != self.unlabeledValue]
     # init the yn1 and y0
     y0  = [[1 if (x == l) else 0 for x in y] for l in labels]
     yn1 = y0
     # function to set the probability to 1 if it was labeled in the source
     toOrgLabels      = np.vectorize(lambda x, y : 1 if y == 1 else x , otypes=[np.int0])
     # function to set the index's of the source labeled
     toOrgLabelsIndex = np.vectorize(lambda x, y, z : z if y == 1 else x , otypes=[np.int0])
     lastLabels       = np.argmax(y0, axis=0)
     while True:
         yn1 = yn1 * matrix
         #first matrix to labels
         ynLablesIndex = np.argmax(yn1, axis=0)
         # row-normalize
         yn1 /= yn1.max()
         yn1 = toOrgLabels(yn1, y0)
         for x in y0:
             ynLablesIndex = toOrgLabelsIndex(ynLablesIndex, x, y0.index(x))
         #second original labels to result
         if np.array_equiv(ynLablesIndex, lastLabels):
             break
         lastLabels = ynLablesIndex
     # result is the index of the labels -> cast index to the given labels
     toLabeles = np.vectorize(lambda x : labels[x])
     return np.array(toLabeles(lastLabels))[0]