Python_numpy_pandas_matplotlib 이해하기_20160815

Embed Size (px)

Citation preview

PowerPoint

Python mathematics/numpy/pandas/matplotlibMoon Yong Joon1

1. Numpy 2. Numpy 3. 4. 5. 6. //

2

7. 8.

3

9.Matplotlib 10. Pandas DataFrame (2)11. Pandas panel(3)12. Pandas Serires (1)

4

1. NumpyMoon Yong Joon5

NdArrayMoon Yong Joon6

ndarray class7

ndarray Ndarray data-type , array scalar 8

ndarray Ndarray shape, dtype,strides

9

ndarray Ndarray shape, dtype,strides

10

len()

11

ndarray 12

numpy.ndarray : 1a = numpy.array([1,2,3])exampleDescriptionndarray.ndima = numpy.array([1,2,3])>>> a.ndim1 ndarray ndarray.shape>>> a.shape(3,) ndarray ndarray.size>>> a.size3 ndarray ndarray.dtype>>> a.dtypedtype(int32) ndarray ndarray.itemsize>>> a.itemsize4 ndarray ndarray.data>>> type(a.data)>>> a.data.__str__()'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00' ndarray itemsize hex

13

numpy.ndarray : 2exampleDescriptionndarray.reali = np.array([1+2j, 2+3j])i.real #array([ 1., 2.])i.imag # array([ 2., 3.]) ndarray ndarray.imag ndarray ndarray.stridesi.Strides #(16,) ndarray ndarray.basex = np.array([1,2,3]) y = x[1:] y.base is x # True y.base # array([1, 2, 3]) ndarray ndarray.flat x = np.arange(1,7).reshape(2,3) x # array([[1, 2, 3], [4, 5, 6]]) x.flat[3] # 4x.T # array([[1, 4],[2, 5],[3, 6]]) x.T.flat[3] # 5 ndarray index ndarray.T ndarray

14

15

ndarray ndarray ndarray copy copy

Slice ndarray

16

Range 17

range numpy.arange ndarray numpy.arange array

18

19

F () = c() * 9 / 5 + 32 loop

ndarray array ndarray 20

21

__getitem__ numpy.ndarray __getitem__

22

__setitem__ Index slice __setitem__ override

23

24

list ndarray numpy.ndarray python list

25

26

numpy , ndarray numpy module

ndarray class

27

python

28

numpy.copy vs ndarray.copycopy(obj, order='K') , obj.copy(order=C) order {'C', 'F', 'A', 'K'}. 'C' : C-order, 'F' : Fortran-order, 'A' : fortran F, C, 'K' : obj

29

30

save/load ndarray (: nd) load

31

numpy 32

ndarray 1ndarray array( []),,

33

ndarray 2ndarray numpy.int_numpy.float_numpy.bool_array( []),,

( )34

ndarray35

ndarray numpy array ndarray

36

ndarray ndarray float

37

Moon Yong Joon38

: 1 Ndarray array ndarray dtype asarray ndarray ndarray arange range ndarray ones dtype 1 ones_like dtype 1 zero ones 0

39

: 2 Ndarray zeros_like ones_like 0dmfh codnsekempty empty_like eye n*n 1 0identity n*n linspace ndarray

40

array 41

numpy.array numpy.array ndarray

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndim=0)42

array : 0numpy.array (scalar value) arrary

43

array : 1 . , , ndim(), shape(), (dtype)

44

3, 3 [0,0][0,1][0,2][1,0][1,1][1,2][2,0][2,1][2,2]Row : Column: 012012Index [][][, ]Slice [, ]45

. , , ndim(), shape(), (dtype)

46

array :nnumpy.array sequence

47

zeros 48

numpy.zeros numpy.zeros ndarray

numpy.zeros(shape, dtype=float, order='C')49

zeors ndarray (int,float) ndarray

50

zeors : ndarray (int,float) ndarray

51

numpy.zeros numpy.zeros shape zero

52

eye 53

numpy.eye numpy.eye ndarray K 1(1.0) N,M nM array

numpy.eye(N, M=None, k=0, dtype=)54

numpy.eye k K 1(1.0)

K=0K=1K=2K=-1K=-2

55

numpy.eye numpy.eye 1 . k

56

identity 57

numpy.identity numpy.identiry ndarray 1

numpy.identity(n, dtype=None)58

numpy.identity numpy.identity 1

59

linspace 60

numpy.linspace num( )

linspace(start, stop, num=50, endpoint=True, retstep=False)61

linspace 1 ndarray random

62

linspace Linspace ndarray

63

linspace :2Linspace endpoint false

64

numpy.linspace 1 ndarray retstep True

65

arange66

arange : 1arange

67

(index) (slicing)

68

: Broadcasting scalar npl[2:5] 3 42 [42,42,42]

69

loop . Ndarray copy()

70

Moon Yong Joon71

72

Ndarray

+ : - : * : / : ** : % :

73

Dot :ndarray ndarray

74

Matrix /75

Matrix A B

76

Matrix A B

77

78

* scalaScala

79

Matrix 80

Matrix A B A B

A(m*k)B(k*n)

C(m*n).=

81

Dot : ndarray(2 2) (2 2) 2 2 ndarray

82

Dot : ndarray (4,3)*(3,1) ndarray (4,1) 100

83

Dot :matrix(2 2) (2 2) 2 2 matrix

84

Matrix (*)Dot

85

Matrix (*) : asmatrixMatrix ndarray ndarray matrix

86

Vector 87

vector (vector, : cross product) () 3 . a = [0,0,1] b = [0,1,0] a*b = [0-1,0-0,0-0] = [-1,0,0] :a*b = (a2b3a3b2, a3b1a1b3, a1b2a2b1)

88

cross np.cross

89

array/Moon Yong Joon90

91

axis : 2Axis 0 , 1

92[0,0][0,1][0,2][1,0][1,1][1,2]Row : Column: 012012

axis : 3Axis , 0 , 1 2 93

67891011Row : Column: 012012012345Row : Column: 012012

axis : 3Axis , 0 , 1 2 94

67891011Row : Column: 012012012345Row : Column: 012012

2 95

[ , ]96

: , [0,0][0,1][0,2][1,0][1,1][1,2][2,0][2,1][2,2]Row : Column: 012012[0,0][0,1][0,2][1,0][1,1][1,2][2,0][2,1][2,2]Row : Column: 012012

97

: [0,0][0,1][0,2][1,0][1,1][1,2][2,0][2,1][2,2]Row : Column: 012012

98

:

[0,0][0,1][0,2][1,0][1,1][1,2][2,0][2,1][2,2]Row : Column: 01201299

N 100

: / 7*4 99

[, ]

Slicing

[ , ] 101

argmax/argmin [ ]

102

103

ndarray ndarray ndarray . Scala broadcasting ndarray bool ndarray ndarrayndarrayndarray=104

: / [data1 int bool astype

108

Fancy Indexing109

Fancy indexing : booleanNdarray ndarray

110

Fancy indexing : 1 e0 e1

111

Fancy indexing : e0 e1

112

Fancy indexing :expressionndarray 3

113

Fancy indexing :nonzeroNonzero zero ndarray

114

arrayMoon Yong Joon115

dtype 116

ndarray index

xyzarray[x] x 117

Array : string dtype 1

118

dtype 119

Array : list dtype

120

ndarray : 1 ndarray (int, ) 1

121

Array : dict -1 dtype dict names , formats

122

Array : dict - 2 dtype dict , index, Key . Key

123

ndarray : 2 1ndarray (int, ) 2

124

ndarray : 2 2ndarray (int, float) 2

125

ndarray 126

numpy.array numpy.array sequence

ndarray

127

numpy.array numpy.array

128

numpy.array numpy.array sequence

129

ndarray 130

dtype names

131

dtype classMoon Yong Joon132

133

Data type : 1numpy

Data typeDescriptionbool_Boolean (True or False) stored as a byteint_Default integer type (same as Clong; normally eitherint64orint32)intcIdentical to Cint(normallyint32orint64)intpInteger used for indexing (same as Cssize_t; normally eitherint32orint64)int8Byte (-128 to 127)int16Integer (-32768 to 32767)int32Integer (-2147483648 to 2147483647)int64Integer (-9223372036854775808 to 9223372036854775807)uint8Unsigned integer (0 to 255)uint16Unsigned integer (0 to 65535)uint32Unsigned integer (0 to 4294967295)uint64Unsigned integer (0 to 18446744073709551615)

134

Data type : 2numpy

Data typeDescriptionfloat_Shorthand forfloat64.float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissafloat32Single precision float: sign bit, 8 bits exponent, 23 bits mantissafloat64Double precision float: sign bit, 11 bits exponent, 52 bits mantissacomplex_Shorthand forcomplex128.complex64Complex number, represented by two 32-bit floats (real and imaginary components)complex128Complex number, represented by two 64-bit floats (real and imaginary components)

135

Int/float/ bool_ numpy ndarray

136

numpy.dtype 137

numpy.dtype numpy.dtype ndarray

numpy.dtype(object, align=False, copy=False)138

numpy.dtype (1)Array array-scalar type

139

numpy.dtype (2)Array , Structured type : two field

Structured type : one field140

array-protocol type string(: big-endian,|: not-relevant) tBit field'b'boolean'i'(signed) integer'u'unsigned integer'f'floating-point'c'complex-floating point'O'(Python) objects'S','a'(byte-)string'U'Unicode'V'raw data (void)

.141

Ndarray 1 Ndarray int8, uint8i1,u1 1byites int16, uint16i2,u2 2byites int32, uint32i4,u4 4byites int64, uint64i8,u8 8byites float16f2 float32f4, f . C float float64f8, d , C double, Python floatfloat128f16, g complex64c8 32 2 complex128c16 64 2

.142

Ndarray 2 Ndarray complex256c32 128 2 bool? True, False objectO string_S - 1unicode_U

.143

144

Byte (: big-endian,|: not-relevant),.: big-endian|: not-relevant (LSB) . , 12 2 1100 0011 . . (MSB) , 12 2 1100 1100 . endian 145

Little-endian .>>> ar = np.array([1,2,3])>>> ar.tostring()'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'>>> l = ar.tosting()>>> ar.itemsize8>>> l[0:8]x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x008bytes 146

big-endian .>>> ar = np.array([1,2,3],dtype='>i8')>>> ar.tostring()'\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03>>> ar.dtype dtype('>i8')x00\x00\x00\x00\x00\x00\x00\x01

8bytes 147

not-relevantendian .>>> sr = np.array(['a','b',"abc"],dtype='|S10')>>> sr.tostring()'a\x00\x00\x00\x00\x00\x00\x00\x00\x00b\x00\x00\x00\x00\x00\x00\x00\x00\x00abc\x00\x00\x00\x00\x00\x00\x00>>> sr.dtypedtype('S10')>>> sr.tostring()[0:10] 'a\x00\x00\x00\x00\x00\x00\x00\x00\x00'

10bytes 148

numpy.dtype 149

dtype - 1 Method Array exampleDescription charimport numpy as np

f_ = np.float_(1.0)print f_,f_.dtype.char, f_.itemsizeprint f_.dtype.namesprint f_.dtype.nameprint f_.dtype.shapeprint f_.dtype.num

#1.0 d 8Nonefloat64()12 itemsize name namesOrdered list field . Noneshape numBuiltin

150

dtype - 2 Method Array exampleDescription typeimport numpy as np

f_ = np.float_(1.0)print f_.dtype.typeprint f_.dtype.strprint f_.dtype.isbuiltinprint f_.dtype.byteorderprint f_.dtype.alignmentprint f_.dtype.isalignedstruct

#