微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将ruby转换为python

我正在重写一些从RubyPython代码.该代码适用于Perceptron的第8.2.6节中列出的Perceptron.我之前从未使用过Ruby,我不理解这一部分:

def test_weights(weights, domain, num_inputs)
  correct = 0
  domain.each do |pattern|
    input_vector = Array.new(num_inputs) {|k| pattern[k].to_f}
    output = get_output(weights, input_vector)
    correct += 1 if output.round == pattern.last
  end
  return correct
end

一些解释:num_inputs是一个整数(在我的例子中是2),domain是一个数组列表:[[1,0,1],[0,0,0]等]

我不明白这一行:

input_vector = Array.new(num_inputs) {|k| pattern[k].to_f}

它创建一个包含2个值的数组,每个值| k |存储模式[k] .to_f,但什么是模式[k] .to_f?

解决方法:

试试这个:

input_vector = [float(pattern[i]) for i in range(num_inputs)]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