numpy tutorial(final) 20160303
TRANSCRIPT
Shape Manipulation (1)u Changing the shape of an array• An Array has a shape given by the number of elements along
each axis:>>> a = np.floor(10*np.random.random((3,4))) >>> a array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.], [ 8., 9., 3., 6.]
]) >>> a.shape (3, 4)
2
numpy.floor• 소수점 버리고반올림
Shape Manipulation (2)u The shape of an array can be changed with various
commands:>>> a.ravel() # flatten the arrayarray([ 2., 8., 0., 6., 4., 5., 1., 1., 8., 9., 3., 6.]) >>> a.shape = (6, 2) >>> a.Tarray([[ 2., 0., 4., 1., 8., 3.],
[ 8., 6., 5., 1., 9., 6.]])
3
280645118936
204183865196280645118936
a.shape = (6, 2) a.T
Shape Manipulation (3)u Stacking together different arrays
4
>>> a = np.array([1,2,3])>>> aarray([1,2,3])>>> b = np.array([4,5,6])>>> barray([4,5,6])>>> np.vstack((a,b))array([[1,2,3],
[4,5,6]])>>> np.hstack((a,b))array([1,2,3,4,5,6])
Shape Manipulation (4)u Stacking together different arrays
• The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to vstack only for 1D arrays:
5
>>> from numpy import newaxis>>> np.column_stack((a,b)) # With 2D arraysarray([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])
123 456
a b 142536
np.column_stack((a,b))
Shape Manipulation (5)u Stacking together different arrays
• The function newaxis add an axis to N-dimensional array
6
>>> a = np.array( [[1,2][3,4]])
>>> a[0,newaxis]array([[1,2]])>>> a[1,newaxis]array([[3,4]])>>> a[:,newaxis,:]array( [[[1,2]], #shape
[[3,4]]] #(2,1,2)>>> a[newaxis,:,:]array( [[[1,2], #shape
[3,4]]]) #(1,2,2)
a[:,newaxis,:] = a[range(0,1), newaxis, range(0,1)
a[[1,2],[3,4]]
a[0][1,2]
a[1][3,4]
Adding an axis
Adding an axis [[3,4]]
[[1,2]]
a[:,newaxis,:]array( [ [[1,2]],
[[3,4]] ])
Shape Manipulation (6)u Stacking together different arrays
• The function newaxis add an axis to N-dimensional array
7
>>> a = np.array( [[1,2][3,4]])
>>> a[0,newaxis]array([[1,2]])>>> a[1,newaxis]array([[3,4]])>>> a[:,newaxis,:]array( [[[1,2]], #shape
[[3,4]]] #(2,1,2)>>> a[newaxis,:,:]array( [[[1,2], #shape
[3,4]]]) #(1,2,2)
a[[1,2],[3,4]]
shape(2,2)
a[[[1,2]],[[3,4]]]
a[:,newaxis,:]
shape(2,1,2)
Shape Manipulation (7)u Stacking together different arrays
• The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to vstack only for 1D arrays:
8
>>> np.column_stack((a[:,newaxis],b[:,newaxis]))array([[ 4., 2.],
[ 2., 8.]])>>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is differentarray([[ 4.],
[ 2.],[ 2.],[ 8.]])
42
28
a[:,newaxis]
b[:,newaxis]
4228
np.vstack
Shape Manipulation (8)u Splitting one array into several smaller ones
9
>>> a = np.floor(10*np.random.random((2,12)))>>> aarray([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])>>> np.hsplit(a,3)[array([[ 9., 5., 6., 3.],
[ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.],[ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.],[ 2., 2., 4., 0.]])]
956368079727149221062240
95631492
68072106
97272240
Split a into 3
Shape Manipulation (9)u Splitting one array into several smaller ones
10
>>> a = np.floor(10*np.random.random((2,12)))>>> aarray([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])>>> np.hsplit(a,(3,4))[array([[ 9., 5., 6.],
[ 1., 4., 9.]]), array([[ 3.],
[ 2.]]), array([[ 6., 8., 0., 7., 9., 7., 2., 7.],
[ 2., 1., 0., 6., 2., 2., 4., 0.]])]
956368079727149221062240
956149
32
6807972721062240
# Split a after the third and the fourth column
Copies and views (1)u No copy at all
• Simple assignments make no copy of array objects or of their data.
11
>>> a = np.arange(12)>>> b = a>>> b is aTrue>>> b.shape = 3,4>>> a.shape(3, 4) a
a.shape
b
b.shapeno new object is createda and b are two names for the same ndarray object
ndarrayobject
Copies and views (2)u No copy at all
• Python passes mutable objects as references, so function calls make no copy.
12
>>> def f(x):... print(id(x))...>>> id(a) # id is a unique identifier of an object148293216>>> f(a)148293216
Copies and views (3)u View or Shallow Copy
• Different array objects can share the same data. The view method creates a new array object that looks at the same data.
13
a
a.view
c
c.viewc.base
a.base
ndarrayobject1
ndarrayobject1’
>>> c = a.view()>>> c is aFalse>>> c.base is a True>>> c.flags.owndataFalse>>>>>> c.shape = 2,6>>> a.shape(3, 4)>>> c[0,4] = 1234 >>> aarray([[ 0, 1, 2, 3],
[1234, 5, 6, 7],[ 8, 9, 10, 11]])
data
01234567891011
01234567891011
a c
Copies and views (4)u View or Shallow Copy
• Different array objects can share the same data. The view method creates a new array object that looks at the same data.
14
a
a.view
c
c.viewc.base
a.base
ndarrayobject1
ndarrayobject1’
ndarray.flags.owndata• The�array�owns�the�memory�it�uses�or�borrows�it�from�another�object.
data
>>> c = a.view()>>> c is aFalse>>> c.base is a True>>> c.flags.owndataFalse>>>>>> c.shape = 2,6>>> a.shape(3, 4)>>> c[0,4] = 1234 >>> aarray([[ 0, 1, 2, 3],
[1234, 5, 6, 7],[ 8, 9, 10, 11]])
Copies and views (5)u View or Shallow Copy
• Different array objects can share the same data. The view method creates a new array object that looks at the same data.
15
>>> c = a.view()>>> c is aFalse>>> c.base is a True>>> c.flags.owndataFalse>>>>>> c.shape = 2,6>>> a.shape(3, 4)>>> c[0,4] = 1234 >>> aarray([[ 0, 1, 2, 3],
[1234, 5, 6, 7],[ 8, 9, 10, 11]])
a
a.view
c
c.viewc.base
a.base
ndarrayobject1
ndarrayobject1’
data
01234567891011
01234567891011
a c
Copies and views (6)u View or Shallow Copy
• Slicing an array returns a view of it:
16
>>> aarray([[ 0, 1, 2, 3],
[1234, 5, 6, 7],[ 8, 9, 10, 11]])
>>> s = a[ : , 1:3] >>> s[:] = 10 >>> aarray([[ 0, 10, 10, 3],
[1234, 10, 10, 7],[ 8, 10, 10, 11]])
Copies and views (7)u Deep Copy
• The copy method makes a complete copy of the array and its data.
17
>>> d = a.copy()>>> d is aFalse>>> d.base is aFalse>>> d[0,0] = 9999>>> aarray([[ 0, 10, 10, 3],
[1234, 10, 10, 7],[ 8, 10, 10, 11]])
a
a.shape
d
d.shape
ndarrayobject
ndarrayobject
# a new array object with new data is created# d doesn't share anything with a