Garimpagem de Dados

Aula 2 - NumPy

06/10/2017


In [ ]:
import numpy as np

In [ ]:
a = np.array([1, 2, 3])
print(a)

print('')

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

In [ ]:
a + b

In [ ]:
a * b

In [ ]:
b - a

In [ ]:
a**2

In [ ]:
10*np.sin(a) # seno trigonométrico

In [ ]:
b<35

In [ ]:
print('Axis 1: %s' % b[0])
print(np.average(b))
print(b.sum())
print(b.min())
print(b.max())

In [ ]:
b.sum(axis=0) # sum of each column

In [ ]:
b.min(axis=1) # min of each row

In [ ]:
b.cumsum(axis=1) # cumulative sum along each row

In [ ]:
a = np.zeros((3, 5))
print(a)

In [ ]:
np.ones((2,3,4), dtype=np.int16) # dtype can also be specified

In [ ]:
np.empty([2,3]) # uninitialized, output may vary

In [ ]:
c = np.full(5, 2, dtype=np.float)
print(c)

d = np.full((2, 2), 10)
print(d)

In [ ]:
np.arange(10, 30, 5)

In [ ]:
np.arange(0, 2, 0.3) # it accepts float arguments

In [ ]:
c = np.random.rand(2, 3)
print(c)

In [ ]:
np.transpose(c)

In [ ]:
d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [ ]:
print(d)

In [ ]:
d[:, 0]

In [ ]:
d[:, 1]

In [ ]:
d[:, 0:2]

In [ ]:
d[:, 2]

In [ ]:
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # start:stop:step

In [ ]:
np.save('/tmp/x.npy', x)
del(x)
x = np.load('/tmp/x.npy')
print(x)

In [ ]:
# Usar memory mapping quando o conteúdo não couber em memória RAM
x_mm = np.memmap('x_mm.npy', dtype='float32', mode='w+', shape=(1000, 100))
x_mm[999][99] = 12.5
print(x_mm[999][99])

# Delete the memmap instance to close the file (the mmap file is not deleted). 
# Deletion flushes memory changes to disk before removing the object.
del(x_mm)

# Load
x_mm = np.memmap('x_mm.npy', dtype='float32', mode='r', shape=(1000, 100))
print(x_mm[999][99])

Exercícios

  1. Como calcular o tamanho da memória usada de qualquer array?
  2. Crie a matriz 3x3 com valores em um range de 0 a 8
  3. Crie um vetor randômico de tamanho 30 e calcule a média

In [ ]:
#1
t = np.zeros((10, 10))
print('%d bytes' % (t.size * t.itemsize))

In [ ]:
#2
f = np.arange(9).reshape(3,3)
print(f)

In [ ]:
#3
z = np.random.random(30)

In [ ]:
print(z)
print(z.mean())

100 exercícios de NumPy

https://github.com/rougier/numpy-100


In [ ]: