SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
Clustering com numpy e Cython
       Anderson Berg
   XIII Encontro do PUG-PE
Agrupamento de dados
K-Means
Matrizes
Exemplo simples

>>> from numpy import *

>>>a = arange(10).reshape(2,5)

>>>a
array([[0, 1, 2, 3, 4],
      [5, 6, 7, 8, 9]])
Criação de arrays
>>> a = array( [2,3,4] )


>>> b = array( [ (1.5,2,3), (4,5,6) ] )

>>> b

array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]])


>>> zeros( (3,4) )

array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])

>>> ones( (2,3,4), dtype=int16 )

array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1,

1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16)
Outras Operações

>>> b = arange( 4 )
>>> b**2
array([0, 1, 4, 9])
>>> a = random.random((2,3))
>>> a
array([[ 0.6903007 ,    0.39168346,   0.16524769],
        [ 0.48819875,   0.77188505,   0.94792155]])
Ainda outras operações


>>> a.sum()
3.4552372100521485
>>> a.min()
0.16524768654743593
>>> a.max()
0.9479215542670073

>>> a[:,1]
array([0.39168346, 0.77188505])

>>> sum(a[:,1])
1.1635685099999999
Primeiros Passos
helloworld.pyx:

print "Hello World"


setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
   cmdclass = {'build_ext': build_ext},
   ext_modules = [Extension("helloworld",
["helloworld.pyx"])] )



$ python setup.py build_ext --inplace


>>> import helloworld
Hello World
Advanced Mode

def primes(int kmax):
   cdef int n, k, i
   cdef int p[1000]
   result = []
   if kmax > 1000:
       kmax = 1000
   k = 0
   n = 2
   while k < kmax:
       i = 0
       while i < k and n % p[i] != 0:
          i = i + 1
       if i == k:
          p[k] = n
          k = k + 1
          result.append(n)
       n = n + 1
   return result
Sem Cython   Com Cython
Clustering com numpy e Cython
  Anderson Berg - @berg_pe
   XIII Encontro do PUG-PE

Mais conteúdo relacionado

Mais procurados

python高级内存管理
python高级内存管理python高级内存管理
python高级内存管理rfyiamcool
 
밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AINAVER Engineering
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.Dr. Volkan OBAN
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析Takashi Kitano
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPaweł Dawczak
 
Extending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::UtilExtending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::UtilNova Patch
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
Advanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part IIAdvanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part IIDr. Volkan OBAN
 
Groovy collection api
Groovy collection apiGroovy collection api
Groovy collection apitrygvea
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181Mahmoud Samir Fayed
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..Dr. Volkan OBAN
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in ElixirJesse Anderson
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 

Mais procurados (20)

python高级内存管理
python高级内存管理python高级内存管理
python高级内存管理
 
밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI
 
Py3k
Py3kPy3k
Py3k
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.
 
Slides
SlidesSlides
Slides
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
Extending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::UtilExtending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::Util
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Advanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part IIAdvanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part II
 
Groovy collection api
Groovy collection apiGroovy collection api
Groovy collection api
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
Five
FiveFive
Five
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in Elixir
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Intro
IntroIntro
Intro
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
 

Destaque

Wikilytics
WikilyticsWikilytics
Wikilyticspugpe
 
Migrando do App Engine para o Heroku
Migrando do App Engine para o HerokuMigrando do App Engine para o Heroku
Migrando do App Engine para o HerokuFilipe Ximenes
 
Coding Dojo e Test Driven Development
Coding Dojo e Test Driven DevelopmentCoding Dojo e Test Driven Development
Coding Dojo e Test Driven Developmentpugpe
 
Rain Toolbox - Previsão de Chuvas
Rain Toolbox -  Previsão de ChuvasRain Toolbox -  Previsão de Chuvas
Rain Toolbox - Previsão de Chuvaspugpe
 
