简体   繁体   English

从 C++ 应用程序调用 python 多元线性回归代码

[英]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.当您希望不同的语言进行协作时,您有两种可能的方式。

  1. the easy way: external program and communication through pipes/files:最简单的方法:外部程序和通过管道/文件进行通信:

    • you prepare the input data in your C++ program and save it to a file您在 C++ 程序中准备输入数据并将其保存到文件中
    • you launch a python interpretor to execute your python script with fork/exec (on Linux), spawn (on Windows) or simply with 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 文件
    • you read the output file from your C++ application您从 C++ 应用程序中读取 output 文件
    • alternatively, you could use pipes instead of plain files或者,您可以使用管道而不是普通文件
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM