機械学習超入門III 〜機械学習の基礎、パーセプトロンを30分で作って学ぶ〜 を読んでRubyで書いてみました。 以下ソースコード module MachineLearning class Perceptron attr_reader :w def initialize w={} @w = w end def predict vector_x vector_x.reduce(0) do |y, v| k, x = v (w[k]) ? y + (w[k]*x) : y end end def train vector_x, t y = predict(vector_x) return unless (y*t) < 0 vector_x.each do |k, x| w[k] += t * x end end end end # 訓練データ # 暖色 => 1 # 寒色 => -1 trai