Numpy (work in progress; not well commented)


In [2]:
import numpy as np
import scipy as sp
import numpy.linalg as la
import os
import os.path
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
% matplotlib inline

In [3]:
# create a 4x3 matrix of zeros
a=np.zeros((4,3))
a


Out[3]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

In [4]:
# the identity matrix
I = np.identity(3, dtype='float') # d stands for double
I


Out[4]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [5]:
# create a matrix
M=np.matrix([[1,2],[3,4]])
M


Out[5]:
matrix([[1, 2],
        [3, 4]])

In [6]:
# display type, shape, and size
type(M), M.shape, M.size


Out[6]:
(numpy.matrixlib.defmatrix.matrix, (2, 2), 4)

In [7]:
# transpose of the matrix
M.T


Out[7]:
matrix([[1, 3],
        [2, 4]])

In [8]:
# another way to compute the transpose
np.transpose(M)


Out[8]:
matrix([[1, 3],
        [2, 4]])

In [9]:
# inverse of a matrix
M.I


Out[9]:
matrix([[-2. ,  1. ],
        [ 1.5, -0.5]])

In [10]:
# another way to get the inverse
np.linalg.inv(M)


Out[10]:
matrix([[-2. ,  1. ],
        [ 1.5, -0.5]])

In [11]:
# generate an array of equally spaced elements
L=np.linspace(0,1)
L


Out[11]:
array([ 0.        ,  0.02040816,  0.04081633,  0.06122449,  0.08163265,
        0.10204082,  0.12244898,  0.14285714,  0.16326531,  0.18367347,
        0.20408163,  0.2244898 ,  0.24489796,  0.26530612,  0.28571429,
        0.30612245,  0.32653061,  0.34693878,  0.36734694,  0.3877551 ,
        0.40816327,  0.42857143,  0.44897959,  0.46938776,  0.48979592,
        0.51020408,  0.53061224,  0.55102041,  0.57142857,  0.59183673,
        0.6122449 ,  0.63265306,  0.65306122,  0.67346939,  0.69387755,
        0.71428571,  0.73469388,  0.75510204,  0.7755102 ,  0.79591837,
        0.81632653,  0.83673469,  0.85714286,  0.87755102,  0.89795918,
        0.91836735,  0.93877551,  0.95918367,  0.97959184,  1.        ])

In [12]:
type(L), L.shape, L.size


Out[12]:
(numpy.ndarray, (50,), 50)

In [13]:
# reshape the array - maintains the same size
LL=L.reshape((10,5))
LL


Out[13]:
array([[ 0.        ,  0.02040816,  0.04081633,  0.06122449,  0.08163265],
       [ 0.10204082,  0.12244898,  0.14285714,  0.16326531,  0.18367347],
       [ 0.20408163,  0.2244898 ,  0.24489796,  0.26530612,  0.28571429],
       [ 0.30612245,  0.32653061,  0.34693878,  0.36734694,  0.3877551 ],
       [ 0.40816327,  0.42857143,  0.44897959,  0.46938776,  0.48979592],
       [ 0.51020408,  0.53061224,  0.55102041,  0.57142857,  0.59183673],
       [ 0.6122449 ,  0.63265306,  0.65306122,  0.67346939,  0.69387755],
       [ 0.71428571,  0.73469388,  0.75510204,  0.7755102 ,  0.79591837],
       [ 0.81632653,  0.83673469,  0.85714286,  0.87755102,  0.89795918],
       [ 0.91836735,  0.93877551,  0.95918367,  0.97959184,  1.        ]])

In [14]:
# get the shape and size
LL.shape, LL.size


Out[14]:
((10, 5), 50)

In [15]:
# another array
np.linspace(0,1,20)


Out[15]:
array([ 0.        ,  0.05263158,  0.10526316,  0.15789474,  0.21052632,
        0.26315789,  0.31578947,  0.36842105,  0.42105263,  0.47368421,
        0.52631579,  0.57894737,  0.63157895,  0.68421053,  0.73684211,
        0.78947368,  0.84210526,  0.89473684,  0.94736842,  1.        ])

In [16]:
# yet another line space array
x = np.linspace(0,2*np.pi)
x


