Machine learning for Ruby
Machine learning for Ruby
Check out this post for more info on machine learning with Rails
Add this line to your application’s Gemfile:
gem "eps"On Mac, also install OpenMP:
brew install libompCreate a model
data = [
{bedrooms: 1, bathrooms: 1, price: 100000},
{bedrooms: 2, bathrooms: 1, price: 125000},
{bedrooms: 2, bathrooms: 2, price: 135000},
{bedrooms: 3, bathrooms: 2, price: 162000}
]
model = Eps::Model.new(data, target: :price)
puts model.summaryMake a prediction
model.predict(bedrooms: 2, bathrooms: 1)Store the model
File.write("model.pmml", model.to_pmml)Load the model
pmml = File.read("model.pmml")
model = Eps::Model.load_pmml(pmml)A few notes:
predict to make multiple predictions at onceOften, the goal of building a model is to make good predictions on future data. To help achieve this, Eps splits the data into training and validation sets if you have 30+ data points. It uses the training set to build the model and the validation set to evaluate the performance.
If your data has a time associated with it, it’s highly recommended to use that field for the split.
Eps::Model.new(data, target: :price, split: :listed_at)Otherwise, the split is random. There are a number of other options as well.
Performance is reported in the summary.
Typically, the best way to improve performance is feature engineering.
Features are extremely important for model performance. Features can be:
For numeric features, use any numeric type.
{bedrooms: 4, bathrooms: 2.5}For categorical features, use strings or booleans.
{state: "CA", basement: true}Convert any ids to strings so they’re treated as categorical features.
{city_id: city_id.to_s}For dates, create features like day of week and month.
{weekday: sold_on.strftime("%a"), month: sold_on.strftime("%b")}For times, create features like day of week and hour of day.
{weekday: listed_at.strftime("%a"), hour: listed_at.hour.to_s}For text features, use strings with multiple words.
{description: "a beautiful house on top of a hill"}This creates features based on word count.
You can specify text features explicitly with:
Eps::Model.new(data, target: :price, text_features: [:description])You can set advanced options with:
text_features: {
description: {
min_occurrences: 5, # min times a word must appear to be included in the model
max_features: 1000, # max number of words to include in the model
min_length: 1, # min length of words to be included
case_sensitive: true, # how to treat words with different case
tokenizer: /\s+/, # how to tokenize the text, defaults to whitespace
stop_words: ["and", "the"] # words to exclude from the model
}
}We recommend putting all the model code in a single file. This makes it easy to rebuild the model as needed.
In Rails, we recommend creating a app/ml_models directory. Be sure to restart Spring after creating the directory so files are autoloaded.
bin/spring stopHere’s what a complete model in app/ml_models/price_model.rb may look like:
class PriceModel We recommend adding monitoring and guardrails as well if you retrain automatically
Create an Active Record model to store the predictive model.
```sh
rails generate model Model key:string:uniq data:textStore the model with:
store = Model.where(key: "price").first_or_initialize
store.update(data: model.to_pmml)Load the model with:
data = Model.find_by!(key: "price").data
model = Eps::Model.load_pmml(data)You can use IRuby to run Eps in Jupyter notebooks. Here’s how to get IRuby working with Rails.
Specify a weight for each data point
Eps::Model.new(data, weight: :weight)You can also pass an array
Eps::Model.new(data, weight: [1, 2, 3])Weights are supported for metrics as well
Eps.metrics(actual, predicted, weight: weight)Reweighing is one method to mitigate bias in training data
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
To get started with development:
git clone https://github.com/ankane/eps.git
cd eps
bundle install
bundle exec rake testNo open issues yet, or sync has not completed.