-
Notifications
You must be signed in to change notification settings - Fork 2
/
07-prep_data.rb
63 lines (50 loc) · 1.09 KB
/
07-prep_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
59
60
61
62
63
# 手書き文字サンプルの抽出
CHARS = /[036]/ # 抽出する数字(任意の個数の数字を指定可能)
num = 600 # 抽出する文字数
labels = File.open('train-labels.txt', 'r')
images = File.open('train-images.txt', 'r')
labels_out = File.open('sample-labels.txt', 'w')
images_out = File.open('sample-images.txt', 'w')
while num > 0 do
begin
label = labels.readline
image = images.readline
rescue EOFError
break
end
if label !~ CHARS
next
end
line = image.split(' ').inject('') do |line, c|
line += c.to_i > 127 ? '1,' : '0,'
end
line = line[0..-2]
labels_out.puts(label)
images_out.puts(line)
num -= 1
end
labels.close
images.close
labels_out.close
images_out.close
images = File.open('sample-images.txt', 'r')
samples = File.open('samples.txt', 'w')
c = 0
while c < 10 do
begin
line = images.readline
rescue EOFError
break
end
x = 0
line.split(',').each do |s|
samples.write(s.to_i == 1 ? '#' : ' ')
x += 1
if x % 28 == 0
samples.puts('')
end
end
c += 1
end
images.close
samples.close