Out[16]:
array([ 0.        ,  0.12822827,  0.25645654,  0.38468481,  0.51291309,
        0.64114136,  0.76936963,  0.8975979 ,  1.02582617,  1.15405444,
        1.28228272,  1.41051099,  1.53873926,  1.66696753,  1.7951958 ,
        1.92342407,  2.05165235,  2.17988062,  2.30810889,  2.43633716,
        2.56456543,  2.6927937 ,  2.82102197,  2.94925025,  3.07747852,
        3.20570679,  3.33393506,  3.46216333,  3.5903916 ,  3.71861988,
        3.84684815,  3.97507642,  4.10330469,  4.23153296,  4.35976123,
        4.48798951,  4.61621778,  4.74444605,  4.87267432,  5.00090259,
        5.12913086,  5.25735913,  5.38558741,  5.51381568,  5.64204395,
        5.77027222,  5.89850049,  6.02672876,  6.15495704,  6.28318531])

In [17]:
x.size


Out[17]:
50

In [18]:
# this produces an array
x=np.arange(15)
x


Out[18]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [19]:
# this one produces a list
y = list(range(15))
print(y)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

In [20]:
type(x), type(y)


Out[20]:
(numpy.ndarray, list)

In [21]:
a=np.arange(24).reshape(6,4)
a


Out[21]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])

In [22]:
a.shape, a.size, a.ndim


Out[22]:
((6, 4), 24, 2)

In [23]:
a.dtype, a.dtype.name, a.itemsize, type(a)


Out[23]:
(dtype('int64'), 'int64', 8, numpy.ndarray)

In [24]:
b = np.array( [ (1.5,2,3), (4,5,6) ] )
b


Out[24]:
array([[ 1.5,  2. ,  3. ],
       [ 4. ,  5. ,  6. ]])

In [25]:
c = np.array( [ [1,2], [3,4] ], dtype=complex )
c


Out[25]:
array([[ 1.+0.j,  2.+0.j],
       [ 3.+0.j,  4.+0.j]])

In [26]:
np.arange( 10, 30, 5 )


Out[26]:
array([10, 15, 20, 25])

In [27]:
np.arange(0,15,1)


Out[27]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [28]:
np.arange( 0, 2, 0.3 )


Out[28]:
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

In [29]:
np.arange(15)


Out[29]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [30]:
print(np.arange(10000))


[   0    1    2 ..., 9997 9998 9999]

In [31]:
print(np.arange(10000).reshape(100,100))


[[   0    1    2 ...,   97   98   99]
 [ 100  101  102 ...,  197  198  199]
 [ 200  201  202 ...,  297  298  299]
 ..., 
 [9700 9701 9702 ..., 9797 9798 9799]
 [9800 9801 9802 ..., 9897 9898 9899]
 [9900 9901 9902 ..., 9997 9998 9999]]

In [32]:
x=np.arange(10000)
x[1000:1100]


Out[32]:
array([1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010,
       1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021,
       1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032,
       1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043,
       1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054,
       1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065,
       1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 1074, 1075, 1076,
       1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087,
       1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098,
       1099])

In [33]:
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
c = a-b
print (a)
print (b)
print (c)


[20 30 40 50]
[0 1 2 3]
[20 29 38 47]

In [34]:
L=[1,2,3]
L**2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-7a8f9171244c> in <module>()
      1 L=[1,2,3]
----> 2 L**2

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [35]:
b**2


Out[35]:
array([0, 1, 4, 9])

In [36]:
10*np.sin(a)


Out[36]:
array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])

In [37]:
a < 35


Out[37]:
array([ True,  True, False, False], dtype=bool)

In [38]:
A = np.array( [[1,1],[0,1]] )

In [39]:
B = np.array( [[2,0],[3,4]] )

In [40]:
A*B # element by element multi


Out[40]:
array([[2, 0],
       [0, 4]])

In [41]:
np.dot(A,B) # matrix multiply


Out[41]:
array([[5, 4],
       [3, 4]])

In [42]:
a = np.ones((2,3), dtype=int)
b = np.random.random((2,3))
print (a)
print (b)


[[1 1 1]
 [1 1 1]]
[[ 0.64772373  0.32199686  0.3445488 ]
 [ 0.72026436  0.10646652  0.73383519]]

In [43]:
a *= 3 # a = a * 3

In [44]:
b += a # b = b + a
b


Out[44]:
array([[ 3.64772373,  3.32199686,  3.3445488 ],
       [ 3.72026436,  3.10646652,  3.73383519]])

In [45]:
a += b
a


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-45-508502a9e0ae> in <module>()
----> 1 a += b
      2 a

TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

In [46]:
a = a + b
a


