In [34]:
import numpy as np
from IPython.core.display import HTML

In [35]:
x = np.array([1,2,3]) # 1D vector
print(x.shape)
x


(3,)
Out[35]:
array([1, 2, 3])

In [36]:
y = x.reshape(-1,1) # vertical 2D vector
y


Out[36]:
array([[1],
       [2],
       [3]])

In [37]:
z = x.reshape(1,-1) # horiz 2D vector
print(z)
z.shape


[[1 2 3]]
Out[37]:
(1, 3)

In [38]:
A = np.array([[1,2,8,9],[3,4,22,1]])
A


Out[38]:
array([[ 1,  2,  8,  9],
       [ 3,  4, 22,  1]])

In [39]:
B = np.ones((100,100))
for i in range(100):
    for j in range(100):
        B[i,j] = i+j
B


Out[39]:
array([[  0.,   1.,   2., ...,  97.,  98.,  99.],
       [  1.,   2.,   3., ...,  98.,  99., 100.],
       [  2.,   3.,   4., ...,  99., 100., 101.],
       ...,
       [ 97.,  98.,  99., ..., 194., 195., 196.],
       [ 98.,  99., 100., ..., 195., 196., 197.],
       [ 99., 100., 101., ..., 196., 197., 198.]])

In [40]:
YELLOW = "#fefecd" # "#fbfbd0" # "#FBFEB0"
BLUE = "#D9E6F5"
GREEN = "#cfe2d4"
class Prefs: pass
prefs = Prefs()
prefs.max_str_len = 20         # how many chars before we abbreviate with ...?
prefs.max_horiz_array_len = 40 # how many chars before it's too wide and we go vertical?
prefs.max_list_elems = 10      # how many elements max to display in list (unused so far)
prefs.float_precision = 5      # how many decimal places to show for floats

In [53]:
def gr_1darray_html(data, bgcolor=YELLOW):
    if not isinstance(data,np.ndarray):
        return " "
    if data.ndim > 1:
        return gr_1darray_html(data, bgcolor)
    if data.ndim > 2:
        return " "

    ncols = len(data)
    header = '<table BORDER="0" CELLPADDING="0" CELLBORDER="1" CELLSPACING="0">\n'
    tail = "</table>\n"

    # grab slice of max elements from matrix
    coloversize = False
    midpoint = prefs.max_list_elems//2
    if ncols > prefs.max_list_elems:
        colslice = list(np.arange(0,midpoint)) + list(np.arange(ncols-midpoint,ncols))
        data = data[colslice]
        ncols=len(colslice)
        coloversize = True

    cells = []
    for j in range(ncols):
        if coloversize and j==midpoint:
            cells.append( '<td cellspacing="0" cellpadding="2" bgcolor="%s" border="0" align="center"><font color="#444443" point-size="10">...</font></td>\n' % bgcolor)
        value = data[j]
        if isinstance(value, float):
            if str(value).endswith('.0'):
                value = str(value)[:-1]             
            else:
                value = round(value, prefs.float_precision)
        if isinstance(value, float) and str(value).endswith('.0'):
            value = str(value)[:-1]                
        if len(str(value)) > prefs.max_str_len:
            value = abbrev_and_escape(str(value))
        cell = '<td cellspacing="0" cellpadding="3" bgcolor="%s" border="0" align="center"><font color="#444443" point-size="10">%s</font></td>\n' % (bgcolor, value)
        cells.append( cell )

    row = '<tr>' + ''.join(cells) + '</tr>\n'

    return header + row + tail

