21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

198 Processamento numérico

print y

# [[ 1. 1. 1.]

# [ 1. 1. 1.]]

print 'Arranjos podem gerar novos arranjos:'

# numpy.round() é uma função do numpy

# semelhante ao builtin round(), porém aceita

# arranjos como parâmetro

cos = numpy.round(numpy.cos(z), 1)

print cos

# [ 1. 0.9 0.5 0.1 -0.4 -0.8 -1. -0.9 -0.7]

print 'Multiplicando cada elemento por um escalar:'

print 5 * z

# [ 0. 2.5 5. 7.5 10. 12.5 15. 17.5 20. ]

print 'Somando arranjos elemento por elemento:'

print z + cos

# [ 1. 1.4 1.5 1.6 1.6 1.7 2. 2.6 3.3]

print 'Redimensionando o arranjo:'

z.shape = 3, 3

print z

# [[ 0. 0.5 1. ]

# [ 1.5 2. 2.5]

# [ 3. 3.5 4. ]]

print 'Arranjo transposto:'

print z.transpose()

# [[ 0. 1.5 3. ]

# [ 0.5 2. 3.5]

# [ 1. 2.5 4. ]]

print '"Achata" o arranjo:'

print z.flatten()

# [ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. ]

print 'O acesso aos elementos funciona como nas listas:'

print z[1]

# [ 1.5 2. 2.5]

print 'Caso especial, diferente da lista:'

print z[1, 1]

# 2.0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!