Shape, size e dtype
Legga l'anatomia di un array.
Shape, size e dtype è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Read the Array's Anatomy
Every array carries metadata about itself. Three labels tell you almost everything: its shape, its size, and its dtype. Let's read each one. 🔍
Shape: Rows and Columns
The shape is a tuple of dimension lengths. A 3-by-4 grid reports (3, 4): three rows down, four columns across.
a = np.zeros((3, 4))
a.shape # (3, 4)Shape Tells You the Layout
Shape is your first sanity check. If a model expects (100, 5) and you hand it (5, 100), the shape mismatch is where bugs hide.
ndim: How Many Dimensions
The ndim attribute counts dimensions. A flat row is 1, a table is 2, a stack of tables is 3. It is just the length of the shape.
a.ndim # 2Size: Total Element Count
The size is the total number of elements, the product of every shape value. A (3, 4) array has a size of 12.
a.size # 12dtype: The One Shared Type
The dtype names the single type all elements share, like int64 or float64. It controls both precision and memory use.
np.array([1, 2, 3]).dtype # int64Choose Your dtype on Purpose
You can set the type yourself with the dtype argument. Use a smaller type like int32 to save memory on huge arrays.
np.array([1, 2, 3], dtype='int32')dtype Affects Precision
float32 holds fewer decimals than float64. Tiny rounding gaps add up, so pick the type your numbers actually need.
Convert With astype
Already have an array? astype returns a copy in a new type, handy for turning floats into clean integers.
f = np.array([1.0, 2.0])
f.astype('int64')itemsize: Bytes Per Element
The itemsize tells you bytes per element. Multiply it by size to estimate the array's total memory footprint.
a.itemsize # 8 for float64Inspect Before You Compute
Checking shape and dtype first saves hours. They reveal mismatched data and wrong types before a calculation quietly goes wrong. ✅
Quick Check
Let's test your read of an array's shape.
Recap
You can now read an array at a glance: shape for layout, ndim and size for counts, and dtype for the shared type. These four checks catch most bugs early. 🎉
Domande Frequenti
La lezione «Shape, size e dtype» è gratuita?
Sì — il testo completo di «Shape, size e dtype» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.
Cosa imparerò in «Shape, size e dtype»?
Legga l'anatomia di un array. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Data Science Academy?
Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Shape, size e dtype»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Data Science Academy?
Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Da una lista Python a un ndarray
- Shape, size e dtype
- Calcolo vettorializzato senza cicli
- Indicizzazione e slicing degli array