Cambie la forma y aplane arrays
Cambie las dimensiones sin perder datos.
Cambie la forma y aplane arrays es una lección gratuita de Data Science Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Data Science Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Data Science Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Same Data, New Shape
An array is just numbers plus a shape. Reshaping rearranges those numbers into new dimensions without copying or changing a single value.
Calling reshape
Use reshape to lay 12 numbers out as a 3 by 4 grid. The total count must stay the same, or NumPy raises an error.
import numpy as np
a = np.arange(12)
b = a.reshape(3, 4)Rows Times Columns
The new shape only works if the dimensions multiply to the original size. Here 3 times 4 equals 12, so the reshape fits perfectly.
Let NumPy Do the Math
Pass -1 for one dimension and NumPy computes it for you. It is a handy shortcut when you only know one side of the grid.
a = np.arange(12)
b = a.reshape(3, -1) # becomes 3 x 4Going to 1D With ravel
Need a flat line of numbers again? ravel collapses any array into a single dimension, reading values row by row.
grid = np.arange(6).reshape(2, 3)
flat = grid.ravel() # [0 1 2 3 4 5]flatten Makes a Copy
The flatten method also returns 1D, but always as a fresh copy. Editing the result never touches the original array.
flat = grid.flatten()
flat[0] = 99 # grid is untouchedravel May Share Memory
Unlike flatten, ravel often returns a view that shares memory with the source. Change the view and the original can change too.
Add a Dimension
A 1D array can grow into a column with reshape. Turning shape (3,) into (3, 1) is common before stacking or matrix math.
v = np.array([1, 2, 3])
col = v.reshape(3, 1)Transpose Flips Axes
The .T attribute swaps rows and columns. A 2 by 3 array instantly becomes 3 by 2, mirroring the data across its diagonal.
grid = np.arange(6).reshape(2, 3)
turned = grid.T # shape 3 x 2Shape Must Stay Consistent
You cannot reshape 10 numbers into a 3 by 4 grid. The element count on both sides must match, so plan dimensions before you reshape.
Why Reshaping Matters
Models and plotting tools expect specific shapes. Reshaping lets one block of numbers serve a vector, a matrix, or an image as needed.
Quick Check
You want a guaranteed independent 1D copy of an array.
Reshape Recap
You reshaped data while keeping its values, used -1 for auto sizing, and learned that flatten copies while ravel may share memory. 🎯
Preguntas frecuentes
¿La lección «Cambie la forma y aplane arrays» es gratis?
Sí — el texto completo de «Cambie la forma y aplane arrays» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Data Science Academy, actualiza a CoddyKit PRO. El curso de Data Science Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Cambie la forma y aplane arrays»?
Cambie las dimensiones sin perder datos. Practicas Data Science Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Data Science Academy?
No se requiere experiencia previa. Data Science Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Cambie la forma y aplane arrays»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Data Science Academy?
Sí. Cada lección de Data Science Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Cambie la forma y aplane arrays
- Suma, media y el truco de axis
- Máscaras booleanas para seleccionar
- Números aleatorios y semillas