added lab1

parent a6f47644
No preview for this file type
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore Byebug command history file.
.byebug_history
data_points =[]
k=5
data_point_a = { :label =>"cat", :features => [12, 1, 1],:dist=>999 }
data_point_b = { :label =>"cat", :features => [13, 3, 1],:dist=>999 }
data_point_c = { :label =>"cat", :features => [15, 1, 1],:dist=>999 }
data_point_d = { :label =>"cat", :features => [3, 12, 2],:dist=>999 }
data_point_e = { :label =>"dog", :features => [4, 11, 2],:dist=>999 }
data_point_f = { :label =>"dog", :features => [1, 11, 2],:dist=>999 }
data_point_g = { :label =>"cat", :features => [11, 4, 1],:dist=>999 }
data_point_h = { :label =>"cat", :features => [13, 2, 1],:dist=>999 }
data_point_i = { :label =>"dog", :features => [3, 13, 2],:dist=>999 }
data_point_j = { :label =>"dog", :features => [7, 12, 2],:dist=>999 }
data_points<<data_point_a
data_points<<data_point_b
data_points<<data_point_c
data_points<<data_point_d
data_points<<data_point_e
data_points<<data_point_f
data_points<<data_point_g
data_points<<data_point_h
data_points<<data_point_i
data_points<<data_point_j
unknown = {label:nil,features:[14, 2, 1]}
distance_label = []
def modified_bubble_sort(result)
for i in 0 ..result.count-1
for j in 0 ..result.count-1
if result[j+1] != nil
if result[j][:dist] > result[j+1][:dist]
temp = result[j]
result[j] = result[j+1]
result[j+1] = temp
end
end
end
end
result
end
def euclidean(v1, v2)
total = 0
v1.each_with_index do |v1, index|
total = total + (v1 - v2[index])**2
end
Math.sqrt(total)
end
def use_knn( data, k, unknown_data_point)
resulting_array_of_stuff = []
#data.each do |d|
#assign distances to entries
#d[:dist] = euclidean(d[:features], unknown_data_point[:features])
#puts " entry #{d[:label]} distance: #{d[:dist]}"
#end
sorted_data = modified_bubble_sort(data)
#data.each do |d|
# puts " sorted entry #{d[:label]} distance: #{d[:dist]}"
#end
cat_count=0
dog_count=0
for x in 0 .. k-1
if sorted_data[x][:label] == "cat"
cat_count = cat_count +1
else
if sorted_data[x][:label] == "dog"
dog_count = dog_count +1
else
end
end
end
if cat_count>dog_count
type="cat"
else
if cat_count<dog_count
type="dog"
else
type="something in between"
end
end
type
end
puts use_knn(data_points,k,unknown)
\ No newline at end of file
...@@ -73,6 +73,9 @@ GEM ...@@ -73,6 +73,9 @@ GEM
mini_portile2 (2.1.0) mini_portile2 (2.1.0)
minitest (5.9.0) minitest (5.9.0)
multi_json (1.12.1) multi_json (1.12.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
nokogiri (1.6.8-x86-mingw32) nokogiri (1.6.8-x86-mingw32)
mini_portile2 (~> 2.1.0) mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7) pkg-config (~> 1.1.7)
...@@ -124,6 +127,7 @@ GEM ...@@ -124,6 +127,7 @@ GEM
actionpack (>= 4.0) actionpack (>= 4.0)
activesupport (>= 4.0) activesupport (>= 4.0)
sprockets (>= 3.0.0) sprockets (>= 3.0.0)
sqlite3 (1.3.11)
sqlite3 (1.3.11-x86-mingw32) sqlite3 (1.3.11-x86-mingw32)
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.5) thread_safe (0.3.5)
...@@ -143,6 +147,7 @@ GEM ...@@ -143,6 +147,7 @@ GEM
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
PLATFORMS PLATFORMS
ruby
x86-mingw32 x86-mingw32
DEPENDENCIES DEPENDENCIES
...@@ -158,3 +163,6 @@ DEPENDENCIES ...@@ -158,3 +163,6 @@ DEPENDENCIES
tzinfo-data tzinfo-data
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
web-console (~> 2.0) web-console (~> 2.0)
BUNDLED WITH
1.12.5
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment