コード例 #1
0
ファイル: kmeans4_main.py プロジェクト: ayvak/AMDM
#reading the number of vertices,edges and partitions required
with open(f_name) as f:
    first_line = f.readline()
graph_key = [int(s) for s in first_line.split() if s.isdigit()]

n_vert = graph_key[0]
n_edge = graph_key[1]
n_partition = graph_key[2]
#Constructing graph
G = nx.Graph()
G.add_nodes_from(range(n_vert))
G.add_edges_from(graph)

#Computing laplacian
L = nx.laplacian_matrix(G)
L = csr_matrix.astype(L, dtype='f')

# Computing eigen vectors
eig_val, eig_vec = la.eigsh(L, 2, which='SM')


def distance(p1, p2):
    return np.sum((p1 - p2)**2)


# initialisation algorithm K++
def initialize(data, k):
    ''' 
    intialized the centroids for K-means++ 
    inputs: 
        data - numpy array of data points having shape (200, 2) 
コード例 #2
0
labels1 = np.ndarray(shape=([len(ntypes1),2]),dtype=int)
labels2 = np.ndarray(shape=([len(ntypes2),2]),dtype=int)

for i, j in enumerate(sorted(ntypes1.keys())):
    if j in zgats: labels1[i] = [1,0]
    else: labels1[i] = [0,1]

for i, j in enumerate(sorted(ntypes2.keys())):
    if j in zgats: labels2[i] = [1,0]
    else: labels2[i] = [0,1]

feat1 = csr_matrix(features1)
feat2 = csr_matrix(features2)

feat_csr1 = csr_matrix.astype(feat1,np.float32)
feat_csr2 = csr_matrix.astype(feat2,np.float32)

x = open("ind.logdec.x","wt")
tx = open("ind.logdec.tx","wt")
allx = open("ind.logdec.allx","wt")
graf = open("ind.logdec.graph","wt")
y = open("ind.logdec.y","wt")
ty = open("ind.logdec.ty","wt")
ally = open("ind.logdec.ally","wt")

pkl.dump(labels2,y)
pkl.dump(labels2,ally)
pkl.dump(labels1,ty)
pkl.dump(feat_csr2,x)
pkl.dump(feat_csr2,allx)
コード例 #3
0
def to_ndarray(l, dtype=int):
    assert len(l) > 0
    assert len(l[0]) > 0
    rows = len(l)
    cols = len(l[0])
    arr = np.ndarray(shape=(rows, cols), dtype=int)
    for i, row in enumerate(l):
        arr[i] = row
    return arr


labels_test = to_ndarray(labels_test)
labels_train = to_ndarray(labels_train)
labels_train_all = to_ndarray(labels_train_all)
feat_csr_test = csr_matrix.astype(csr_matrix(features_test), np.float32)
feat_csr_train = csr_matrix.astype(csr_matrix(features_train), np.float32)
feat_csr_train_all = csr_matrix.astype(csr_matrix(features_train_all),
                                       np.float32)

f_x = open("ind.logdec.x", "wb")
f_tx = open("ind.logdec.tx", "wb")
f_allx = open("ind.logdec.allx", "wb")
f_graf = open("ind.logdec.graph", "wb")
f_y = open("ind.logdec.y", "wb")
f_ty = open("ind.logdec.ty", "wb")
f_ally = open("ind.logdec.ally", "wb")

print("label lengths (test, train, all, cnt):", len(labels_test),
      len(labels_train), len(labels_train_all), len(nfanins)),
print("features size (test, train, all, cnt):", feat_csr_test.shape,