7

I've got an 18x18 2d numpy array (it's a confusion matrix)...and I need/would like to display it as a table in an ipython notebook.

When I simply print it out, it displays with overlap--the rows are so long they take up two lines.

Is there a library that will allow me to print this array in a sort of spreadsheet format?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Chris
  • 28,822
  • 27
  • 83
  • 158

3 Answers3

17

You can use Pandas for that.

import pandas as pd
print pd.DataFrame(yourArray)
0

A job for matrepr:

from matrepr import mdisplay

mat = np.random.random((18, 18))
mdisplay(mat, floatfmt=".2f", max_rows=18, max_cols=18)

matrepr html output

for a text table use mprint instead, or to_str for the string without printing. Use indices=False and title=False to get just the matrix.

Adam
  • 16,808
  • 7
  • 52
  • 98
-2

Note: Konstantinos proposal holds only for 1-D and 2-D arrays!

You can use numpy.array2string():

from pprint import pprint
import numpy as np

array = np.array([[1,2,3], [4,5,6]])
print(np.array2string(array).replace('[[',' [').replace(']]',']'))

Output:

 [1 2 3]
 [4 5 6]

See also: Printing Lists as Tabular Data

guyomd
  • 22
  • 4