Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
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
  1. from sklearn.metrics import classification_report
  2. from mlflow.models.signature import infer_signature
  3. import mlflow
  4. from data.datamanager import data_loader
  5. from skl2onnx import convert_sklearn
  6. from skl2onnx.common.data_types import FloatTensorType, Int64TensorType, StringTensorType
  7. def convert_dataframe_schema(df, drop=None):
  8. inputs = []
  9. for k, v in zip(df.columns, df.dtypes):
  10. if drop is not None and k in drop:
  11. continue
  12. if v == 'int64':
  13. t = Int64TensorType([None, 1])
  14. elif v == 'float64':
  15. t = FloatTensorType([None, 1])
  16. else:
  17. t = StringTensorType([None, 1])
  18. inputs.append((k, t))
  19. return inputs
  20. def model_metrics(clf, data_path):
  21. x_test, y_test = data_loader(data_path)
  22. metrics = classification_report(y_test, clf.predict(x_test), output_dict=True)
  23. return metrics
  24. def convert_sklearn_mlflow(clf, x_sample):
  25. signature = infer_signature(x_sample, clf.predict(x_sample))
  26. input_example = {}
  27. for i in x_sample.columns:
  28. input_example[i] = x_sample[i][0]
  29. mlflow.sklearn.save_model(clf, "best_model", signature=signature, input_example=input_example)
  30. return
  31. def convert_sklearn_onnx(clf, x_sample):
  32. inputs = convert_dataframe_schema(x_sample)
  33. onnx_model = convert_sklearn(clf, 'model_pipeline', inputs, target_opset=12)
  34. with open("model.onnx", "wb") as f:
  35. f.write(onnx_model.SerializeToString())
  36. return onnx_model
  37. def onnx_input(x):
  38. inputs = {c: x[c].values for c in x.columns}
  39. for k in inputs:
  40. inputs[k] = inputs[k].reshape((inputs[k].shape[0], 1))
  41. return inputs
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...