wizata-dsapi 2.0.0.dev24__tar.gz → 2.0.0.dev25__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {wizata_dsapi-2.0.0.dev24/wizata_dsapi.egg-info → wizata_dsapi-2.0.0.dev25}/PKG-INFO +1 -1
- wizata_dsapi-2.0.0.dev25/wizata_dsapi/models/__init__.py +1 -0
- wizata_dsapi-2.0.0.dev25/wizata_dsapi/models/common.py +272 -0
- wizata_dsapi-2.0.0.dev25/wizata_dsapi/version.py +1 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25/wizata_dsapi.egg-info}/PKG-INFO +1 -1
- wizata_dsapi-2.0.0.dev24/wizata_dsapi/models/__init__.py +0 -1
- wizata_dsapi-2.0.0.dev24/wizata_dsapi/models/common.py +0 -116
- wizata_dsapi-2.0.0.dev24/wizata_dsapi/version.py +0 -1
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/LICENSE.txt +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/README.rst +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/setup.cfg +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/setup.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/__init__.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/api_config.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/api_dto.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/api_interface.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/bucket.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/business_label.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/context.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/dashboard.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/dataframe_toolkit.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/datapoint.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/datastore.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/ds_dataframe.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/dsapi_json_encoder.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/edge_config.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/edge_device.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/edge_module.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/evaluation.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/execution.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/execution_log.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/experiment.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/graylog_log.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/group_system.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/ilogger.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/insight.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/mlmodel.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/mobile_asset.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/model_toolkit.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/notification.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/paged_query_result.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/pipeline.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/pipeline_image.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/plot.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/plots/__init__.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/plots/common.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/plots/theme.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/request.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/script.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/scripts/__init__.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/scripts/common.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/search.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/solution_component.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/streamlit_utils.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/template.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/template_config.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/trigger.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/twin.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/twinregistration.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/user.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/wizard_function.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/wizard_request.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/wizata_dsapi_client.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi/words.py +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi.egg-info/SOURCES.txt +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi.egg-info/dependency_links.txt +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi.egg-info/requires.txt +0 -0
- {wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .common import linear_regression, logistic_regression, isolation_forest, gradiant_boost_classifier, setpoint_optimizer, SetpointOptimizer
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import wizata_dsapi
|
|
2
|
+
|
|
3
|
+
import pandas
|
|
4
|
+
import numpy
|
|
5
|
+
|
|
6
|
+
import sklearn
|
|
7
|
+
import sklearn.linear_model
|
|
8
|
+
import sklearn.ensemble
|
|
9
|
+
import sklearn.neighbors
|
|
10
|
+
import sklearn.pipeline
|
|
11
|
+
import sklearn.preprocessing
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def extract_target_feat(context: wizata_dsapi.Context, single: bool = True):
|
|
15
|
+
"""
|
|
16
|
+
return a list of target_feat columns names if not single value or the single value target feat name
|
|
17
|
+
raise an error if configuration mismatch
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
if "target_feat" not in context.properties:
|
|
21
|
+
raise ValueError(f"training script requires a proper target_feat")
|
|
22
|
+
|
|
23
|
+
target_feat = context.properties["target_feat"]
|
|
24
|
+
if isinstance(target_feat, str):
|
|
25
|
+
if single:
|
|
26
|
+
return target_feat
|
|
27
|
+
else:
|
|
28
|
+
return [target_feat]
|
|
29
|
+
elif isinstance(target_feat, list):
|
|
30
|
+
if single:
|
|
31
|
+
if len(target_feat) == 1:
|
|
32
|
+
return target_feat[0]
|
|
33
|
+
else:
|
|
34
|
+
raise ValueError(f"expecting only one target_feat but found {len(target_feat)}")
|
|
35
|
+
else:
|
|
36
|
+
return [target_feat]
|
|
37
|
+
else:
|
|
38
|
+
raise TypeError(f'target_feat must be a str or a list of str but found {target_feat.__class__.__name__}')
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def linear_regression(context: wizata_dsapi.Context):
|
|
42
|
+
"""Train a linear regression model on all features to predict a single target column."""
|
|
43
|
+
df = context.dataframe
|
|
44
|
+
|
|
45
|
+
model_config = context.get_model_config()
|
|
46
|
+
if not model_config.has_target_feat():
|
|
47
|
+
raise ValueError(f'linear_regression requires a target feat')
|
|
48
|
+
target_feat_name = context.properties["target_feat"]
|
|
49
|
+
|
|
50
|
+
x = df.drop(columns=[target_feat_name])
|
|
51
|
+
y = df[target_feat_name]
|
|
52
|
+
|
|
53
|
+
model = sklearn.linear_model.LinearRegression()
|
|
54
|
+
model.fit(x, y)
|
|
55
|
+
|
|
56
|
+
context.set_model(model, features=x.columns)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def logistic_regression(context: wizata_dsapi.Context):
|
|
60
|
+
"""Train a logistic regression classifier on all features to predict a binary target column."""
|
|
61
|
+
df = context.dataframe
|
|
62
|
+
|
|
63
|
+
model_config = context.get_model_config()
|
|
64
|
+
if not model_config.has_target_feat():
|
|
65
|
+
raise ValueError(f'logistic_regression requires a target feat')
|
|
66
|
+
target_feat_name = context.properties["target_feat"]
|
|
67
|
+
|
|
68
|
+
x = df.drop(columns=[target_feat_name])
|
|
69
|
+
y = df[target_feat_name]
|
|
70
|
+
|
|
71
|
+
model = sklearn.linear_model.LogisticRegression()
|
|
72
|
+
model.fit(x, y.astype(int))
|
|
73
|
+
|
|
74
|
+
context.set_model(model, features=x.columns)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def isolation_forest(context: wizata_dsapi.Context):
|
|
78
|
+
"""Train an Isolation Forest for unsupervised anomaly detection using a sensitivity level (1-5)."""
|
|
79
|
+
|
|
80
|
+
model_config = context.get_model_config()
|
|
81
|
+
if model_config.has_target_feat():
|
|
82
|
+
raise ValueError(f'isolation_forest does not requires a target feat')
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
if context.properties['sensitivity'] is None:
|
|
86
|
+
raise KeyError("sensitivity is none")
|
|
87
|
+
sensitivity = int(context.properties['sensitivity'])
|
|
88
|
+
sensitivities = [0.05, 0.15, 0.25, 0.35, 0.4]
|
|
89
|
+
contamination = sensitivities[sensitivity - 1]
|
|
90
|
+
except Exception as e:
|
|
91
|
+
raise ValueError(f'cannot extract sensitivity integer from 0 to 4 due to {e}')
|
|
92
|
+
|
|
93
|
+
df = context.dataframe.copy()
|
|
94
|
+
model = sklearn.ensemble.IsolationForest(contamination=contamination)
|
|
95
|
+
df['isolation_forest_predict'] = model.fit_predict(df)
|
|
96
|
+
context.set_model(model, features=df.columns)
|
|
97
|
+
return df
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def gradiant_boost_classifier(context: wizata_dsapi.Context):
|
|
101
|
+
"""Train a Gradient Boosting classifier on all features to predict a target column."""
|
|
102
|
+
df = context.dataframe
|
|
103
|
+
|
|
104
|
+
model_config = context.get_model_config()
|
|
105
|
+
if not model_config.has_target_feat():
|
|
106
|
+
raise ValueError(f'gradiant_boost_classifier requires a target feat')
|
|
107
|
+
target_feat_name = context.properties["target_feat"]
|
|
108
|
+
|
|
109
|
+
x = df.drop(columns=[target_feat_name])
|
|
110
|
+
y = df[target_feat_name]
|
|
111
|
+
|
|
112
|
+
model = sklearn.ensemble.GradientBoostingClassifier(random_state=0).fit(x, y)
|
|
113
|
+
context.set_model(model, features=df.columns)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class SetpointOptimizer:
|
|
117
|
+
"""
|
|
118
|
+
Wraps a KNN quality forecaster (StandardScaler + KNeighborsRegressor) with a grid-search
|
|
119
|
+
setpoint recommendation method.
|
|
120
|
+
|
|
121
|
+
At training time, the underlying pipeline learns quality = f(telemetry + setpoints) from
|
|
122
|
+
historical data. At inference, recommend(X) keeps each row's telemetry fixed and grid-searches
|
|
123
|
+
over stored setpoint bounds (5th-95th percentile of train data) to find the combination that
|
|
124
|
+
minimizes (or maximizes) predicted quality.
|
|
125
|
+
|
|
126
|
+
:ivar pipeline: fitted sklearn Pipeline (StandardScaler -> KNeighborsRegressor).
|
|
127
|
+
:ivar setpoint_cols: ordered list of setpoint column names auto-detected at train time.
|
|
128
|
+
:ivar feature_cols: ordered list of all feature columns used at training (telemetry + setpoints).
|
|
129
|
+
:ivar bounds: dict mapping each setpoint column name to a (low, high) tuple.
|
|
130
|
+
:ivar direction: 'minimize' or 'maximize' the target quality.
|
|
131
|
+
:ivar grid_size: number of points per setpoint axis in the grid search.
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(self, pipeline, setpoint_cols, feature_cols, bounds, direction, grid_size):
|
|
135
|
+
self.pipeline = pipeline
|
|
136
|
+
self.setpoint_cols = list(setpoint_cols)
|
|
137
|
+
self.feature_cols = list(feature_cols)
|
|
138
|
+
self.bounds = dict(bounds)
|
|
139
|
+
self.direction = direction
|
|
140
|
+
self.grid_size = int(grid_size)
|
|
141
|
+
|
|
142
|
+
def predict(self, X):
|
|
143
|
+
"""Return predicted quality for each row — dual-use for validation or regular predict-mode pipelines."""
|
|
144
|
+
return self.pipeline.predict(X)
|
|
145
|
+
|
|
146
|
+
def recommend(self, X):
|
|
147
|
+
"""
|
|
148
|
+
For each row of X, return the grid-search best setpoint combination.
|
|
149
|
+
:param X: features matrix (DataFrame or ndarray) with columns matching feature_cols.
|
|
150
|
+
:return: ndarray of shape [n_rows, n_setpoints] in the order of self.setpoint_cols.
|
|
151
|
+
"""
|
|
152
|
+
if not isinstance(X, pandas.DataFrame):
|
|
153
|
+
X = pandas.DataFrame(X, columns=self.feature_cols)
|
|
154
|
+
|
|
155
|
+
grids = [
|
|
156
|
+
numpy.linspace(self.bounds[sp][0], self.bounds[sp][1], self.grid_size)
|
|
157
|
+
for sp in self.setpoint_cols
|
|
158
|
+
]
|
|
159
|
+
mesh = numpy.array(numpy.meshgrid(*grids)).reshape(len(self.setpoint_cols), -1).T
|
|
160
|
+
|
|
161
|
+
recs = numpy.zeros((len(X), len(self.setpoint_cols)))
|
|
162
|
+
for i, (_, row) in enumerate(X.iterrows()):
|
|
163
|
+
candidates = pandas.DataFrame(
|
|
164
|
+
numpy.tile(row.values, (len(mesh), 1)),
|
|
165
|
+
columns=self.feature_cols
|
|
166
|
+
)
|
|
167
|
+
for j, sp in enumerate(self.setpoint_cols):
|
|
168
|
+
candidates[sp] = mesh[:, j]
|
|
169
|
+
preds = self.pipeline.predict(candidates)
|
|
170
|
+
if self.direction == "maximize":
|
|
171
|
+
best_idx = int(numpy.argmax(preds))
|
|
172
|
+
else:
|
|
173
|
+
best_idx = int(numpy.argmin(preds))
|
|
174
|
+
recs[i] = mesh[best_idx]
|
|
175
|
+
|
|
176
|
+
return recs
|
|
177
|
+
|
|
178
|
+
def output_names(self, suffix: str = "_recommended"):
|
|
179
|
+
"""Suggest MLModelConfig.output_columns_names matching the setpoint order (e.g. for UI prefill)."""
|
|
180
|
+
return [f"{sp}{suffix}" for sp in self.setpoint_cols]
|
|
181
|
+
|
|
182
|
+
def get_inference_contract(self):
|
|
183
|
+
"""Self-describing inference contract — the platform reads this to override MLModelConfig at runtime.
|
|
184
|
+
Returns the function to invoke (.recommend) and the output column names that will be produced, in order."""
|
|
185
|
+
return {
|
|
186
|
+
"function": "recommend",
|
|
187
|
+
"output_columns_names": self.output_names(),
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def setpoint_optimizer(context: wizata_dsapi.Context):
|
|
192
|
+
"""Train a KNN-based setpoint optimizer that learns quality = f(telemetry + setpoints) and at
|
|
193
|
+
inference recommends optimal setpoint values. Setpoint columns are auto-detected via
|
|
194
|
+
BusinessType.SET_POINTS on context.datapoints; bounds are the 5th-95th percentile of train data.
|
|
195
|
+
|
|
196
|
+
Required MLModelConfig:
|
|
197
|
+
- train_script = 'wizata.models.setpoint_optimizer'
|
|
198
|
+
- target_feat = '<quality column name>'
|
|
199
|
+
- function = 'recommend' (or 'predict' for quality forecasting only)
|
|
200
|
+
- output_columns_names = ['<sp1>_recommended', '<sp2>_recommended', ...] in the order setpoints appear.
|
|
201
|
+
The trained model exposes `.output_names()` to suggest a matching default.
|
|
202
|
+
|
|
203
|
+
Properties:
|
|
204
|
+
- k: KNN neighbors (default 5)
|
|
205
|
+
- grid_size: points per setpoint axis (default 10 — total cost is grid_size^n_setpoints per row)
|
|
206
|
+
- direction: 'minimize' (default) or 'maximize' the target
|
|
207
|
+
"""
|
|
208
|
+
df = context.dataframe
|
|
209
|
+
|
|
210
|
+
model_config = context.get_model_config()
|
|
211
|
+
if not model_config.has_target_feat():
|
|
212
|
+
raise ValueError(f'setpoint_optimizer requires a target_feat (the quality column to optimize)')
|
|
213
|
+
|
|
214
|
+
target = context.properties["target_feat"]
|
|
215
|
+
if isinstance(target, list):
|
|
216
|
+
if len(target) != 1:
|
|
217
|
+
raise ValueError(f'setpoint_optimizer requires exactly one target_feat column')
|
|
218
|
+
target = target[0]
|
|
219
|
+
|
|
220
|
+
if target not in df.columns:
|
|
221
|
+
raise ValueError(f"target_feat '{target}' not found in dataframe columns")
|
|
222
|
+
|
|
223
|
+
datapoints = context.datapoints or {}
|
|
224
|
+
setpoint_cols = [
|
|
225
|
+
col for col, dp in datapoints.items()
|
|
226
|
+
if col in df.columns
|
|
227
|
+
and col != target
|
|
228
|
+
and dp.business_type == wizata_dsapi.BusinessType.SET_POINTS
|
|
229
|
+
]
|
|
230
|
+
|
|
231
|
+
if not setpoint_cols:
|
|
232
|
+
raise ValueError(
|
|
233
|
+
"no setpoint datapoints found in context (BusinessType.SET_POINTS) — "
|
|
234
|
+
"the optimizer needs at least one setpoint column to optimize"
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
k = int(context.properties.get("k", 5))
|
|
238
|
+
grid_size = int(context.properties.get("grid_size", 10))
|
|
239
|
+
direction = context.properties.get("direction", "minimize")
|
|
240
|
+
if direction not in ("minimize", "maximize"):
|
|
241
|
+
raise ValueError(f"direction must be 'minimize' or 'maximize', got '{direction}'")
|
|
242
|
+
|
|
243
|
+
x = df.drop(columns=[target])
|
|
244
|
+
y = df[target]
|
|
245
|
+
|
|
246
|
+
pipeline = sklearn.pipeline.Pipeline([
|
|
247
|
+
("scaler", sklearn.preprocessing.StandardScaler()),
|
|
248
|
+
("knn", sklearn.neighbors.KNeighborsRegressor(n_neighbors=k)),
|
|
249
|
+
])
|
|
250
|
+
pipeline.fit(x, y)
|
|
251
|
+
|
|
252
|
+
bounds = {
|
|
253
|
+
sp: (float(x[sp].quantile(0.05)), float(x[sp].quantile(0.95)))
|
|
254
|
+
for sp in setpoint_cols
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
optimizer = SetpointOptimizer(
|
|
258
|
+
pipeline=pipeline,
|
|
259
|
+
setpoint_cols=setpoint_cols,
|
|
260
|
+
feature_cols=list(x.columns),
|
|
261
|
+
bounds=bounds,
|
|
262
|
+
direction=direction,
|
|
263
|
+
grid_size=grid_size,
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
context.set_model(optimizer, features=x.columns)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "2.0.0.dev25"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from .common import linear_regression, logistic_regression, isolation_forest, gradiant_boost_classifier
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import wizata_dsapi
|
|
2
|
-
|
|
3
|
-
import pandas
|
|
4
|
-
import numpy
|
|
5
|
-
|
|
6
|
-
import sklearn
|
|
7
|
-
import sklearn.linear_model
|
|
8
|
-
import sklearn.ensemble
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def extract_target_feat(context: wizata_dsapi.Context, single: bool = True):
|
|
12
|
-
"""
|
|
13
|
-
return a list of target_feat columns names if not single value or the single value target feat name
|
|
14
|
-
raise an error if configuration mismatch
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
if "target_feat" not in context.properties:
|
|
18
|
-
raise ValueError(f"training script requires a proper target_feat")
|
|
19
|
-
|
|
20
|
-
target_feat = context.properties["target_feat"]
|
|
21
|
-
if isinstance(target_feat, str):
|
|
22
|
-
if single:
|
|
23
|
-
return target_feat
|
|
24
|
-
else:
|
|
25
|
-
return [target_feat]
|
|
26
|
-
elif isinstance(target_feat, list):
|
|
27
|
-
if single:
|
|
28
|
-
if len(target_feat) == 1:
|
|
29
|
-
return target_feat[0]
|
|
30
|
-
else:
|
|
31
|
-
raise ValueError(f"expecting only one target_feat but found {len(target_feat)}")
|
|
32
|
-
else:
|
|
33
|
-
return [target_feat]
|
|
34
|
-
else:
|
|
35
|
-
raise TypeError(f'target_feat must be a str or a list of str but found {target_feat.__class__.__name__}')
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def linear_regression(context: wizata_dsapi.Context):
|
|
39
|
-
"""Train a linear regression model on all features to predict a single target column."""
|
|
40
|
-
df = context.dataframe
|
|
41
|
-
|
|
42
|
-
model_config = context.get_model_config()
|
|
43
|
-
if not model_config.has_target_feat():
|
|
44
|
-
raise ValueError(f'linear_regression requires a target feat')
|
|
45
|
-
target_feat_name = context.properties["target_feat"]
|
|
46
|
-
|
|
47
|
-
x = df.drop(columns=[target_feat_name])
|
|
48
|
-
y = df[target_feat_name]
|
|
49
|
-
|
|
50
|
-
model = sklearn.linear_model.LinearRegression()
|
|
51
|
-
model.fit(x, y)
|
|
52
|
-
|
|
53
|
-
context.set_model(model, features=x.columns)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def logistic_regression(context: wizata_dsapi.Context):
|
|
57
|
-
"""Train a logistic regression classifier on all features to predict a binary target column."""
|
|
58
|
-
df = context.dataframe
|
|
59
|
-
|
|
60
|
-
model_config = context.get_model_config()
|
|
61
|
-
if not model_config.has_target_feat():
|
|
62
|
-
raise ValueError(f'logistic_regression requires a target feat')
|
|
63
|
-
target_feat_name = context.properties["target_feat"]
|
|
64
|
-
|
|
65
|
-
x = df.drop(columns=[target_feat_name])
|
|
66
|
-
y = df[target_feat_name]
|
|
67
|
-
|
|
68
|
-
model = sklearn.linear_model.LogisticRegression()
|
|
69
|
-
model.fit(x, y.astype(int))
|
|
70
|
-
|
|
71
|
-
context.set_model(model, features=x.columns)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def isolation_forest(context: wizata_dsapi.Context):
|
|
75
|
-
"""Train an Isolation Forest for unsupervised anomaly detection using a sensitivity level (1-5)."""
|
|
76
|
-
|
|
77
|
-
model_config = context.get_model_config()
|
|
78
|
-
if model_config.has_target_feat():
|
|
79
|
-
raise ValueError(f'isolation_forest does not requires a target feat')
|
|
80
|
-
|
|
81
|
-
try:
|
|
82
|
-
if context.properties['sensitivity'] is None:
|
|
83
|
-
raise KeyError("sensitivity is none")
|
|
84
|
-
sensitivity = int(context.properties['sensitivity'])
|
|
85
|
-
sensitivities = [0.05, 0.15, 0.25, 0.35, 0.4]
|
|
86
|
-
contamination = sensitivities[sensitivity - 1]
|
|
87
|
-
except Exception as e:
|
|
88
|
-
raise ValueError(f'cannot extract sensitivity integer from 0 to 4 due to {e}')
|
|
89
|
-
|
|
90
|
-
df = context.dataframe.copy()
|
|
91
|
-
model = sklearn.ensemble.IsolationForest(contamination=contamination)
|
|
92
|
-
df['isolation_forest_predict'] = model.fit_predict(df)
|
|
93
|
-
context.set_model(model, features=df.columns)
|
|
94
|
-
return df
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
def gradiant_boost_classifier(context: wizata_dsapi.Context):
|
|
98
|
-
"""Train a Gradient Boosting classifier on all features to predict a target column."""
|
|
99
|
-
df = context.dataframe
|
|
100
|
-
|
|
101
|
-
model_config = context.get_model_config()
|
|
102
|
-
if not model_config.has_target_feat():
|
|
103
|
-
raise ValueError(f'gradiant_boost_classifier requires a target feat')
|
|
104
|
-
target_feat_name = context.properties["target_feat"]
|
|
105
|
-
|
|
106
|
-
x = df.drop(columns=[target_feat_name])
|
|
107
|
-
y = df[target_feat_name]
|
|
108
|
-
|
|
109
|
-
model = sklearn.ensemble.GradientBoostingClassifier(random_state=0).fit(x, y)
|
|
110
|
-
context.set_model(model, features=df.columns)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "2.0.0.dev24"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{wizata_dsapi-2.0.0.dev24 → wizata_dsapi-2.0.0.dev25}/wizata_dsapi.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|