-
Notifications
You must be signed in to change notification settings - Fork 124
/
attributes.rb
127 lines (90 loc) · 2.61 KB
/
attributes.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
###
# to run use:
# ruby ./attributes.rb
require 'punks'
punks = Punks::Dataset.read( './cryptopunks-classic.csv' )
pp punks.size
#=> 10000
punk = punks[0]
pp punk.id
#=> 0
pp punk.type
#=> "Female"
pp punk.accessories.size
#=> 3
pp punk.accessories[0]
#=> "Green Eye Shadow"
pp punk.accessories[1]
#=> "Earring"
pp punk.accessories[2]
#=> "Blonde Bob"
## Popularity & Rarity by Punk Types (5)
counter = Hash.new(0) # a hash (table) - let's (auto-)default to 0 for values
punks.each do |punk|
counter[ punk.type ] += 1
end
pp counter.size
#=> 5
pp counter
#=> {"Female"=>3840, "Male"=>6039, "Zombie"=>88, "Ape"=>24, "Alien"=>9}
counter = counter.sort { |l,r| l[1]<=>r[1] }
#=> [["Alien", 9], ["Ape", 24], ["Zombie", 88], ["Female", 3840], ["Male", 6039]]
counter.each do |rec|
name = rec[0]
count = rec[1]
percent = Float(count*100)/Float(punks.size)
puts '%-12s | %4d (%5.2f %%)' % [name, count, percent]
end
## Popularity & Rarity by Accessories (87)
counter = {}
punks.each do |punk|
punk.accessories.each do |acc|
rec = counter[ acc ] ||= { count: 0,
by_type: Hash.new(0)
}
rec[ :count ] += 1
rec[ :by_type ][ punk.type ] += 1
end
end
pp counter.size
#=> 87
pp counter
#=> {"Green Eye Shadow"=>{:count=>271,
# :by_type=>{"Female"=>271}},
# "Earring"=>{:count=>2459,
# :by_type=>{"Female"=>933,"Male"=>1498,"Zombie"=>22,"Ape"=>3, "Alien"=>3}},
# "Blonde Bob"=>{:count=>147,
# :by_type=>{"Female"=>147}},
# "Smile"=>{:count=>238,
# :by_type=>{"Male"=>234, "Zombie"=>4}},
# "Mohawk"=>{:count=>441,
# :by_type=>{"Male"=>286, "Female"=>153, "Zombie"=>2}},
# ... }
counter.each do |rec|
name = rec[0]
count = rec[1][:count]
percent = Float(count*100)/Float(punks.size)
print '%-20s | %4d (%5.2f %%) | ' % [name, count, percent]
## add by type - highest first (lowest last)
types = rec[1][:by_type]
types = types.sort { |l,r| r[1]<=>l[1] }
print types.map {|type| "#{type[0]} (#{type[1]})" }.join( ' · ')
print "\n"
end
### Popularity & Rarity by Accessory Count (0 to 7)
counter = Hash.new(0)
punks.each do |punk|
counter[ punk.accessories.size ] += 1
end
pp counter.size
#=> 8
pp counter
#=> {"3"=>4501, "2"=>3560, "1"=>333, "4"=>1420, "5"=>166, "0"=>8, "6"=>11, "7"=>1}
counter = counter.sort { |l,r| l[0]<=>r[0] }
## pretty print
counter.each do |rec|
name = rec[0]
count = rec[1]
percent = Float(count*100)/Float(punks.size)
puts '%-12s | %4d (%5.2f %%)' % [name, count, percent]
end