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
In [6]:
timeit list_times(larr, 1.1)
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)
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
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
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
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]:
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
Out[12]:
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
In [14]:
recarr = np.zeros((2, ), dtype='i4, f4, a10')
recarr
Out[14]:
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]:
In [17]:
recarr[:] = toadd
recarr
Out[17]:
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]:
In [19]:
# Reference by names:
print 'recarr["Integers"]:\n', recarr['Integers']
print
print 'recarr["Integers"][0]:\n', recarr['Integers'][0]
In [20]:
x = np.zeros(2, dtype=('i4, f4, a10'))
x[0] = (1, 1, 'H')
x[1] = (10.0, 10, 10)
print x
In [21]:
# Again, be aware of the fact that python's pointer.
y = x['f1']
y *= 2
print x
In [22]:
x = np.zeros(3, dtype='3int8, float32, (2,3)float64')
x
Out[22]:
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
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]:
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']
In [26]:
y.dtype.fields
Out[26]:
In [ ]:
In [123]:
# Indexing:
alist = [[1, 2], [3, 4]]
arr = np.array(alist)
print 'arr:\n', arr
In [124]:
arr[0, 1]
Out[124]:
In [125]:
arr[:,1]
Out[125]:
In [126]:
arr[1,:]
Out[126]:
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
In [147]:
new_arr1 = arr[ind]
print 'new_arr1:\n', new_arr1
new_arr2 = arr[ind2]
print 'new_arr2:\n', new_arr2
In [144]:
# np.delete(who, where):
new_arr = np.delete(arr, ind)
new_arr
Out[144]:
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 [ ]: