SlideShare a Scribd company logo
1 of 27
Download to read offline
Machine Learning
in Ruby
An Introduction
kNN Algorithm
Who am I?
Brad Arner
Head of Product
website: http://www.bradfordarner.com
email: brad@capstory.me
twitter: @bradfordarner
github: github.com/arnerjohn
The Goal
Make Machine Learning
Less Scary
The Caveat
Machine Learning is a
Huge Topic
I will talk about a general,
workhorse algorithm
k Nearest Neighbor
(kNN)
Fill in missing information
kNN Algorithm
What is the value of a home?
$250K
$275K
$280K
$240K
$235K
$215K
$240K
$195K
$300K
$210K
$250K
$????
1650 sq ft 2100 sq ft 1950 sq ft 1700 sq ft 2100 sq ft 1800 sq ft
2100 sq ft1700 sq ft2100 sq ft 1650 sq ft 1800 sq ft1950 sq ft
kNN Algorithm
What is in a name?
k =>
N =>
N =>
Number of Elements
Nearest / Distance
Neighbors / More Exist
kNN Algorithm
What is in a name?
k =>
N =>
N =>
Number of Elements
Nearest / Distance
Neighbors / More Exist
k = 3
Square Feet
Value
Rooms
kNN Algorithm
What is in a name?
k =>
N =>
N =>
Number of Elements
Nearest / Distance
Neighbors / More Exist
k = 5
Square Feet
Value
Rooms
kNN Algorithm
What is in a name?
k =>
N =>
N =>
Number of Elements
Nearest / Distance
Neighbors / More Exist
Square Feet
Value
Rooms
kNN Algorithm
What is in a name?
k =>
N =>
N =>
Number of Elements
Nearest / Distance
Neighbors / More Exist
Value: $200K
Value: $220K
Value: $210K
Value: $195K
Value: $215K
Estimated Value: $208K
The Code
github.com/arnerjohn/machine_learning_in_ruby
eventually @
http://www.machinelearninginruby.com
The Code
General Organization
class Property
attr_accessor :rooms, :area, :type
def initialize(options = {})
options = {
rooms: 1,
area: 500,
type: false
}.merge(options)
@rooms = options[:rooms]
@area = options[:area]
@type = options[:type]
@neighbors = []
@distance = nil
@guess = nil
end
.
.
.
end
class Orchestrator
def initialize(k)
@property_list = []
@k = k
@rooms = { max: 0, min: 10000, range: 0 }
@area = { max: 0, min: 10000, range: 0 }
end
.
.
.
end
The Code
Program Execution
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
The Code
Load Data
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def load_training_data
file = CSV.read(“data.csv”, { headers: true })
file.each do |line|
property = Property.new({rooms:
line[“rooms”].to_i, area: line[“area”].to_i, type:
line[“type”]})
add(property)
end
end
The Code
Load Data
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def scale_features
rooms_array = self.filter_knowns.map do |p|
property.rooms
end
area_array = self.filter_knowns.map do |p|
property.area
end
@rooms[:min] = rooms_array.min
@rooms[:max] = rooms_array.max
@rooms[:range] = rooms_array.max -
rooms_array.min
@area[:min] = area_array.min
@area[:max] = area_array.max
@area[:range] = area_array.max -
area_array.min
end
The Code
Find Neighbors
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def filter_unknowns
property_list.select do |property|
property.type == false
end
end
def determine_unknowns
self.filter_unknowns.each do |property|
property.neighbors = self.filter_knowns
property.calculate_neighbor_distances(self.ro
oms[:range], self.area[:range])
property.guess_type(self.k)
end
end
The Code
Calculate Distances
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def calculate_neighbor_distances(room_range,
area_range)
@neighbors.each do |neighbor|
rooms_delta = neighbor.rooms - self.rooms
area_delta = neighbor.area - self.area
rooms_delta = rooms_delta / room_range.to_f
area_delta = area_delta / area_range.to_f
neighbor.distance =
Math.sqrt(rooms_delta*rooms_delta +
area_delta*area_delta)
end
end
The Code
Guess Type
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def guess_type(k)
guess_hash =
gen_guess_hash(self.sort_neigbors_by_distance.
take(k))
@guess = assign_guess(guess_hash)
msg = %Q{
Property attrs => rooms: #{ @rooms }, area:
#{ @area }
The property type is guessed to be: #{ @guess
}
}
puts msg
return @guess
end
The Code
Guess Type
property_list = Orchestrator.new(3)
property_list.load_training_data
property_list.scale_features
property_list.add( Property.new({ rooms: 2, area:
1550, type: false }) )
property_list.add( Property.new({ rooms: 4, area:
1800, type: false }) )
property_list.determine_unknowns
def gen_guess_hash(properties)
guess_hash = Hash.new(0)
properties.each do |property|
guess_hash[property.type] += 1
end
return guess_hash
end
def assign_guess(guess_hash)
highest = 0
guess = ""
guess_hash.each do |key, value|
if value > highest
highest = value
guess = key
end
end
return guess
end
Questions?
Thank You!

