Module: DNN::Iris

Defined in:
lib/dnn/datasets/iris.rb

Constant Summary collapse

URL_CSV =
"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
SETOSA =

Iris-setosa

0
VERSICOLOR =

Iris-versicolor

1
VIRGINICA =

Iris-virginica

2

Class Method Summary collapse

Class Method Details

.downloadsObject



17
18
19
20
# File 'lib/dnn/datasets/iris.rb', line 17

def self.downloads
  return if File.exist?(url_to_file_name(URL_CSV))
  Downloader.download(URL_CSV)
end

.load(shuffle = false, shuffle_seed = rand(1 << 31)) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dnn/datasets/iris.rb', line 22

def self.load(shuffle = false, shuffle_seed = rand(1 << 31))
  downloads
  csv_array = CSV.read(url_to_file_name(URL_CSV)).reject(&:empty?)
  x = Numo::SFloat.zeros(csv_array.length, 4)
  y = Numo::SFloat.zeros(csv_array.length)
  csv_array.each.with_index do |(sepal_length, sepal_width, petal_length, petal_width, classes), i|
    x[i, 0] = sepal_length.to_f
    x[i, 1] = sepal_width.to_f
    x[i, 2] = petal_length.to_f
    x[i, 3] = petal_width.to_f
    y[i] = case classes
           when "Iris-setosa"
             SETOSA
           when "Iris-versicolor"
             VERSICOLOR
           when "Iris-virginica"
             VIRGINICA
           else
             raise DNN_Iris_LoadError, "Unknown class name '#{classes}' for iris"
    end
  end
  if shuffle
    if RUBY_VERSION.split(".")[0].to_i >= 3
      orig_seed = Random.seed
    else
      orig_seed = Random::DEFAULT.seed
    end
    srand(shuffle_seed)
    indexs = (0...csv_array.length).to_a.shuffle
    x[indexs, true] = x
    y[indexs] = y
    srand(orig_seed)
  end
  [x, y]
end