# Exercise: Know Your Listings

Write three functions that split Riverside's 300 apartment listings into features and a label, sum up a column's mean, median and standard deviation, and rank the features by how strongly each moves with the rent, and let pytest check them.

Apprentice · AI Basics · Python · Updated November 19, 2026

By Gregor Dzierzon

Before anyone builds a rent model for Riverside, someone has to know what's in its 300 listings: which column is the answer, what a typical rent is and how far rents stray from it, and which columns move with the rent at all. You'll write the three functions that answer those questions, the first look you'll take at every dataset from here on. Tests call each function and check what it gives back, and a notebook with a histogram and a scatter plot comes with the exercise for looking at the data while you work.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-ml-know-your-listings](https://github.com/ask-codey/exercise-ml-know-your-listings), also linked from the **Exercise** box beside the lesson. Clone it into your `python-course` folder. In VS Code's terminal:

```bash
cd ~/python-course
git clone https://github.com/ask-codey/exercise-ml-know-your-listings.git
```

Git makes a folder, `exercise-ml-know-your-listings`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and make the exercise's virtual environment. On Windows (PowerShell):

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

On a Mac (zsh):

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

The prompt starts with `(.venv)` once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

- `listings.csv`: the 300 listings, our own made-up data, the same file the chapter's lessons use.
- `seed_data.py`: the script that made the CSV, with the pattern it planted. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.
- `listings_stats.py`: the three functions, for you to finish, with `ID_COLUMN` (`listing_id`, the column that only names a row) and `LABEL` (`monthly_rent`), ready to use.
- `explore.ipynb`: a notebook for looking at the listings, with a histogram of the rents and a scatter plot of size against rent, and for trying your code. Choose the `.venv` kernel with **Select Kernel** (top right) before you run it. The tests don't read it.
- `report.py`: describes the listings with your three functions and prints what each gave. Ready to use.
- `tests/`: the tests, which call each function and check what comes back, and `conftest.py`, which gives each test a fresh copy of the listings. You don't need to change them.
- `requirements.txt` and `pytest.ini`: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

## What to do

Every function in `listings_stats.py` is already declared with its name and parameters, and for now it stops with a `NotImplementedError` that says *Not built yet.* The docstring under each one says what it should do, with examples:

```python
def center_and_spread(listings, column):
    """Return a column's middle and spread as a dictionary, each value rounded to 2 decimal places.

    The keys are "mean", "median" and "std" (the standard deviation, dividing
    by one less than the count, as pandas' std() and statistics.stdev do).
    Give back plain Python numbers: float() turns pandas' answer into one.

    With listings.csv:
    center_and_spread(listings, "monthly_rent") -> {"mean": 1373.97, "median": 1345.0, "std": 356.87}
    For a column holding 1100, 900, 1900, 1000 and 1100:
    -> {"mean": 1200.0, "median": 1100.0, "std": 400.0}
    """
    raise NotImplementedError("Not built yet.")
```

In each function, replace the `raise NotImplementedError("Not built yet.")` line with your own code:

1. **`features_and_label(listings, label)`** returns `X` and `y`: `X` is every column except `ID_COLUMN` and the label, in the table's order, and `y` is the label column. With `"monthly_rent"`, `X` has the six columns from `size_sqft` to `floor`. With `"bedrooms"`, the rent becomes one of the features. The table it's given stays as it was.
2. **`center_and_spread(listings, column)`** returns a dictionary of the column's `"mean"`, `"median"` and `"std"`, each rounded to 2 decimal places, as plain Python numbers: `{"mean": 1373.97, "median": 1345.0, "std": 356.87}` for the rent.
3. **`ranked_by_correlation(listings, label)`** returns the feature names (the columns `features_and_label` gives) from the strongest correlation with the label to the weakest, by its size with the sign ignored: a feature at −0.9 comes before one at 0.5.

`ranked_by_correlation` can call `features_and_label`, so the ID and the label are left out in one place.

## Check your work

Run the tests from the exercise folder, with the virtual environment on:

```bash
python -m pytest
```

At first all 13 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example *center_and_spread(a table of 1250 and 1350, 'monthly_rent') should return {'mean': 1300.0, 'median': 1300.0, 'std': 70.71}, but it returned {'mean': 1300.0, 'median': 1300.0, 'std': 50.0}.* (That one divided by the count instead of one less than it.)

To see your functions at work on the whole file, run `python report.py`.

## Hints

- **Look before you write.** Run the notebook's first cells: `listings.head(10)`, `listings.describe()`, the histogram and the scatter plot.
- **A new table, not a changed one.** `drop(columns=[...])` gives back a table without those columns and leaves `listings` alone, as *Features and Labels* shows.
- **One column, three numbers.** `listings[column]` is one column, and its `mean()`, `median()` and `std()` are the numbers *The Middle and the Spread* explains. `round(float(...), 2)` turns each into a plain number with two decimal places.
- **Every feature against the label.** `X.corrwith(y)` gives each column of `X` its correlation with `y`, the r of *How Two Columns Move Together*. `.abs()` drops the signs, `.sort_values(ascending=False)` puts the biggest first, and `list(... .index)` gives the names in that order.

## When you're done

Every test is green: pytest's last line says `13 passed`. And `python report.py` prints:

```text
1) listings: 300
2) features: ['size_sqft', 'bedrooms', 'bathrooms', 'minutes_to_downtown', 'has_parking', 'floor']
3) label: monthly_rent
4) rent, mean: 1373.97
5) rent, median: 1345.0
6) rent, standard deviation: 356.87
7) size, mean and standard deviation: 855.8 225.53
8) features, strongest correlation with the rent first: ['size_sqft', 'bedrooms', 'bathrooms', 'minutes_to_downtown', 'has_parking', 'floor']
```

Now compare line 8 with what `seed_data.py` planted: rent rises $1.50 a square foot, falls $10 a minute from downtown, and gains $120 with parking, while bedrooms, bathrooms and the floor add nothing. Why do bedrooms and bathrooms rank above minutes and parking? In the notebook, work out the correlation of `size_sqft` with `bedrooms`: what does it tell you?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-ml-know-your-listings`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-ml-know-your-listings.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-ml-know-your-listings`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

Features and a label, a middle and a spread, and a ranking of what moves with the label: three small functions that make the first look at any dataset quick, repeatable and checked.

## Key terms

- [Feature](https://www.ask-codey.com/glossary/feature)
- [Label](https://www.ask-codey.com/glossary/label-ai)
- [Mean](https://www.ask-codey.com/glossary/mean)
- [Median](https://www.ask-codey.com/glossary/median)
- [Standard Deviation](https://www.ask-codey.com/glossary/standard-deviation)
- [Correlation](https://www.ask-codey.com/glossary/correlation)
- [pytest](https://www.ask-codey.com/glossary/pytest)

## Related

- [Features and Labels](https://www.ask-codey.com/lessons/ml-features-and-labels)
- [The Middle and the Spread](https://www.ask-codey.com/lessons/ml-stats-center-and-spread)
- [How Two Columns Move Together](https://www.ask-codey.com/lessons/ml-stats-relationships)

Source: https://www.ask-codey.com/lessons/exercise-ml-know-your-listings
