learn_kit 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
 - data/.gitignore +12 -0
 - data/.rspec +3 -0
 - data/.rubocop.yml +57 -0
 - data/.travis.yml +5 -0
 - data/CODE_OF_CONDUCT.md +74 -0
 - data/Gemfile +6 -0
 - data/LICENSE.txt +21 -0
 - data/README.md +72 -0
 - data/Rakefile +6 -0
 - data/bin/console +14 -0
 - data/bin/setup +8 -0
 - data/learn_kit.gemspec +30 -0
 - data/lib/learn_kit.rb +7 -0
 - data/lib/learn_kit/knn.rb +19 -0
 - data/lib/learn_kit/knn/predict.rb +79 -0
 - data/lib/learn_kit/naive_bayes/gaussian.rb +25 -0
 - data/lib/learn_kit/naive_bayes/gaussian/fit.rb +38 -0
 - data/lib/learn_kit/naive_bayes/gaussian/predict.rb +59 -0
 - data/lib/learn_kit/naive_bayes/gaussian/score.rb +20 -0
 - data/lib/learn_kit/version.rb +3 -0
 - metadata +134 -0
 
    
        checksums.yaml
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ---
         
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: ca7aeb09d91fc1451ba01359299e250c379b932a289951ff976b9a8024bc00cb
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: f1422a68f87762cedba48736a8bebdac372ae8e9c52f0e3bc750b48d356c1b63
         
     | 
| 
      
 5 
     | 
    
         
            +
            SHA512:
         
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: a94136b6145991bb659c5b8e2e6306bef7ebb56d0852e479e2f1ae68283afd3eadcee28e730581bf609bfdea8a5e6ece20a21fd892390362c4ec79e663d40b4f
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: b1ff7601ce22526978ba5b3981e91fbd821b5e9356097274297213b62a154e7d779df7813d78d6cbf3fa23bc00cb2a52462ee09a03ec0ae9d65e7912ae19d508
         
     | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/.rubocop.yml
    ADDED
    
    | 
         @@ -0,0 +1,57 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            AllCops:
         
     | 
| 
      
 2 
     | 
    
         
            +
              Exclude:
         
     | 