def gr_2darray_html(data, bgcolor=YELLOW):
    if len(data)==0:
        return " "
    if not isinstance(data,np.ndarray):
        return " "
    if data.ndim == 1:
        return gr_1darray_html(data, bgcolor)
    if data.ndim > 2:
        return " "

    nrows,ncols = data.shape
    header = '<table BORDER="0" CELLPADDING="0" CELLBORDER="1" CELLSPACING="0">\n'
    tail = "</table>\n"

    # grab slice of max elements from matrix
    coloversize = rowoversize = False
    midpoint = prefs.max_list_elems//2
    if nrows > prefs.max_list_elems:
        rowslice = list(np.arange(0,midpoint)) + list(np.arange(nrows-midpoint,nrows))
        data = data[rowslice]
        nrows=len(rowslice)
        rowoversize = True
    if ncols > prefs.max_list_elems:
        colslice = list(np.arange(0,midpoint)) + list(np.arange(ncols-midpoint,ncols))
        data = data[:,colslice]
        ncols=len(colslice)
        coloversize = True

    rows = []
    for i in range(nrows):
        if rowoversize and i==midpoint:
            rows.append( '<tr><td bgcolor="%s" cellpadding="2" border="0" align="center"><font color="#444443" point-size="10">&#8942;</font></td><td bgcolor="%s" cellpadding="2" border="0" colspan="%d"></td></tr>\n' % (bgcolor,bgcolor,ncols))
        cells = []
        for j in range(ncols):
            if coloversize and j==midpoint:
                cells.append( '<td cellspacing="0" cellpadding="2" bgcolor="%s" border="0" align="center"><font color="#444443" point-size="10">...</font></td>\n' % bgcolor)
            value = data[i,j]
            if isinstance(value, float):
                if str(value).endswith('.0'):
                    value = str(value)[:-1]             
                else:
                    value = round(value, prefs.float_precision)
            if len(str(value)) > prefs.max_str_len:
                value = abbrev_and_escape(str(value))
            cell = '<td cellspacing="0" cellpadding="3" bgcolor="%s" border="0" align="center"><font color="#444443" point-size="10">%s</font></td>\n' % (bgcolor, value)
            cells.append( cell )
        row = '<tr>' + ''.join(cells) + '</tr>\n'
        rows.append(row)

    return header + ''.join(rows) + tail

h = gr_2darray_html(np.array([1,3,4]))
h = gr_2darray_html(B)

#print(h)
HTML(h)


Out[53]:
0. 1. 2. 3. 4. ... 95. 96. 97. 98. 99.
1. 2. 3. 4. 5. ... 96. 97. 98. 99. 100.
2. 3. 4. 5. 6. ... 97. 98. 99. 100. 101.
3. 4. 5. 6. 7. ... 98. 99. 100. 101. 102.
4. 5. 6. 7. 8. ... 99. 100. 101. 102. 103.
95. 96. 97. 98. 99. ... 190. 191. 192. 193. 194.
96. 97. 98. 99. 100. ... 191. 192. 193. 194. 195.
97. 98. 99. 100. 101. ... 192. 193. 194. 195. 196.
98. 99. 100. 101. 102. ... 193. 194. 195. 196. 197.
99. 100. 101. 102. 103. ... 194. 195. 196. 197. 198.

In [54]:
import graphviz

def gr_ndarray_node(nodename, data, bgcolor=YELLOW):
    shape="box"
    html = gr_2darray_html(data, bgcolor=bgcolor)
    return '%s [shape="%s", space="0.0", margin="0.01", fontcolor="#444443", fontname="Helvetica", label=<%s>];\n' % (nodename,shape,html)

def myviz(data):
    s = """
    digraph G {
        nodesep=.05;
        node [penwidth="0.5", width=.1,height=.1];
    """

    s += gr_ndarray_node('node%d'%id(data), data)

    s += "}\n"
    return graphviz.Source(s)
myviz(A)


Out[54]:
G node4768272096 1 2 8 9 3 4 22 1

In [55]:
myviz(B)


Out[55]:
G node4768301424 0. 1. 2. 3. 4. ... 95. 96. 97. 98. 99. 1. 2. 3. 4. 5. ... 96. 97. 98. 99. 100. 2. 3. 4. 5. 6. ... 97. 98. 99. 100. 101. 3. 4. 5. 6. 7. ... 98. 99. 100. 101. 102. 4. 5. 6. 7. 8. ... 99. 100. 101. 102. 103. 95. 96. 97. 98. 99. ... 190. 191. 192. 193. 194. 96. 97. 98. 99. 100. ... 191. 192. 193. 194. 195. 97. 98. 99. 100. 101. ... 192. 193. 194. 195. 196. 98. 99. 100. 101. 102. ... 193. 194. 195. 196. 197. 99. 100. 101. 102. 103. ... 194. 195. 196. 197. 198.

In [43]:
myviz(np.array([['abc','x'],['hi','you are yellow']]))


Out[43]:
G node4768270016 abc x hi you are yellow

In [45]:
import subprocess
import sys

reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = set([r.decode().split('==')[0] for r in reqs.split()])
if 'numpy' in installed_packages:
    print('hi')


hi

In [11]:
import sys

if 'numpy' not in sys.modules:
    import numpy
    print("import")
np = sys.modules['numpy']
np.ndarray


Out[11]:
numpy.ndarray

In [4]:
import imp

imp.find_module('numpy')


Out[4]:
(None, '/Users/parrt/anaconda3/lib/python3.6/site-packages/numpy', ('', '', 5))