Data and modeling
Machine learning X and Y
In machine learning, X is the table of input features and y is the target you want to predict, one row per example. Getting them right means choosing the row unit, the target, and only the columns available at prediction time.
How X and y are used in real machine learning work
The standard input to every model
Libraries expect X as a table with one row per example and one column per feature, and y as the matching target for each row. Getting this shape right is the first step of any supervised project.
Real examplescikit-learn Getting Started guideThe guide explains that fit takes a samples matrix X of shape (n_samples, n_features), with samples as rows and features as columns, and target values y.
Keeping leaked information out of X
Columns that are only known after the outcome, or that encode the outcome indirectly, make a model look far better than it will be in use. Choosing X means asking what is known at the moment of prediction.
Real exampleLeakage and the Reproducibility Crisis in ML-based Science (Kapoor and Narayanan, 2023)The authors show that data leakage is widespread in ML-based science and present a taxonomy of eight types of leakage, from textbook errors to open research problems.
Spotting shortcuts hidden in X
Even when every column looks legitimate, inputs can carry signals about where or how data were collected. A model can learn those shortcuts instead of the real pattern, which shows up when it is tested on data from elsewhere.
Real examplePneumonia detection across hospital systems (Zech et al., PLOS Medicine 2018)The study found that convolutional networks for chest radiographs could identify the hospital system and department, which could confound their pneumonia predictions.
Checking the quality of y
Labels come from people or processes and contain mistakes. Errors in y, especially in the test set, can change which model looks best.
Real examplePervasive Label Errors in Test Sets (Northcutt, Athalye and Mueller, 2021)The authors estimate an average of at least 3.3 percent label errors across the test sets of 10 widely used datasets, including at least 6 percent of the ImageNet validation set.
Can you learn to build X and y for machine learning?
Yes. X and y are the first idea in supervised learning and need only basic Python and pandas. The skill that takes practice is choosing the right row unit, target and columns for a real problem.
- Python basicsLearn lists, dictionaries and functions so table operations make sense.Learn it in Python
- NumPy arrays and pandas tablesSelect columns, drop rows, encode categories and convert a DataFrame into X and y arrays.Learn it in Data Tools
- Framing a supervised problemDecide what one row represents, what the target is, and which columns are available at prediction time.Learn it in Machine Learning
- Splitting and evaluatingSplit X and y into training and test sets, grouped or by time where needed, and compare a model with a baseline.Learn it in Machine Learning
Where to go next after X and y
If you already work in the field
If you know your data well, you already know which columns are recorded after the fact and what the outcome really means. Write down the row unit, target and allowed columns for one problem before any modelling.
If you come from data science or ML
If you are comfortable with models, practise the framing with domain experts: ask how and when each column is recorded and how the label is produced. Most serious errors in X and y are found in those conversations.
Roles that do this workData scientistMachine learning engineerData analystData engineer
Tools you will use
| Tool | What you use it for |
|---|---|
| NumPy | Hold X as a two-dimensional array and y as a one-dimensional array, and check their shapes. |
| pandas | Select feature columns and the target from a DataFrame and keep rows aligned. |
| scikit-learn | Fit any estimator with fit(X, y) and split X and y together for testing. |
Public data to practise on
- Bank Marketing (UCI): Phone-marketing records with client features and whether they subscribed; the documentation flags call duration as a leakage risk.
- Palmer Penguins: Body measurements of three penguin species, a small table for practising X and y in classification or regression.
Your first project
Download the Bank Marketing dataset, build X without the call-duration column (it is only known after the call), predict subscription with logistic regression, and compare it with a majority-class baseline using ROC AUC.
Common questions
Why is X capital and y lowercase?
It follows maths notation: capital letters for matrices and lowercase for vectors. X is a two-dimensional table of features and y is a one-dimensional array of targets.
What is the difference between features and labels?
Features are the inputs in X that the model uses to make a prediction. The label, or target, is the value in y the model is trained to predict.
Does unsupervised learning use y?
No. Unsupervised methods such as clustering and PCA use only X. In scikit-learn they accept a y argument only for API consistency and ignore it.
Should the ID column be in X?
Usually not. An ID carries no general information and can let a model memorise records or pick up ordering artefacts. Keep it aside for joining and grouping.
Courses for these skills
- Data Tools: NumPy, pandas, Matplotlib and scikit-learnPrepare arrays and tables without losing column meaning.23 published lessons · all planned lessons open
- Practical Machine Learning in PythonFit and compare estimators with sound validation.51 published lessons · more lessons in production
- Statistics and Probability for Machine LearningUnderstand base rates, sampling and uncertainty.41 published lessons · all planned lessons open
- Python for AI and Machine LearningWrite functions and inspect exceptions while building a repeatable analysis.33 published lessons · all planned lessons open
New to programming or maths? Start with Python, or follow the machine learning path in order. Published lessons are free to read; compare Free and Pro.