Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com Python
A cobra também é inteligente!
1
Membro do Muriçoca Labs - Labs de Projetos com Machine Learning
2
O que é Python ?
6=7%#E!)")F#C>)&&
G."#A$%)F.&H&.8IA%.*1&-=$:#.$)7,J&
!"#$%&'()*+&)*,'&
!"#$%&$'()*') -."&#&#$&/0121345&
+,-$,&(&./-) &&!"#$%&#&
*')&01-)#23'0) 444)5-()%(&)
6"#1&7')
6"(+0'6)')
89:"0)*') 444);(&)
&+,'#*',) <"<0"-1':&)
444)=)+&:-1'6)
$"$')
+>):90:%0-?)
+&*,/-444)
*&*-6?+,-:'66
:%+!A*1*.:BA%*1-#7A*1"ACAD1,& &('#1-?)
"#1',8&:'@)
6)%!7.%7#819:#;+1<=>;+1;+?@1,&
3#%$/$()967*2$2 !"#$%&'$()*
<&/59;&=967*2 +,$-.$/&0(12
3(:;&/$28*/2 345&/&67*28$2
.98*/2 .98*/2
3#%$/$()967*2$2 !"#$%&'$()*
<&/59;&=967*2 +,$-.$/&0(12
3(:;&/$28*/2 345&/&67*28$2
.98*/2 .98*/2
012*2-#"+,-$3$
4)*(".)5"+,-$ !"#$%&'$()*
4(5657$%8% +,$-./$0&1(**
!"#"$%-"./*)*$ !"#"$%&'()*)+,-$
.'/,'#$% !"#$%&'()"()$%
.'/0"1'23'* *('+,"()$%*--%
23+%+/.-4(/)5)
!
6$%#-0$7-4(/
!"#$%&'$()*+
6#.*').'57+,
,-$./0$1&2(++
4/8.+,1,
,-.-)!/-01%$%) !"#$%$&'()*+),-*(%)
2$34/+, !"#$%&$'()#*+,
(-54/+,1+ -./*"0+,1,
http://archive.ics.uci.edu/ml/
Repositórios de Dados
UCI, MovieLens, AWS, KDD, etc.
def class2int(s):
! if s == 'Iris-setosa':
! ! return 1
! elif s == 'Iris-versicolor':
! ! return 0 http://archive.ics.uci.edu/ml/datasets/Iris
! else:
! ! return 2 %n1, n2, n3, n4, class
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
ary1 = loadtxt('iris.data', delimiter=',', 4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
converters={4: lambda s: class2int(s)}, 5.4,3.9,1.7,0.4,Iris-setosa
skiprows=1) 4.6,3.4,1.4,0.3,Iris-setosa
...
print ary1
http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html#numpy.loadtxt
WEB
11
Bibliotecas como BeautifulSoap
<html>
<body>
<table>
<tr> from BeautifulSoup import BeautifulSoup
<td>
3.0
</td> soup = BeautifulSoup(''.join(html))
<td>
4.0
</td> table = soup.find('table')
</tr> 3.0 4.0
<tr>
<td> 5.0 6.2 rows = table.findAll('tr')
5.0
</td> for tr in rows:
<td>
6.2 cols = tr.findAll('td')
</td> for td in cols:
</tr>
</table> text = ''.join(td.find(text=True))
</body>
</html>
print text+"|",
print
!"#$%& '()$%&
! "#$%&$'#()*( ! >?)&8#()*(-8'#($@A*8(
+*,,-.*$'-/(0-,-( *.(=B'C#$(0-,-(
1#.0&'-23#( ',-:-8C-,(1#.(
14*$561-( A*'#,*/(*(.-',4D*/(
! 789*:,-(;4$*-,<( ! E-/*-)#(*.("F(:*.(
=,#1*//-.*$'#( #G.4D-)#(
!!"#$%&&'''()*+$,(-./!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"#$%&&012$,()*+$,(-./!
http://conference.scipy.org/scipy2011/
http://enthought.com
Ipython - http://ipython.org/
Ferramenta essencial para todo desenvolvedor que vai trabalhar com Python + Scipy + Numpy + Matplotlib
Scipy/Numpy Matlab
Gratuito Pago
Matplotlib - http://matplotlib.sourceforge.net/
Python 2D Plotting library
http://www.scipy.org/Cookbook/Matplotlib
!!!"#$%$&'()*+,&*-"*./+*01"
$ ipython --pylab
>>> plot(x,sin(x))
>>> plot(x,cos(x))
20
http://new.scipy.org/download.html
!"#$%&%'()*+)#*,-+.&)#
! ou
!"#$%&'()*&+#($,-./'0&$121&+#3'2'-)'(&$01)13,241(
5))$677-'89("#$%9,2:7+,8-0,1+95)40&
$ !sudo apt-get install python-numpy
! ,;
$ sudo apt-get install python-scipy
! !"#$%&#'()*#!++'",#-.)$)/0",
! !"#$%&#'()*#!++'",#-.)$*1(",
5))$677-;4$%9("#$%9,2:7
5))$6778889("#$%9,2:
!"#$%&'()%*%*+,-./
Arrays e Matrizes
! !""#$%
>>> import numpy as np
! &#'"()*%
>>> a = np.array([0,1,2,3,4,5],
[10,11,12,13,14,15], 0 1 2 3 4 5
[20,21,22,23,24,25],
[30,31,32,33,34,35], 10 11 12 13 14 15
[40,41,42,43,44,45], 20 21 22 23 24 25
[50,51,52,53,54,55])
30 31 32 33 34 35
!!!"#$%&'()*
!""!#$%&'()*+ 40 41 42 43 44 45
50 51 52 53 54 55
!!!"#$+(&+(*
!""!#$%%))'(),*'(((((((
%,)'(,,**+
!!!"#$(&,*
!""!#$%-'.-'--'&-')-',-*+
!!!"#$%&&%'&&%(
#))#*+$$%,'"%%'"%-('"
$-,'"-%'"--((.
!"#$%&'()%*%*+,-./
Arrays e Matrizes
! !""#$%
>>> import &#'"()*%
numpy !as np
>>> a = np.matrix([0,1,2,3,4,5],
0 1 2 3 4 5
[10,11,12,13,14,15],
[20,21,22,23,24,25], 10 11 12 13 14 15
[30,31,32,33,34,35],
20 21 22 23 24 25
[40,41,42,43,44,45],
[50,51,52,53,54,55]) 30 31 32 33 34 35
40 41 42 43 44 45
>>> m_t = a.transpose()
50 51 52 53 54 55
>>> mask = a < 30
Arrays e Matrizes
>>> a = np.array([1,2,3],
[4,5,6],
[7,8,9])
>>> np.mean(a[0,:]) #media
>>> np.std(a[:,1]) #desvio-padrao
>>> np.min(a) #minimo
>>> np.max(a) #maximo
>>> b = a.T #transposta
>>> m = np.dot(a,b) #multipl.
>>> r = np.random.random((100,00))
>>> i = np.linalg.inv(r) #inversa
>>> eigval, eigvec = numpy.linalg.eig(r) #auto-vetores
Marcel
MarcelCaraciolo
Caraciolo- -@marcelcaraciolo
@marcelcaraciolo
24
Explorando o Scipy
>>> a = np.array([1,2,3],
[4,5,6],
[7,8,9])
>>> np.mean(a[0,:])
>>> np.std(a[:,1])
>>> np.min(a)
>>> np.max(a)
>>> b = a.T
>>> m = np.dot(a,b)
>>> r = np.random.random((100,00))
>>> i = np.linalg.inv(r)
>>> eigval, eigvec = numpy.linalg.eig(r)
Estatísticas (scipy.stats)
Clusterização (scipy.cluster)
!"#$%&'()*+,-./$0(1230"#$%&'4
Algoritmos de Agrupamento
! !"#$%&'($)*+,*!#%-./(,0'$
Atualmente, !'-/"(,0',1*/.,0/)*$*!"#$%&'()!"#*+,%'-
! apenas o K-Means
! ./012,341(+$(%52$&+,6%+1(&71('85$29,',1&%+1
!"#$%&'()*+,-./$0(1230"#$%&'4
Clusterização (scipy.cluster)
! !"#$%&'
(
* !"#$%&'+,
01 2#,3&4+5#6+
-
. 7859$,$:56;<,2#,3&:++;=;3:>?',#,2#,2;+5@A3;:+
Fonte: http://www.slideshare.net/santiagosilas/computao-cientfica-com-numpy-e-scipy-7797060
!"#$%&'()*&+,-#./0123'
Clusterização (scipy.cluster)
! !"#$%&'(')*%+'
Scikit-learn http://scikit-learn.sourceforge.net/stable/
Toolkit de aprendizagem de máquina com algoritmos como PCA, SVM, Bayes, etc.
Divisi2 http://csc.media.mit.edu/docs/divisi2/
Pandas http://statsmodels.sourceforge.net/
Sympy - http://code.google.com/p/sympy/
*Crab https://github.com/muricoca/crab
Atualmente, !'-/"(,0',1*/.,0/)*$*!"#$%&'()!"#*+,%'-
! apenas o K-Means
! ./012,341(+$(%52$&+,6%+1(&71('85$29,',1&%+1
1. Documentação
http://docs.scipy.org/doc/
1I. Tutoriais
http://scipy-lectures.github.com/
http://pyscience-brasil.wikidot.com/
IV. Livros
Computação Científica com Python por Flávio Coelho, Editora Lulu.com