CsanadT commited on
Commit
6f36987
·
1 Parent(s): 41d42ac

Delete iris-training-pipeline.py

Browse files
Files changed (1) hide show
  1. iris-training-pipeline.py +0 -100
iris-training-pipeline.py DELETED
@@ -1,100 +0,0 @@
1
- import os
2
- import modal
3
-
4
- LOCAL=True
5
-
6
- if LOCAL == False:
7
- stub = modal.Stub()
8
- image = modal.Image.debian_slim().apt_install(["libgomp1"]).pip_install(["hopsworks", "seaborn", "joblib", "scikit-learn"])
9
-
10
- @stub.function(image=image, schedule=modal.Period(days=1), secret=modal.Secret.from_name("HOPSWORKS_API_KEY"))
11
- def f():
12
- g()
13
-
14
-
15
- def g():
16
- import hopsworks
17
- import pandas as pd
18
- from sklearn.neighbors import KNeighborsClassifier
19
- from sklearn.metrics import accuracy_score
20
- from sklearn.metrics import confusion_matrix
21
- from sklearn.metrics import classification_report
22
- import seaborn as sns
23
- from matplotlib import pyplot
24
- from hsml.schema import Schema
25
- from hsml.model_schema import ModelSchema
26
- import joblib
27
-
28
- # You have to set the environment variable 'HOPSWORKS_API_KEY' for login to succeed
29
- project = hopsworks.login()
30
- # fs is a reference to the Hopsworks Feature Store
31
- fs = project.get_feature_store()
32
-
33
- # The feature view is the input set of features for your model. The features can come from different feature groups.
34
- # You can select features from different feature groups and join them together to create a feature view
35
- try:
36
- feature_view = fs.get_feature_view(name="iris_modal", version=1)
37
- except:
38
- iris_fg = fs.get_feature_group(name="iris_modal", version=1)
39
- query = iris_fg.select_all()
40
- feature_view = fs.create_feature_view(name="iris_modal",
41
- version=1,
42
- description="Read from Iris flower dataset",
43
- labels=["variety"],
44
- query=query)
45
-
46
- # You can read training data, randomly split into train/test sets of features (X) and labels (y)
47
- X_train, X_test, y_train, y_test = feature_view.train_test_split(0.2)
48
-
49
- # Train our model with the Scikit-learn K-nearest-neighbors algorithm using our features (X_train) and labels (y_train)
50
- model = KNeighborsClassifier(n_neighbors=2)
51
- model.fit(X_train, y_train.values.ravel())
52
-
53
- # Evaluate model performance using the features from the test set (X_test)
54
- y_pred = model.predict(X_test)
55
-
56
- # Compare predictions (y_pred) with the labels in the test set (y_test)
57
- metrics = classification_report(y_test, y_pred, output_dict=True)
58
- results = confusion_matrix(y_test, y_pred)
59
-
60
- # Create the confusion matrix as a figure, we will later store it as a PNG image file
61
- df_cm = pd.DataFrame(results, ['True Setosa', 'True Versicolor', 'True Virginica'],
62
- ['Pred Setosa', 'Pred Versicolor', 'Pred Virginica'])
63
- cm = sns.heatmap(df_cm, annot=True)
64
- fig = cm.get_figure()
65
-
66
- # We will now upload our model to the Hopsworks Model Registry. First get an object for the model registry.
67
- mr = project.get_model_registry()
68
-
69
- # The contents of the 'iris_model' directory will be saved to the model registry. Create the dir, first.
70
- model_dir="iris_model"
71
- if os.path.isdir(model_dir) == False:
72
- os.mkdir(model_dir)
73
-
74
- # Save both our model and the confusion matrix to 'model_dir', whose contents will be uploaded to the model registry
75
- joblib.dump(model, model_dir + "/iris_model.pkl")
76
- fig.savefig(model_dir + "/confusion_matrix.png")
77
-
78
-
79
- # Specify the schema of the model's input/output using the features (X_train) and labels (y_train)
80
- input_schema = Schema(X_train)
81
- output_schema = Schema(y_train)
82
- model_schema = ModelSchema(input_schema, output_schema)
83
-
84
- # Create an entry in the model registry that includes the model's name, desc, metrics
85
- iris_model = mr.python.create_model(
86
- name="iris_modal",
87
- metrics={"accuracy" : metrics['accuracy']},
88
- model_schema=model_schema,
89
- description="Iris Flower Predictor"
90
- )
91
-
92
- # Upload the model to the model registry, including all files in 'model_dir'
93
- iris_model.save(model_dir)
94
-
95
- if __name__ == "__main__":
96
- if LOCAL == True :
97
- g()
98
- else:
99
- with stub.run():
100
- f()