Python 获取名称为的mlflow实验的运行id?

Python 获取名称为的mlflow实验的运行id?,python,mlflow,Python,Mlflow,我目前在mlflow中创建了一个实验,并在实验中创建了多次运行 from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_squared_error import mlflow experiment_name="experiment-1" mlflow.set_experiment(experiment_name) no_of_trees=[100,200,300

我目前在mlflow中创建了一个实验,并在实验中创建了多次运行

from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
import mlflow

experiment_name="experiment-1"
mlflow.set_experiment(experiment_name)

no_of_trees=[100,200,300]
depths=[2,3,4]
for trees in no_of_trees:
    for depth in depths:
        with mlflow.start_run() as run:
            model=RandomForestRegressor(n_estimators=trees, criterion='mse',max_depth=depth)
            model.fit(x_train, y_train)
            predictions=model.predict(x_cv)
            mlflow.log_metric('rmse',mean_squared_error(y_cv, predictions))

创建跑步记录后,我想为这个实验获得最佳跑步id。现在,我可以通过查看mlflow的UI来获得最佳运行,但我们如何才能正确运行程序?

我们可以从实验名称中获得实验id,并且可以使用python API获得最佳运行

experiment_name = "experiment-1"
current_experiment=dict(mlflow.get_experiment_by_name(experiment_name))
experiment_id=current_experiment['experiment_id']
通过使用实验id,我们可以得到所有的运行,并可以根据如下指标对它们进行排序。在下面的代码中,rmse是我的度量名称(因此,基于度量名称,它可能会有所不同)

df = mlflow.search_runs([experiment_id], order_by=["metrics.rmse DESC"])
best_run_id = df.loc[0,'run_id']