Clustering com numpy e cython
-
Upload
anderson-dantas -
Category
Documents
-
view
1.796 -
download
1
description
Transcript of Clustering com numpy e cython
![Page 1: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/1.jpg)
Clustering com numpy e Cython
Anderson Berg
XIII Encontro do PUG-PE
![Page 2: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/2.jpg)
Agrupamento de dados
![Page 3: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/3.jpg)
K-Means
![Page 4: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/4.jpg)
Matrizes
![Page 5: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/5.jpg)
![Page 6: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/6.jpg)
![Page 7: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/7.jpg)
>>> from numpy import *
>>>a = arange(10).reshape(2,5)
>>>a array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
Exemplo simples
![Page 8: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/8.jpg)
>>> 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)
Criação de arrays
![Page 9: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/9.jpg)
>>> 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]])
Outras Operações
![Page 10: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/10.jpg)
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
![Page 11: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/11.jpg)
![Page 12: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/12.jpg)
print "Hello World"
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
Primeiros Passos
>>> import helloworld Hello World
helloworld.pyx:
setup.py:
![Page 13: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/13.jpg)
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
Advanced Mode
![Page 14: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/14.jpg)
Sem Cython Com Cython
![Page 15: Clustering com numpy e cython](https://reader033.fdocuments.net/reader033/viewer/2022051109/547bb9f15806b5f93f8b467f/html5/thumbnails/15.jpg)
Clustering com numpy e Cython
Anderson Berg - @berg_pe
XIII Encontro do PUG-PE