Types
framework3.base.base_types
¶
Float = float | np.float16 | np.float32 | np.float64
module-attribute
¶
Type alias for float values, including numpy float types.
IncEx = 'set[int] | set[str] | dict[int, Any] | dict[str, Any] | None'
module-attribute
¶
Type alias for inclusion/exclusion specifications in data processing.
SkVData = np.ndarray | pd.DataFrame | spmatrix | csr_matrix
module-attribute
¶
Type alias for scikit-learn compatible data structures.
TxyData = TypeVar('TxyData', SkVData, VData)
module-attribute
¶
Type variable constrained to SkVData or VData for use in XYData.
TypePlugable = TypeVar('TypePlugable')
module-attribute
¶
Generic type variable for pluggable types in the framework.
VData = np.ndarray | pd.DataFrame | spmatrix | list | torch.Tensor
module-attribute
¶
Type alias for various data structures used in the framework.
__all__ = ['XYData', 'VData', 'SkVData', 'IncEx', 'TypePlugable']
module-attribute
¶
JsonEncoderkwargs
¶
Bases: TypedDict
Source code in framework3/base/base_types.py
XYData
dataclass
¶
Bases: Generic[TxyData]
A dataclass representing data for machine learning tasks, typically features (X) or targets (Y).
This class is immutable and uses slots for memory efficiency. It provides a standardized way to handle various types of data used in machine learning pipelines.
Attributes:
Name | Type | Description |
---|---|---|
_hash |
str
|
A unique identifier or hash for the data. |
_path |
str
|
The path where the data is stored or retrieved from. |
_value |
TxyData | Callable[..., TxyData]
|
The actual data or a callable that returns the data. |
Methods:
Name | Description |
---|---|
train_test_split |
Split the data into training and testing sets. |
split |
Create a new XYData instance with specified indices. |
mock |
Create a mock XYData instance for testing or placeholder purposes. |
concat |
Concatenate a list of data along the specified axis. |
ensure_dim |
Ensure the input data has at least two dimensions. |
as_iterable |
Convert the data to an iterable form. |
Example
import numpy as np
from framework3.base.base_types import XYData
# Create a mock XYData instance with random data
features = np.random.rand(100, 5)
labels = np.random.randint(0, 2, 100)
x_data = XYData.mock(features, hash="feature_data", path="/data/features")
y_data = XYData.mock(labels, hash="label_data", path="/data/labels")
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = x_data.train_test_split(x_data.value, y_data.value, test_size=0.2)
# Access the data
print(f"Training features shape: {X_train.value.shape}")
print(f"Training labels shape: {y_train.value.shape}")
# Create a subset of the data
subset = x_data.split(range(50))
print(f"Subset shape: {subset.value.shape}")
Note
This class is designed to work with various data types including numpy arrays, pandas DataFrames, scipy sparse matrices, and PyTorch tensors.
Source code in framework3/base/base_types.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
value
property
¶
Property to access the actual data.
This property ensures that if _value is a callable, it is called to retrieve the data. Otherwise, it returns the data directly.
Returns:
Name | Type | Description |
---|---|---|
TxyData |
TxyData
|
The actual data (numpy array, pandas DataFrame, scipy sparse matrix, etc.). |
Note
This property may modify the _value attribute if it's initially a callable.
__init__(_hash, _path, _value)
¶
as_iterable()
¶
Convert the _value
attribute to an iterable, regardless of its underlying type.
This method provides a consistent way to iterate over the data, handling different data types appropriately.
Returns:
Name | Type | Description |
---|---|---|
Iterable |
Iterable
|
An iterable version of |
Raises:
Type | Description |
---|---|
TypeError
|
If the value type is not compatible with iteration. |
Source code in framework3/base/base_types.py
concat(x, axis=-1)
staticmethod
¶
Concatenate a list of data along the specified axis.
This static method handles concatenation for various data types, including sparse matrices and other array-like structures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
list[TxyData]
|
List of data to concatenate. |
required |
axis
|
int
|
Axis along which to concatenate. Defaults to -1. |
-1
|
Returns:
Name | Type | Description |
---|---|---|
XYData |
XYData
|
A new XYData instance with the concatenated data. |
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid axis is specified for sparse matrix concatenation. |
Example
Source code in framework3/base/base_types.py
ensure_dim(x)
staticmethod
¶
Ensure the input data has at least two dimensions.
This static method is a wrapper around the ensure_dim function, which adds a new axis to 1D arrays or lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
list | ndarray
|
Input data to ensure dimensions. |
required |
Returns:
Type | Description |
---|---|
list | ndarray
|
list | np.ndarray: Data with at least two dimensions. |
Source code in framework3/base/base_types.py
mock(value, hash=None, path=None)
staticmethod
¶
Create a mock XYData instance for testing or placeholder purposes.
This static method allows for easy creation of XYData instances, particularly useful in testing scenarios or when placeholder data is needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
TxyData | Callable[..., TxyData]
|
The data or a callable that returns the data. |
required |
hash
|
str | None
|
A hash string for the data. Defaults to "Mock" if None. |
None
|
path
|
str | None
|
A path string for the data. Defaults to "/tmp" if None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
XYData |
XYData
|
A new XYData instance with the provided or default values. |
Source code in framework3/base/base_types.py
split(indices)
¶
Split the data into a new XYData instance with the specified indices.
This method creates a new XYData instance containing only the data corresponding to the provided indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indices
|
Iterable[int]
|
The indices to select from the data. |
required |
Returns:
Name | Type | Description |
---|---|---|
XYData |
XYData
|
A new XYData instance containing the selected data. |
Example
Source code in framework3/base/base_types.py
train_test_split(x, y, test_size, random_state=42)
¶
Split the data into training and testing sets.
This method uses sklearn's train_test_split function to divide the data into training and testing sets for both features (X) and targets (Y).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
TxyData
|
The feature data to split. |
required |
y
|
TxyData | None
|
The target data to split. Can be None for unsupervised learning. |
required |
test_size
|
float
|
The proportion of the data to include in the test split (0.0 to 1.0). |
required |
random_state
|
int
|
Seed for the random number generator. Defaults to 42. |
42
|
Returns:
Type | Description |
---|---|
XYData
|
Tuple[XYData, XYData, XYData, XYData]: A tuple containing (X_train, X_test, y_train, y_test), |
XYData
|
each wrapped in an XYData instance. |
Example
Source code in framework3/base/base_types.py
_(x)
¶
Ensure that a list has at least two dimensions by converting it to a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
list
|
Input list. |
required |
Returns:
Name | Type | Description |
---|---|---|
SkVData |
SkVData
|
A numpy array with at least two dimensions. |
Source code in framework3/base/base_types.py
concat(x, axis)
¶
Base multimethod for concatenation. Raises an error for unsupported types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
Data to concatenate. |
required |
axis
|
int
|
Axis along which to concatenate. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
Always raised as this is the base method for unsupported types. |
Source code in framework3/base/base_types.py
ensure_dim(x)
¶
Base multimethod for ensuring dimensions. Raises an error for unsupported types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
Data to ensure dimensions for. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
Always raised as this is the base method for unsupported types. |