Out[46]:
array([[ 6.64772373,  6.32199686,  6.3445488 ],
       [ 6.72026436,  6.10646652,  6.73383519]])

In [80]:
a = np.random.random((2,3))

In [81]:
b = np.arange(12).reshape(3,4)
b


Out[81]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [82]:
b.sum(axis=0) # sum across columns


Out[82]:
array([12, 15, 18, 21])

In [83]:
b.min(axis=1)


Out[83]:
array([0, 4, 8])

In [84]:
b.cumsum(axis=1)


Out[84]:
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]])

In [85]:
B = np.arange(3)

In [86]:
np.exp(B)


Out[86]:
array([ 1.        ,  2.71828183,  7.3890561 ])

In [87]:
np.sqrt(B)


Out[87]:
array([ 0.        ,  1.        ,  1.41421356])

In [88]:
C = np.array([2., -1., 4.])

In [89]:
np.add(B, C)


Out[89]:
array([ 2.,  0.,  6.])

In [90]:
a = np.arange(10)**3
a


Out[90]:
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])

In [91]:
a[2:]


Out[91]:
array([  8,  27,  64, 125, 216, 343, 512, 729])

In [92]:
a[2:5]


Out[92]:
array([ 8, 27, 64])

In [93]:
a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000

In [94]:
a[ : :-1] # reversed a


Out[94]:
array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])

In [96]:
a = np.floor(10*np.random.random((3,4)))

In [97]:
a.shape


Out[97]:
(3, 4)

In [98]:
a.ravel() # flatten the array


Out[98]:
array([ 2.,  6.,  6.,  3.,  9.,  8.,  7.,  2.,  9.,  3.,  5.,  8.])

In [99]:
a.shape = (6, 2)

In [100]:
a.transpose()


Out[100]:
array([[ 2.,  6.,  9.,  7.,  9.,  5.],
       [ 6.,  3.,  8.,  2.,  3.,  8.]])

In [101]:
a.resize((2,6))

In [102]:
a.reshape(3,-1)


Out[102]:
array([[ 2.,  6.,  6.,  3.],
       [ 9.,  8.,  7.,  2.],
       [ 9.,  3.,  5.,  8.]])

In [105]:
a = np.floor(10*np.random.random((2,2)))

In [106]:
b = np.floor(10*np.random.random((2,2)))

In [107]:
np.vstack((a,b))


Out[107]:
array([[ 7.,  7.],
       [ 3.,  7.],
       [ 9.,  1.],
       [ 2.,  6.]])

In [108]:
np.hstack((a,b))


Out[108]:
array([[ 7.,  7.,  9.,  1.],
       [ 3.,  7.,  2.,  6.]])

In [109]:
np.column_stack((a,b))


Out[109]:
array([[ 7.,  7.,  9.,  1.],
       [ 3.,  7.,  2.,  6.]])

In [111]:
a=np.array([4.,2.])
b=np.array([2.,8.])

In [113]:
a[:,np.newaxis]  # This allows to have a 2D columns vector


Out[113]:
array([[ 4.],
       [ 2.]])

In [115]:
np.column_stack((a[:,np.newaxis],b[:,np.newaxis]))


Out[115]:
array([[ 4.,  2.],
       [ 2.,  8.]])

In [116]:
np.vstack((a[:,np.newaxis],b[:,np.newaxis])) # The behavior of vstack is different


Out[116]:
array([[ 4.],
       [ 2.],
       [ 2.],
       [ 8.]])

In [117]:
a = np.floor(10*np.random.random((2,12)))

In [118]:
np.hsplit(a,3)   # Split a into 3


Out[118]:
[array([[ 4.,  8.,  3.,  4.],
        [ 7.,  6.,  3.,  2.]]), array([[ 0.,  0.,  3.,  5.],
        [ 9.,  1.,  5.,  7.]]), array([[ 3.,  5.,  0.,  2.],
        [ 7.,  4.,  9.,  6.]])]

In [119]:
np.hsplit(a,(3,4))   # Split a after the third and the fourth column


Out[119]:
[array([[ 4.,  8.,  3.],
        [ 7.,  6.,  3.]]), array([[ 4.],
        [ 2.]]), array([[ 0.,  0.,  3.,  5.,  3.,  5.,  0.,  2.],
        [ 9.,  1.,  5.,  7.,  7.,  4.,  9.,  6.]])]

In [120]:
a = np.arange(12)
b=a

In [121]:
b is a


Out[121]:
True

In [122]:
b.shape = 3,4

In [123]:
a.shape


