Unable to export Random Forest model to ONNX format in Azure ML Studio

Liron Hagbi 20 Reputation points
2025-03-06T12:48:58.84+00:00

Hello, I am using Azure Machine Learning Studio and have built a Random Forest model. I would like to export this model to the ONNX format for use in an external development environment. However, I cannot find the option to export the model to ONNX within the studio. Could you please guide me on how to achieve this?

Azure AI Language
Azure AI Language
An Azure service that provides natural language capabilities including sentiment analysis, entity extraction, and automated question answering.
472 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vikram Singh 2,390 Reputation points Microsoft Employee
    2025-03-07T04:45:21.9666667+00:00

    Hi @Liron Hagbi

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you're having trouble exporting your Random Forest model to the ONNX format in Azure Machine Learning Studio. While Azure ML Studio doesn't provide a direct option to export Random Forest models to ONNX, you can achieve this using the sklearn-onnx package in Python. Here’s a step-by-step guide to help you:

    Step 1: Install Required Packages: First, you need to install the sklearn-onnx package. You can do this using pip:

    pip install skl2onnx onnxruntime
    

    Step 2: Convert the Model to ONNX: Next, you can convert your trained Random Forest model to the ONNX format using the sklearn-onnx package. Here’s an example:

    import skl2onnx
    from skl2onnx import convert_sklearn
    from skl2onnx.common.data_types import FloatTensorType
    import joblib
    
    # Load your trained Random Forest model
    model = joblib.load('path_to_your_model.pkl')
    
    # Define the initial type
    initial_type = [('float_input', FloatTensorType([None, model.n_features_in_]))]
    
    # Convert the model
    onnx_model = convert_sklearn(model, initial_types=initial_type)
    
    # Save the ONNX model
    with open("random_forest_model.onnx", "wb") as f:
        f.write(onnx_model.SerializeToString())
    

    Step 3: Verify the ONNX Model: You can verify the ONNX model using the onnxruntime package to ensure it works correctly:

    import onnxruntime as rt
    import numpy as np
    
    # Load the ONNX model
    sess = rt.InferenceSession("random_forest_model.onnx")
    
    # Prepare input data
    input_name = sess.get_inputs().name
    label_name = sess.get_outputs().name
    data = np.array([[...]], dtype=np.float32)  # Replace with your input data
    
    # Run the model
    pred = sess.run([label_name], {input_name: data})
    print(pred)
    

    For more detailed guidance, you can refer to the official Microsoft documentation:

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    If the reply was helpful, please don't forget to upvote and/or Accept the answer, this can be beneficial to other community members.

    Thanks


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.