How to dump a boolean matrix in numpy?

2024/9/23 9:13:48

I have a graph represented as a numpy boolean array (G.adj.dtype == bool). This is homework in writing my own graph library, so I can't use networkx. I want to dump it to a file so that I can fiddle with it, but for the life of me I can't work out how to make numpy dump it in a recoverable fashion.

I've tried G.adj.tofile, which wrote the graph correctly (ish) as one long line of True/False. But fromfile barfs on reading this, giving a 1x1 array, and loadtxt raises a ValueError: invalid literal for int. np.savetxt works but saves the matrix as a list of 0/1 floats, and loadtxt(..., dtype=bool) fails with the same ValueError.

Finally, I've tried networkx.from_numpy_matrix with networkx.write_dot, but that gave each edge [weight=True] in the dot source, which broke networkx.read_dot.

Answer

To save:

numpy.savetxt('arr.txt', G.adj, fmt='%s')

To recover:

G.adj = numpy.genfromtxt('arr.txt', dtype=bool)

HTH!

https://en.xdnf.cn/q/71782.html

Related Q&A

Cant append_entry FieldList in Flask-wtf more than once

I have a form with flask-wtf for uploading images, also file field can be multiple fields. my form: class ComposeForm(Form):attachment = FieldList(FileField(_(file)), _(attachment))add_upload = SubmitF…

What is the best way to use python code from Scala (or Java)? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Java Python Integration There is some code written in Python and I need to use it from Scala. The code uses some native C.…

Pandas groupby week given a datetime column

Lets say I have the following data sample:df = pd.DataFrame({date:[2011-01-01,2011-01-02,2011-01-03,2011-01-04,2011-01-05,2011-01-06,2011-01-07,2011-01-08,2011-01-09,2011-12-30,2011-12-31],revenue:[5,3…

Django form to indicate input type

Another basic question Im afraid which Im struggling with. Ive been through the various Django documentation pages and also search this site. The only thing I have found on here was back in 2013 which…

run multi command in the same jupyter cells

Im trying to display 2 output of 2 lines in the same time, I use Panda library and it seems like it display only the output of second line:import pandas as pd data = {"state": ["Ohio&quo…

Pandas how to get rows with consecutive dates and sales more than 1000?

I have a data frame called df: Date Sales 01/01/2020 812 02/01/2020 981 03/01/2020 923 04/01/2020 1033 05/01/2020 988 ... ...How can I get the first occurrence of 7 conse…

Use Python alongside C# in Windows UWP app

I started writing an application in Python, but I now want to switch to C# and UWP. I know that you cannot write a UWP app in Python, but I am trying to see if I can write some code in Python and acces…

How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?

Im using python and of course you cant loop through every pixel of a large image very quickly, so I defer to a C DLL.I want to do something like this:img = QImage("myimage.png").constBits() i…

Just returning the text of elements in xpath (python / lxml)

I have an XML structure like this:mytree = """ <path><to><nodes><info>1</info><info>2</info><info>3</info></nodes></to> …

Python function parameter: tuple/list

My function expects a list or a tuple as a parameter. It doesnt really care which it is, all it does is pass it to another function that accepts either a list or tuple:def func(arg): # arg is tuple or …