More Related Content

What's hot

150970116028 2140705
150970116028 2140705150970116028 2140705
150970116028 2140705Manoj Shahu
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesManishPrajapati78
 
Altitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsAltitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsFastly
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Lettuce example using scenarios outline
Lettuce example using scenarios outlineLettuce example using scenarios outline
Lettuce example using scenarios outlineKaren Wiznia
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)Eirik George Tsarpalis
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional ProgrammingSovTech
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel ProgrammingAlex Moore
 

What's hot (20)

Heap sort
Heap sortHeap sort
Heap sort
 
150970116028 2140705
150970116028 2140705150970116028 2140705
150970116028 2140705
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Illustrator_Sample
Illustrator_SampleIllustrator_Sample
Illustrator_Sample
 
Heap
HeapHeap
Heap
 
Altitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsAltitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & Applications
 
lecture 6
lecture 6lecture 6
lecture 6
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Lettuce example using scenarios outline
Lettuce example using scenarios outlineLettuce example using scenarios outline
Lettuce example using scenarios outline
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Linux class 15 26 oct 2021
Linux class 15   26 oct 2021Linux class 15   26 oct 2021
Linux class 15 26 oct 2021
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Insertion operation
Insertion operationInsertion operation
Insertion operation
 
Heap sort
Heap sort Heap sort
Heap sort
 
MBrace: Cloud Computing with F#
MBrace: Cloud Computing with F#MBrace: Cloud Computing with F#
MBrace: Cloud Computing with F#
 
MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
 

Similar to Machine Learning In Ruby

Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivitynklmish
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202Mahmoud Samir Fayed
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?UFPA
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29Bilal Ahmed
 
RIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTMichael Galpin
 
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfAnton Arhipov
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWorkhorse Computing
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageDroidConTLV
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Two Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesTwo Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesKevin London
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Type safe embedded domain-specific languages
Type safe embedded domain-specific languagesType safe embedded domain-specific languages
Type safe embedded domain-specific languagesArthur Xavier
 
Cocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftCocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftMichele Titolo
 

Similar to Machine Learning In Ruby (20)

Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivity
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
RIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWT
 
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 
Linq intro
Linq introLinq intro
Linq intro
 
PostThis
PostThisPostThis
PostThis
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Two Trains and Other Refactoring Analogies
Two Trains and Other Refactoring AnalogiesTwo Trains and Other Refactoring Analogies
Two Trains and Other Refactoring Analogies
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Type safe embedded domain-specific languages
Type safe embedded domain-specific languagesType safe embedded domain-specific languages
Type safe embedded domain-specific languages
 
Cocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftCocoa Design Patterns in Swift
Cocoa Design Patterns in Swift
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

