NumPy
Fundamentos para Análisis de Datos en NumPy y Pandas
Dimensiones en NumPy y Pandas: De Escalares a Tensors
Arrays en NumPy
Introducción al álgebra lineal con NumPy
Indexación y Slicing
Broadcasting y Operaciones Lógicas en NumPy
Elementos Únicos y sus Conteos: Copias y Vistas
Transformación de Arrays: Reshape y Manipulación
Caso Práctico de Análisis de Datos
Cálculos Matriciales en NumPy
Ejercicios en NumPy
Pandas
Pandas para Manipulación de Datos
Creación de Dataframes en Pandas
Estructuras de Datos en Pandas y Funciones
Uso de iloc y loc en Pandas
Manejo de Datos Faltantes en Pandas
Creación y Manipulación de Columnas en Pandas
Agrupaciones con groupby
Filtrado de datos con condiciones en Pandas
Reestructuración de datos: Pivot y Reshape en Pandas
Fusión de DataFrames en Pandas
Manejo de Series Temporales en Pandas
Matplotlib
Introducción a Matplotlib gráfico de líneas y dispersión
Personalización de Gráficos en Matplotlib
Gráficos de Barras y Diagramas de Pastel
Gráficos de Histograma y Boxplot para distribuciones
Series de tiempo y manejo de fechas con Matplotlib
Subplots y Layouts Avanzados
Proyecto de Análisis de Datos de Retail
Caso de Estudio (Parte I). Limpieza de datos
Caso de Estudio (Parte II). Creación de columnas
Caso de Estudio (Parte III). Graficación y análisis de resultados
Proyecto Final: Creación de Portafolio de Análisis de Datos
You don't have access to this class
Keep learning! Join and start boosting your career
Broadcasting is a powerful feature in NumPy that allows you to perform arithmetic operations on arrays of different sizes and shapes efficiently. Instead of iterating over each element of the arrays to perform the operations, NumPy automatically extends smaller arrays to match the dimensions of larger arrays, without duplicating data. This not only optimizes memory usage, but also significantly speeds up operations.
Broadcasting allows operations between arrays of different dimensions to be performed as if all arrays had the same shape. NumPy extends smaller arrays to the shape of the larger one implicitly, facilitating operations without the need to copy data.
Discounting Application: Suppose we have an array representing the prices of several products and another array with a percentage discount that applies to all products. With broadcasting, we can apply the discount without the need for an explicit loop.
import numpy as np prices = np.array([100, 200, 300]) discount = np.array([0.9]) discounted_prices = prices * discount print(discounted_prices) # Output: [ 90. 180. 270.]
Operations with Multidimensional Arrays: We can perform elementary operations between arrays of different dimensions.
a = np.array([[[0.0, 0.0, 0.0, 0.0], [10.0, 10.0, 10.0, 10.0], [20.0, 20.0, 20 . 0], [30.0, 30.0, 30.0]]]) b = np.array([1.0, 2.0, 2.0, 3.0]) result = a + b print(result)# Output:# [[ 1.2. 3.]# [11. 12. 13.]# [21. 22. 23.]# [31. 32. 33. 33.]]]
Broadcasting is crucial because it allows writing more concise and readable code, avoiding explicit loops and taking advantage of NumPy's internal optimizations to perform operations quickly and efficiently. This is especially useful in data analysis and machine learning, where large volumes of data are handled and fast computations are required.
For broadcasting to work, array dimensions must meet certain rules:
Examples:
Scalar and 1D Array:
a = np.array([1, 2, 3]) b = 2result = a * b print(result) # Output: [2 4 6].
1D Array and 2D Array:
a = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) b = np.array([1, 0, 1]) result = a * b print(result)# Output:# [[1 0 3]# [4 0 6]# [7 0 9]]
2D Array and 3D Array:
a = np.array([[[[1], [2], [3]], [[4], [5], [6]]]]) b = np.array([1, 2, 3]) result = a * b print(result)# Output:# [[[ 1 23]# [ 2 46]# [ 3 69]]# #[[ 4 8 12]# [ 5 10 15]# [ 6 12 18]]]]
Broadcasting in NumPy is an essential technique for performing arithmetic operations efficiently on arrays of different sizes and shapes. Understanding and applying broadcasting rules allows you to write cleaner and more optimized code, crucial for data analysis and machine learning tasks.
Explore these features and discover how broadcasting can improve the efficiency of your Python calculations.
Contributions 22
Questions 3
Want to see more contributions, questions and answers from the community?