Creating And Fitting A Regression Model In Julia | by Emmett Boudreau | Towards Data Science

Lathe 0.0.9 has just been merged to the Master branch, and with it comes a host of new features that will certainly be exciting to show off. Machine-learning in Julia is now easier than it has ever been before. The only issues currently facing machine-learning in Julia are the lack of a Python-size ecosystem, and the lack of documentation. Fortunately, for Lathe users there are a lot of features and tools at our disposal, and even better:
Lathe has good documentation!
I found another CSV data-set to work with online. To load this CSV into Julia, we will need the first of our three dependencies, CSV.jl. So with that in mind, the first step is to jump into the Pkg REPL, by first entering the Julia REPL, and then pressing ]. From this REPL we can add our three dependencies:
julia> ]pkg> add "Lathe"#Unstablepkg> add "DataFrames"pkg> add "CSV"
Now that we have CSV.jl on our system, we can bring it into Julia with using, and read in our data with the CSV.read() method:
using CSVdf = CSV.read("weatherHistory.csv")
There are a lot of options with this data-set for both continuous, and categorical features, which will first and foremost allow me to show off Lathe.preprocess’s newest method,
One Hot Encode
The first thing I decided to do is separate the data into two data-frames, one that I would use for our model, and the other will be used to show off the one hot encoder. I did this by first doing
show(df, allcols = true)
and then secondly I constructed two new DataFrame types with the corresponding features:
using DataFrameshotdf = DataFrame(:Humidity => df[:Humidity], :Type => df[Symbol("Precip Type")])df = DataFrame(:Humidity => df[:Humidity],:WindSpeed => df[Symbol("Wind Speed (km/h)")])
Just for the sake of this example, I’m going to pretend that I have no idea how to use Lathe in order to show-off the in-code documentation in Lathe. Firstly, of course we’ll need to load Lathe with using:
using Lathe
And then we can use Julia’s ?() method on the package itself:
?(Lathe)|====== Lathe - Easily ML =====|= = = = = v. 0.0.9 = = = = = ||==============================|__Lathe.stats|__Lathe.validate|__Lathe.preprocess|__Lathe.models|__Use ?(Lathe.package) for information![uuid]38d8eb38-e7b1-11e9-0012-376b6c802672[deps]DataFrames.jlRandom.jl
From this, we are greeted with a module tree which displays three modules:
Lathe.stats
Lathe.validate
Lathe.preprocess
Lathe.models
For a one hot encoder, we’re certainly going to be looking into Lathe.preprocess. The doc-string even clearly states that we can use ?(Lathe.package) for more information, so let’s try that!
And now we can see the OneHotEncode() function inside of the module tree, so let’s do the same on it:
?(Lathe.preprocess.OneHotEncode)
From this we get a brief description, parameters, and even usage. It looks like the function takes a data-frame, along with a symbol representing the column we wish to encode. This of course works about how you would anticipate:
Off-hand methods aside, let’s get back into our original data-frame with our two features, Humidity and Wind Speed. Since the data-set had no missing values on this particular column, and I’m not going to be scaling any features, I decided the first thing to do was go ahead and TrainTestSplit:
using Lathe.preprocess: TrainTestSplittrain, test = TrainTestSplit(df)
This will return two new data-frames, train will account for 75 percent of the data, whereas test will account for the remaining 25. We can adjust this by adding a float percentage after df:
train, test = TrainTestSplit(df, .5)
This would split the data evenly.
For me personally, I like to assign all of my data to variables that can easily be changed prior to even thinking about doing a model. Here is how you do that in Julia:
x = :Humidityy = :WindSpeedtrainX = train[x]trainy = train[y]testX = test[x]testy = test[y]
Next up, we just need to import our model, our prediction function, and then we can construct and fit!
using Lathe.models: predict, LinearRegressionmodel = LinearRegression(trainX, trainy)yhat = predict(model, testX)
And here is the result:
That’s really how easy it is! How can you be upset with that? I’m excited for where Lathe will be in the future and how the module could potential effect Julia in general as a whole. As of right now, Lathe is probably the easiest ML package that you could ever use, and I think most of the learning curve stems from using DataFrames.jl, not Lathe. Overall I’m very excited for what is to come of Lathe in the next version, 0.1.0, and beyond!