Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
@@ -7,4 +7,58 @@ sdk: docker
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
10 |
+
```
|
11 |
+
import mlflow
|
12 |
+
import numpy as np
|
13 |
+
from sklearn.linear_model import LinearRegression
|
14 |
+
from sklearn.metrics import mean_squared_error
|
15 |
+
from sklearn.datasets import make_regression
|
16 |
+
from sklearn.model_selection import train_test_split
|
17 |
+
|
18 |
+
# Set the tracking URI to point to your MLflow server
|
19 |
+
mlflow.set_tracking_uri("https://subhrajit-mohanty-mlflow.hf.space")
|
20 |
+
|
21 |
+
# Create an experiment or use an existing one
|
22 |
+
experiment_name = "file-store-experiment"
|
23 |
+
try:
|
24 |
+
experiment_id = mlflow.create_experiment(experiment_name)
|
25 |
+
except:
|
26 |
+
experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
|
27 |
+
|
28 |
+
# Set the experiment
|
29 |
+
mlflow.set_experiment(experiment_name)
|
30 |
+
|
31 |
+
# Create a simple dataset
|
32 |
+
X, y = make_regression(n_samples=100, n_features=5, noise=0.1, random_state=42)
|
33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
34 |
+
|
35 |
+
# Start an MLflow run
|
36 |
+
with mlflow.start_run(run_name="metrics_only_example"):
|
37 |
+
# Log parameters
|
38 |
+
mlflow.log_param("model_type", "LinearRegression")
|
39 |
+
mlflow.log_param("n_features", X.shape[1])
|
40 |
+
mlflow.log_param("n_samples", X.shape[0])
|
41 |
+
|
42 |
+
# Train the model
|
43 |
+
model = LinearRegression()
|
44 |
+
model.fit(X_train, y_train)
|
45 |
+
|
46 |
+
# Make predictions and evaluate
|
47 |
+
y_pred = model.predict(X_test)
|
48 |
+
mse = mean_squared_error(y_test, y_pred)
|
49 |
+
|
50 |
+
# Log metrics only (avoiding artifact storage)
|
51 |
+
mlflow.log_metric("mse", mse)
|
52 |
+
mlflow.log_metric("r2_score", model.score(X_test, y_test))
|
53 |
+
|
54 |
+
# Log coefficients as parameters instead of storing the model
|
55 |
+
for i, coef in enumerate(model.coef_):
|
56 |
+
mlflow.log_param(f"coef_{i}", coef)
|
57 |
+
mlflow.log_param("intercept", model.intercept_)
|
58 |
+
|
59 |
+
print(f"Run completed with MSE: {mse:.4f}")
|
60 |
+
print(f"Metrics and parameters have been logged to MLflow")
|
61 |
+
print(f"Run ID: {mlflow.active_run().info.run_id}")
|
62 |
+
```
|
63 |
+
|
64 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|