[英]Call python Multivariate linear regression code from C++ Application
I am doing my research in networking and I want to implement linear regression on my own data set(numeric).我正在研究网络,我想在我自己的数据集(数字)上实现线性回归。 I used python
for multivariate linear regression using sklearn
library and now I want to embed/call that python
implementation from my c++
code.我使用python
使用sklearn
库进行多元线性回归,现在我想从我的c++
代码中嵌入/调用python
实现。
AS if I say that c++
is my application and python code is a service which provides services to my c++
code.... and I need pyhton code output in c++
code so I want to call python executable in c++
. AS if I say that c++
is my application and python code is a service which provides services to my c++
code.... and I need pyhton code output in c++
code so I want to call python executable in c++
.
can I do that?我可以这样做吗?
I search a lot as boost one is but I am not understanding how can I do... I used jupyter notebook
for python
and eclipse for c++.我搜索了很多,但我不明白我该怎么做...我使用jupyter notebook
python
和 eclipse c++。
x = final_tf[['LocalLoadHigh', 'LocalLoadLow', 'TransitLoadHigh',
'TransitLoadLow','phaseTotalBlocking', 'phaseTotalLocalBlocking', 'phaseTotalTransitBlocking', 'PBlockingLocalHigh',
'PBlockingLocalLow', 'PBlockingTransitHigh', 'PBlockingTransitLow',
'UtilizationHigh', 'UtilizationLow']]
# Create target variable, y
y = final_tf['WavelengthGroup']
# Import model
# Create linear regression object
lm = LinearRegression()
print(np.isfinite(x).all())
model = lm.fit(x,y)
predictions = lm.predict(x)
print(predictions)
lm.score(x,y)
lm.coef_
When you want different languages to collaborate, you have 2 possible ways.当您希望不同的语言进行协作时,您有两种可能的方式。
the easy way: external program and communication through pipes/files:最简单的方法:外部程序和通过管道/文件进行通信:
system
;你启动一个 python 解释器来执行你的 python 脚本,使用 fork/exec(在 Linux 上),spawn(在 Windows 上)或简单地使用system
; the python script reads the input file, apply the regression tools and save the results to an output file python 脚本读取输入文件,应用回归工具并将结果保存到 output 文件the hard way: embed a Python interpretor in your C++ program and use the Python API or a tool like swift or sip to manage the passing of data from one language to the other. the hard way: embed a Python interpretor in your C++ program and use the Python API or a tool like swift or sip to manage the passing of data from one language to the other.
This will certainly be more efficient (less overhead of launching processes and serializing data) but the way to embed Python as a dynamic library depends on the actual OS.这肯定会更有效(启动进程和序列化数据的开销更少),但是将 Python 作为动态库嵌入的方式取决于实际的操作系统。 Refer to the Python documentation for your system.请参阅您的系统的 Python 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.