问题
I am developing an Android app that uses sensor data from the phone to classify activities. I also really prefer scikit-learn to any of the Java machine learning libraries. So I created a very minimal REST api using Django and scikit learn to train sensor data using support vector machines and return model information.
My question is this: how can I use the model scikit-learn produces on my phone to make predictions? So far I've considered extending the api so that whenever the phone wants to make a prediction, it sends the data to the api to get one. But I'd much rather be able to write some Java code or use a Java library to do the predicting. Sending data for training to the api isn't a problem, for that's not done in real time --- it's only done when the data has already been collected. Sending data for real-time predictions doesn't seem workable, however.
Doing this with logistic regression is a lot easier as the prediction formula and model parameters are pretty simple; I could abandon svms and use this instead, but I'd also like to have svms available.
Anyone aware of someone doing this before? Is there a doable-in-a-relatively-short-time-by-someone-without-a-PhD-in-numerical-computing-or-machine-learning way to do this? Detailed steps aren't necessary, just an outline of how to use the components of the svm that scikit-learn produces.
回答1:
Most of packages with SVM (scikit-learn too) rely on libsvm implementation. But you don't need 99% of code from libsvm and you don't have to be PhD, because you already have all parameters after learning inside scikit-learn. All what you need - any simple linear algebra library (only for vector*vector operation) in java to implement a decision function.
If you are using linear kernel in SVC - it's relatively easy, because scikit-learn automatically converts all those complicated dual coefficients and support vectors into simple hyperplane coefficients, thus decision function becomes equivalent to logistic regression, all what you need here - dot product - look here Exporting SVM classifiers from sklearn to Java codebase
In case with non-linear kernel - again only decision function is needed, but now you have to understand what is support vectors, what is dual coefficients, what is kernel, and you have to implement your non-linear kernel in java. I think that it's not easy task to implement decision function for non-linear SVC without understanding how SVC optimization process works, i'll give you some links:
- Layout of SVC attributes
- Decision Function
- Where all these dual coefficients and support vectors come from
Or you can just find any SVM library for java and learn model with same parameters you choose in SVC (C, eps, etc). I think it's easiest solution for non-linear kernels. SVM is well-known method, and i think that learning with same parameters and dataset will give same results on any good implementation (besides that most of implementations and bindings, as i said, rely on libsvm, in this case equality is guaranteed).
来源:https://stackoverflow.com/questions/33535103/using-trained-scikit-learn-svm-classifiers-in-android