feature¶
Feature representation and feature sets.
Feature
dataclass
¶
Represents a single feature with metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Feature name |
dtype |
FeatureType
|
Data type of feature |
origin |
FeatureOrigin
|
How the feature was created |
source_columns |
list
|
Original columns used to create this feature |
transformation |
str
|
Description of transformation applied |
explanation |
(str, optional)
|
Human-readable explanation of the feature |
code |
(str, optional)
|
Python code that generates this feature |
importance |
(float, optional)
|
Feature importance score |
metadata |
dict
|
Additional metadata |
Source code in featcopilot/core/feature.py
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 | |
compute(df)
¶
Compute feature values from DataFrame using stored code.
The stored code is executed in a single shared namespace
with df, np and pd bound as names alongside a
curated set of safe Python builtins (len, range,
sum, numeric / sequence constructors, etc.) so common
idioms work without giving the snippet a Python import system
— __import__ is intentionally NOT in the safe builtins, so
an import foo statement inside the snippet raises at exec
time. The snippet must bind its output to a name called
result.
.. note::
This is not a security sandbox for untrusted code.
pd is in scope, which means the snippet can reach
pandas' file I/O helpers (pd.read_csv, pd.read_parquet,
df.to_csv, ...), and dunder attribute access on objects
reachable from df / np / pd is not blocked. The
builtin whitelist limits the namespace available to plain
Python idioms; it does not isolate FeatCopilot from the
ambient process. Stored snippets must therefore come from a
trusted source (your own code generator, a vetted feature
store, or a transform-rule registry you control).
A fresh copy of the safe-builtins dict is passed into exec
on every call so that any mutation the snippet performs on
__builtins__ (rebinding entries, del, pop) does not
bleed into subsequent compute calls. Likewise the
data-bound namespace is constructed fresh per call. Using a
SINGLE dict for both globals and locals is what makes
free variables inside comprehensions and lambdas — which Python
resolves against the enclosing function's globals, not the
caller's locals — see df, np and pd correctly.
With separate locals and globals dicts a snippet such
as [df['c'].iloc[i] for i in range(len(df))] would
otherwise raise NameError because the implicit comprehension
function's body looks df up in the (empty) globals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input data |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Computed feature values |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in featcopilot/core/feature.py
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 | |
from_dict(data)
classmethod
¶
Create feature from dictionary.
Source code in featcopilot/core/feature.py
to_dict()
¶
Convert feature to dictionary.
Source code in featcopilot/core/feature.py
FeatureOrigin
¶
Bases: Enum
Origin/source of feature.
Source code in featcopilot/core/feature.py
FeatureSet
¶
Collection of features with operations for manipulation.
Provides methods for adding, removing, filtering, and combining features.
Source code in featcopilot/core/feature.py
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
add(feature)
¶
compute_all(df)
¶
Compute all features that have code defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input data |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with computed features |
Source code in featcopilot/core/feature.py
filter_by_importance(min_importance)
¶
Filter features by minimum importance.
filter_by_origin(origin)
¶
filter_by_type(dtype)
¶
get(name)
¶
get_explanations()
¶
get_names()
¶
merge(other)
¶
remove(name)
¶
sort_by_importance(descending=True)
¶
Sort features by importance.