In [1]:
%timeit
import numpy as np

In [2]:
arr = np.arange(1e7)

In [3]:
larr = arr.tolist()

In [4]:
# list by default does not support broadcasting.
# Here we define a helper function to do broadcasting for list.
def list_times(alist, scalar):
    for i, val in enumerate(alist):
        alist[i] = val * scalar
    return alist

In [5]:
# np.array support broadcast. (And faster than the helper function.)
timeit arr * 1.1


  File "<ipython-input-5-67401cb7a16f>", line 2
    timeit arr * 1.1
             ^
SyntaxError: invalid syntax

In [6]:
timeit list_times(larr, 1.1)


1 loops, best of 3: 1.83 s per loop

In [7]:
# np.matrix can only handle 2-dim array.
# This will give you an error
arr = np.zeros((3, 3, 3))
mat = np.matrix(arr)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-c8cf30a8869d> in <module>()
      2 # This will give you an error
      3 arr = np.zeros((3, 3, 3))
----> 4 mat = np.matrix(arr)

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.pyc in __new__(subtype, data, dtype, copy)
    245             else:
    246                 intype = N.dtype(dtype)
--> 247             new = data.view(subtype)
    248             if intype != data.dtype:
    249                 return new.astype(intype)

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.pyc in __array_finalize__(self, obj)
    290                 return
    291             elif (ndim > 2):
--> 292                 raise ValueError, "shape too large to be a matrix."
    293         else:
    294             newshape = self.shape

ValueError: shape too large to be a matrix.

In [ ]:
# This will not.
arr = np.zeros((3, 3))
mat = np.matrix(arr)
arr

In [ ]:
# Covert a list into np.array
alist = [1, 2, 3]
arr = np.array(alist)
arr

In [ ]:
# Creating matrix with np.
elements = [[1, 2, 3], [2, 2, 2], [3, 3, 3]]
mat = np.matrix(elements)

In [ ]:
mat

In [ ]:
# zero array: 1-dim
arr1 = np.zeros(5)
print 'arr1\n', arr1

# zero array: 2-dim
arr2 = np.zeros((2, 3))
print 'arr2\n', arr2

# zero array: multi-dim
arr3 = np.zeros((2, 2, 3))
print 'arr3\n', arr3

In [ ]:
# array range from 0 to 99
arr_range = np.arange(100)
print arr_range

In [ ]:
# array range from 10 to 99
arr_range2 = np.arange(10, 100)
print arr_range2

In [ ]:
# linespace in numpy: goes from 0 to 1 with 1000 steps
linsp = np.linspace(0, 1, 1000)
print linsp

In [ ]:
# log linspace: np.logspace(start, end, steps, base) 
# --> goes from base**start to base**end by 100 steps in log scale.
linsp2 = np.logspace(0, 1, 100, base = 10.0)
print linsp2

In [ ]:
# Change the data type of your np.array with astype() method.
cube1 = np.zeros((5, 5)).astype(int)
print cube1
# Other option such as np.float16, np.float32 and np.float64(default).
cube2 = np.zeros((5, 5)).astype(np.float64) + 1
print cube2

In [ ]:
# Even simpler way to do the job above.
cube1 = np.zeros(2, dtype = int)
print cube1
cube2 = np.zeros(2, dtype=np.float32)
print cube2

In [ ]:
# Reshape an array to arbitrary shape (dimensions).
cube = np.arange(100)
reshaped_cube = cube.reshape((10, 10))
print reshaped_cube

In [ ]:
# 4-D array by reshpae
arr = np.arange(10000)
arr2 = arr.reshape((10, 10, 10, 10))
print arr2

In [ ]:
# ravel will return an flatten 1-D array 
print arr2.ravel()

In [8]:
# Difference between .flat-->(attributes) and .flatten --> (method).
arr = np.array([1, 2, 3, 4])
arr = arr.reshape((2, 2))
print 'original arr:\n', arr

# You can use .flat to change the value of elements in an array.
arr[1] = 10
print 'assign arr[1] to 10:\n', arr

arr = np.array([1, 2, 3, 4])
arr = arr.reshape((2, 2))
arr.flat[1] = 10
print 'arr.flat[1] (Notice the differnce with arr[1] = 10 above):\n', arr

