2

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

BFir
  • 333
  • 3
  • 13
  • possible duplicate of [NumPy: Pretty print tabular data](http://stackoverflow.com/questions/9712085/numpy-pretty-print-tabular-data) – Ffisegydd Apr 04 '14 at 14:16
  • 1
    You could use [`numpy.set_string_function`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_string_function.html#numpy.set_string_function) to produce arbitrary strings from numpy arrays (using unicode and whatever string hack you can come up with). But I have no idea of what output is produced by Sage so I don't know what you are talking about. Also I believe in the IPython Notebook you could use a LaTeX formatter for the arrays. – Bakuriu Apr 04 '14 at 14:45
  • Please say what is your problem with the default formatting. – jrennie Apr 04 '14 at 19:54

4 Answers4

5

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

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • This can be interesting. Is there a direct command for the conversion form Numpy matrices to Sympy? Do you have some simple examples? – BFir Apr 05 '14 at 13:16
  • 1
    If the array is formatted like `[[1, 2], [3, 4]]`, where `[1, 2]`, and `[3, 4]` are the rows of the matrix, then you can just use `Matrix(array)`. – asmeurer Apr 05 '14 at 17:01
  • Note, you'll want to do your actual computations with numpy. Only use SymPy for the printing. – asmeurer Apr 05 '14 at 17:02
  • Perfect, thanks a lot, the method works well in the IPython notebook. Now I have to study a bit the formatting options of Sympy, because with your example I obtain a nice typeset matrix with square brackets, but the numbers in Sympy acquire a decimal point (1->1.0 and so on). Do you know why? – BFir Apr 06 '14 at 16:55
3

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
Lee
  • 29,398
  • 28
  • 117
  • 170
1

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)
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • Hi read the official documentation, but did not find a way to improve the quality of the output directly with Numpy options. – BFir Apr 05 '14 at 13:11
0

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.

keturn
  • 4,780
  • 3
  • 29
  • 40
  • Thanks, I did not know ipy_table. It seems good for formatting tables, but I would like some options specific for matrices. – BFir Apr 05 '14 at 13:13