Verified import paths — ran on the pinned version, not inferred.
Train a decision tree and make a prediction
require 'decisiontree'
# Example: predict whether to play tennis based on weather
attributes = ['outlook', 'temperature', 'humidity', 'wind']
training = [
['sunny', 'hot', 'high', 'weak', 'no'],
['sunny', 'hot', 'high', 'strong', 'no'],
['overcast', 'hot', 'high', 'weak', 'yes'],
['rainy', 'mild', 'high', 'weak', 'yes'],
['rainy', 'cool', 'normal', 'weak', 'yes'],
['rainy', 'cool', 'normal', 'strong', 'no'],
['overcast', 'cool', 'normal', 'strong', 'yes'],
['sunny', 'mild', 'high', 'weak', 'no'],
['sunny', 'cool', 'normal', 'weak', 'yes'],
['rainy', 'mild', 'normal', 'weak', 'yes'],
['sunny', 'mild', 'normal', 'strong', 'yes'],
['overcast', 'mild', 'high', 'strong', 'yes'],
['overcast', 'hot', 'normal', 'weak', 'yes'],
['rainy', 'mild', 'high', 'strong', 'no']
]
dec_tree = DecisionTree::ID3Tree.new(attributes, training, 'no', :continuous)
dec_tree.train
test = ['sunny', 'hot', 'high', 'weak']
prediction = dec_tree.predict(test)
puts prediction