Out[123]:
(3, 4)

In [126]:
def f(x):print (id(x))

In [128]:
id(a)


Out[128]:
4548695584

In [129]:
f(a)


4548695584

In [130]:
c = a.view()

In [131]:
c is a


Out[131]:
False

In [132]:
c.base is a                        # c is a view of the data owned by a


Out[132]:
True

In [133]:
c.flags.owndata


Out[133]:
False

In [134]:
c.shape = 2,6

In [135]:
a.shape


Out[135]:
(3, 4)

In [136]:
c[0,4] = 1234  
a


Out[136]:
array([[   0,    1,    2,    3],
       [1234,    5,    6,    7],
       [   8,    9,   10,   11]])

In [137]:
s = a[ : , 1:3]     # spaces added for clarity; could also be written "s = a[:,1:3]"
s


Out[137]:
array([[ 1,  2],
       [ 5,  6],
       [ 9, 10]])

In [138]:
s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10

In [139]:
d = a.copy()   
d is a


Out[139]:
False

In [140]:
d.base is a                           # d doesn't share anything with a


Out[140]:
False

In [141]:
a = np.random.random((5,6))
b = np.random.random((5,6))
print('a=',a)
print('b=',b)


a= [[ 0.2863049   0.16740393  0.02132271  0.00547049  0.21569355  0.31088458]
 [ 0.30653953  0.74127512  0.96366039  0.7935999   0.69186805  0.32507092]
 [ 0.00386846  0.29404876  0.91248613  0.85026218  0.12766697  0.65173565]
 [ 0.2034962   0.81125518  0.73143003  0.29577997  0.86519902  0.76259531]
 [ 0.33568129  0.17469643  0.62047163  0.51638077  0.85166517  0.14789705]]
b= [[ 0.69735436  0.47163302  0.80670036  0.56472493  0.43430678  0.01084155]
 [ 0.66405217  0.53097946  0.29787316  0.13460867  0.70111405  0.1985088 ]
 [ 0.25464495  0.2806203   0.79101136  0.58117701  0.64808857  0.14585198]
 [ 0.36342641  0.00936457  0.35364102  0.74726289  0.94654606  0.46872107]
 [ 0.64524547  0.95774734  0.32479161  0.53867946  0.22906437  0.79624848]]

In [12]:
# write a to a text file. a will be unravelled and written as one line
a.tofile(os.path.join('data', 'out01.txt'), sep=",")

In [ ]:
# save an array to a binary file in NumPy .npy format
np.savetxt(os.path.join('data', 'out02.txt'), a)

In [ ]:
# save an array to a binary file in NumPy .npy format
np.savetxt(os.path.join('data', 'out03.txt'), (a.ravel(), b.ravel()))

In [ ]:
# save an array to a binary file in NumPy .npy format
np.savetxt(os.path.join('data', 'out04.txt'), b, fmt='%1.4e')

In [ ]:
# Save an array to a binary file in NumPy .npy format.
np.save(os.path.join('data', 'out05'), a)
np.load(os.path.join('data', 'out05.npy'))

In [ ]:
# Save several arrays into a single file in uncompressed .npz format
np.savez(os.path.join('data', 'out06'), a,b)
npzfile = np.load(os.path.join('data', 'out06.npz'))

In [4]:
np.savez(os.path.join('data', 'out07'), a,b, avg=a, std=b)
npzfile = np.load(os.path.join('data', 'out07.npz'))

In [ ]:
# Save several arrays into a single file in uncompressed .npz format
np.savez(os.path.join('data', 'out08'), b=b, c=a+b)
npzfile = np.load(os.path.join('data', 'out08.npz'))

In [ ]:
# Save several arrays into a single file in uncompressed .npz format
np.savez_compressed(os.path.join('data', 'out8'), b=b, c=a+b)
npzfile = np.load(os.path.join('data', 'out08.npz'))

In [7]:
a = np.fromstring('0 1 2 3 4 5 6 7 8 9 10', dtype=int, sep=' ')
print(a)


[0 1 2 3 4]

In [ ]:
a = np.fromstring('0 1 2 3 4 5 6 7 8 9 10', dtype=int, sep=' ', count=5)
print(a)

In [ ]:
a.tolist()

