4 Replies
      Latest reply on Feb 29, 2020 5:48 AM by asrichardson
      SAIK1065 Level 1 Level 1 (0 points)

        Hey everyone,

        So I have creted a classifier using scikit learn. However, the data model I get is a .pkl (Pickle) file. I am wondering if Core ML supports the conversion of .pkl files or only .csv files. Also, if Core ML does not support .pkl files, how can I convert my file into a .csv file? Thanks!

         

        EDIT: So basically I would like to know if I can convert .pkl files to a .mlmodel and how can I do so

        • Re: Core ML Supports Scikit-learn?
          Brent_ Apple Staff Apple Staff (0 points)

          Core ML supports models converted from scikit-learn.  The coremltools documentation has an example of converting a scikit-learn model. Here is the code from that documentation:

           

          from sklearn.linear_model import LinearRegression
          import pandas as pd
          
          # Load data
          data = pd.read_csv('houses.csv')
          
          # Train a model
          model = LinearRegression()
          model.fit(data[["bedroom", "bath", "size"]], data["price"])
          
          # Convert and save the scikit-learn model
          import coremltools
          coreml_model = coremltools.converters.sklearn.convert(model,
                                                              ["bedroom", "bath", "size"],
                                                              "price")
          
          coreml_model.save('HousePricer.mlmodel')
          
          
          
          

           

          Hope this helps!

          • Re: Core ML Supports Scikit-learn?
            srikris Apple Staff Apple Staff (30 points)

            Pickle is a Python protocol that is used to seralize arbitary Python code and classes. You should be able to write code to load the scikit-learn model from your pickle file (that code is usually custom to your model) and then you can convert the scikit-learn model to CoreML format using coremltools.

            • Re: Core ML Supports Scikit-learn?
              SAIK1065 Level 1 Level 1 (0 points)

              I know how to convert a model to a Core ML model but what I can't figure out is how to write code to load the scikit-learn model from the pickle file. The model I am using comes from this tutorial: http://radimrehurek.com/data_science_python/

              At the end of the tutorial: I load the model with this command:

              svm_detector_reloaded = cPickle.load(open('sms_spam_detector.pkl'))
              
              

              What I don't know is how to take this file and turn it into a Scikit-Learn file. Thanks!