Pergunta

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).

Foi útil?

Solução

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

Outras dicas

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

Perhaps what you need is numpy.set_printoptions(), take a look in the documentation. One example about how to change the precision of the printed outputs:

np.set_printoptions(precision=2)

For html ipython-notebooks, there's ipy_table, which lets you render two-dimensional arrays in an HTML table instead of their default string format.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top