In [6]:
a = np.fromstring('\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)
print(a)


[1 2 3]

In [8]:
np.fromstring('1, 2', dtype=int, sep=',')


Out[8]:
array([1, 2])

In [8]:
a = np.array([1, 2])
a.tolist()


Out[8]:
[1, 2]

In [11]:
a = np.array([[1, 2], [3, 4]])

In [12]:
list(a)


Out[12]:
[array([1, 2]), array([3, 4])]

In [13]:
a.tolist


Out[13]:
<function tolist>

In [14]:
a = np.genfromtxt('data'+os.sep+'file.txt', dtype=int,comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values='N/A')

In [15]:
a


Out[15]:
array([[ 1,  2,  3,  4,  5,  6],
       [ 3,  5,  6, -1,  2,  3],
       [ 1,  2,  3,  4,  5,  6]])

In [16]:
a = np.genfromtxt('data'+os.sep+'file.txt',comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values='N/A')

In [17]:
a


Out[17]:
array([[  1.,   2.,   3.,   4.,   5.,   6.],
       [  3.,   5.,   6.,  nan,   2.,   3.],
       [  1.,   2.,   3.,   4.,   5.,   6.]])

In [18]:
a = np.genfromtxt('data'+os.sep+'file.txt', dtype=int,comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values=10)

In [19]:
a


Out[19]:
array([[ 1,  2,  3,  4,  5,  6],
       [ 3,  5,  6, -1,  2,  3],
       [ 1,  2,  3,  4,  5,  6]])

In [20]:
a=np.arange(100)
a[::3]


Out[20]:
array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48,
       51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])

In [21]:
a[10:88:3]


Out[21]:
array([10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58,
       61, 64, 67, 70, 73, 76, 79, 82, 85])

In [22]:
a[::-1]


Out[22]:
array([99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83,
       82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66,
       65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
       48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,
       31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
       14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0])

In [ ]:
file_name = os.path.join('data', 'bindata.bin')
fin = open(file_name, 'rb')
data = np.fromfile(fin,dtype=np.float32,count=5)
for num in data:
    print ('%d'),%num

In [ ]:
data2 = np.fromfile(fin,dtype=np.float32,count=6)
print (data2)
type(data2)

In [ ]:
data3= np.fromfile(fin,dtype=np.float32,count=4)

for num in data3:
    print '%e' %num

In [ ]:
data4 = np.fromfile(fin,dtype=np.float32,count=-1)
print data4
print len(data4)
fin.close()

In [ ]:
with open(file_name, "rb") as fin:
    fin.seek(4)
    ncols = int(np.fromfile(fin, dtype=np.float32, count = 1))
    print 'Num Cols: ', int(ncols)
    
    #go back to the beginning of the file
    fin.seek(0)
    
    pfluc_dtype = np.dtype([
       ("N", np.float32),
       ("ncols", np.float32),
       ("Q", np.float32),
       ("U", np.float32),
       ("fs", np.float32),
       #array with ncols
       ("x", (np.float32, (ncols))),
       ("L", np.float32),
       ("W", np.float32),
       ("D", np.float32),
       ("pRef", np.float32)
       #
       ])
    data = np.fromfile(fin, dtype=pfluc_dtype, count =1)
    
    print 'Data read using "np.fromfile": '
    print 'N', pfluc_dtype.fields['N'], ': ', data['N']
    print 'ncols', pfluc_dtype.fields['ncols'], ': ', data['ncols']
    print 'Q', pfluc_dtype.fields['Q'], ': ', data['Q']
    print 'U', pfluc_dtype.fields['U'], ': ', data['U']
    print 'fs', pfluc_dtype.fields['fs'], ': ', data['fs'] 
    print 'x', pfluc_dtype.fields['x'], ': ', data['x']
    print 'L', pfluc_dtype.fields['L'], ': ', data['L']
    print 'W', pfluc_dtype.fields['W'], ': ', data['W']
    print 'D', pfluc_dtype.fields['D'], ': ', data['D']
    print 'p Ref', pfluc_dtype.fields['pRef'], ': ', data['pRef']

In [ ]:
import matplotlib.pyplot as plt
plt.plot(data4[0:int(data['N'])])

In [ ]:
plt.plot(data4[::6])

In [ ]:
a = np.genfromtxt('data'+os.sep+'file.txt', dtype=int,comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values='N/A')

In [ ]:
a = np.genfromtxt('data'+os.sep+'file.txt',comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values='N/A')

In [ ]:
a = np.genfromtxt('data'+os.sep+'file.txt', dtype=int,comments='#',
                  delimiter=',', skip_header=1,missing='', missing_values=10)

In [24]:
img = mpimg.imread('images'+os.sep+'stinkbug.png')

