FeatureSchema#
- class scikitplot.feature_selection.FeatureSchema(features, policy, n_rows)[source]#
Immutable, fitted description of a table’s columns.
- Parameters:
- featurestuple of FeatureSpec
One spec per column, in training column order.
- policyFeaturePolicy
The policy the schema was fitted with.
- n_rowsint
Training rows.
- Parameters:
features (tuple[FeatureSpec, ...])
policy (FeaturePolicy)
n_rows (int)
See also
profile_featuresFit a schema.
FeatureProfilerFit a schema inside a scikit-learn pipeline.
Notes
User note. A schema is fitted state: build it from training data once, keep it (
save), andauditlater data against it.Examples
>>> import pandas as pd >>> from scikitplot.feature_selection import profile_features >>> X = pd.DataFrame( ... { ... "age": [21, 35, 47, 52, 33, 41] * 5, ... "city": ["a", "b", "a", "c", "b", "a"] * 5, ... "vip": [True, False] * 15, ... "churn": [0, 1] * 15, ... } ... ) >>> schema = profile_features(X, {"roles": {"target": ["churn"]}}) >>> schema.groups(by="kind") {'binary': ['vip'], 'categorical_nominal': ['city'], 'numeric_discrete': ['age']} >>> schema["age"].is_("discrete"), schema["churn"].role (True, 'target')
- assessment_table()[source]#
Return a features-by-assessments table of states.
- Returns:
- pandas.DataFrame
Index: feature names; columns: assessment names; values: state strings.
- Return type:
- audit(X)[source]#
Compare later data’s columns and dtypes with the fitted schema.
- Parameters:
- Xpandas.DataFrame
Validation, test or production data.
- Returns:
- pandas.DataFrame
One row per finding with columns
column,issue(missing_column,extra_column,order_changed,family_changed,dtype_changed),expected,observed. Empty when the structure matches.dtype_changedwith an unchanged family (int64toInt64) is informational.
- Parameters:
X (DataFrame)
- Return type:
Notes
The schema is not changed. Distribution changes are the job of
ReferenceProfile.
- binning_plan()[source]#
Return the binning recommendation of every numeric feature.
- Returns:
- pandas.DataFrame
Columns
state,rule,n_bins,rule_n_bins,strategy,edges,reasons; indexed by feature.
- Return type:
- feature(key)[source]#
Return a feature by name (or position).
- Parameters:
- keystr or int
Column name, or position.
- Returns:
- FeatureSpec
The feature.
- Raises:
- KeyError
If the name is unknown.
- Parameters:
- Return type:
- features: tuple[FeatureSpec, ...][source]#
- classmethod from_dict(data)[source]#
Rebuild a schema from
to_dictoutput.- Parameters:
- datamapping
Stored form.
- Returns:
- FeatureSchema
The schema.
- Raises:
- IncompatibleSchemaError
For another
schema_versionor artefact type.- ValueError
For an off-contract record or a policy digest that does not match the stored policy.
- Parameters:
- Return type:
- groups(by='kind', *, role='feature')[source]#
Group feature names.
- Parameters:
- by{“kind”, “family”, “role”, “tag”, “assessment”}, default=”kind”
Grouping key.
"assessment"lists, per assessment, the features whose state is"true";"tag"lists, per tag, the tagged features (a feature can be in several groups).- rolestr or sequence of str or None, default=”feature”
Only features with these roles; None for all.
- Returns:
- dict of str to list of str
Group name (sorted) to member names (schema order); empty groups are omitted.
- Parameters:
- Return type:
- classmethod load(path, format=None)[source]#
Read a schema written by
save.- Parameters:
- pathstr or os.PathLike
Source file.
- formatstr or None, default=None
Explicit format.
- Returns:
- FeatureSchema
The schema.
- Parameters:
- Return type:
- classmethod loads(text, format='json')[source]#
Parse a schema document.
- Parameters:
- textstr
The document.
- format{“json”, “yaml”, “toml”}, default=”json”
Input format.
- Returns:
- FeatureSchema
The schema.
- Parameters:
- Return type:
- policy: FeaturePolicy[source]#
- rename(mapping)[source]#
Return a schema whose features are renamed, keeping old names as aliases.
- Parameters:
- mappingmapping of str to str
Old name to new name; unlisted features keep their names.
- Returns:
- FeatureSchema
The renamed schema (identity, stats and assessments unchanged).
- Raises:
- KeyError
If a key is not a feature name.
- ValueError
If the result has duplicate names.
- Parameters:
- Return type:
- select(*, kind=None, family=None, role='feature', tag=None, is_=(), is_not=(), names=None)[source]#
Return the names of the features that satisfy every given condition.
- Parameters:
- kind, familystr or sequence of str or None, default=None
Accept features of any of these kinds / families.
- rolestr or sequence of str or None, default=”feature”
Accept these roles; None accepts every role.
- tagstr or sequence of str or None, default=None
Accept features carrying at least one of these tags.
- is_sequence of str, default=()
Assessments that must all be
"true".- is_notsequence of str, default=()
Assessments that must not be
"true"(unknownpasses).- namessequence of str or None, default=None
Restrict to these names.
- Returns:
- list of str
Matching names in schema order.
- Raises:
- ValueError
For an unknown kind, family, role or assessment name.
- Parameters:
- Return type:
Examples
>>> import pandas as pd >>> from scikitplot.feature_selection import profile_features >>> X = pd.DataFrame({"a": [1.5, 2.5, 3.5, 95.5] * 5, "b": list("xyzx") * 5}) >>> schema = profile_features(X) >>> schema.select(kind="numeric_continuous", is_=("skewed",)) ['a']