In [15]:
import scipy.misc
import matplotlib.pyplot as plt
%matplotlib inline
plt.gray()
img = scipy.misc.face().astype('float32')
plt.imshow(img)
img.dtype, img.shape, img.size


Out[15]:
(dtype('float32'), (768, 1024, 3), 2359296)

In [9]:
import numpy
scores= numpy.array([101, 103,84],dtype='float32')
scores


Out[9]:
array([ 101.,  103.,   84.], dtype=float32)

In [10]:
a=numpy.array(['Clesse', 'Idle', 'Gilliam'],dtype='str_')
a.dtype


Out[10]:
dtype('<U7')

In [12]:
dt = numpy.dtype([('name',numpy.str_,16),('grades',numpy.float64,(2,))])
dt


Out[12]:
dtype([('name', '<U16'), ('grades', '<f8', (2,))])

In [24]:
A=numpy.array([[1,2,3,4,5,6,7,8],[2,4,6,8,10,12,14,16]])
print (A)
print (A[0:2,0:8:2])
print(A[0:10,-1:0:-2])
print(A[:,::-2])
print(A[::-2])
print(A[[0,0,1,1],[0,3,2,5]])  # use the idx, must be same size

print(A[numpy.ix_([0,1],[0,3])])


[[ 1  2  3  4  5  6  7  8]
 [ 2  4  6  8 10 12 14 16]]
[[ 1  3  5  7]
 [ 2  6 10 14]]
[[ 8  6  4  2]
 [16 12  8  4]]
[[ 8  6  4  2]
 [16 12  8  4]]
[[ 2  4  6  8 10 12 14 16]]
[ 1  4  6 12]
[[1 4]
 [2 8]]

In [16]:
value=0; import scipy.misc; img=scipy.misc.face()
for item in img.flat: value+=item
value

img.tofile("face.tx",sep=" ",format="%i")

print(img.min(), img.max(), img.ptp())
A=img.clip(img.min(),img.min()+100)
print(A.min(),A.max(),A.ptp())

plt.imshow(A)


0 255 255
0 100 100
Out[16]:
<matplotlib.image.AxesImage at 0x7f6dc2680eb8>

In [6]:
import numpy 
A=numpy.array([11,13,15,17,19,18,16,14,12,10])
A.argsort(kind='mergesort')
A.sort()
print(A)


[10 11 12 13 14 15 16 17 18 19]

In [11]:
A=numpy.array([[1,1,1],[2,2,2],[3,3,3]])
print(A)
print(A.mean())
print(A.mean(axis=0))
print(A.mean(axis=1))


[[1 1 1]
 [2 2 2]
 [3 3 3]]
2.0
[ 2.  2.  2.]
[ 1.  2.  3.]

In [23]:
Z= numpy.zeros((5,5))
print(Z)
numpy.ones((5,5))
numpy.identity(3)
numpy.eye(4,k=1)


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

In [29]:
B=numpy.fromfunction(( lambda i,j: i*j),(4,4))
print(B)
numpy.fromfunction(( lambda i,j: i*j),(4,4)) == 0


[[ 0.  0.  0.  0.]
 [ 0.  1.  2.  3.]
 [ 0.  2.  4.  6.]
 [ 0.  3.  6.  9.]]
Out[29]:
array([[ True,  True,  True,  True],
       [ True, False, False, False],
       [ True, False, False, False],
       [ True, False, False, False]], dtype=bool)

In [32]:
numpy.where(B != 0)

numpy.putmask(B, B%2 != 0, B**2+1)
print(B)


[[  0.   0.   0.   0.]
 [  0.   2.   2.  10.]
 [  0.   2.   4.   6.]
 [  0.  10.   6.  82.]]

In [36]:
L1 = numpy.arange(-1,1,0.3)
print(L1)
L2=numpy.linspace(-1,1,4)
print(L2)
L3=numpy.logspace(-1,1,4)
print(L3)

print(numpy.meshgrid(L2,L3))


[-1.  -0.7 -0.4 -0.1  0.2  0.5  0.8]
[-1.         -0.33333333  0.33333333  1.        ]
[  0.1          0.46415888   2.15443469  10.        ]
[array([[-1.        , -0.33333333,  0.33333333,  1.        ],
       [-1.        , -0.33333333,  0.33333333,  1.        ],
       [-1.        , -0.33333333,  0.33333333,  1.        ],
       [-1.        , -0.33333333,  0.33333333,  1.        ]]), array([[  0.1       ,   0.1       ,   0.1       ,   0.1       ],
       [  0.46415888,   0.46415888,   0.46415888,   0.46415888],
       [  2.15443469,   2.15443469,   2.15443469,   2.15443469],
       [ 10.        ,  10.        ,  10.        ,  10.        ]])]

In [38]:
print(numpy.mgrid[0:5,0:5])

print(numpy.ogrid[0:5,0:5])


[[[0 0 0 0 0]
  [1 1 1 1 1]
  [2 2 2 2 2]
  [3 3 3 3 3]
  [4 4 4 4 4]]

 [[0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]]]
[array([[0],
       [1],
       [2],
       [3],
       [4]]), array([[0, 1, 2, 3, 4]])]

In [45]:
B=numpy.ones((3,3))
checker2by2=numpy.zeros((6,6))
checker2by2[0:3,0:3] = checker2by2[3:6,3:6]=B

#numpy.tile(checker2by2,(4,4))

In [44]:
import scipy.ndimage
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
text = scipy.ndimage.imread('Chap_02_text_image.png')


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-44-7f9e5aa645af> in <module>()
      3 import matplotlib.pyplot as plt
      4 get_ipython().magic('matplotlib inline')
----> 5 text = scipy.ndimage.imread('Chap_02_text_image.png')

/usr/local/lib/python3.4/dist-packages/scipy/ndimage/io.py in imread(fname, flatten, mode)
     41                           " instructions.")
     42 
---> 43     im = Image.open(fname)
     44     if mode:
     45         im = im.convert(mode)

/usr/local/lib/python3.4/dist-packages/PIL/Image.py in open(fp, mode)
   2217     if isPath(fp):
   2218         filename = fp
-> 2219         fp = builtins.open(fp, "rb")
   2220     else:
   2221         filename = ""

FileNotFoundError: [Errno 2] No such file or directory: 'Chap_02_text_image.png'

In [ ]: