Selectbox `format_func` with classes

I have the following code where I would like the user to choose 3 machine learning models. I want the UI to look nice, so instead of having to select the class name, I want to format all the options like so:

records = [
        {'model':LogisticRegression(),'name':'Logistic Regression'},
        {'model':DecisionTreeClassifier(),'name':'Decision Tree Classifier'}
]

with st.sidebar:
    with st.form('Form1'):
        selection1 = st.selectbox(
            'Model 1', options=records,
            format_func=lambda record: f'{record["name"]}'
        )
        model1 = selection1.get('model')

        selection2 = st.selectbox(
            'Model 2', options=records,
            format_func=lambda record: f'{record["name"]}'
        )
        model2 = selection2.get('model')

        selection3 = st.selectbox(
            'Model 3', options=records,
            format_func=lambda record: f'{record["name"]}'
        )
        model3 = selection3.get('model')

        submitted = st.form_submit_button('Run')

if not submitted:
    st.write('foo')

else:
    st.write(
        model1,model2,model3
    )

However, I get an ValueError when trying this

ValueError: {'model': LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='warn', n_jobs=None, penalty='l2',
                   random_state=None, solver='warn', tol=0.0001, verbose=0,
                   warm_start=False), 'name': 'Logistic Regression'} is not in iterable

I’ve done this before except I didn’t uses classes. For example, If I change everything to a string, like so

records = [
        {'model':'LogisticRegression()','name':'Logistic Regression'},
        {'model':'DecisionTreeClassifier()','name':'Decision Tree Classifier'}
]

The code runs, but I obviously can’t use those models since they are just strings. So my question is, how can I format classes within a selectbox? Again, I don’t want

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='warn', n_jobs=None, penalty='l2',
                   random_state=None, solver='warn', tol=0.0001, verbose=0,
                   warm_start=False)

to be an option in the selectbox, but rather just

Logistic Regression

and then have the selection assigned to the class.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.