I have tried this link to embed Python function in C++ application. https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code I am using Spyder for Python version 3.5 and Eclipse for c++ on Ubuntu Operating System. The program that is mentioned in this link is working properly but when I changed function then it is not accessing my function/program. Previous Python function includes:
def getInteger():
print(‘Python function getInteger() called’)
c = 100*50/30
return c
and the client C++ code:
#include
#include
#include
#include “Python.h”
#include
using namespace std;
int main() {
CPyInstance hInstance;
PySys_SetPath( L".:res/scripting:/home/madiha");
CPyObject pModule = PyImport_Import(PyUnicode_FromString(“pyemb3”));
if(pModule)
{
CPyObject pFunc = PyObject_GetAttrString(pModule, “getInteger”);
if(pFunc && PyCallable_Check(pFunc))
{
CPyObject pValue = PyObject_CallObject(pFunc, NULL);
printf("C: getInteger() = %ld
", PyLong_AsLong(pValue));
}
else
{
printf("ERROR: function getInteger()
");
}
}
else
{
printf("ERROR: Module not imported
");
}
if(!getchar()) getchar();
return 0;
}
is shows output as:
Python function getInteger() called
C: getInteger() = 166
now i am changing code of Python function “getInteger” with the below code. and it is not accessing my function… no syntax error shown,…just go to else phase and shows “modue not imported”…although my python function is working correctly when i run it separately…
#!/usr/bin/env python
coding: utf-8
In[ ]:
Import model
import pandas as pd
from sklearn.linear_model import LinearRegression
import pickle
Read data
df = pd.read_csv(‘/home/madiha/Final_DataSet.csv’)
tf = df
final_tf = pd.DataFrame(tf)
tf = df
final_tf = pd.DataFrame(tf)
Create features variable, x
x_train = final_tf[[‘LocalLoadHigh’, ‘LocalLoadLow’, ‘TransitLoadHigh’,
‘TransitLoadLow’,‘phaseTotalBlocking’, ‘phaseTotalLocalBlocking’, ‘phaseTotalTransitBlocking’, ‘PBlockingLocalHigh’,
‘PBlockingLocalLow’, ‘PBlockingTransitHigh’, ‘PBlockingTransitLow’,
‘UtilizationHigh’, ‘UtilizationLow’]]
# Create target variable, y
y_train = final_tf[‘WavelengthGroup’]
Create linear regression object
lm = LinearRegression()
model = lm.fit(x_train,y_train)
def getInteger():
save the model to disk
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
df = pd.read_csv('/home/madiha/workspace/IBKSIM/OneRowRegressionFiles/15_s1packetscheduler0.csv')
tf = df
final_tf = pd.DataFrame(tf)
# Create features variable, x
x_test = final_tf[['LocalLoadHigh', 'LocalLoadLow', 'TransitLoadHigh',
'TransitLoadLow','phaseTotalBlocking', 'phaseTotalLocalBlocking', 'phaseTotalTransitBlocking', 'PBlockingLocalHigh',
'PBlockingLocalLow', 'PBlockingTransitHigh', 'PBlockingTransitLow',
'UtilizationHigh', 'UtilizationLow']]
# load the model from disk
loaded_model = pickle.load(open(filename, 'rb'))
predictions = loaded_model.predict(x_test)
print(predictions)
return predictions
i want to return this prediction value back to main C++ Project