Hello,
I am trying to execute the code below and it is giving me an error, mainly because the Stata takes the command 'end' to end the python mode as a termination of the entire foreach loop:
gen Alpha = .
gen AUC = .
local i = 0
range alphas 0.0 1.0 20
foreach a in alphas {
i++
python:
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import metrics # import scikit-learn metrics module for accuracy calculation
from sfi import Data
import numpy as np
import pandas as pd
a = Data.get("a")
# predict using the best value for alpha
mnb = MultinomialNB(alpha = a, class_prior = None, fit_prior = True)
# calculate probability of each class on the test set
# '[:, 1]' at the end extracts the probability for each pharmacy to be under compliance
Y_mnb_score = mnb.fit(X_train, np.ravel(Y_train)).predict_proba(X_test)[:, 1]
# make test_compliance python variable
test_compliance = Y_test['compliance']
# transfer the python variables Y_mnb_score and test_compliance to STATA
Data.setObsTotal(len(Y_mnb_score))
Data.addVarFloat('mnbScore')
Data.store(var = 'mnbScore', obs = None, val = Y_mnb_score)
Data.setObsTotal(len(test_compliance))
Data.addVarFloat('testCompliance')
Data.store(var = 'testCompliance', obs = None, val = test_compliance)
end // this 'end' is causing a problem
roctab testCompliance mnbScore
replace AUC= r(area) in `i'
replace Alpha = `a’
} // loop not working
How can I use the Stata-python integration module inside of the foreach loop?
Thank you,
I am trying to execute the code below and it is giving me an error, mainly because the Stata takes the command 'end' to end the python mode as a termination of the entire foreach loop:
gen Alpha = .
gen AUC = .
local i = 0
range alphas 0.0 1.0 20
foreach a in alphas {
i++
python:
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import metrics # import scikit-learn metrics module for accuracy calculation
from sfi import Data
import numpy as np
import pandas as pd
a = Data.get("a")
# predict using the best value for alpha
mnb = MultinomialNB(alpha = a, class_prior = None, fit_prior = True)
# calculate probability of each class on the test set
# '[:, 1]' at the end extracts the probability for each pharmacy to be under compliance
Y_mnb_score = mnb.fit(X_train, np.ravel(Y_train)).predict_proba(X_test)[:, 1]
# make test_compliance python variable
test_compliance = Y_test['compliance']
# transfer the python variables Y_mnb_score and test_compliance to STATA
Data.setObsTotal(len(Y_mnb_score))
Data.addVarFloat('mnbScore')
Data.store(var = 'mnbScore', obs = None, val = Y_mnb_score)
Data.setObsTotal(len(test_compliance))
Data.addVarFloat('testCompliance')
Data.store(var = 'testCompliance', obs = None, val = test_compliance)
end // this 'end' is causing a problem
roctab testCompliance mnbScore
replace AUC= r(area) in `i'
replace Alpha = `a’
} // loop not working
How can I use the Stata-python integration module inside of the foreach loop?
Thank you,
Comment