Pandas Dataframe Algorithms
Pandas Dataframes
Pandas dataframes are obviously not going to scale as well as our Spark and SQL Algorithms, but for 'moderate' sized data these algorithms provide some nice functionality.
Pandas Dataframe Algorithms
SageWorks has a growing set of algorithms and data processing tools for Pandas Dataframes. In general these algorithm will take a dataframe as input and give you back a dataframe with additional columns.
FeatureSpaceProximity: A class for neighbor lookups using KNN with optional target information.
FeatureSpaceProximity
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
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 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 |
|
__init__(df, features, id_column, target=None, neighbors=10)
FeatureSpaceProximity: A class for neighbor lookups using KNN with optional target information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Pandas DataFrame |
required |
features
|
list
|
List of feature column names |
required |
id_column
|
str
|
Name of the ID column |
required |
target
|
str
|
Optional name of the target column to include target-based functionality (default: None) |
None
|
neighbors
|
int
|
Number of neighbors to use in the KNN model (default: 10) |
10
|
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
from_model(model)
classmethod
Create a FeatureSpaceProximity instance from a SageWorks model object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
A SageWorks model object. |
required |
Returns:
Name | Type | Description |
---|---|---|
FeatureSpaceProximity |
FeatureSpaceProximity
|
A new instance of the FeatureSpaceProximity class. |
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
get_neighbor_indices_and_distances()
Retrieve neighbor indices and distances for all points in the dataset.
neighbors(query_id, radius=None, include_self=True)
Return neighbors of the given query ID, either by fixed neighbors or within a radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_id
|
Union[str, int]
|
The ID of the query point. |
required |
radius
|
float
|
Optional radius within which neighbors are to be searched, else use fixed neighbors. |
None
|
include_self
|
bool
|
Whether to include the query ID itself in the neighbor results. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Filtered DataFrame that includes the query ID, its neighbors, and optionally target values. |
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
neighbors_bulk(query_df, radius=None, include_self=False)
Return neighbors for each row in the given query dataframe, either by fixed neighbors or within a radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_df
|
DataFrame
|
Pandas DataFrame with the same features as the training data. |
required |
radius
|
float
|
Optional radius within which neighbors are to be searched, else use fixed neighbors. |
None
|
include_self
|
bool
|
Boolean indicating whether to include the query ID in the neighbor results. |
False
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame with query ID, neighbor IDs, neighbor targets, and neighbor distances. |
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
outliers()
Compute a unified 'outlier' score based on either 'target_z' or 'target_consistency'.
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
target_consistency()
Compute a Neighborhood Consistency Score for CATEGORICAL targets.
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
target_summary(query_id)
WIP: Provide a summary of target values in the neighborhood of the given query ID
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
target_z_scores()
Compute Z-Scores for NUMERIC target values.
Source code in src/sageworks/algorithms/dataframe/feature_space_proximity.py
ResidualsCalculator
Bases: BaseEstimator
, TransformerMixin
A custom transformer for calculating residuals using cross-validation or an endpoint.
This transformer performs K-Fold cross-validation (if no endpoint is provided), or it uses the endpoint to generate predictions and compute residuals. It adds 'prediction', 'residuals', 'residuals_abs', 'prediction_100', 'residuals_100', and 'residuals_100_abs' columns to the input DataFrame.
Attributes:
Name | Type | Description |
---|---|---|
model_class |
Union[RegressorMixin, XGBRegressor]
|
The machine learning model class used for predictions. |
n_splits |
int
|
Number of splits for cross-validation. |
random_state |
int
|
Random state for reproducibility. |
endpoint |
Optional
|
The SageWorks endpoint object for running inference, if provided. |
Source code in src/sageworks/algorithms/dataframe/residuals_calculator.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
__init__(endpoint=None, reference_model_class=XGBRegressor)
Initializes the ResidualsCalculator with the specified parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
endpoint
|
Optional
|
A SageWorks endpoint object to run inference, if available. |
None
|
reference_model_class
|
Union[RegressorMixin, XGBRegressor]
|
The reference model class for predictions. |
XGBRegressor
|
Source code in src/sageworks/algorithms/dataframe/residuals_calculator.py
fit(X, y)
Fits the model. If no endpoint is provided, fitting involves storing the input data and initializing a reference model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input features. |
required |
y
|
Series
|
The target variable. |
required |
Returns:
Name | Type | Description |
---|---|---|
self |
BaseEstimator
|
Returns an instance of self. |
Source code in src/sageworks/algorithms/dataframe/residuals_calculator.py
transform(X)
Transforms the input DataFrame by adding 'prediction', 'residuals', 'residuals_abs', 'prediction_100', 'residuals_100', and 'residuals_100_abs' columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input features. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The transformed DataFrame with additional columns. |
Source code in src/sageworks/algorithms/dataframe/residuals_calculator.py
DimensionalityReduction: Perform Dimensionality Reduction on a DataFrame
DimensionalityReduction
Source code in src/sageworks/algorithms/dataframe/dimensionality_reduction.py
__init__()
DimensionalityReduction: Perform Dimensionality Reduction on a DataFrame
fit_transform(df, features=None, projection='TSNE')
Fit and Transform the DataFrame Args: df: Pandas DataFrame features: List of feature column names (default: None) projection: The projection model to use (TSNE, MDS or PCA, default: PCA) Returns: Pandas DataFrame with new columns x and y
Source code in src/sageworks/algorithms/dataframe/dimensionality_reduction.py
resolve_coincident_points(df)
staticmethod
Resolve coincident points in a DataFrame Args: df(pd.DataFrame): The DataFrame to resolve coincident points in Returns: pd.DataFrame: The DataFrame with resolved coincident points
Source code in src/sageworks/algorithms/dataframe/dimensionality_reduction.py
test()
Test for the Dimensionality Reduction Class
Source code in src/sageworks/algorithms/dataframe/dimensionality_reduction.py
Questions?
The SuperCowPowers team is happy to answer any questions you may have about AWS and SageWorks. Please contact us at sageworks@supercowpowers.com or on chat us up on Discord