In [25]:
img


Out[25]:
array([[[ 0.40784314,  0.40784314,  0.40784314],
        [ 0.40784314,  0.40784314,  0.40784314],
        [ 0.40784314,  0.40784314,  0.40784314],
        ..., 
        [ 0.42745098,  0.42745098,  0.42745098],
        [ 0.42745098,  0.42745098,  0.42745098],
        [ 0.42745098,  0.42745098,  0.42745098]],

       [[ 0.41176471,  0.41176471,  0.41176471],
        [ 0.41176471,  0.41176471,  0.41176471],
        [ 0.41176471,  0.41176471,  0.41176471],
        ..., 
        [ 0.42745098,  0.42745098,  0.42745098],
        [ 0.42745098,  0.42745098,  0.42745098],
        [ 0.42745098,  0.42745098,  0.42745098]],

       [[ 0.41960785,  0.41960785,  0.41960785],
        [ 0.41568628,  0.41568628,  0.41568628],
        [ 0.41568628,  0.41568628,  0.41568628],
        ..., 
        [ 0.43137255,  0.43137255,  0.43137255],
        [ 0.43137255,  0.43137255,  0.43137255],
        [ 0.43137255,  0.43137255,  0.43137255]],

       ..., 
       [[ 0.43921569,  0.43921569,  0.43921569],
        [ 0.43529412,  0.43529412,  0.43529412],
        [ 0.43137255,  0.43137255,  0.43137255],
        ..., 
        [ 0.45490196,  0.45490196,  0.45490196],
        [ 0.4509804 ,  0.4509804 ,  0.4509804 ],
        [ 0.4509804 ,  0.4509804 ,  0.4509804 ]],

       [[ 0.44313726,  0.44313726,  0.44313726],
        [ 0.44313726,  0.44313726,  0.44313726],
        [ 0.43921569,  0.43921569,  0.43921569],
        ..., 
        [ 0.4509804 ,  0.4509804 ,  0.4509804 ],
        [ 0.44705883,  0.44705883,  0.44705883],
        [ 0.44705883,  0.44705883,  0.44705883]],

       [[ 0.44313726,  0.44313726,  0.44313726],
        [ 0.4509804 ,  0.4509804 ,  0.4509804 ],
        [ 0.4509804 ,  0.4509804 ,  0.4509804 ],
        ..., 
        [ 0.44705883,  0.44705883,  0.44705883],
        [ 0.44705883,  0.44705883,  0.44705883],
        [ 0.44313726,  0.44313726,  0.44313726]]], dtype=float32)

In [47]:
img2 = mpimg.imread('images'+os.sep+'315px-Chaetodon_melannotus_edit4.jpg')

In [48]:
imgplot = plt.imshow(img)
plt.show()



In [49]:
lum_img = img[:,:,0]
imgplot = plt.imshow(lum_img)
plt.show()



In [50]:
imgplot = plt.imshow(lum_img)
imgplot.set_cmap('hot')
#imgplot.set_cmap('spectral')
plt.colorbar()
plt.show()



In [51]:
plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='k', ec='k')
plt.show()



In [52]:
img2


Out[52]:
array([[[235, 248, 238],
        [226, 239, 229],
        [222, 233, 225],
        ..., 
        [  3,   6,  23],
        [  3,   6,  23],
        [  3,   6,  23]],

       [[237, 251, 238],
        [233, 247, 234],
        [229, 241, 231],
        ..., 
        [  2,   5,  22],
        [  2,   5,  22],
        [  2,   5,  22]],

       [[216, 229, 212],
        [231, 244, 227],
        [236, 247, 233],
        ..., 
        [  1,   4,  21],
        [  1,   4,  21],
        [  1,   4,  21]],

       ..., 
       [[ 51,  41,  42],
        [ 39,  29,  28],
        [ 18,   8,   6],
        ..., 
        [  8,  12,  13],
        [  0,   3,   9],
        [  0,   1,  11]],

       [[ 37,  17,  28],
        [ 27,   7,  16],
        [ 32,  13,  19],
        ..., 
        [  0,   1,   6],
        [  0,   2,  11],
        [  0,   3,  15]],

       [[ 46,  17,  35],
        [ 28,   0,  15],
        [ 38,  10,  24],
        ..., 
        [  0,   2,   9],
        [  0,   4,  15],
        [  0,   2,  14]]], dtype=uint8)

In [53]:
imgplot2 = plt.imshow(img2)
plt.show()