Machine Learning In Ruby

  • 2. kNN Algorithm Who am I? Brad Arner Head of Product website: http://www.bradfordarner.com email: brad@capstory.me twitter: @bradfordarner github: github.com/arnerjohn
  • 6. Machine Learning is a Huge Topic
  • 7. I will talk about a general, workhorse algorithm
  • 9. Fill in missing information
  • 10. kNN Algorithm What is the value of a home? $250K $275K $280K $240K $235K $215K $240K $195K $300K $210K $250K $???? 1650 sq ft 2100 sq ft 1950 sq ft 1700 sq ft 2100 sq ft 1800 sq ft 2100 sq ft1700 sq ft2100 sq ft 1650 sq ft 1800 sq ft1950 sq ft
  • 11. kNN Algorithm What is in a name? k => N => N => Number of Elements Nearest / Distance Neighbors / More Exist
  • 12. kNN Algorithm What is in a name? k => N => N => Number of Elements Nearest / Distance Neighbors / More Exist k = 3 Square Feet Value Rooms
  • 13. kNN Algorithm What is in a name? k => N => N => Number of Elements Nearest / Distance Neighbors / More Exist k = 5 Square Feet Value Rooms
  • 14. kNN Algorithm What is in a name? k => N => N => Number of Elements Nearest / Distance Neighbors / More Exist Square Feet Value Rooms
  • 15. kNN Algorithm What is in a name? k => N => N => Number of Elements Nearest / Distance Neighbors / More Exist Value: $200K Value: $220K Value: $210K Value: $195K Value: $215K Estimated Value: $208K
  • 18. The Code General Organization class Property attr_accessor :rooms, :area, :type def initialize(options = {}) options = { rooms: 1, area: 500, type: false }.merge(options) @rooms = options[:rooms] @area = options[:area] @type = options[:type] @neighbors = [] @distance = nil @guess = nil end . . . end class Orchestrator def initialize(k) @property_list = [] @k = k @rooms = { max: 0, min: 10000, range: 0 } @area = { max: 0, min: 10000, range: 0 } end . . . end
  • 19. The Code Program Execution property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns
  • 20. The Code Load Data property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def load_training_data file = CSV.read(“data.csv”, { headers: true }) file.each do |line| property = Property.new({rooms: line[“rooms”].to_i, area: line[“area”].to_i, type: line[“type”]}) add(property) end end
  • 21. The Code Load Data property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def scale_features rooms_array = self.filter_knowns.map do |p| property.rooms end area_array = self.filter_knowns.map do |p| property.area end @rooms[:min] = rooms_array.min @rooms[:max] = rooms_array.max @rooms[:range] = rooms_array.max - rooms_array.min @area[:min] = area_array.min @area[:max] = area_array.max @area[:range] = area_array.max - area_array.min end
  • 22. The Code Find Neighbors property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def filter_unknowns property_list.select do |property| property.type == false end end def determine_unknowns self.filter_unknowns.each do |property| property.neighbors = self.filter_knowns property.calculate_neighbor_distances(self.ro oms[:range], self.area[:range]) property.guess_type(self.k) end end
  • 23. The Code Calculate Distances property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def calculate_neighbor_distances(room_range, area_range) @neighbors.each do |neighbor| rooms_delta = neighbor.rooms - self.rooms area_delta = neighbor.area - self.area rooms_delta = rooms_delta / room_range.to_f area_delta = area_delta / area_range.to_f neighbor.distance = Math.sqrt(rooms_delta*rooms_delta + area_delta*area_delta) end end
  • 24. The Code Guess Type property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def guess_type(k) guess_hash = gen_guess_hash(self.sort_neigbors_by_distance. take(k)) @guess = assign_guess(guess_hash) msg = %Q{ Property attrs => rooms: #{ @rooms }, area: #{ @area } The property type is guessed to be: #{ @guess } } puts msg return @guess end
  • 25. The Code Guess Type property_list = Orchestrator.new(3) property_list.load_training_data property_list.scale_features property_list.add( Property.new({ rooms: 2, area: 1550, type: false }) ) property_list.add( Property.new({ rooms: 4, area: 1800, type: false }) ) property_list.determine_unknowns def gen_guess_hash(properties) guess_hash = Hash.new(0) properties.each do |property| guess_hash[property.type] += 1 end return guess_hash end def assign_guess(guess_hash) highest = 0 guess = "" guess_hash.each do |key, value| if value > highest highest = value guess = key end end return guess end