I am creating a website which uses PHP on its back end. Now I am adding machine learning capabilities to my website which are accessed by HTTP methods GET and POST. But since sklearn is in Python, how do I enable my PHP code to call python based sklearn code? Or is there a way I can use some library to call Python code?
-
1What have you considered? – Maor Veitsman Oct 26 '17 at 06:46
-
@Maor Veitsman I am a beginner in this aspect. So, I am looking for starting directions. – Amazing Hacker Oct 26 '17 at 06:48
-
Possible Duplicate of [Running a Python script from PHP](https://stackoverflow.com/questions/19735250/running-a-python-script-from-php) – Sachin Oct 26 '17 at 06:55
-
Sameer Mahajan is right, you can use the [sklearn-porter](https://github.com/nok/sklearn-porter/), e.g. a [decision tree in PHP](https://github.com/nok/sklearn-porter/blob/master/examples/estimator/classifier/DecisionTreeClassifier/php/basics.ipynb). – Darius Jan 14 '18 at 18:41
2 Answers
One possible option for you to consider is: https://github.com/nok/sklearn-porter

- 484
- 1
- 8
- 27
I think you can achieve your goal, without calling python
code directly (embedding it) from PHP
code.
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 ...
let's say that you have built a machine learning model using sklearn
(possibly naive Bayes model), which predicts the probability of each category. 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.
For building API
in python
you can use: Flask-RESTful, or Django 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.

- 448
- 4
- 12