简体   繁体   English

如何在我的 PHP 后端代码中嵌入 sklearn 代码?

[英]How do I embed sklearn code in my PHP backend code?

I am creating a website which uses PHP on its back end.我正在创建一个在其后端使用 PHP 的网站。 Now I am adding machine learning capabilities to my website which are accessed by HTTP methods GET and POST.现在我正在向我的网站添加机器学习功能,这些功能可以通过 HTTP 方法 GET 和 POST 访问。 But since sklearn is in Python, how do I enable my PHP code to call python based sklearn code?但是由于 sklearn 是在 Python 中,我如何使我的 PHP 代码能够调用基于 Python 的 sklearn 代码? Or is there a way I can use some library to call Python code?或者有没有办法可以使用某个库来调用 Python 代码?

您可以考虑的一种可能选择是: https : //github.com/nok/sklearn-porter

I think you can achieve your goal, without calling python code directly (embedding it) from PHP code.我认为您可以实现您的目标,而无需从PHP代码直接调用(嵌入) python代码。

For example, let's suppose you have a web application, which is built using PHP , and your application contains Articles , and you want to add the feature of categorizing (tagging/classification) of those articles into some categories: News, Sport, Medical, Science, etc ...例如,假设您有一个使用PHP构建的 Web 应用程序,并且您的应用程序包含文章,并且您想添加将这些文章分类(标记/分类)到某些类别中的功能:新闻、体育、医疗、科学等...

let's say that you have built a machine learning model using sklearn (possibly naive Bayes model), which predicts the probability of each category.假设您已经使用sklearn (可能是朴素贝叶斯模型)构建了一个机器学习模型,它可以预测每个类别的概率。 and you want to use this model within your application.并且您想在您的应用程序中使用此模型。

Now, you can export this model as an API , and make other applications use it, in our case, your web application will send the Article to this API , and get a list of predictions for each category.现在,您可以将此模型导出为API ,并让其他应用程序使用它,在我们的示例中,您的 Web 应用程序会将Article发送到此API ,并获取每个类别的预测列表。

For building API in python you can use: Flask-RESTful , or Django REST要在python构建API ,您可以使用: Flask-RESTfulDjango REST

While this approach has some disadvantages , I think it's much better to design your application(s) with modularity in mind, keeping each service (functionality) separate from the other, makes your system more loosely coupled .虽然这种方法有一些缺点,但我认为设计您的应用程序时考虑到模块化要好得多,将每个服务(功能)彼此分开,使您的系统更加松散耦合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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