CoreML – Building the model for Boston Prices

In the last post we have taken a look at the Boston Prices dataset loaded directly from Scikit-learn. In this post we are going to build a linear regression model and convert it to a .mlmodel to be used in an iOS app.

We are going to need some modules:

The cormeltools is the module that will enable the conversion to use our model in iOS.

Let us start by defining a main function to load the dataset:

In the code above we have loaded the dataset and created a pandas dataframe to hold the data and the names of the columns. As we mentioned in the previous post, we are going to use only the crime rate and the number of rooms to create our model:

Please note that we are separating the target variable from the predictor variables. Although this dataset in not too large, we are going to follow best practice and split the data into training and testing sets:

We will only use the training set in the creation of the model and will test with the remaining data points.

The line of code above assumes that we have defined the function gym_boston as follows:

Notice that we are using the LinearRegression implementation in Scikit-learn. Let us go back to the main function we are building and extract the coefficients for our linear model. Refer to the CoreML – Linear Regression post to remember that type of model that we are building is of the form  y=\alpha + \beta_1 x_1 + \beta_2 x_2 + \epsilon:

We can also take a look at some metrics that tell let us evaluate our model against the test data:

CoreML conversion

And now for the big moment: We are going to convert our model to an .mlmodel object!! Ready?

We are using the sklearn.convert method of coremltools.converters to create the my_model model with the necessary inputs (i.e. crime and rooms) and output ( price). Finally we save the model in a file with the name PriceBoston.mlmodel.

Et voilà! In the next post we will start creating an iOS app to use the model we have just built.

You can look at the code (in development) in my github site here.

Also published on Medium.

One thought on “CoreML – Building the model for Boston Prices

This site uses Akismet to reduce spam. Learn how your comment data is processed.