An implementation of the Core ML framework using a linear regression model from sklearn and using the iris dataset. Obviously the iris dataset is not a linear regression, but currently classification is not supported. This model is largely based on the Apple provided Mars example, except implementing the iris dataset. I will also post how to actually integrate the model in Xcode (it's really easy) as well as export the model using Juypter Notebook.
You need the following in order to create your own model and integrate it into your app (if you're using SKLearn):
- Xcode 9 Beta:https://developer.apple.com/download/ (you probably need a developer log in here, but you do not need a PAID developer account)
- Numpy, Scipy(sklearn), Pandas 2a. I recommend using Jupyter Notebook, but you don't have to.
- Python coremltools library: http://pythonhosted.org/coremltools/
- You can generally follow the Python script I have here in the project. Depending on your model you'll have to tweak some things, but the pattern is the same. After you fit your model, convert and save the model using coremltools. I highly recommend adding things like descriptions to your variables (as shown in my Python example), as well as not using any sort of crazy names in your variable names. That may give you trouble.
- Once you have saved your generated .mlmodel file, open Xcode, and then drag the file into your project folder. Xcode will automatically generate the model in Swift.
- Instantiate an instance of the model. In this example, I just did let variableName = Iris() and you're good to go. Now you can excute the class methods.
WTF: My python stuff is all messed up
Answer: Use Homebrew as your package manager
WTF: My Core ML model can't be found?
Answer: In the beta, for some reason right now the coremltools generated file in Xcode sometimes bugs out and doesn't show up. Clean your project and restart Xcode. You can view the Xcode generated model by clicking on the model, then under Model Class selecting the -> next to Swift Generated Code. You can change the method names, but if you don't you'll need to use those method names to instantiate the generated class. If you can't click, or no arrow is present with words saying (Swift generated source) then you're Xcode is bugging out and needs to be restarted. So far I don't think a clean and build fixes it, so I just restart xcode and then there's no issue. This will be fixed at a later date I'm sure.