Criando comunidades bem sucedidas
Criando comunidades bem sucedidasCriando comunidades bem sucedidas
Criando comunidades bem sucedidaspugpe
 
Python Debugger - PUG-PE
Python Debugger - PUG-PE Python Debugger - PUG-PE
Python Debugger - PUG-PE Arthur Alvim
 
(entregando djangoapps)@tangerinalab - pugpe xv
(entregando djangoapps)@tangerinalab - pugpe xv(entregando djangoapps)@tangerinalab - pugpe xv
(entregando djangoapps)@tangerinalab - pugpe xvraonyaraujo
 
Palestra sobre Inteligência Coletiva
Palestra sobre Inteligência ColetivaPalestra sobre Inteligência Coletiva
Palestra sobre Inteligência Coletivapugpe
 
Blender Com Python
Blender Com PythonBlender Com Python
Blender Com Pythonpugpe
 
NoSQL com Cassandra e Python
NoSQL com Cassandra e PythonNoSQL com Cassandra e Python
NoSQL com Cassandra e Pythonpugpe
 
Porque foi bom aprender com Python
Porque foi bom aprender com PythonPorque foi bom aprender com Python
Porque foi bom aprender com PythonFernando Rocha
 
Apresentando o I Toró de Palestras do PUG-PE
Apresentando o I Toró de Palestras do PUG-PEApresentando o I Toró de Palestras do PUG-PE
Apresentando o I Toró de Palestras do PUG-PEMarcel Caraciolo
 
Computação Científica com Python
Computação Científica com PythonComputação Científica com Python
Computação Científica com PythonHugo Serrano
 
Python e Cadeias de Markov GHMM
Python e Cadeias de Markov GHMMPython e Cadeias de Markov GHMM
Python e Cadeias de Markov GHMMpugpe
 
Peça seu código em casamento: Votos, Tópicos e TDD
Peça seu código em casamento: Votos, Tópicos e TDDPeça seu código em casamento: Votos, Tópicos e TDD
Peça seu código em casamento: Votos, Tópicos e TDDRafael Carício
 
Python na formacao_de_jovens
Python na formacao_de_jovensPython na formacao_de_jovens
Python na formacao_de_jovensMarcos Egito
 

Destaque (20)

Wikilytics
WikilyticsWikilytics
Wikilytics
 
Migrando do App Engine para o Heroku
Migrando do App Engine para o HerokuMigrando do App Engine para o Heroku
Migrando do App Engine para o Heroku
 
Coding Dojo e Test Driven Development
Coding Dojo e Test Driven DevelopmentCoding Dojo e Test Driven Development
Coding Dojo e Test Driven Development
 
REST APIs com Django
REST APIs com DjangoREST APIs com Django
REST APIs com Django
 
Rain Toolbox - Previsão de Chuvas
Rain Toolbox -  Previsão de ChuvasRain Toolbox -  Previsão de Chuvas
Rain Toolbox - Previsão de Chuvas
 
Pug
PugPug
Pug
 
Criando comunidades bem sucedidas
Criando comunidades bem sucedidasCriando comunidades bem sucedidas
Criando comunidades bem sucedidas
 
Python Debugger - PUG-PE
Python Debugger - PUG-PE Python Debugger - PUG-PE
Python Debugger - PUG-PE
 
(entregando djangoapps)@tangerinalab - pugpe xv
(entregando djangoapps)@tangerinalab - pugpe xv(entregando djangoapps)@tangerinalab - pugpe xv
(entregando djangoapps)@tangerinalab - pugpe xv
 
Palestra sobre Inteligência Coletiva
Palestra sobre Inteligência ColetivaPalestra sobre Inteligência Coletiva
Palestra sobre Inteligência Coletiva
 
Blender Com Python
Blender Com PythonBlender Com Python
Blender Com Python
 
Pep 8
Pep 8Pep 8
Pep 8
 
Arduino e python
Arduino e pythonArduino e python
Arduino e python
 