arr.flat[[0, 2]] = 33
print 'arr.flat[[0, 2]] = 33:\n', arr

# .flatten will return a 1-D copied array from an np.array object.
arr2 = arr.flatten()
print 'arr.flatten():\n', arr2


original arr:
[[1 2]
 [3 4]]
assign arr[1] to 10:
[[ 1  2]
 [10 10]]
arr.flat[1] (Notice the differnce with arr[1] = 10 above):
[[ 1 10]
 [ 3  4]]
arr.flat[[0, 2]] = 33:
[[33 10]
 [33  4]]
arr.flatten():
[33 10 33  4]

In [9]:
# One thing keep in mind is that above arrays are just different views of "the same" data
# in the memory. See example below.
arr = np.array([1, 2, 3, 4])
arr2 = arr.reshape((2, 2))
print 'origin arr2:\n', arr2

arr.flat[0] = 0
print 'The modification to arr will affect arr2:\n', arr2


origin arr2:
[[1 2]
 [3 4]]
The modification to arr will affect arr2:
[[0 2]
 [3 4]]

In [10]:
# You can use np.copy function to prevent such things.
arr = np.array([1, 2, 3, 4])
arr3 = np.copy(arr)
arr.flat[0] = 4
print 'arr:\n', arr
print 'arr3:\n', arr3


arr:
[4 2 3 4]
arr3:
[1 2 3 4]

In [11]:
# Using np.array to store data with different data types.
# test now is an 2x2 array where each entity of test is of 
# '32-bits int'(i4), '32-bits float'(f4) and 'string of length 5'(a5).
test = np.zeros((2,2), dtype=('i4, f4, a5'))
test


Out[11]:
array([[(0, 0.0, ''), (0, 0.0, '')],
       [(0, 0.0, ''), (0, 0.0, '')]], 
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S5')])

In [12]:
# Since we assign 'a5' to the third element of each entity of test, any string 
# length over 5 will be truncated.
# See example below:
test[1,1][2] = 'abcdefg'
print 'Only "abcde" is stored into test rather than "abcdefg".'
test


Only "abcde" is stored into test rather than "abcdefg".
Out[12]:
array([[(0, 0.0, ''), (0, 0.0, '')],
       [(0, 0.0, ''), (0, 0.0, 'abcde')]], 
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S5')])

In [13]:
# One interesting example:
recarr = np.zeros((2, 3), dtype=('i4, f4'))
print 'recarr:\n', recarr
recarr2 = np.zeros((2, 3), dtype=('i4, f4, a10'))
print 'recarr2:\n', recarr2
recarr3 = np.zeros((2, ), dtype=('i4, f4, a10'))
print 'recarr3:\n', recarr3


recarr:
[[(0, 0.0) (0, 0.0) (0, 0.0)]
 [(0, 0.0) (0, 0.0) (0, 0.0)]]
recarr2:
[[(0, 0.0, '') (0, 0.0, '') (0, 0.0, '')]
 [(0, 0.0, '') (0, 0.0, '') (0, 0.0, '')]]
recarr3:
[(0, 0.0, '') (0, 0.0, '')]

In [14]:
recarr = np.zeros((2, ), dtype='i4, f4, a10')
recarr


Out[14]:
array([(0, 0.0, ''), (0, 0.0, '')], 
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])

In [15]:
col1 = np.arange(2) + 1
col2 = np.arange(2, dtype = np.float32)
col3 = ['Hello', 'World']

In [16]:
# zip: build-in matching function.
toadd = zip(col1, col2, col3)
toadd


Out[16]:
[(1, 0.0, 'Hello'), (2, 1.0, 'World')]

In [17]:
recarr[:] = toadd
recarr


Out[17]:
array([(1, 0.0, 'Hello'), (2, 1.0, 'World')], 
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])

In [18]:
# From above examples, we can see that the colnames for each column 
# are 'f0', 'f1', 'f2', ..., etc. We can change the colnames by flowing:
recarr.dtype.names = ('Integers', 'Floats', 'Strings')
recarr


Out[18]:
array([(1, 0.0, 'Hello'), (2, 1.0, 'World')], 
      dtype=[('Integers', '<i4'), ('Floats', '<f4'), ('Strings', '|S10')])

