Arrays, tables and data shapes
Arrays, tables and data shapes
A school spreadsheet has rows for students and columns for measurements. A pandas DataFrame offers the same basic organization inside Python, while a NumPy array stores numerical values in a shape suitable for calculations. Keeping track of the shape prevents many confusing model errors.
For most scikit-learn tabular estimators, the inputs form a two-dimensional table: observations down the rows and features across the columns. A single target for each observation is usually a one-dimensional sequence. We conventionally call these X and y; their names are conventions, not special Python keywords.
One feature still needs a column dimension
Selecting students[["hours"]] keeps a DataFrame with one column. The double brackets matter: the outer brackets select from the table, and the inner list identifies the columns to keep. Converting it with to_numpy() produces shape (3, 1).
Selecting students["score"] gives a Series, a one-dimensional column of values. Its array has shape (3,). Each row in X must correspond to the same student's target in y. An equal row count is necessary, but it does not prove the rows still match after separate sorting or filtering.
What you’ll learn
- Read observation and feature dimensions.
- Select a two-dimensional input table and one-dimensional target.
- Distinguish row labels from row positions.
One feature still needs a column dimension
DataFrame
Named columns organize three student observations.
hours | scoreInput X
Keep the feature as a column rather than a flat list.
shape (3, 1)Target y
One score corresponds to every input row.
shape (3,)
Inspect a three-student table
Prepare and run
Use Python in the course environment. The input values are included in this program; no separate data file is needed.
Python · complete runnable example
import numpy as np
import pandas as pd
students = pd.DataFrame({"hours": [1, 2, 3], "score": [50, 60, 70]})
X = students[["hours"]].to_numpy()
y = students["score"].to_numpy()
print("X shape:", X.shape)
print("y shape:", y.shape)
print("Average score:", float(np.mean(y)))Expected output
X shape: (3, 1)
y shape: (3,)
Average score: 60.0Follow the result
The table contains three study-hour values and three scores. X has three rows and one feature; y has three target values. NumPy averages the target scores to 60. This summary tells us about the supplied observations, not the average for every student in the school.
Add another feature column
Your task: Add a practice-attempts column. If all three students remain, what is the input shape?
Python · code for this task
import numpy as np
import pandas as pd
students = pd.DataFrame({"hours": [1, 2, 3], "score": [50, 60, 70]})
X = students[["hours"]].to_numpy()
y = students["score"].to_numpy()
print("X shape:", X.shape)
print("y shape:", y.shape)
print("Average score:", float(np.mean(y)))Worked answer
It becomes (3, 2) when you select both feature columns with students[["hours", "practice_attempts"]]. The target still has shape (3,). Adding a feature changes the number of columns, not the number of students.
Indexes are labels, not always positions
A DataFrame index identifies rows. After filtering, the labels may skip numbers. loc selects labels, while iloc selects integer positions. Check which one you need rather than assuming the first row must be labelled zero.
Columns can contain different types, including strings, numbers and missing values. A numeric array is not automatically clean or meaningful. Inspect types and units before feeding a table into an algorithm; a student identifier is a number but usually not a meaningful quantity for distance calculations.
Check your understanding
One feature still needs a column dimension: what to remember
Keep observations down the rows and features across the columns. Preserve the alignment between X and y, and distinguish labels from positions when selecting from a DataFrame.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.