Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print an aligned numpy array with (text) row and column labels?

Is there any elegant way to exploit the correct spacing feature of print numpy.array to get a 2D array, with proper labels, that aligns properly? For example, given an array with 4 rows and 5 columns, how can I provide the array and appropriately sized lists corresponding to the row and header columns to generate some output that looks like this?

      A   B   C   D   E
Z [[ 85  86  87  88  89]
Y  [ 90 191 192  93  94]
X  [ 95  96  97  98  99]
W  [100 101 102 103 104]]

If I naively try:

import numpy
x = numpy.array([[85, 86, 87, 88, 89], \
                 [90, 191, 192, 93, 94], \
                 [95, 96, 97, 98, 99], \
                 [100,101,102,103,104]])

row_labels = ['Z', 'Y', 'X', 'W']


print "     A   B   C   D   E"
for row, row_index in enumerate(x):
    print row_labels[row_index], row

I get:

      A   B   C   D   E
Z  [85  86  87  88  89]
Y  [90 191 192  93  94]
X  [95  96  97  98  99]
W  [100 101 102 103 104]

Is there any way i can get things to line up intelligently? I am definitely open to using any other library if there is a better way to solve my problem.

like image 858
wwwilliam Avatar asked Feb 19 '11 01:02

wwwilliam


People also ask

How do I get rows and columns of a NumPy array?

In the NumPy with the help of shape() function, we can find the number of rows and columns. In this function, we pass a matrix and it will return row and column number of the matrix. Return: The number of rows and columns.

Are NumPy arrays row column or column row?

Data in NumPy arrays can be accessed directly via column and row indexes, and this is reasonably straightforward. Nevertheless, sometimes we must perform operations on arrays of data such as sum or mean of values by row or column and this requires the axis of the operation to be specified.

How do I concatenate a NumPy array vertically?

NumPy: vstack() function The vstack() function is used to stack arrays in sequence vertically (row wise). This is equivalent to concatenation along the first axis after 1-D arrays of shape (N,) have been reshaped to (1,N). The arrays must have the same shape along all but the first axis.

How do I print a column of a NumPy array?

Access the ith column of a Numpy array using EllipsisPass the ith index along with the ellipsis to print the returned array column.


2 Answers

You can use IPython notebook + Pandas for that. Type your original example in IPython notebook:

import numpy
x = numpy.array([[85, 86, 87, 88, 89], 
                 [90, 191, 192, 93, 94], 
                 [95, 96, 97, 98, 99], 
                 [100,101,102,103,104]])

row_labels = ['Z', 'Y', 'X', 'W']
column_labels = ['A', 'B', 'C', 'D', 'E']

Then create a DataFrame:

import pandas
df = pandas.DataFrame(x, columns=column_labels, index=row_labels)

And then view it:

enter image description here

like image 180
Mikhail Korobov Avatar answered Oct 09 '22 04:10

Mikhail Korobov


Assuming all matrix numbers have at most 3 digits, you could replace the last part with this:

print "     A   B   C   D   E"
for row_label, row in zip(row_labels, x):
    print '%s [%s]' % (row_label, ' '.join('%03s' % i for i in row))

Which outputs:

     A   B   C   D   E
Z [ 85  86  87  88  89]
Y [ 90 191 192  93  94]
X [ 95  96  97  98  99]
W [100 101 102 103 104]

Formatting with '%03s' results in a string of length 3 with left padding (using spaces). Use '%04s' for length 4 and so on. The full format string syntax is explained in the Python documentation.

like image 42
scoffey Avatar answered Oct 09 '22 05:10

scoffey