0

我的问题是,我无法从 csv 文件中读取双精度,而只能读取整数。因此,当我尝试使用增长数组进行计算时,会出现错误“-infinity”和“NaN”:

    require 'csv'
    filename = 'file.csv'

    csv_data = CSV.read(filename, :col_sep => ";")
    csv_data.shift
    price1, price2 = csv_data.transpose.map { |a| a.map(&:to_i)}


    #calulate yields
    yields1 = Array.new
    yields2 = Array.new

    price1.each_cons(2) { |c| 
    yields1.push(Math.log(c[1]/c[0]))
     }

    price2.each_cons(2) { |c| 
    yields2.push(Math.log(c[1]/c[0]))
     }

    # Statistics

    module Enumerable

     def mean
      mean = self.reduce(:+)/self.length.to_f
      return mean
     end
    end

    puts "mean yields1 = " + yields1.mean.to_s

我的两个问题:

  1. 如何从 CSV 文件中读取 Doubles?
  2. 错误的错误在哪里?
4

1 回答 1

1

改变

price1, price2 = csv_data.transpose.map { |a| a.map(&:to_i)}

price1, price2 = csv_data.transpose.map { |a| a.map(&:to_f)}
于 2012-04-07T10:42:43.803 回答