Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Anello92 authored Mar 26, 2021
1 parent daee7a3 commit 658c300
Showing 1 changed file with 342 additions and 0 deletions.
342 changes: 342 additions & 0 deletions Vetorizacao-Funcoes-Operacoes-Arrays-NumPy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vetorização"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Embora possamos usar list comprehension e função map em arrays numpy, este pode não ser o melhor método e a maneira mais eficiente de se obter o mesmo resultado seria através de vetorização. \n",
"\n",
"Vetorização nos permite aplicar uma função a um array inteiro, ao invés de aplicar a função elemento a elemento (similar ao que fazemos com as funções map() e filter())."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ao trabalhar com objetos NumPy e Pandas, existem maneiras mais eficientes de se aplicar uma função a um conjunto de elementos, que serão mais velozes que a aplicação de loops for."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"array1 = np.random.randint(0, 50, 20)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([22, 16, 8, 44, 14, 6, 8, 17, 19, 45, 41, 35, 0, 4, 27, 34, 40,\n",
" 28, 9, 1])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Criando um função\n",
"def calc_func(num):\n",
" if num < 10:\n",
" return num ** 3\n",
" else:\n",
" return num ** 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-76346cc62124>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Para que a função funcione no objeto array do NumPy, ela precisa ser vetorizada\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcalc_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-4-6cc727382582>\u001b[0m in \u001b[0;36mcalc_func\u001b[0;34m(num)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Criando um função\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalc_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
]
}
],
"source": [
"# Para que a função funcione no objeto array do NumPy, ela precisa ser vetorizada\n",
"calc_func(array1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"?np.vectorize"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Vetorizando a função\n",
"v_calc_func = np.vectorize(calc_func)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.vectorize"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type (v_calc_func)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 484, 256, 512, 1936, 196, 216, 512, 289, 361, 2025, 1681,\n",
" 1225, 0, 64, 729, 1156, 1600, 784, 729, 1])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Aplicando a função vetorizada ao array3 NumPy\n",
"v_calc_func(array1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[484,\n",
" 256,\n",
" 512,\n",
" 1936,\n",
" 196,\n",
" 216,\n",
" 512,\n",
" 289,\n",
" 361,\n",
" 2025,\n",
" 1681,\n",
" 1225,\n",
" 0,\n",
" 64,\n",
" 729,\n",
" 1156,\n",
" 1600,\n",
" 784,\n",
" 729,\n",
" 1]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Aplicando a função map() sem vetorizar a função\n",
"list(map(calc_func, array1))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[484,\n",
" 256,\n",
" 512,\n",
" 1936,\n",
" 196,\n",
" 216,\n",
" 512,\n",
" 289,\n",
" 361,\n",
" 2025,\n",
" 1681,\n",
" 1225,\n",
" 0,\n",
" 64,\n",
" 729,\n",
" 1156,\n",
" 1600,\n",
" 784,\n",
" 729,\n",
" 1]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Podemos usar list comprehension para obter o mesmo resutado, sem vetorizar a função\n",
"[calc_func(x) for x in array1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"No Python 3, a list comprehension recebeu atualizações e ficou muito mais rápida e eficiente, uma vez que ela é amplamente utilizada em programação Python. Lembre-se sempre de checar a documentação antes de decidir como você irá manipular suas estruturas de dados."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"18.2 µs ± 373 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
"8.89 µs ± 183 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
"8.06 µs ± 100 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"# Função vetorizada\n",
"%timeit v_calc_func(array1)\n",
"\n",
"# List comprehension\n",
"%timeit [calc_func(x) for x in array1]\n",
"\n",
"# Função map()\n",
"%timeit list(map(calc_func, array1))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Criando um array com valores maiores\n",
"array2 = np.random.randint(0, 100, 20 * 10000)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"63 ms ± 1.48 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"76.5 ms ± 344 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"71.7 ms ± 183 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"# Função vetorizada\n",
"%timeit v_calc_func(array2)\n",
"\n",
"# List comprehension\n",
"%timeit [calc_func(x) for x in array2]\n",
"\n",
"# Função map()\n",
"%timeit list(map(calc_func, array2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Utilizar as versões mais recentes de um software pode trazer problemas de compatibilidade com aplicações existentes, mas é grande a possibilidade trazerem melhorias em performance e novas funcionalidades."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}

0 comments on commit 658c300

Please sign in to comment.