Sub topic 2
Loading the California housing data
This chapter loads the dataset and checks that it matches what the project expects. It assumes you know basic Python and pandas (DataFrames, selecting columns, describe). It also assumes you have numpy, pandas, matplotlib and scikit-learn installed, and that you know what a feature and a target are.
What the dataset contains
The California housing dataset comes from the 1990 US census. Each row describes one census block group, which is a small area of a few hundred to a few thousand people. The target is the median house value in that block group. Eight numeric columns describe the area.
| Column | Meaning |
|---|---|
| MedInc | Median income in the block group, in tens of thousands of dollars |
| HouseAge | Median age of houses, in years |
| AveRooms | Average number of rooms per household |
| AveBedrms | Average number of bedrooms per household |
| Population | People living in the block group |
| AveOccup | Average number of household members |
| Latitude | Latitude of the block group |
| Longitude | Longitude of the block group |
| MedHouseVal | Target: median house value, in units of $100,000 |
A MedHouseVal of 2.5 therefore means $250,000. Keep this unit in mind. Every error metric you compute later is in these same units.
Fetching the data
scikit-learn ships a loader for this dataset. Passing as_frame=True returns pandas objects instead of plain numpy arrays.
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(df.shape)
print(df.head())
print(df.describe())The first call downloads the data and caches it on disk, by default in a scikit_learn_data folder in your home directory. Later calls read from that cache, so they work offline.
Reading the output
df.shape should print (20640, 9). That is 20,640 rows and 9 columns: eight features and the target. df.head() shows the first five rows, and df.describe() gives the count, mean, spread and quartiles of each column.
The frame attribute holds the features and the target together, with the target as the last column. The same object also exposes housing.data (features only) and housing.target (the target only). This project uses the combined frame so that you can explore everything in one place. You will separate features from target when you split the data.
Quick checks before moving on
Run a few checks now. They cost almost nothing and catch problems early.
print(df.isna().sum().sum())
print(df.dtypes.unique())
print(df["MedHouseVal"].max())The first line counts missing values across the whole frame, and it should print 0. The second should show only float64. The third prints the largest target value, which is 5.00001. Many block groups share this maximum because the original census data capped house values at $500,000. You will see this ceiling again when you look at errors.
Common errors
- Network failure on the first call. The error comes from the download, not from your code. Retry on a working connection.
- Getting arrays instead of a DataFrame. Without
as_frame=True, the result has no.frameattribute. Add the argument. - Misreading the units. Treating MedHouseVal as dollars makes every error look tiny. Multiply by 100,000 when you report results.
Exercise
After loading the data, you want features and target as separate objects without using df.drop. Which two attributes of housing give them, and what is the unit of the target?
Show answer
housing.data holds the eight feature columns and housing.target holds MedHouseVal. The target is in units of $100,000, so a value of 3.2 means $320,000.