Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stata-Python interface sfi.setLocal is not working

    Hello,

    I am trying to fit a naive Bayes model on Stata dataset by using the Stata-Python interface.
    I am trying to use the -setLocal- Macro of the Python sfi module to transfer the value of best_alpha into Stata, but it seems that the value isn't being transferred, although there is no error message.
    My code are below:

    Code:
    // enter the python mode
    python:
    
    parameters = [{'alpha':np.linspace(0.1,4.6,10)}]
    alpha_grid = parameters[0]['alpha'].tolist()
    alpha_str = ' '.join(str(e) for e in alpha_grid)
    Macro.setLocal("alphaGrid",alpha_str)
    
    # running the grid search on the dataset X_train and Y_train, based on the roc_auc scoring criteria.
    # there are other options for scoring such as f1 scores, etc.
    clf = GridSearchCV(MultinomialNB(), parameters, cv=10, scoring='accuracy')
    clf.fit(X_train, np.ravel(Y_train))
    
    # find the best value for alpha
    best_alpha = clf.best_params_['alpha'] 
    
    best_alpha
    >> 0.1
    
    ##### transfer the python variable best_alpha as STATA local macro 'bestAlpha'
    Macro.setLocal('bestAlpha', 'best_alpha')
    
    end
    
    // back to Stata mode
    // the value of bestAlpha ( = 0.1) is not displaying
    display "Best alpha " `bestAlpha'
    What am I doing wrong here?

    Thank you,

    PS: Macro.setLocal documentation can be found here: https://www.stata.com/python/api16/Macro.html

  • #2
    Code:
    python:
    
    from sfi import Macro, Scalar
     
    best_alpha = 0.1
    
    Macro.setLocal('bestAlpha', str(best_alpha) )
    
    Scalar.setValue('bestAlpha', best_alpha )
    
    end
    
    macro li
    scalar di

    Comment

    Working...
    X