def make_distance(Ad, A, vertex_num, edge_num, B): #把B转换为对角阵再转换为全矩阵 D = np.diag(B) #Y_transposed为Y的转置矩阵 A_transposed = np.transpose(A) #L2为A和A的转职的积 L2 = np.dot(A, A_transposed) #求L2的费德勒向量 v = fiedler(L2) #构建距离矩阵初始化为空 size_of_distance_matrix = (edge_num, 5) Distance = np.zeros(size_of_distance_matrix) index1 = 0 index2 = 0 count = 0 for i in range(0, edge_num): for j in range(0, vertex_num): if (A[j][i] == 1): index1 = j elif (A[j][i] == -1): index2 = j break Distance[count][0] = index1 + 1 Distance[count][1] = index2 + 1 Distance[count][2] = i + 1 Distance[count][3] = 0 if (B[index1] == 1 or B[index2] == 1): Distance[count][4] = 1 / math.exp(100) else: w = 1 / math.exp(max((abs(v[index1]), abs(v[index2])))) Distance[count][4] = w * ((v[index1] - v[index2])**2) count = count + 1 return Distance
def fiedler_determinant_test ( ): #*****************************************************************************80 # ## FIEDLER_DETERMINANT_TEST tests FIEDLER_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 January 2015 # # Author: # # John Burkardt # from fiedler import fiedler from r8vec_uniform_ab import r8vec_uniform_ab from r8mat_print import r8mat_print print '' print 'FIEDLER_DETERMINANT_TEST' print ' FIEDLER_DETERMINANT computes the FIEDLER determinant.' m = 5 n = m x_lo = -5.0 x_hi = +5.0 seed = 123456789 x, seed = r8vec_uniform_ab ( n, x_lo, x_hi, seed ) a = fiedler ( m, n, x ) r8mat_print ( m, n, a, ' FIEDLER matrix:' ) value = fiedler_determinant ( n, x ) print '' print ' Value = %g' % ( value ) print '' print 'FIEDLER_DETERMINANT_TEST' print ' Normal end of execution.' return
def fiedler_determinant_test(): #*****************************************************************************80 # ## FIEDLER_DETERMINANT_TEST tests FIEDLER_DETERMINANT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 January 2015 # # Author: # # John Burkardt # from fiedler import fiedler from r8vec_uniform_ab import r8vec_uniform_ab from r8mat_print import r8mat_print print '' print 'FIEDLER_DETERMINANT_TEST' print ' FIEDLER_DETERMINANT computes the FIEDLER determinant.' m = 5 n = m x_lo = -5.0 x_hi = +5.0 seed = 123456789 x, seed = r8vec_uniform_ab(n, x_lo, x_hi, seed) a = fiedler(m, n, x) r8mat_print(m, n, a, ' FIEDLER matrix:') value = fiedler_determinant(n, x) print '' print ' Value = %g' % (value) print '' print 'FIEDLER_DETERMINANT_TEST' print ' Normal end of execution.' return
def main(): fn = sys.argv[1] col = int(sys.argv[2]) fo=open(fn) (adj_list, iByn, nByi) = fiedler.file_parse(fo, node2=1, val_col=col) fo.close() fn = os.path.basename(fn) fied = fiedler.fiedler(adj_list, fn=fn, plot=False, n_fied=2) fied["adj"] = adj_list fied["iByn"] = iByn fied["nByi"] = nByi fo = open(fn + ".json", "w") json.dump(fied, fo) fo.close()
def RequestHandler(info,outfo): """ Parse input,do caclulation, transform into proper format and dump json. info: a file like object containing a sif like file outfo: a file like object to write json to """ #Change 2 to 1 below to accept 2 column files. #Could also parse json of the form [["node1","node2"],...] with something like #(adj_list,iByn,nByi)=fiedler.file_parse(["%s\t%s"%(e[0],e[1]) for e in json.load(info)],0,2) (adj_list,iByn,nByi)=fiedler.file_parse(info,0,2) fied=fiedler.fiedler(adj_list,plot=False,n_fied=2) out = fied["f1"][:] for i,name in enumerate(nByi): out[i]={"name":name} for key in fied: if not key in ["iByn", "nByi"]: out[i][key]=fied[key][i] json.dump(out,outfo)
def main(): fn = sys.argv[1] fo = open(fn) cutoff = 1.0 if len(sys.argv) > 2: cutoff = float(sys.argv[2]) #fn += "cutoff"+str(cutoff) (adj_list, iByn, nByi, spliter_by_feature) = parseRfPredict(fo, cutoff) fo.close() fied = fiedler.fiedler(adj_list, fn=fn, plot=False, n_fied=2) fied["adj"] = adj_list fied["iByn"] = iByn fied["nByi"] = nByi #fied["sByf"] = spliter_by_feature # fo = open(os.path.basename(fn) +".cutoff."+ str(filter_min) + ".splitters", "w") # json.dump(spliter_by_feature, fo, indent=2) # fo.close() outfn=os.path.basename(fn) +".cutoff."+ str(cutoff) + ".json" fo = open(outfn, "w") print "Outputing fiedler results for %s to %s"%(os.path.abspath(fn),os.path.abspath(outfn)) json.dump(fied, fo, indent=2) fo.close()
def main(): fn = sys.argv[1] filter_min = "" filter_min = float(sys.argv[2]) col = int(sys.argv[3]) filter_col = col if len(sys.argv)>4: filter_col=int(sys.argv[4]) print "Parseing %s min %s val_col %s filter_col %s"%(os.path.abspath(fn),filter_min,col,filter_col) (adj_list, iByn, nByi) = filename_parse(fn, filter_min, col, filter_col) fn = os.path.basename(fn) fied = fiedler.fiedler(adj_list, fn=fn + str(filter_min), plot=False, n_fied=2) fied["adj"] = adj_list fied["iByn"] = iByn fied["nByi"] = nByi outfn=os.path.basename(fn) +".cutoff."+ str(filter_min) + ".json" fo = open(outfn, "w") print "Outputing fiedler results for %s to %s"%(os.path.abspath(fn),os.path.abspath(outfn)) json.dump(fied, fo) fo.close()