NoSQL com Cassandra e Python
NoSQL com Cassandra e PythonNoSQL com Cassandra e Python
NoSQL com Cassandra e Python
 
Porque foi bom aprender com Python
Porque foi bom aprender com PythonPorque foi bom aprender com Python
Porque foi bom aprender com Python
 
Apresentando o I Toró de Palestras do PUG-PE
Apresentando o I Toró de Palestras do PUG-PEApresentando o I Toró de Palestras do PUG-PE
Apresentando o I Toró de Palestras do PUG-PE
 
Computação Científica com Python
Computação Científica com PythonComputação Científica com Python
Computação Científica com Python
 
Python e Cadeias de Markov GHMM
Python e Cadeias de Markov GHMMPython e Cadeias de Markov GHMM
Python e Cadeias de Markov GHMM
 
Peça seu código em casamento: Votos, Tópicos e TDD
Peça seu código em casamento: Votos, Tópicos e TDDPeça seu código em casamento: Votos, Tópicos e TDD
Peça seu código em casamento: Votos, Tópicos e TDD
 
Python na formacao_de_jovens
Python na formacao_de_jovensPython na formacao_de_jovens
Python na formacao_de_jovens
 

Semelhante a Clustering com numpy e cython

Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Dr. Volkan OBAN
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPaulo Sergio Lemes Queiroz
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnMasashi Shibata
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and PolynomialAroosa Rajput
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수용 최
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
Python 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxTseChris
 
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...Provectus
 

Semelhante a Clustering com numpy e cython (20)

Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Advanced Concepts in Python
Advanced Concepts in PythonAdvanced Concepts in Python
Advanced Concepts in Python
 
SCIPY-SYMPY.pdf
SCIPY-SYMPY.pdfSCIPY-SYMPY.pdf
SCIPY-SYMPY.pdf
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Python modulesfinal
Python modulesfinalPython modulesfinal
Python modulesfinal
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPU
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Python 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptx
 
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...
Федор Поляков (Looksery) “Face Tracking на мобильных устройствах в режиме реа...
 

Clustering com numpy e cython

  • 1. Clustering com numpy e Cython Anderson Berg XIII Encontro do PUG-PE
  • 5.
  • 6.
  • 7. Exemplo simples >>> from numpy import * >>>a = arange(10).reshape(2,5) >>>a array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
  • 8. Criação de arrays >>> a = array( [2,3,4] ) >>> b = array( [ (1.5,2,3), (4,5,6) ] ) >>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> zeros( (3,4) ) array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) >>> ones( (2,3,4), dtype=int16 ) array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16)
  • 9. Outras Operações >>> b = arange( 4 ) >>> b**2 array([0, 1, 4, 9]) >>> a = random.random((2,3)) >>> a array([[ 0.6903007 , 0.39168346, 0.16524769], [ 0.48819875, 0.77188505, 0.94792155]])
  • 10. Ainda outras operações >>> a.sum() 3.4552372100521485 >>> a.min() 0.16524768654743593 >>> a.max() 0.9479215542670073 >>> a[:,1] array([0.39168346, 0.77188505]) >>> sum(a[:,1]) 1.1635685099999999
  • 11.
  • 12. Primeiros Passos helloworld.pyx: print "Hello World" setup.py: from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("helloworld", ["helloworld.pyx"])] ) $ python setup.py build_ext --inplace >>> import helloworld Hello World
  • 13. Advanced Mode def primes(int kmax): cdef int n, k, i cdef int p[1000] result = [] if kmax > 1000: kmax = 1000 k = 0 n = 2 while k < kmax: i = 0 while i < k and n % p[i] != 0: i = i + 1 if i == k: p[k] = n k = k + 1 result.append(n) n = n + 1 return result
  • 14. Sem Cython Com Cython
  • 15. Clustering com numpy e Cython Anderson Berg - @berg_pe XIII Encontro do PUG-PE