Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
Created October 9, 2012 23:37
Show Gist options
  • Save jmdeldin/3862171 to your computer and use it in GitHub Desktop.
Save jmdeldin/3862171 to your computer and use it in GitHub Desktop.
def add_vectors(x, y); x.zip(y).map { |xj, yj| xj + yj }; end
def mult_scalar_to_vector(x, vec); vec.map { |v| v * x }; end
def dot_product(vec1, vec2)
vec1.zip(vec2).map { |v1, v2| v1 * v2 }.reduce(:+)
end
# Hypothesis function
#
# @param [Array] w Array of weights
# @param [Array] x Array of data
#
# @return [Integer]
def h(w, x)
dot_product(w, x) >= 0 ? 1 : 0
end
# Perceptron training algorithm.
#
# @param [Array] w Array of weights
# @param [Array] x Matrix of weights
# @param [Array] y Array of classes
# @param [Float] alpha Learning rate
#
# @return [Array] Weights that correctly classify the input
def perceptron(w, x, y, alpha)
iters = 0
converged = false
until converged
errors = 0
iters += 1
x.each_with_index do |xj, j|
hw = h(w, xj)
if y[j] != hw
w = add_vectors w, mult_scalar_to_vector(alpha * (y[j] - hw), xj)
p w
errors += 1
end
end
break if errors == 0
end
[w, iters]
end
if $0 == __FILE__
w = [0, 2, 1]
x = [
[1, 2, 1],
[2, 2, 1],
[4, 1, 1],
[5, 1, 1],
]
y = [1, 0, 1, 0]
puts "Initial Weights = #{w.inspect}"
wf, iters = perceptron(w, x, y, 0.5)
puts "\nFinal Weight = #{wf.inspect}"
puts "Converged after #{iters} iterations"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment