Arrays umformen und flatten
Ändern Sie Dimensionen, ohne Daten zu verlieren.
Arrays umformen und flatten ist eine kostenlose Data Science Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Data Science Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Arrays umformen und flatten“ kostenlos?
Ja — der vollständige Text von „Arrays umformen und flatten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Data Science Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Arrays umformen und flatten“?
Ändern Sie Dimensionen, ohne Daten zu verlieren. Du übst Data Science Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Data Science Academy zu starten?
Keine Vorkenntnisse erforderlich. Data Science Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Arrays umformen und flatten“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Data Science Academy-Lektion Code schreiben und ausführen?
Ja. Jede Data Science Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Arrays umformen und flatten
- Summe, Mittelwert und der Axis-Trick
- Boolesche Masken zur Auswahl
- Zufallszahlen und Seeds