Spaces:
Sleeping
Sleeping
Commit ·
6f27897
1
Parent(s): 974b6cb
port config
Browse files
demo.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mlflow
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.datasets import load_iris
|
| 4 |
+
from sklearn.linear_model import LogisticRegression
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load Iris dataset
|
| 9 |
+
iris = load_iris()
|
| 10 |
+
|
| 11 |
+
# Split dataset into X features and Target variable
|
| 12 |
+
X = pd.DataFrame(data = iris["data"], columns= iris["feature_names"])
|
| 13 |
+
y = pd.Series(data = iris["target"], name="target")
|
| 14 |
+
|
| 15 |
+
# Split our training set and our test set
|
| 16 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y)
|
| 17 |
+
|
| 18 |
+
# Visualize dataset
|
| 19 |
+
X_train.head()
|
| 20 |
+
|
| 21 |
+
os.environ["APP_URI"] = "https://licorne2lc-mlflow.hf.space" # For demo purpose, teachers can use "https://antoinekrajnc-mlflow-server-demo.hf.space"
|
| 22 |
+
|
| 23 |
+
# Set your variables for your environment
|
| 24 |
+
EXPERIMENT_NAME="demo-mlflow-experiemnt"
|
| 25 |
+
|
| 26 |
+
# Set tracking URI to your Hugging Face application
|
| 27 |
+
mlflow.set_tracking_uri(os.environ["APP_URI"])
|
| 28 |
+
|
| 29 |
+
# Set experiment's info
|
| 30 |
+
mlflow.set_experiment(EXPERIMENT_NAME)
|
| 31 |
+
|
| 32 |
+
# Get our experiment info
|
| 33 |
+
experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)
|
| 34 |
+
|
| 35 |
+
# Call mlflow autolog
|
| 36 |
+
mlflow.sklearn.autolog()
|
| 37 |
+
|
| 38 |
+
with mlflow.start_run(experiment_id = experiment.experiment_id):
|
| 39 |
+
# Specified Parameters
|
| 40 |
+
c = 0.5
|
| 41 |
+
|
| 42 |
+
# Instanciate and fit the model
|
| 43 |
+
lr = LogisticRegression(C=c)
|
| 44 |
+
lr.fit(X_train.values, y_train.values)
|
| 45 |
+
|
| 46 |
+
# Store metrics
|
| 47 |
+
predicted_qualities = lr.predict(X_test.values)
|
| 48 |
+
accuracy = lr.score(X_test.values, y_test.values)
|
| 49 |
+
|
| 50 |
+
# Print results
|
| 51 |
+
print("LogisticRegression model")
|
| 52 |
+
print("Accuracy: {}".format(accuracy))
|
| 53 |
+
|
| 54 |
+
# Log Metric
|
| 55 |
+
mlflow.log_metric("Accuracy", accuracy)
|
| 56 |
+
|
| 57 |
+
# Log Param
|
| 58 |
+
mlflow.log_param("C", c)
|
| 59 |
+
|
| 60 |
+
# Log model
|
| 61 |
+
mlflow.sklearn.log_model(lr, "model")
|