简体   繁体   English

如何将邻接矩阵保存到文件?

[英]How to save adjacency matrix to a file?

I have an adjacency matrix of the graph like this one:我有一个像这样的图的邻接矩阵:

在此处输入图片说明

I want to savethis matrix as it is in a csv or txt file, what should I do?我想将这个矩阵保存在 csv 或 txt 文件中,我该怎么办?

I have tried the following link我试过以下链接

https://stackoverflow.com/questions/46462487/how-to-write-an-adjacency-matrix-to-a-file-and-read-from-the-file-you-have-writt Gives me error

and others dont give me the right output而其他人没有给我正确的输出

Please I need your help拜托我需要你的帮忙

Thank you谢谢

Here you go:干得好:

import numpy as np
# create random adjacency matrix
A = np.random.randint(0,2,(10,10))
A[np.arange(10),np.arange(10)] = 1
# save to .txt file
filename='adjacency_matrix.txt'
np.savetxt(filename,A)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM