Feature and label notation
- Machine Learning course
- Beginner
- Lesson 5/112
- 5 min
In ML, features are typically represented as matrix X and labels as vector y.
Feature matrix X
By convention inputs are written as capital X and labels as lowercase y. X is two-dimensional (examples × features), y is one-dimensional (one label per example).
import numpy as np
X = np.array([[170, 65], [160, 55], [180, 80]]) # height, weight
y = np.array([1, 0, 1])
print(X.ndim, y.ndim) # 2 1The capital letter for a matrix and the lowercase for a vector come from mathematical notation.
reshape(-1, 1)
The way to turn single-feature data into a column is reshape(-1, 1). -1 means "work out this dimension yourself":
x = np.array([10, 20, 30])
X = x.reshape(-1, 1)
print(X.shape) # (3, 1)The labels should stay one-dimensional; turning y into a column causes a warning in some models.
Practice Turn into a column
Turn the array x into X with reshape(-1, 1); print X.shape and X.ndim separated by a space.
Starter code
import numpy as np
x = np.array([4, 8, 15, 16, 23, 42])
# buraya yazSample solution
import numpy as np
x = np.array([4, 8, 15, 16, 23, 42])
X = x.reshape(-1, 1)
print(X.shape, X.ndim)Expected output
(6, 1) 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why can't <code>X = np.array([10, 20, 30])</code> be given to scikit-learn directly?
- Three examples are too few
- NumPy arrays aren't accepted
- It is one-dimensional; X must be two-dimensional Correct answer
- The numbers are integers
Explanation: It must be converted to a single-column two-dimensional array with reshape(-1, 1).
2 Turn the one-dimensional array into a single-column matrix.
X = x.reshape(____)
X = x.reshape(-1, 1)
Explanation: reshape(-1, 1) works out the row count automatically and produces a single column.
3 Match the notations.
| Term | Meaning |
|---|---|
X |
Feature matrix |
y |
Label vector |
ndim |
Number of dimensions |
reshape(-1, 1) |
Turning into one column |