Somma, media e il trucco degli assi
Agregghi lungo le righe o attraverso le colonne.
Somma, media e il trucco degli assi è 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.
Summaries in One Call
NumPy can collapse a whole array into a single number. Methods like sum and mean turn many values into one quick summary.
a = np.array([2, 4, 6])
total = a.sum() # 12Mean Is the Average
The mean divides the total by the count. For the array above it returns 4.0, the everyday average you already know.
avg = a.mean() # 4.0The Whole Array by Default
With no extra argument, these methods reduce everything to one scalar. Every element joins the calculation, ignoring rows and columns.
m = np.arange(6).reshape(2, 3)
m.sum() # 15Meet the axis Argument
The real power is axis. It tells NumPy which direction to collapse, so you can summarize per row or per column instead.
axis=0 Goes Down
Set axis=0 to collapse the rows. NumPy walks down each column and returns one value per column.
m.sum(axis=0) # [3 5 7]axis=1 Goes Across
Set axis=1 to collapse the columns. NumPy sweeps across each row and returns one value per row.
m.sum(axis=1) # [3 12]A Memory Trick
Think of axis as the dimension that disappears. Collapsing axis 0 removes the row dimension, leaving a result shaped like one row.
Same Idea for mean
The axis rule is identical for mean, min, max, and more. Learn it once and it applies across every reduction NumPy offers.
m.mean(axis=0) # [1.5 2.5 3.5]Keep the Dimensions
Pass keepdims=True to keep the collapsed axis as size 1. This makes the result broadcast cleanly back against the original array.
m.sum(axis=1, keepdims=True) # shape 2 x 1Counting Along an Axis
Reductions pair with shape to answer real questions: total per category, average per day, or a count of items down each column.
Pick the Axis on Purpose
Choosing the wrong axis is a classic bug. Always ask which dimension you want to keep, then collapse the other one.
Quick Check
You have a 2D array of rows and columns.
Axis Recap
You summarized whole arrays, then steered axis to summarize per row or per column. Remember: the axis you name is the one that vanishes. 👍
Domande Frequenti
La lezione «Somma, media e il trucco degli assi» è gratuita?
Sì — il testo completo di «Somma, media e il trucco degli assi» è 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 «Somma, media e il trucco degli assi»?
Agregghi lungo le righe o attraverso le colonne. 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 «Somma, media e il trucco degli assi»?
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
- Ridimensionare e appiattire gli array
- Somma, media e il trucco degli assi
- Maschere booleane per la selezione
- Numeri casuali e seed