Skip to content

california_housing

CaliforniaHousing

Bases: Dataset

A PyTorch Dataset for regression. A toy data set from scikit-learn. Data Set Characteristics: * Number of Instances: 20640 * Number of Attributes: 8 numeric, predictive attributes and the target * Attribute Information: - MedInc median income in block group - HouseAge median house age in block group - AveRooms average number of rooms per household - AveBedrms average number of bedrooms per household - Population block group population - AveOccup average number of household members - Latitude block group latitude - Longitude block group longitude * Missing Attribute Values: None * Target: The target variable is the median house value for California districts, expressed in hundreds of thousands of dollars ($100,000). This dataset was obtained from the StatLib repository: https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html This dataset was derived from the 1990 U.S. census, using one row per census block group. A block group is the smallest geographical unit for which the U.S. Census Bureau publishes sample data (a block group typically has a population of 600 to 3,000 people). A household is a group of people residing within a home. Since the average number of rooms and bedrooms in this dataset are provided per household, these columns may take surprisingly large values for block groups with few households and many empty houses, such as vacation resorts.

Parameters:

Name Type Description Default
feature_type dtype

The data type of the features. Defaults to torch.float.

float
target_type dtype

The data type of the targets. Defaults to torch.long.

float
train bool

Whether the dataset is for training or not. Defaults to True.

True

Attributes:

Name Type Description
data Tensor

The data features.

targets Tensor

The data targets.

Examples:

>>> from torch.utils.data import DataLoader
    from spotpython.data.california_housing import CaliforniaHousing
    import torch
    dataset = CaliforniaHousing(feature_type=torch.float32, target_type=torch.float32)
    # Set batch size for DataLoader
    batch_size = 5
    # Create DataLoader
    dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
    # Iterate over the data in the DataLoader
    for batch in dataloader:
        inputs, targets = batch
        print(f"Batch Size: {inputs.size(0)}")
        print("---------------")
        print(f"Inputs: {inputs}")
        print(f"Targets: {targets}")
Source code in spotpython/data/california_housing.py
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class CaliforniaHousing(Dataset):
    """
    A PyTorch Dataset for regression. A toy data set from scikit-learn.
    Data Set Characteristics:
    * Number of Instances: 20640
    * Number of Attributes: 8 numeric, predictive attributes and the target
    * Attribute Information:
        - MedInc median income in block group
        - HouseAge median house age in block group
        - AveRooms average number of rooms per household
        - AveBedrms average number of bedrooms per household
        - Population block group population
        - AveOccup average number of household members
        - Latitude block group latitude
        - Longitude block group longitude
    * Missing Attribute Values: None
    * Target: The target variable is the median house value for California districts,
        expressed in hundreds of thousands of dollars ($100,000).
        This dataset was obtained from the StatLib repository:
        https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html
        This dataset was derived from the 1990 U.S. census, using one row per census block group.
        A block group is the smallest geographical unit for which the U.S. Census Bureau publishes
        sample data (a block group typically has a population of 600 to 3,000 people).
        A household is a group of people residing within a home. Since the average number of rooms
        and bedrooms in this dataset are provided per household, these columns may take surprisingly
        large values for block groups with few households and many empty houses, such as vacation resorts.

    Args:
        feature_type (torch.dtype): The data type of the features. Defaults to torch.float.
        target_type (torch.dtype): The data type of the targets. Defaults to torch.long.
        train (bool): Whether the dataset is for training or not. Defaults to True.

    Attributes:
        data (Tensor): The data features.
        targets (Tensor): The data targets.

    Examples:
        >>> from torch.utils.data import DataLoader
            from spotpython.data.california_housing import CaliforniaHousing
            import torch
            dataset = CaliforniaHousing(feature_type=torch.float32, target_type=torch.float32)
            # Set batch size for DataLoader
            batch_size = 5
            # Create DataLoader
            dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
            # Iterate over the data in the DataLoader
            for batch in dataloader:
                inputs, targets = batch
                print(f"Batch Size: {inputs.size(0)}")
                print("---------------")
                print(f"Inputs: {inputs}")
                print(f"Targets: {targets}")
    """

    def __init__(
        self, feature_type: torch.dtype = torch.float, target_type: torch.dtype = torch.float, train: bool = True
    ) -> None:
        super().__init__()
        self.feature_type = feature_type
        self.target_type = target_type
        self.train = train
        self.names = self.get_names()
        self.data, self.targets = self._load_data()

    def _load_data(self) -> tuple:
        """Loads the data from scikit-learn and returns the features and targets.

        Returns:
            tuple: A tuple containing the features and targets.

        Examples:
            >>> from spotpython.data.california_housing import CaliforniaHousing
                dataset = CaliforniaHousing()
                print(dataset.data.shape)
                print(dataset.targets.shape)
                torch.Size([20640, 8])
                torch.Size([20640])
        """
        feature_df, target_df = fetch_california_housing(return_X_y=True, as_frame=True)
        # Convert DataFrames to PyTorch tensors
        feature_tensor = torch.tensor(feature_df.values, dtype=self.feature_type)
        target_tensor = torch.tensor(target_df.values, dtype=self.target_type)

        return feature_tensor, target_tensor

    def __getitem__(self, idx: int) -> tuple:
        """
        Returns the feature and target at the given index.

        Args:
            idx (int): The index.

        Returns:
            tuple: A tuple containing the feature and target at the given index.

        Examples:
            >>> from spotpython.data.california_housing import CaliforniaHousing
                dataset = CaliforniaHousing()
                print(dataset.data.shape)
                print(dataset.targets.shape)
                torch.Size([20640, 8])
                torch.Size([20640])
        """
        feature = self.data[idx]
        target = self.targets[idx]
        return feature, target

    def __len__(self) -> int:
        """
        Returns the length of the dataset.

        Returns:
            int: The length of the dataset.

        Examples:
            >>> from spotpython.data.california_housing import CaliforniaHousing
                dataset = CaliforniaHousing()
                print(len(dataset))
                20640
        """
        return len(self.data)

    def extra_repr(self) -> str:
        """
        Returns a string representation of the dataset.

        Returns:
            str: A string representation of the dataset.

        Examples:
            >>> from spotpython.light import CSVDataset
            >>> dataset = CSVDataset()
            >>> print(dataset)
            Split: Train

        """
        split = "Train" if self.train else "Test"
        return f"Split: {split}"

    def get_names(self) -> list:
        """
        Returns the names of the features.

        Returns:
            list: A list containing the names of the features.

        Examples:
            >>> from spotpython.data.california_housing import CaliforniaHousing
                dataset = CaliforniaHousing()
                print(dataset.get_names())
                ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
        """
        housing = fetch_california_housing()
        return housing.feature_names

__getitem__(idx)

Returns the feature and target at the given index.

Parameters:

Name Type Description Default
idx int

The index.

required

Returns:

Name Type Description
tuple tuple

A tuple containing the feature and target at the given index.

Examples:

>>> from spotpython.data.california_housing import CaliforniaHousing
    dataset = CaliforniaHousing()
    print(dataset.data.shape)
    print(dataset.targets.shape)
    torch.Size([20640, 8])
    torch.Size([20640])
Source code in spotpython/data/california_housing.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def __getitem__(self, idx: int) -> tuple:
    """
    Returns the feature and target at the given index.

    Args:
        idx (int): The index.

    Returns:
        tuple: A tuple containing the feature and target at the given index.

    Examples:
        >>> from spotpython.data.california_housing import CaliforniaHousing
            dataset = CaliforniaHousing()
            print(dataset.data.shape)
            print(dataset.targets.shape)
            torch.Size([20640, 8])
            torch.Size([20640])
    """
    feature = self.data[idx]
    target = self.targets[idx]
    return feature, target

__len__()

Returns the length of the dataset.

Returns:

Name Type Description
int int

The length of the dataset.

Examples:

>>> from spotpython.data.california_housing import CaliforniaHousing
    dataset = CaliforniaHousing()
    print(len(dataset))
    20640
Source code in spotpython/data/california_housing.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def __len__(self) -> int:
    """
    Returns the length of the dataset.

    Returns:
        int: The length of the dataset.

    Examples:
        >>> from spotpython.data.california_housing import CaliforniaHousing
            dataset = CaliforniaHousing()
            print(len(dataset))
            20640
    """
    return len(self.data)

extra_repr()

Returns a string representation of the dataset.

Returns:

Name Type Description
str str

A string representation of the dataset.

Examples:

>>> from spotpython.light import CSVDataset
>>> dataset = CSVDataset()
>>> print(dataset)
Split: Train
Source code in spotpython/data/california_housing.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def extra_repr(self) -> str:
    """
    Returns a string representation of the dataset.

    Returns:
        str: A string representation of the dataset.

    Examples:
        >>> from spotpython.light import CSVDataset
        >>> dataset = CSVDataset()
        >>> print(dataset)
        Split: Train

    """
    split = "Train" if self.train else "Test"
    return f"Split: {split}"

get_names()

Returns the names of the features.

Returns:

Name Type Description
list list

A list containing the names of the features.

Examples:

>>> from spotpython.data.california_housing import CaliforniaHousing
    dataset = CaliforniaHousing()
    print(dataset.get_names())
    ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
Source code in spotpython/data/california_housing.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def get_names(self) -> list:
    """
    Returns the names of the features.

    Returns:
        list: A list containing the names of the features.

    Examples:
        >>> from spotpython.data.california_housing import CaliforniaHousing
            dataset = CaliforniaHousing()
            print(dataset.get_names())
            ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']
    """
    housing = fetch_california_housing()
    return housing.feature_names