In [19]:
# Reference by names:
print 'recarr["Integers"]:\n', recarr['Integers']
print
print 'recarr["Integers"][0]:\n', recarr['Integers'][0]


recarr["Integers"]:
[1 2]

recarr["Integers"][0]:
1

In [20]:
x = np.zeros(2, dtype=('i4, f4, a10'))
x[0] = (1, 1, 'H')
x[1] = (10.0, 10, 10)
print x


[(1, 1.0, 'H') (10, 10.0, '10')]

In [21]:
# Again, be aware of the fact that python's pointer.
y = x['f1']
y *= 2
print x


[(1, 2.0, 'H') (10, 20.0, '10')]

In [22]:
x = np.zeros(3, dtype='3int8, float32, (2,3)float64')
x


Out[22]:
array([([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.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.0]])], 
      dtype=[('f0', '|i1', (3,)), ('f1', '<f4'), ('f2', '<f8', (2, 3))])

In [23]:
x1 = np.zeros((3, 2), dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))])
print 'x1:\n', x1
x2 = np.zeros((2, 3), dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))])
print 'x2:\n', x2


x1:
[[(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.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, 0.0, [[0.0, 0.0], [0.0, 0.0]]) (0.0, 0.0, [[0.0, 0.0], [0.0, 0.0]])]]
x2:
[[(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.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, 0.0, [[0.0, 0.0], [0.0, 0.0]])
  (0.0, 0.0, [[0.0, 0.0], [0.0, 0.0]])]]

In [24]:
# dtype as list.
# dtype = [('Var1name', 'V1type', (dim1, dim2, ...)), ...]
x = np.zeros(3, dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2,2))])
x


Out[24]:
array([(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.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, 0.0]], [[0.0, 0.0], [0.0, 0.0]]])], 
      dtype=[('x', '<f4'), ('y', '<f4'), ('value', '<f4', (2, 2, 2))])

In [25]:
# dtype as dictionary.
# dtype = {'names':[colname1, colname2, ...], 'formats':['format1', 'format2', ...]}
y = np.zeros((3, 2), dtype = {'names':['x', 'y'], 'formats':['(2,2)f4', 'i4']})
print y[0, 0]['x']


[[ 0.  0.]
 [ 0.  0.]]

In [26]:
y.dtype.fields


Out[26]:
<dictproxy {'x': (dtype(('float32',(2, 2))), 0), 'y': (dtype('int32'), 16)}>

In [ ]:


In [123]:
# Indexing:
alist = [[1, 2], [3, 4]]
arr = np.array(alist)
print 'arr:\n', arr


arr:
[[1 2]
 [3 4]]

In [124]:
arr[0, 1]


Out[124]:
2

In [125]:
arr[:,1]


Out[125]:
array([2, 4])

In [126]:
arr[1,:]


Out[126]:
array([3, 4])

In [146]:
# np.where(): its function is just like which() in R.
arr = np.arange(10, 16)
ind = np.where(arr > 13)
ind2 = arr > 13
print 'ind:\n', ind
print 'ind2:\n', ind2


ind:
(array([4, 5]),)
ind2:
[False False False False  True  True]

In [147]:
new_arr1 = arr[ind]
print 'new_arr1:\n', new_arr1
new_arr2 = arr[ind2]
print 'new_arr2:\n', new_arr2


new_arr1:
[14 15]
new_arr2:
[14 15]

In [144]:
# np.delete(who, where):
new_arr = np.delete(arr, ind)
new_arr


Out[144]:
array([10, 11, 12, 13])

In [ ]:


In [33]:
%matplotlib inline
import matplotlib.pyplot as plt

In [58]:
img1 = np.zeros((20, 20)) + 3
img1[4:-4, 4:-4] = 6
img1[7:-7, 7:-7] = 9

In [59]:
imgplot1 = plt.imshow(img1)



In [51]:
compound_ind = (img1 > 3) & (img1 < 7)
img2 = np.copy(img1)
img2[compound_ind] = 0
imgplot2 = plt.imshow(img2)



In [61]:
index1 = img1 > 3
index2 = img1 < 7
index3 = img1 == 9
compound_ind2 = (index1 | index2) & index3
img3 = np.copy(img1)
img3[compound_ind2] = 0
imgplot3 = plt.imshow(img3)



In [ ]: