You are on page 1of 63

Python

June 23, 2016

Python

2
Python

3
Anaconda
Ipython IDE ipyhton IDE
Anaconda Canopy Enthought Free
Express Anaconda

https://www.continuum.io/downloads

4
Windows Anaconda
TUNA
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

windows64 Anaconda3-4.3.1-Windows-x86_64.exe

Anaconda3-4.3.1-Windows-x86_64

5
Linux Anaconda


6
Spyder

Pyhton
IPyhton
7
python

Python

Python Python

Canopy Code Editor Tools/Package Manager


Mlpy

windows dos

-pip install Mlpy

8
Python I

N ndarray
numpy
C/C++ Fortran

Python
scipy

MATLAB Scilab GNU Octave

matplotlib
numpy
pandas
numpy

9
Python II

scikit-
learn SVM
Kmeans DBSCAN


NLTK
50

Mlpy Cython


feature /
Shogun
C++ Matlab R Octave Python
linux
10
Python III

PyBrain ( )


Milk SVMs K-NN

PyMVPA

Pattern web
HTML

Monte Python


Theano

11

Pyhton

numpy

Reference Guide: numpy-ref-1.11.0.pdf, numpy-html-1.11.0.zip

User Guide: numpy-user-1.11.0.pdf

Package source: numpy-1.11.2.tar.gz, numpy-1.11.2.zip

Old sources: numpy-1.10.4.tar.gz, numpy-1.10.4.zip

12

> Import numpy

python

python python

13

1 help( )

2 pdf

3 / /
; * ? np.*ran*?

help( )

help

14

15
Python
Python

( = )

pandas.mean(axis=None, skipna=None, level=None,


numeric_only=None, **kwargs), axis=None 0
skipna=None True numeric_only=None False

pandas.DataFrame.plot(x=None, y=None,
kind=line,)
16
Python

mean()

sum()

plot()

sort()

R
,

plot()

17
Pyhton ?


Tab

log() binom.test()

log10() fisher_exact()

exp() chisquare()

sin() friedmanchisquare()

cos() mean()

tan() sd()

arcsin() var()

arccos() .

18
Python

boxplot(column=None, by=None, ax=None, fontsize=None,


rot=0, grid=True, figsize=None, layout=None,
return_type=None, **kwds)

data pandas DataFrame

column

by

ax

19

2 + 2

a = 2 a = b = 4

>>> b

>>> 4

>>> print a

>>> 4

20

Python

1. , China china

2.
2result result2

3. _ anova-result1

4.

and, not, from, for, def, class, if, continue, break

21

int 100, 0, -4.335

float 1.5 -34.0

str China

bool True, False

list [foo, beijing]

dict {name: Frank, age: 36}

tuple ('physics', 'chemistry', 1997, 2000)

22
(class)
(vector)

[1,2,3]; ["a","a","b","b","c]

(matrix)

df = pd.DataFrame(np.random.randn(3,4),
index=pd.date_range('2008-01-10', periods=3),
columns=list('ABCD'))

A B C D

2008-01-10 -0.463339 1.003919 -0.074833 0.951425

2008-01-11 -0.484619 1.691674 1.911932 -0.060644

2008-01-12 -0.282851 2.419038 0.283416 0.546126


23

(array)

k k in 1:n, n

(n = 1) (n = 2) (n >= 3)

(dataframe)

pandas /
R dataframe

(list)

24

+, -, *, /, //, **, %

>, <, <=, >=, ==, !=, <>

=, +=, -=, *=, /=, //=, %= **=

and, or, not

&, |, ^, ~, <<, >>

25


1 Column
names

1 40 15 22 600 25
2 51 12 26 350 30
3 46 11 20 390 45

4 38 12 24 260 20

5 49 10 25 220 33
Entry

Row names
26

(dataframe)

(column) (vector)

(row) (vector)

(1)

(2)

27

read.table() read.csv()
txt csv read.table()
read_csv()

pandas excel, ,
SAS, HTML, SQL, STATA

28
read.csv()

tips=pd.read_csv('D:\\Projects\\Python\\Courses\\ch08\\ti
ps.csv')

read_table() read_csv()

29

height = [1.75, 1.80, 1.65, 1.90, 1.74, 1.91]

weight = [60, 72, 57, 90, 95, 72]

sq_height = height ** 2

ratio = weight/sq_height

30

read_table/read_csv txt/csv
excel read_excel

Pandas

31

>30

32

character: ["China", "Korea", "Japan", "UK", "USA]

numeric: [1, 3, 6, 7, 3, 8, 6, 4]

logical: [True, False, True, False]

complex: [1+2j, 5, 4+1j]

33

np.array(),np.zeros(), np.arange(),
np.linspace(), np.indices(), np.empty()

np.array([2,3,1,0])

np.zeros((2, 3))

np.arange(10)

np.linspace(1., 4., 6)

np.indices((3,3))

np.empty((2,5))

34

random.randint(low, high=None, size=None)

random.uniform(low=0.0, hight=1.0, size=None)

stats.uniform.pdf(x, *args, **kwds)

stats.uniform.cdf(x, *args, **kwds)

stats.uniform.ppf(q, *args, **kwds)

35

np.array(data),np.matrix(data),np.zeros((2,3)),
np.empty((4,5)), np.eye(N), pd.DataFrame(array)

np.array() data

arr = np.array([[1,2,3], [4,5,6]])

arr

[0] [1] [2]

[0] 1 2 3

[1] 4 5 6

np.zeros((2,3)), np.empty((4,5)) 2X3 4X5

reshape

arr = np.arange(12).reshape(3,4) 3 4

36

:pd.DataFrame()

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),

'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

head() # 5

tail() # 5

37

( )

list()

list(character,numeric,logical,matrix)

38

python type() array


Series dtype (?)

isnumeric()/isdigit() #

isalpha() #

df.isnull() #

np.isnan(array) # na

39

int() float() #

str() #

array map(int/float/str,source) #

source

40

arr = np.array([5260, 5470, 5640, 6180, 6390, 6515, 6805,
7515, 7515, 8230, 8770])

arr[5]; # 5 np 0

arr[1:5] # 1 4

arr[:5] # 4

arr[3:] # 3

41

df = pd.DataFrame(np.arange(12).reshape(3,4),
index=pd.date_range('2010-01-01', periods=3),
columns=list('ABCD'))
A B C D

2010-01-01 0 1 2 3
(1) 2010-01-02 4 5 6 7
df[A] df.A 2010-01-03 8 9 10 11

(2) [,]

df.ix[1]; 1

df.ix[:2010-01-02, B]; 0 1 B

df.ix[df.B >5, :2] df B 5 0 1

42

[][]

lst = [[1,2,3], [a,b]], lst[1][1] b

[]

43

TRUE
FALSE

arr > 7000

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE


TRUE TRUE TRUE

np.where(cond, xarr, yarr)

44

np.sort()/np.partition()/np.sort_complex() /
/

a = np.array([[1,4],[3,1]])

np.sort(a)

order

45
history

ipython history

History/hist #

46

Scripting

47

(scripting language)
ASCII

Windows , PHP, Perl, Python, Ruby, JavaScript

48

IDE (integrated development environment)


, Windows VisualStudio, JetBrains PyCharm, Enthought Canopy

Python

Windows ,Notepad++, UltraEdit

Linux Vi, Vim, Emacs

49

# plot.py

df = pd.DataFrame(np.random.randn(10,4).cumsum(0),
index=np.arange(0,100,10), columns=list('ABCD'))

50

1 python

ipyhton d:\plot.py

2 ipython

%run plot.py

3 ipython

ctrl+c, ctrl+v

51

52

Python
C,C++

def ( 1= ,):

53

54
if
if( ):

p = 0.03

if( ): if(p<=0.05):

1 print("p <= 0.05!")

else: else:

2 print("p > 0.05!")

55
for, while
for( in ):
for link in links:

print (link)

while( ):
count = 0

while (count < 9):

print ('The count is: %d % count)

count = count + 1

56

Python return

return

python /

57


python try except

try:
print 2/'0'
except ZeroDivisionError:
print ' 0'
except Exception:
print '

58

:

import numpy as np

def rcal(x,y):

z = x**2 + y**2

result = np.sqrt(z)

return result

rcal(3,4)

59

60

to_csv()

to_excel()

to_json()

to_sql()

to_stata()

to_msgpack()

plt.savefig ()

61
Python
iPython : http://ipython.org/

Anaconda https://docs.continuum.io/

NumPy: http://www.numpy.org/

Pandas http://pandas.pydata.org/

SciPy: http://scipy.org/

Skiti-learn http://scikit-learn.org/

Python http://www.pythontab.com/

62

63

You might also like