| 
      
 3 
     | 
    
         
            +
                - bin/*
         
     | 
| 
      
 4 
     | 
    
         
            +
              TargetRubyVersion: 2.5.0
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Metrics/AbcSize:
         
     | 
| 
      
 7 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            Metrics/BlockLength:
         
     | 
| 
      
 10 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            Metrics/ClassLength:
         
     | 
| 
      
 13 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            Metrics/LineLength:
         
     | 
| 
      
 16 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            Metrics/MethodLength:
         
     | 
| 
      
 19 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            Metrics/ModuleLength:
         
     | 
| 
      
 22 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            Metrics/CyclomaticComplexity:
         
     | 
| 
      
 25 
     | 
    
         
            +
              Max: 20
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            Layout/IndentationWidth:
         
     | 
| 
      
 28 
     | 
    
         
            +
              Width: 2
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            Layout/CaseIndentation:
         
     | 
| 
      
 31 
     | 
    
         
            +
              EnforcedStyle: case
         
     | 
| 
      
 32 
     | 
    
         
            +
              SupportedStyles:
         
     | 
| 
      
 33 
     | 
    
         
            +
                - case
         
     | 
| 
      
 34 
     | 
    
         
            +
                - end
         
     | 
| 
      
 35 
     | 
    
         
            +
              IndentOneStep: true
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
            Layout/AlignHash:
         
     | 
| 
      
 38 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            Style/GuardClause:
         
     | 
| 
      
 41 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            Naming:
         
     | 
| 
      
 44 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            Style/FormatStringToken:
         
     | 
| 
      
 47 
     | 
    
         
            +
              EnforcedStyle: template
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
            Style/ExpandPathArguments:
         
     | 
| 
      
 50 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            Style/FrozenStringLiteralComment:
         
     | 
| 
      
 53 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            Style/RescueStandardError:
         
     | 
| 
      
 56 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
    
        data/.travis.yml
    ADDED
    
    
    
        data/CODE_OF_CONDUCT.md
    ADDED
    
    | 
         @@ -0,0 +1,74 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Contributor Covenant Code of Conduct
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            ## Our Pledge
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            In the interest of fostering an open and welcoming environment, we as
         
     | 
| 
      
 6 
     | 
    
         
            +
            contributors and maintainers pledge to making participation in our project and
         
     | 
| 
      
 7 
     | 
    
         
            +
            our community a harassment-free experience for everyone, regardless of age, body
         
     | 
| 
      
 8 
     | 
    
         
            +
            size, disability, ethnicity, gender identity and expression, level of experience,
         
     | 
| 
      
 9 
     | 
    
         
            +
            nationality, personal appearance, race, religion, or sexual identity and
         
     | 
| 
      
 10 
     | 
    
         
            +
            orientation.
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            ## Our Standards
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            Examples of behavior that contributes to creating a positive environment
         
     | 
| 
      
 15 
     | 
    
         
            +
            include:
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            * Using welcoming and inclusive language
         
     | 
| 
      
 18 
     | 
    
         
            +
            * Being respectful of differing viewpoints and experiences
         
     | 
| 
      
 19 
     | 
    
         
            +
            * Gracefully accepting constructive criticism
         
     | 
| 
      
 20 
     | 
    
         
            +
            * Focusing on what is best for the community
         
     | 
| 
      
 21 
     | 
    
         
            +
            * Showing empathy towards other community members
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            Examples of unacceptable behavior by participants include:
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            * The use of sexualized language or imagery and unwelcome sexual attention or
         
     | 
| 
      
 26 
     | 
    
         
            +
            advances
         
     | 
| 
      
 27 
     | 
    
         
            +
            * Trolling, insulting/derogatory comments, and personal or political attacks
         
     | 
| 
      
 28 
     | 
    
         
            +
            * Public or private harassment
         
     | 
| 
      
 29 
     | 
    
         
            +
            * Publishing others' private information, such as a physical or electronic
         
     | 
| 
      
 30 
     | 
    
         
            +
              address, without explicit permission
         
     | 
| 
      
 31 
     | 
    
         
            +
            * Other conduct which could reasonably be considered inappropriate in a
         
     | 
| 
      
 32 
     | 
    
         
            +
              professional setting
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            ## Our Responsibilities
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            Project maintainers are responsible for clarifying the standards of acceptable
         
     | 
| 
      
 37 
     | 
    
         
            +
            behavior and are expected to take appropriate and fair corrective action in
         
     | 
| 
      
 38 
     | 
    
         
            +
            response to any instances of unacceptable behavior.
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            Project maintainers have the right and responsibility to remove, edit, or
         
     | 
| 
      
 41 
     | 
    
         
            +
            reject comments, commits, code, wiki edits, issues, and other contributions
         
     | 
| 
      
 42 
     | 
    
         
            +
            that are not aligned to this Code of Conduct, or to ban temporarily or
         
     | 
| 
      
 43 
     | 
    
         
            +
            permanently any contributor for other behaviors that they deem inappropriate,
         
     | 
| 
      
 44 
     | 
    
         
            +
            threatening, offensive, or harmful.
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            ## Scope
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
            This Code of Conduct applies both within project spaces and in public spaces
         
     | 
| 
      
 49 
     | 
    
         
            +
            when an individual is representing the project or its community. Examples of
         
     | 
| 
      
 50 
     | 
    
         
            +
            representing a project or community include using an official project e-mail
         
     | 
| 
      
 51 
     | 
    
         
            +
            address, posting via an official social media account, or acting as an appointed
         
     | 
| 
      
 52 
     | 
    
         
            +
            representative at an online or offline event. Representation of a project may be
         
     | 
| 
      
 53 
     | 
    
         
            +
            further defined and clarified by project maintainers.
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            ## Enforcement
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
            Instances of abusive, harassing, or otherwise unacceptable behavior may be
         
     | 
| 
      
 58 
     | 
    
         
            +
            reported by contacting the project team at kortirso@gmail.com. All
         
     | 
| 
      
 59 
     | 
    
         
            +
            complaints will be reviewed and investigated and will result in a response that
         
     | 
| 
      
 60 
     | 
    
         
            +
            is deemed necessary and appropriate to the circumstances. The project team is
         
     | 
| 
      
 61 
     | 
    
         
            +
            obligated to maintain confidentiality with regard to the reporter of an incident.
         
     | 
| 
      
 62 
     | 
    
         
            +
            Further details of specific enforcement policies may be posted separately.
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
            Project maintainers who do not follow or enforce the Code of Conduct in good
         
     | 
| 
      
 65 
     | 
    
         
            +
            faith may face temporary or permanent repercussions as determined by other
         
     | 
| 
      
 66 
     | 
    
         
            +
            members of the project's leadership.
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            ## Attribution
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
            This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
         
     | 
| 
      
 71 
     | 
    
         
            +
            available at [http://contributor-covenant.org/version/1/4][version]
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
            [homepage]: http://contributor-covenant.org
         
     | 
| 
      
 74 
     | 
    
         
            +
            [version]: http://contributor-covenant.org/version/1/4/
         
     | 
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            The MIT License (MIT)
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Copyright (c) 2018 kortirso
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy
         
     | 
| 
      
 6 
     | 
    
         
            +
            of this software and associated documentation files (the "Software"), to deal
         
     | 
| 
      
 7 
     | 
    
         
            +
            in the Software without restriction, including without limitation the rights
         
     | 
| 
      
 8 
     | 
    
         
            +
            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         
     | 
| 
      
 9 
     | 
    
         
            +
            copies of the Software, and to permit persons to whom the Software is
         
     | 
| 
      
 10 
     | 
    
         
            +
            furnished to do so, subject to the following conditions:
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be included in
         
     | 
| 
      
 13 
     | 
    
         
            +
            all copies or substantial portions of the Software.
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         
     | 
| 
      
 16 
     | 
    
         
            +
            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         
     | 
| 
      
 17 
     | 
    
         
            +
            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         
     | 
| 
      
 18 
     | 
    
         
            +
            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         
     | 
| 
      
 19 
     | 
    
         
            +
            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         
     | 
| 
      
 20 
     | 
    
         
            +
            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         
     | 
| 
      
 21 
     | 
    
         
            +
            THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,72 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # LearnKit
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 9 
     | 
    
         
            +
            gem 'learn_kit'
         
     | 
| 
      
 10 
     | 
    
         
            +
            ```
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                $ bundle
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                $ gem install learn_kit
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            ### K-Nearest Neighbors
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            Initialize classificator with data set consists from labels and features:
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 25 
     | 
    
         
            +
              data_set = { label1: [[-1, -1], [-2, -1], [-3, -2]], label2: [[1, 1], [2, 1], [3, 2], [-2, -2]] }
         
     | 
| 
      
 26 
     | 
    
         
            +
              clf = LearnKit::Knn.new(data_set: data_set)
         
     | 
| 
      
 27 
     | 
    
         
            +
            ```
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            Predict label for new feature:
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 32 
     | 
    
         
            +
              clf.predict(k: 3, algorithm: 'brute', weight: 'uniform', point: [-1, -2])
         
     | 
| 
      
 33 
     | 
    
         
            +
            ```
         
     | 
| 
      
 34 
     | 
    
         
            +
                k - number of nearest neighbors
         
     | 
| 
      
 35 
     | 
    
         
            +
                algorithm - algorithm for calculation of distances, one of the [brute]
         
     | 
| 
      
 36 
     | 
    
         
            +
                weight - method of weighted neighbors, one of the [uniform|distance]
         
     | 
| 
      
 37 
     | 
    
         
            +
                point - new feature for prediction
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            ### Naive Bayes
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            #### Gaussian
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            Initialize classificator with data set consists from labels and features:
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 46 
     | 
    
         
            +
              data_set = { label1: [[-1, -1], [-2, -1], [-3, -2]], label2: [[1, 1], [2, 1], [3, 2], [-2, -2]] }
         
     | 
| 
      
 47 
     | 
    
         
            +
              clf = LearnKit::NaiveBayes::Gaussian.new(data_set: data_set)
         
     | 
| 
      
 48 
     | 
    
         
            +
            ```
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
            Make fit of test data:
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 53 
     | 
    
         
            +
              clf.fit
         
     | 
| 
      
 54 
     | 
    
         
            +
            ```
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
            Predict label for new feature:
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 59 
     | 
    
         
            +
              clf.predict([-1, -2])
         
     | 
| 
      
 60 
     | 
    
         
            +
            ```
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
            Or show probability for all labels:
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 65 
     | 
    
         
            +
              clf.predict_proba([-1, -2])
         
     | 
| 
      
 66 
     | 
    
         
            +
            ```
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            Calculate accuracy for test data:
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 71 
     | 
    
         
            +
              clf.score
         
     | 
| 
      
 72 
     | 
    
         
            +
            ```
         
     | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/console
    ADDED
    
    | 
         @@ -0,0 +1,14 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env ruby
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'bundler/setup'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'learn_kit'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            # You can add fixtures and/or initialization code here to make experimenting
         
     | 
| 
      
 7 
     | 
    
         
            +
            # with your gem easier. You can also use a different console, if you like.
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            # (If you use this, don't forget to add pry to your Gemfile!)
         
     | 
| 
      
 10 
     | 
    
         
            +
            # require "pry"
         
     | 
| 
      
 11 
     | 
    
         
            +
            # Pry.start
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            require 'irb'
         
     | 
| 
      
 14 
     | 
    
         
            +
            IRB.start(__FILE__)
         
     | 
    
        data/bin/setup
    ADDED
    
    
    
        data/learn_kit.gemspec
    ADDED
    
    | 
         @@ -0,0 +1,30 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
      
 2 
     | 
    
         
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'learn_kit/version'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Gem::Specification.new do |spec|
         
     | 
| 
      
 6 
     | 
    
         
            +
              spec.name = 'learn_kit'
         
     | 
| 
      
 7 
     | 
    
         
            +
              spec.version = LearnKit::VERSION
         
     | 
| 
      
 8 
     | 
    
         
            +
              spec.authors = ['kortirso']
         
     | 
| 
      
 9 
     | 
    
         
            +
              spec.email = ['kortirso@gmail.com']
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              spec.summary = 'Machine Learning library'
         
     | 
| 
      
 12 
     | 
    
         
            +
              spec.description = 'Tools for machine learning with ruby'
         
     | 
| 
      
 13 
     | 
    
         
            +
              spec.homepage = 'https://github.com/kortirso/learn_kit'
         
     | 
| 
      
 14 
     | 
    
         
            +
              spec.license = 'MIT'
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              spec.files = `git ls-files -z`.split("\x0").reject do |f|
         
     | 
| 
      
 17 
     | 
    
         
            +
                f.match(%r{^(test|spec|features)/})
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
              spec.bindir = 'exe'
         
     | 
| 
      
 20 
     | 
    
         
            +
              spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         
     | 
| 
      
 21 
     | 
    
         
            +
              spec.require_paths = ['lib']
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
              spec.required_ruby_version = '>= 2.5.0'
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
              spec.add_development_dependency 'bundler', '~> 1.15'
         
     | 
| 
      
 26 
     | 
    
         
            +
              spec.add_development_dependency 'rake', '~> 10.0'
         
     | 
| 
      
 27 
     | 
    
         
            +
              spec.add_development_dependency 'rspec', '~> 3.0'
         
     | 
| 
      
 28 
     | 
    
         
            +
              spec.add_development_dependency 'rubocop', '~> 0.57.2'
         
     | 
| 
      
 29 
     | 
    
         
            +
              spec.add_dependency 'descriptive_statistics'
         
     | 
| 
      
 30 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/learn_kit.rb
    ADDED
    
    
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require_relative 'knn/predict'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 4 
     | 
    
         
            +
              # K-Nearest Neighbors algorithm
         
     | 
| 
      
 5 
     | 
    
         
            +
              class Knn
         
     | 
| 
      
 6 
     | 
    
         
            +
                class LearnFailure < StandardError; end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                include LearnKit::Knn::Predict
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                attr_reader :data_set, :points
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                # input data
         
     | 
| 
      
 13 
     | 
    
         
            +
                # { label1: [[f1, f2, f3], [f1, f2, f3]], label2: [[f4, f5, f6], [f4, f5, f6]] }
         
     | 
| 
      
 14 
     | 
    
         
            +
                def initialize(args = {})
         
     | 
| 
      
 15 
     | 
    
         
            +
                  @data_set = args[:data_set]
         
     | 
| 
      
 16 
     | 
    
         
            +
                  @points = []
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,79 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Knn
         
     | 
| 
      
 3 
     | 
    
         
            +
                # Fit test data
         
     | 
| 
      
 4 
     | 
    
         
            +
                module Predict
         
     | 
| 
      
 5 
     | 
    
         
            +
                  UNIFORM_WEIGHT = 1
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                  # input data
         
     | 
| 
      
 8 
     | 
    
         
            +
                  # { k: 3, algorithm: 'brute', weight: 'uniform', point: [1, 2, 3] }
         
     | 
| 
      
 9 
     | 
    
         
            +
                  # algorithms: brute
         
     | 
| 
      
 10 
     | 
    
         
            +
                  # weights: uniform, distance
         
     | 
| 
      
 11 
     | 
    
         
            +
                  def predict(args = {})
         
     | 
| 
      
 12 
     | 
    
         
            +
                    calc_distances(args) if points.size.zero?
         
     | 
| 
      
 13 
     | 
    
         
            +
                    prediction(sort_points(args))
         
     | 
| 
      
 14 
     | 
    
         
            +
                  rescue LearnFailure => ex
         
     | 
| 
      
 15 
     | 
    
         
            +
                    puts "LearnFailure: #{ex.message}"
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  private
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                  # calc distances
         
     | 
| 
      
 21 
     | 
    
         
            +
                  def calc_distances(args)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    case args[:algorithm]
         
     | 
| 
      
 23 
     | 
    
         
            +
                      when 'brute' then brute_algorithm(args)
         
     | 
| 
      
 24 
     | 
    
         
            +
                      else []
         
     | 
| 
      
 25 
     | 
    
         
            +
                    end
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  # calculation with brute algorithm
         
     | 
| 
      
 29 
     | 
    
         
            +
                  def brute_algorithm(args)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    data_set.keys.each do |key|
         
     | 
| 
      
 31 
     | 
    
         
            +
                      data_set[key].each do |value|
         
     | 
| 
      
 32 
     | 
    
         
            +
                        raise LearnFailure, "Different points size, error key - #{key}, error value - #{value}" if args[:point].size != value.size
         
     | 
| 
      
 33 
     | 
    
         
            +
                        points << { distance: calc_distance(args[:point], value), label: key }
         
     | 
| 
      
 34 
     | 
    
         
            +
                      end
         
     | 
| 
      
 35 
     | 
    
         
            +
                    end
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  # sort points by distance, select first K, add weight
         
     | 
| 
      
 39 
     | 
    
         
            +
                  def sort_points(args)
         
     | 
| 
      
 40 
     | 
    
         
            +
                    points
         
     | 
| 
      
 41 
     | 
    
         
            +
                      .sort_by { |point| point[:distance] }
         
     | 
| 
      
 42 
     | 
    
         
            +
                      .first(args[:k])
         
     | 
| 
      
 43 
     | 
    
         
            +
                      .map do |point|
         
     | 
| 
      
 44 
     | 
    
         
            +
                        point[:weight] = calc_point_weight(args[:weight], point[:distance])
         
     | 
| 
      
 45 
     | 
    
         
            +
                        point
         
     | 
| 
      
 46 
     | 
    
         
            +
                      end
         
     | 
| 
      
 47 
     | 
    
         
            +
                  end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                  # calc distance between 2 points
         
     | 
| 
      
 50 
     | 
    
         
            +
                  def calc_distance(point1, point2, summ = 0)
         
     | 
| 
      
 51 
     | 
    
         
            +
                    point1.each.with_index do |a, index|
         
     | 
| 
      
 52 
     | 
    
         
            +
                      summ += (a - point2[index])**2
         
     | 
| 
      
 53 
     | 
    
         
            +
                    end
         
     | 
| 
      
 54 
     | 
    
         
            +
                    Math.sqrt(summ)
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                  # prediction
         
     | 
| 
      
 58 
     | 
    
         
            +
                  def prediction(sorted_points, result = {})
         
     | 
| 
      
 59 
     | 
    
         
            +
                    sorted_points.each do |point|
         
     | 
| 
      
 60 
     | 
    
         
            +
                      if result[point[:label]].nil?
         
     | 
| 
      
 61 
     | 
    
         
            +
                        result[point[:label]] = point[:weight]
         
     | 
| 
      
 62 
     | 
    
         
            +
                      else
         
     | 
| 
      
 63 
     | 
    
         
            +
                        result[point[:label]] += point[:weight]
         
     | 
| 
      
 64 
     | 
    
         
            +
                      end
         
     | 
| 
      
 65 
     | 
    
         
            +
                    end
         
     | 
| 
      
 66 
     | 
    
         
            +
                    result.sort_by { |_k, v| v }.reverse[0][0]
         
     | 
| 
      
 67 
     | 
    
         
            +
                  end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                  # calc point weight based on selected type
         
     | 
| 
      
 70 
     | 
    
         
            +
                  def calc_point_weight(weight, distance)
         
     | 
| 
      
 71 
     | 
    
         
            +
                    case weight
         
     | 
| 
      
 72 
     | 
    
         
            +
                      when 'uniform' then UNIFORM_WEIGHT
         
     | 
| 
      
 73 
     | 
    
         
            +
                      when 'distance' then 1 / distance**2
         
     | 
| 
      
 74 
     | 
    
         
            +
                      else UNIFORM_WEIGHT
         
     | 
| 
      
 75 
     | 
    
         
            +
                    end
         
     | 
| 
      
 76 
     | 
    
         
            +
                  end
         
     | 
| 
      
 77 
     | 
    
         
            +
                end
         
     | 
| 
      
 78 
     | 
    
         
            +
              end
         
     | 
| 
      
 79 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,25 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require_relative 'gaussian/fit'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require_relative 'gaussian/predict'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require_relative 'gaussian/score'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 6 
     | 
    
         
            +
              module NaiveBayes
         
     | 
| 
      
 7 
     | 
    
         
            +
                # Gaussian NB
         
     | 
| 
      
 8 
     | 
    
         
            +
                class Gaussian
         
     | 
| 
      
 9 
     | 
    
         
            +
                  include LearnKit::NaiveBayes::Gaussian::Fit
         
     | 
| 
      
 10 
     | 
    
         
            +
                  include LearnKit::NaiveBayes::Gaussian::Predict
         
     | 
| 
      
 11 
     | 
    
         
            +
                  include LearnKit::NaiveBayes::Gaussian::Score
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  attr_reader :data_set, :labels, :feature_size, :fit_results
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  # input data
         
     | 
| 
      
 16 
     | 
    
         
            +
                  # { label1: [[f1, f2, f3], [f1, f2, f3]], label2: [[f4, f5, f6], [f4, f5, f6]] }
         
     | 
| 
      
 17 
     | 
    
         
            +
                  def initialize(args = {})
         
     | 
| 
      
 18 
     | 
    
         
            +
                    @data_set = args[:data_set]
         
     | 
| 
      
 19 
     | 
    
         
            +
                    @labels = data_set.keys
         
     | 
| 
      
 20 
     | 
    
         
            +
                    @feature_size = data_set.values.first[0].size
         
     | 
| 
      
 21 
     | 
    
         
            +
                    @fit_results = {}
         
     | 
| 
      
 22 
     | 
    
         
            +
                  end
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,38 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'descriptive_statistics'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 4 
     | 
    
         
            +
              module NaiveBayes
         
     | 
| 
      
 5 
     | 
    
         
            +
                class Gaussian
         
     | 
| 
      
 6 
     | 
    
         
            +
                  # Fit test data
         
     | 
| 
      
 7 
     | 
    
         
            +
                  module Fit
         
     | 
| 
      
 8 
     | 
    
         
            +
                    # output data
         
     | 
| 
      
 9 
     | 
    
         
            +
                    # { label1: [{fs_std1: 1, fs_mean1: 2, fs_var1: 3}, {..}, {..}], label2: [{fs_std1: 1, fs_mean1: 2, fs_var1: 3}, {..}, {..}] }
         
     | 
| 
      
 10 
     | 
    
         
            +
                    def fit
         
     | 
| 
      
 11 
     | 
    
         
            +
                      labels.each do |label_name|
         
     | 
| 
      
 12 
     | 
    
         
            +
                        fit_results[label_name] = []
         
     | 
| 
      
 13 
     | 
    
         
            +
                        (0...feature_size).each do |index|
         
     | 
| 
      
 14 
     | 
    
         
            +
                          fs = feature_set(index, label_name)
         
     | 
| 
      
 15 
     | 
    
         
            +
                          # statistical properties of the feature set
         
     | 
| 
      
 16 
     | 
    
         
            +
                          fs_std = fs.standard_deviation
         
     | 
| 
      
 17 
     | 
    
         
            +
                          fs_mean = fs.mean
         
     | 
| 
      
 18 
     | 
    
         
            +
                          fs_var = fs.variance
         
     | 
| 
      
 19 
     | 
    
         
            +
                          fit_results[label_name] << { fs_std: fs_std, fs_mean: fs_mean, fs_var: fs_var }
         
     | 
| 
      
 20 
     | 
    
         
            +
                        end
         
     | 
| 
      
 21 
     | 
    
         
            +
                      end
         
     | 
| 
      
 22 
     | 
    
         
            +
                    end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                    private
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                    # Get all the features of a certain index in a given label in one set
         
     | 
| 
      
 27 
     | 
    
         
            +
                    # index - feature index in training set
         
     | 
| 
      
 28 
     | 
    
         
            +
                    # label_name - label name
         
     | 
| 
      
 29 
     | 
    
         
            +
                    def feature_set(index, label_name, feature_set = [])
         
     | 
| 
      
 30 
     | 
    
         
            +
                      data_set[label_name].each do |feature|
         
     | 
| 
      
 31 
     | 
    
         
            +
                        feature_set << feature[index]
         
     | 
| 
      
 32 
     | 
    
         
            +
                      end
         
     | 
| 
      
 33 
     | 
    
         
            +
                      feature_set
         
     | 
| 
      
 34 
     | 
    
         
            +
                    end
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
      
 36 
     | 
    
         
            +
                end
         
     | 
| 
      
 37 
     | 
    
         
            +
              end
         
     | 
| 
      
 38 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,59 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 2 
     | 
    
         
            +
              module NaiveBayes
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Gaussian
         
     | 
| 
      
 4 
     | 
    
         
            +
                  # Predict new data
         
     | 
| 
      
 5 
     | 
    
         
            +
                  module Predict
         
     | 
| 
      
 6 
     | 
    
         
            +
                    # returns prediction of label for feature
         
     | 
| 
      
 7 
     | 
    
         
            +
                    # input data
         
     | 
| 
      
 8 
     | 
    
         
            +
                    # [f1, f2, f3]
         
     | 
| 
      
 9 
     | 
    
         
            +
                    # output data
         
     | 
| 
      
 10 
     | 
    
         
            +
                    # :label2
         
     | 
| 
      
 11 
     | 
    
         
            +
                    def predict(feature_values)
         
     | 
| 
      
 12 
     | 
    
         
            +
                      res = labels.sort_by do |label_name|
         
     | 
| 
      
 13 
     | 
    
         
            +
                        class_probability(feature_values, label_name)
         
     | 
| 
      
 14 
     | 
    
         
            +
                      end
         
     | 
| 
      
 15 
     | 
    
         
            +
                      res[-1]
         
     | 
| 
      
 16 
     | 
    
         
            +
                    end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                    # returns labels with probabilities
         
     | 
| 
      
 19 
     | 
    
         
            +
                    # input data
         
     | 
| 
      
 20 
     | 
    
         
            +
                    # [f1, f2, f3]
         
     | 
| 
      
 21 
     | 
    
         
            +
                    # output data
         
     | 
| 
      
 22 
     | 
    
         
            +
                    # { label1: 0.01, label2: 0.02 }
         
     | 
| 
      
 23 
     | 
    
         
            +
                    def predict_proba(feature_values, result = {})
         
     | 
| 
      
 24 
     | 
    
         
            +
                      labels.each do |label_name|
         
     | 
| 
      
 25 
     | 
    
         
            +
                        result[label_name] = class_probability(feature_values, label_name)
         
     | 
| 
      
 26 
     | 
    
         
            +
                      end
         
     | 
| 
      
 27 
     | 
    
         
            +
                      result
         
     | 
| 
      
 28 
     | 
    
         
            +
                    end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                    private
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                    # compute the final naive Bayesian probability for a given set of features being a part of a given label
         
     | 
| 
      
 33 
     | 
    
         
            +
                    def class_probability(feature_values, label_name)
         
     | 
| 
      
 34 
     | 
    
         
            +
                      class_fraction = 1.0 / labels.size
         
     | 
| 
      
 35 
     | 
    
         
            +
                      feature_bayes = feature_mult(feature_values, label_name)
         
     | 
| 
      
 36 
     | 
    
         
            +
                      feature_bayes * class_fraction
         
     | 
| 
      
 37 
     | 
    
         
            +
                    end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                    # multiply together the feature probabilities for all of the features in a label for given values
         
     | 
| 
      
 40 
     | 
    
         
            +
                    def feature_mult(feature_values, label_name, res = 1.0)
         
     | 
| 
      
 41 
     | 
    
         
            +
                      feature_values.each.with_index do |feature_value, index|
         
     | 
| 
      
 42 
     | 
    
         
            +
                        res *= feature_probability(index, feature_value, label_name)
         
     | 
| 
      
 43 
     | 
    
         
            +
                      end
         
     | 
| 
      
 44 
     | 
    
         
            +
                      res
         
     | 
| 
      
 45 
     | 
    
         
            +
                    end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    def feature_probability(index, value, label_name)
         
     | 
| 
      
 48 
     | 
    
         
            +
                      # select result from training
         
     | 
| 
      
 49 
     | 
    
         
            +
                      fit_result = fit_results[label_name][index]
         
     | 
| 
      
 50 
     | 
    
         
            +
                      # deal with the edge case of a 0 standard deviation
         
     | 
| 
      
 51 
     | 
    
         
            +
                      return fit_result[:fs_mean] == value ? 1.0 : 0.0 if fit_result[:fs_std].zero?
         
     | 
| 
      
 52 
     | 
    
         
            +
                      # calculate the gaussian probability
         
     | 
| 
      
 53 
     | 
    
         
            +
                      exp = - ((value - fit_result[:fs_mean])**2) / (2 * fit_result[:fs_var])
         
     | 
| 
      
 54 
     | 
    
         
            +
                      (1.0 / Math.sqrt(2 * Math::PI * fit_result[:fs_var])) * (Math::E**exp)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
              end
         
     | 
| 
      
 59 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module LearnKit
         
     | 
| 
      
 2 
     | 
    
         
            +
              module NaiveBayes
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Gaussian
         
     | 
| 
      
 4 
     | 
    
         
            +
                  # Calc accuracy for test data
         
     | 
| 
      
 5 
     | 
    
         
            +
                  module Score
         
     | 
| 
      
 6 
     | 
    
         
            +
                    # calculate accuracy for test data
         
     | 
| 
      
 7 
     | 
    
         
            +
                    def score(total = 0, correct = 0)
         
     | 
| 
      
 8 
     | 
    
         
            +
                      labels.each do |label_name|
         
     | 
| 
      
 9 
     | 
    
         
            +
                        data_set[label_name].each do |feature|
         
     | 
| 
      
 10 
     | 
    
         
            +
                          predicted_label = predict(feature)
         
     | 
| 
      
 11 
     | 
    
         
            +
                          correct += 1 if predicted_label == label_name
         
     | 
| 
      
 12 
     | 
    
         
            +
                          total += 1
         
     | 
| 
      
 13 
     | 
    
         
            +
                        end
         
     | 
| 
      
 14 
     | 
    
         
            +
                      end
         
     | 
| 
      
 15 
     | 
    
         
            +
                      total.zero? ? 0 : (correct * 100.0 / total).round(4)
         
     | 
| 
      
 16 
     | 
    
         
            +
                    end
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,134 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: learn_kit
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 7 
     | 
    
         
            +
            - kortirso
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: exe
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2018-09-14 00:00:00.000000000 Z
         
     | 
| 
      
 12 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 13 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 14 
     | 
    
         
            +
              name: bundler
         
     | 
| 
      
 15 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 16 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 17 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 18 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 19 
     | 
    
         
            +
                    version: '1.15'
         
     | 
| 
      
 20 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 21 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 22 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 23 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 24 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 25 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 26 
     | 
    
         
            +
                    version: '1.15'
         
     | 
| 
      
 27 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 28 
     | 
    
         
            +
              name: rake
         
     | 
| 
      
 29 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 30 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 31 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 32 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 33 
     | 
    
         
            +
                    version: '10.0'
         
     | 
| 
      
 34 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 35 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 36 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 37 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 38 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 39 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 40 
     | 
    
         
            +
                    version: '10.0'
         
     | 
| 
      
 41 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 42 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 43 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 44 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 45 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 46 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 47 
     | 
    
         
            +
                    version: '3.0'
         
     | 
| 
      
 48 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 49 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 50 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 51 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 52 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 53 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 54 
     | 
    
         
            +
                    version: '3.0'
         
     | 
| 
      
 55 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 56 
     | 
    
         
            +
              name: rubocop
         
     | 
| 
      
 57 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 58 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 59 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 60 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 61 
     | 
    
         
            +
                    version: 0.57.2
         
     | 
| 
      
 62 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 63 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 64 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 65 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 66 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
      
 67 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 68 
     | 
    
         
            +
                    version: 0.57.2
         
     | 
| 
      
 69 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 70 
     | 
    
         
            +
              name: descriptive_statistics
         
     | 
| 
      
 71 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 72 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 73 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 74 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 75 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 76 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 77 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 78 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 79 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 80 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 81 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 82 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 83 
     | 
    
         
            +
            description: Tools for machine learning with ruby
         
     | 
| 
      
 84 
     | 
    
         
            +
            email:
         
     | 
| 
      
 85 
     | 
    
         
            +
            - kortirso@gmail.com
         
     | 
| 
      
 86 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 87 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 88 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 89 
     | 
    
         
            +
            files:
         
     | 
| 
      
 90 
     | 
    
         
            +
            - ".gitignore"
         
     | 
| 
      
 91 
     | 
    
         
            +
            - ".rspec"
         
     | 
| 
      
 92 
     | 
    
         
            +
            - ".rubocop.yml"
         
     | 
| 
      
 93 
     | 
    
         
            +
            - ".travis.yml"
         
     | 
| 
      
 94 
     | 
    
         
            +
            - CODE_OF_CONDUCT.md
         
     | 
| 
      
 95 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 96 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
      
 97 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 98 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 99 
     | 
    
         
            +
            - bin/console
         
     | 
| 
      
 100 
     | 
    
         
            +
            - bin/setup
         
     | 
| 
      
 101 
     | 
    
         
            +
            - learn_kit.gemspec
         
     | 
| 
      
 102 
     | 
    
         
            +
            - lib/learn_kit.rb
         
     | 
| 
      
 103 
     | 
    
         
            +
            - lib/learn_kit/knn.rb
         
     | 
| 
      
 104 
     | 
    
         
            +
            - lib/learn_kit/knn/predict.rb
         
     | 
| 
      
 105 
     | 
    
         
            +
            - lib/learn_kit/naive_bayes/gaussian.rb
         
     | 
| 
      
 106 
     | 
    
         
            +
            - lib/learn_kit/naive_bayes/gaussian/fit.rb
         
     | 
| 
      
 107 
     | 
    
         
            +
            - lib/learn_kit/naive_bayes/gaussian/predict.rb
         
     | 
| 
      
 108 
     | 
    
         
            +
            - lib/learn_kit/naive_bayes/gaussian/score.rb
         
     | 
| 
      
 109 
     | 
    
         
            +
            - lib/learn_kit/version.rb
         
     | 
| 
      
 110 
     | 
    
         
            +
            homepage: https://github.com/kortirso/learn_kit
         
     | 
| 
      
 111 
     | 
    
         
            +
            licenses:
         
     | 
| 
      
 112 
     | 
    
         
            +
            - MIT
         
     | 
| 
      
 113 
     | 
    
         
            +
            metadata: {}
         
     | 
| 
      
 114 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 115 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 116 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 117 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 118 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 119 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 120 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 121 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 122 
     | 
    
         
            +
                  version: 2.5.0
         
     | 
| 
      
 123 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 124 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 125 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 126 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 127 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 128 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 129 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 130 
     | 
    
         
            +
            rubygems_version: 2.7.6
         
     | 
| 
      
 131 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 132 
     | 
    
         
            +
            specification_version: 4
         
     | 
| 
      
 133 
     | 
    
         
            +
            summary: Machine Learning library
         
     | 
| 
      
 134 
     | 
    
         
            +
            test_files: []
         
     |