class Datasets::Communities

Constants

Record

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/datasets/communities.rb, line 138
def initialize
  super()
  @metadata.id = "communities"
  @metadata.name = "Communities"
  @metadata.url = "https://archive.ics.uci.edu/ml/datasets/communities+and+crime"
  @metadata.description = lambda do
    read_names
  end
end

Public Instance Methods

each() { |record| ... } click to toggle source
# File lib/datasets/communities.rb, line 148
def each
  return to_enum(__method__) unless block_given?

  open_data do |csv|
    csv.each do |row|
      row = row.collect.with_index do |column, i|
        if column == "?"
          nil
        else
          case i
          when 3 # communityname
          # when 124 # LemasGangUnitDeploy
          # 0 means NO, 1 means YES, 0.5 means Part Time
          else
            column = Float(column)
          end
          column
        end
      end
      record = Record.new(*row)
      yield(record)
    end
  end
end

Private Instance Methods

base_url() click to toggle source
# File lib/datasets/communities.rb, line 174
def base_url
  "https://archive.ics.uci.edu/ml/machine-learning-databases/communities"
end
open_data() { |csv| ... } click to toggle source
# File lib/datasets/communities.rb, line 178
def open_data
  data_path = cache_dir_path + "communities.data"
  unless data_path.exist?
    data_url = "#{base_url}/communities.data"
    download(data_path, data_url)
  end
  CSV.open(data_path) do |csv|
    yield(csv)
  end
end
read_names() click to toggle source
# File lib/datasets/communities.rb, line 189
def read_names
  names_path = cache_dir_path + "communities.names"
  unless names_path.exist?
    names_url = "#{base_url}/communities.names"
    download(names_path, names_url)
  end
  names_path.read
end