Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: possible better formatting?

Is there a way to obtain a better format for matrices in Numpy, similar for example to the results obtained with show() in Sage or init_printing() in Sympy?

I am studying the book on Sage by Craig Finch, and in the chapter about Linear Algebra, it is evident the quality and clarity difference between the output by Sage and that by Numpy. Is there a way to improve the Numpy output quality, eventually directly in an IPython notebook?

Just to clarify my request: I am used to Sage notebooks, but would like to explore the new possibilities of Ipython notebooks. In particular I would like to prepare some notebooks about Linear Algebra and Numpy. But the simple plain text output of Numpy is not particularly nice or clear (http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-2-Numpy.ipynb), I would prefer some typeset style,in particular for matrices (brackets, parentheses, vertical bars for determinants and so on).

like image 383
BFir Avatar asked Jan 21 '26 15:01

BFir


2 Answers

You could convert it to a SymPy Matrix and use the init_printing output of that.

like image 118
asmeurer Avatar answered Jan 24 '26 05:01

asmeurer


I'm not familiar with those formats but perhaps you could have a look at pandas and maybe use a DataFrame - this gives numbered rows and columns by default:

import numpy as np
a = np.random.random((5,5))
print a

[[ 0.78022594  0.37348569  0.25796595  0.27718512  0.72702331]
 [ 0.35732242  0.56154946  0.66928828  0.30218237  0.73351843]
 [ 0.94946754  0.94317356  0.94021486  0.60110254  0.56683929]
 [ 0.61902204  0.19068132  0.53713708  0.03889093  0.65818047]
 [ 0.32843129  0.74062189  0.59666774  0.46876727  0.96919562]]

import pandas as pd
b = pd.DataFrame(a)
print b
          0         1         2         3         4
0  0.780226  0.373486  0.257966  0.277185  0.727023
1  0.357322  0.561549  0.669288  0.302182  0.733518
2  0.949468  0.943174  0.940215  0.601103  0.566839
3  0.619022  0.190681  0.537137  0.038891  0.658180
4  0.328431  0.740622  0.596668  0.468767  0.969196
like image 44
atomh33ls Avatar answered Jan 24 '26 06:01

atomh33ls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!