Sha256: 5e5b21028c907b9eba3e6ba160d092082db863000505e02de4c7b51830d20d27

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require "csv"

require_relative "dataset"

module Datasets
  class Iris < Dataset
    Record = Struct.new(:sepal_length,
                        :sepal_width,
                        :petal_length,
                        :petal_width,
                        :class)

    def initialize
      super()
      @metadata.name = "iris"
      @metadata.url = "https://archive.ics.uci.edu/ml/datasets/Iris"
      @metadata.description = lambda do
        read_names
      end
    end

    def each
      return to_enum(__method__) unless block_given?

      open_data do |csv|
        csv.each do |row|
          next if row[0].nil?
          record = Record.new(*row)
          yield(record)
        end
      end
    end

    private
    def open_data
      data_path = cache_dir_path + "iris.csv"
      unless data_path.exist?
        data_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
        download(data_path, data_url)
      end
      CSV.open(data_path, converters: [:numeric]) do |csv|
        yield(csv)
      end
    end

    def read_names
      names_path = cache_dir_path + "iris.names"
      unless names_path.exist?
        names_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.names"
        download(names_path, names_url)
      end
      names_path.read
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
red-datasets-0.0.5 lib/datasets/iris.rb
red-datasets-0.0.4 lib/datasets/iris.rb
red-datasets-0.0.3 lib/datasets/iris.rb
red-datasets-0.0.2 lib/datasets/iris.rb
red-datasets-0.0.1 lib/datasets/iris.rb