├── .github └── workflows │ └── build.yml ├── .gitignore ├── .gitmodules ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── NOTICE.txt ├── README.md ├── Rakefile ├── ext └── outliertree │ ├── ext.cpp │ └── extconf.rb ├── lib ├── outliertree.rb └── outliertree │ ├── dataset.rb │ ├── model.rb │ ├── result.rb │ └── version.rb ├── outliertree.gemspec └── test ├── outliertree_test.rb ├── support ├── predict.py ├── test.csv └── train.csv └── test_helper.rb /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | strategy: 6 | fail-fast: false 7 | matrix: 8 | os: [ubuntu-latest, macos-latest, windows-latest] 9 | runs-on: ${{ matrix.os }} 10 | steps: 11 | - uses: actions/checkout@v4 12 | with: 13 | submodules: true 14 | - uses: ruby/setup-ruby@v1 15 | with: 16 | ruby-version: 3.4 17 | bundler-cache: true 18 | - run: bundle exec rake compile 19 | - run: bundle exec rake test 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | *.lock 10 | *.bundle 11 | *.so 12 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "vendor/outliertree"] 2 | path = vendor/outliertree 3 | url = https://github.com/david-cortes/outliertree 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.4.1 (2025-04-23) 2 | 3 | - Updated OutlierTree to 1.10.0 4 | 5 | ## 0.4.0 (2024-06-11) 6 | 7 | - Updated OutlierTree to 1.9.0 8 | - Dropped support for Ruby < 3.1 9 | 10 | ## 0.3.1 (2023-12-19) 11 | 12 | - Updated OutlierTree to 1.8.2 13 | - Fixed error with Rice 4.1 14 | 15 | ## 0.3.0 (2022-06-13) 16 | 17 | - Updated OutlierTree to 1.8.1 18 | - Dropped support for Ruby < 2.7 19 | 20 | ## 0.2.1 (2021-05-23) 21 | 22 | - Improved performance 23 | 24 | ## 0.2.0 (2021-05-17) 25 | 26 | - Updated to Rice 4 27 | - Dropped support for Ruby < 2.6 28 | 29 | ## 0.1.2 (2021-02-08) 30 | 31 | - Fixed error with missing numeric values 32 | 33 | ## 0.1.1 (2020-08-21) 34 | 35 | - Fixed error when Active Support loaded 36 | 37 | ## 0.1.0 (2020-08-12) 38 | 39 | - First release 40 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | 5 | gem "rake" 6 | gem "rake-compiler" 7 | gem "minitest", ">= 5" 8 | gem "rover-df" 9 | gem "csv" 10 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2019-2020 David Cortes 2 | Copyright (C) 2020-2022 Andrew Kane 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OutlierTree Ruby 2 | 3 | :deciduous_tree: [OutlierTree](https://github.com/david-cortes/outliertree) - explainable outlier/anomaly detection - for Ruby 4 | 5 | Produces human-readable explanations for why values are detected as outliers 6 | 7 | ```txt 8 | Price (2.50) looks low given Department is Books and Sale is false 9 | ``` 10 | 11 | :evergreen_tree: Check out [IsoTree](https://github.com/ankane/isotree-ruby) for an alternative approach that uses Isolation Forest 12 | 13 | [![Build Status](https://github.com/ankane/outliertree-ruby/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/outliertree-ruby/actions) 14 | 15 | ## Installation 16 | 17 | Add this line to your application’s Gemfile: 18 | 19 | ```ruby 20 | gem "outliertree" 21 | ``` 22 | 23 | ## Getting Started 24 | 25 | Prep your data 26 | 27 | ```ruby 28 | data = [ 29 | {department: "Books", sale: false, price: 2.50}, 30 | {department: "Books", sale: true, price: 3.00}, 31 | {department: "Movies", sale: false, price: 5.00}, 32 | # ... 33 | ] 34 | ``` 35 | 36 | Train a model 37 | 38 | ```ruby 39 | model = OutlierTree.new 40 | model.fit(data) 41 | ``` 42 | 43 | Get outliers 44 | 45 | ```ruby 46 | model.outliers(data) 47 | ``` 48 | 49 | ## Parameters 50 | 51 | Pass parameters - default values below 52 | 53 | ```ruby 54 | OutlierTree.new( 55 | max_depth: 4, 56 | min_gain: 0.01, 57 | z_norm: 2.67, 58 | z_outlier: 8.0, 59 | pct_outliers: 0.01, 60 | min_size_numeric: 25, 61 | min_size_categ: 50, 62 | categ_split: "binarize", 63 | categ_outliers: "tail", 64 | numeric_split: "raw", 65 | follow_all: false, 66 | gain_as_pct: true, 67 | nthreads: -1 68 | ) 69 | ``` 70 | 71 | See a [detailed explanation](https://outliertree.readthedocs.io/en/latest/#outliertree.OutlierTree) 72 | 73 | ## Data 74 | 75 | Data can be an array of hashes 76 | 77 | ```ruby 78 | [ 79 | {department: "Books", sale: false, price: 2.50}, 80 | {department: "Books", sale: true, price: 3.00}, 81 | {department: "Movies", sale: false, price: 5.00} 82 | ] 83 | ``` 84 | 85 | Or a Rover data frame 86 | 87 | ```ruby 88 | Rover.read_csv("data.csv") 89 | ``` 90 | 91 | ## Performance 92 | 93 | OutlierTree uses OpenMP when possible for best performance. To enable OpenMP on Mac, run: 94 | 95 | ```sh 96 | brew install libomp 97 | ``` 98 | 99 | Then reinstall the gem. 100 | 101 | ```sh 102 | gem uninstall outliertree --force 103 | bundle install 104 | ``` 105 | 106 | ## Resources 107 | 108 | - [Explainable outlier detection through decision tree conditioning](https://arxiv.org/pdf/2001.00636.pdf) 109 | 110 | ## History 111 | 112 | View the [changelog](https://github.com/ankane/outliertree-ruby/blob/master/CHANGELOG.md) 113 | 114 | ## Contributing 115 | 116 | Everyone is encouraged to help improve this project. Here are a few ways you can help: 117 | 118 | - [Report bugs](https://github.com/ankane/outliertree-ruby/issues) 119 | - Fix bugs and [submit pull requests](https://github.com/ankane/outliertree-ruby/pulls) 120 | - Write, clarify, or fix documentation 121 | - Suggest or add new features 122 | 123 | To get started with development: 124 | 125 | ```sh 126 | git clone --recursive https://github.com/ankane/outliertree-ruby.git 127 | cd outliertree-ruby 128 | bundle install 129 | bundle exec rake compile 130 | bundle exec rake test 131 | ``` 132 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | require "rake/extensiontask" 4 | 5 | task default: :test 6 | Rake::TestTask.new do |t| 7 | t.libs << "test" 8 | t.pattern = "test/**/*_test.rb" 9 | end 10 | 11 | Rake::ExtensionTask.new("outliertree") do |ext| 12 | ext.name = "ext" 13 | ext.lib_dir = "lib/outliertree" 14 | end 15 | 16 | task :check_license do 17 | raise "Missing vendor license" unless File.exist?("vendor/outliertree/LICENSE") 18 | end 19 | 20 | task :remove_ext do 21 | path = "lib/outliertree/ext.bundle" 22 | File.unlink(path) if File.exist?(path) 23 | end 24 | 25 | Rake::Task["build"].enhance [:check_license, :remove_ext] 26 | -------------------------------------------------------------------------------- /ext/outliertree/ext.cpp: -------------------------------------------------------------------------------- 1 | // outliertree 2 | #include 3 | 4 | // rice 5 | #include 6 | #include 7 | 8 | using Rice::Array; 9 | using Rice::Hash; 10 | using Rice::Module; 11 | using Rice::Object; 12 | using Rice::String; 13 | using Rice::Symbol; 14 | using Rice::define_class_under; 15 | using Rice::define_module; 16 | 17 | namespace Rice::detail 18 | { 19 | template 20 | class To_Ruby> 21 | { 22 | public: 23 | VALUE convert(std::vector const & x) 24 | { 25 | auto a = rb_ary_new2(x.size()); 26 | for (const auto& v : x) { 27 | rb_ary_push(a, To_Ruby().convert(v)); 28 | } 29 | return a; 30 | } 31 | }; 32 | 33 | template<> 34 | class To_Ruby> 35 | { 36 | public: 37 | VALUE convert(std::vector const & x) 38 | { 39 | auto a = rb_ary_new2(x.size()); 40 | for (const auto& v : x) { 41 | rb_ary_push(a, To_Ruby().convert(v)); 42 | } 43 | return a; 44 | } 45 | }; 46 | 47 | template<> 48 | struct Type> 49 | { 50 | static bool verify() 51 | { 52 | return true; 53 | } 54 | }; 55 | 56 | template<> 57 | struct Type 58 | { 59 | static bool verify() 60 | { 61 | return true; 62 | } 63 | }; 64 | 65 | template<> 66 | class To_Ruby 67 | { 68 | public: 69 | VALUE convert(ColType const & x) 70 | { 71 | switch (x) { 72 | case Numeric: return Symbol("numeric"); 73 | case Categorical: return Symbol("categorical"); 74 | case Ordinal: return Symbol("ordinal"); 75 | case NoType: return Symbol("no_type"); 76 | } 77 | throw std::runtime_error("Unknown column type"); 78 | } 79 | }; 80 | 81 | template<> 82 | struct Type 83 | { 84 | static bool verify() 85 | { 86 | return true; 87 | } 88 | }; 89 | 90 | template<> 91 | class To_Ruby 92 | { 93 | public: 94 | VALUE convert(SplitType const & x) 95 | { 96 | switch (x) { 97 | case LessOrEqual: return Symbol("less_or_equal"); 98 | case Greater: return Symbol("greater"); 99 | case Equal: return Symbol("equal"); 100 | case NotEqual: return Symbol("not_equal"); 101 | case InSubset: return Symbol("in_subset"); 102 | case NotInSubset: return Symbol("not_in_subset"); 103 | case SingleCateg: return Symbol("single_categ"); 104 | case SubTrees: return Symbol("sub_trees"); 105 | case IsNa: return Symbol("is_na"); 106 | case Root: return Symbol("root"); 107 | } 108 | throw std::runtime_error("Unknown split type"); 109 | } 110 | }; 111 | } 112 | 113 | extern "C" 114 | void Init_ext() 115 | { 116 | Module rb_mOutlierTree = define_module("OutlierTree"); 117 | Module rb_mExt = define_module_under(rb_mOutlierTree, "Ext"); 118 | 119 | define_class_under(rb_mExt, "Cluster") 120 | .define_method("upper_lim", [](Cluster& self) { return self.upper_lim; }) 121 | .define_method("display_lim_high", [](Cluster& self) { return self.display_lim_high; }) 122 | .define_method("perc_below", [](Cluster& self) { return self.perc_below; }) 123 | .define_method("display_lim_low", [](Cluster& self) { return self.display_lim_low; }) 124 | .define_method("perc_above", [](Cluster& self) { return self.perc_above; }) 125 | .define_method("display_mean", [](Cluster& self) { return self.display_mean; }) 126 | .define_method("display_sd", [](Cluster& self) { return self.display_sd; }) 127 | .define_method("cluster_size", [](Cluster& self) { return self.cluster_size; }) 128 | .define_method("split_point", [](Cluster& self) { return self.split_point; }) 129 | .define_method("split_subset", [](Cluster& self) { return self.split_subset; }) 130 | .define_method("split_lev", [](Cluster& self) { return self.split_lev; }) 131 | .define_method("split_type", [](Cluster& self) { return self.split_type; }) 132 | .define_method("column_type", [](Cluster& self) { return self.column_type; }) 133 | .define_method("has_na_branch", [](Cluster& self) { return self.has_NA_branch; }) 134 | .define_method("col_num", [](Cluster& self) { return self.col_num; }); 135 | 136 | define_class_under(rb_mExt, "ClusterTree") 137 | .define_method("parent_branch", [](ClusterTree& self) { return self.parent_branch; }) 138 | .define_method("parent", [](ClusterTree& self) { return self.parent; }) 139 | .define_method("all_branches", [](ClusterTree& self) { return self.all_branches; }) 140 | .define_method("column_type", [](ClusterTree& self) { return self.column_type; }) 141 | .define_method("col_num", [](ClusterTree& self) { return self.col_num; }) 142 | .define_method("split_point", [](ClusterTree& self) { return self.split_point; }) 143 | .define_method("split_subset", [](ClusterTree& self) { return self.split_subset; }) 144 | .define_method("split_lev", [](ClusterTree& self) { return self.split_lev; }); 145 | 146 | define_class_under(rb_mExt, "ModelOutputs") 147 | .define_method("outlier_scores_final", [](ModelOutputs& self) { return self.outlier_scores_final; }) 148 | .define_method("outlier_columns_final", [](ModelOutputs& self) { return self.outlier_columns_final; }) 149 | .define_method("outlier_clusters_final", [](ModelOutputs& self) { return self.outlier_clusters_final; }) 150 | .define_method("outlier_trees_final", [](ModelOutputs& self) { return self.outlier_trees_final; }) 151 | .define_method("outlier_depth_final", [](ModelOutputs& self) { return self.outlier_depth_final; }) 152 | .define_method("outlier_decimals_distr", [](ModelOutputs& self) { return self.outlier_decimals_distr; }) 153 | .define_method("min_decimals_col", [](ModelOutputs& self) { return self.min_decimals_col; }) 154 | .define_method( 155 | "all_clusters", 156 | [](ModelOutputs& self, size_t i, size_t j) { 157 | return self.all_clusters[i][j]; 158 | }) 159 | .define_method( 160 | "all_trees", 161 | [](ModelOutputs& self, size_t i, size_t j) { 162 | return self.all_trees[i][j]; 163 | }); 164 | 165 | rb_mExt 166 | .define_singleton_function( 167 | "fit_outliers_models", 168 | [](Hash options) { 169 | ModelOutputs model_outputs; 170 | 171 | // data 172 | size_t nrows = options.get("nrows"); 173 | size_t ncols_numeric = options.get("ncols_numeric"); 174 | size_t ncols_categ = options.get("ncols_categ"); 175 | size_t ncols_ord = options.get("ncols_ord"); 176 | 177 | double *restrict numeric_data = NULL; 178 | if (ncols_numeric > 0) { 179 | numeric_data = (double*) options.get("numeric_data").c_str(); 180 | } 181 | 182 | int *restrict categorical_data = NULL; 183 | int *restrict ncat = NULL; 184 | if (ncols_categ > 0) { 185 | categorical_data = (int*) options.get("categorical_data").c_str(); 186 | ncat = (int*) options.get("ncat").c_str(); 187 | } 188 | 189 | int *restrict ordinal_data = NULL; 190 | int *restrict ncat_ord = NULL; 191 | if (ncols_ord > 0) { 192 | ordinal_data = (int*) options.get("ordinal_data").c_str(); 193 | ncat_ord = (int*) options.get("ncat_ord").c_str(); 194 | } 195 | 196 | // options 197 | char *restrict cols_ignore = NULL; 198 | int nthreads = options.get("nthreads"); 199 | bool categ_as_bin = options.get("categ_as_bin"); 200 | bool ord_as_bin = options.get("ord_as_bin"); 201 | bool cat_bruteforce_subset = options.get("cat_bruteforce_subset"); 202 | bool categ_from_maj = options.get("categ_from_maj"); 203 | bool take_mid = options.get("take_mid"); 204 | size_t max_depth = options.get("max_depth"); 205 | double max_perc_outliers = options.get("pct_outliers"); 206 | size_t min_size_numeric = options.get("min_size_numeric"); 207 | size_t min_size_categ = options.get("min_size_categ"); 208 | double min_gain = options.get("min_gain"); 209 | bool gain_as_pct = options.get("gain_as_pct"); 210 | bool follow_all = options.get("follow_all"); 211 | double z_norm = options.get("z_norm"); 212 | double z_outlier = options.get("z_outlier"); 213 | 214 | fit_outliers_models( 215 | model_outputs, 216 | numeric_data, 217 | ncols_numeric, 218 | categorical_data, 219 | ncols_categ, 220 | ncat, 221 | ordinal_data, 222 | ncols_ord, 223 | ncat_ord, 224 | nrows, 225 | cols_ignore, 226 | nthreads, 227 | categ_as_bin, 228 | ord_as_bin, 229 | cat_bruteforce_subset, 230 | categ_from_maj, 231 | take_mid, 232 | max_depth, 233 | max_perc_outliers, 234 | min_size_numeric, 235 | min_size_categ, 236 | min_gain, 237 | gain_as_pct, 238 | follow_all, 239 | z_norm, 240 | z_outlier 241 | ); 242 | return model_outputs; 243 | }) 244 | .define_singleton_function( 245 | "find_new_outliers", 246 | [](ModelOutputs& model_outputs, Hash options) { 247 | // data 248 | size_t nrows = options.get("nrows"); 249 | size_t ncols_numeric = options.get("ncols_numeric"); 250 | size_t ncols_categ = options.get("ncols_categ"); 251 | size_t ncols_ord = options.get("ncols_ord"); 252 | 253 | double *restrict numeric_data = NULL; 254 | if (ncols_numeric > 0) { 255 | numeric_data = (double*) options.get("numeric_data").c_str(); 256 | } 257 | 258 | int *restrict categorical_data = NULL; 259 | if (ncols_categ > 0) { 260 | categorical_data = (int*) options.get("categorical_data").c_str(); 261 | } 262 | 263 | int *restrict ordinal_data = NULL; 264 | if (ncols_ord > 0) { 265 | ordinal_data = (int*) options.get("ordinal_data").c_str(); 266 | } 267 | 268 | // options 269 | int nthreads = options.get("nthreads"); 270 | 271 | find_new_outliers( 272 | numeric_data, 273 | categorical_data, 274 | ordinal_data, 275 | nrows, 276 | nthreads, 277 | model_outputs 278 | ); 279 | 280 | return model_outputs; 281 | }); 282 | } 283 | -------------------------------------------------------------------------------- /ext/outliertree/extconf.rb: -------------------------------------------------------------------------------- 1 | require "mkmf-rice" 2 | 3 | $CXXFLAGS += " -std=c++17 $(optflags) -DDONT_THROW_ON_INTERRUPT" 4 | 5 | apple_clang = RbConfig::CONFIG["CC_VERSION_MESSAGE"] =~ /apple clang/i 6 | 7 | # check omp first 8 | if have_library("omp") || have_library("gomp") 9 | $CXXFLAGS += " -Xclang" if apple_clang 10 | $CXXFLAGS += " -fopenmp" 11 | end 12 | 13 | ext = File.expand_path(".", __dir__) 14 | outliertree = File.expand_path("../../vendor/outliertree/src", __dir__) 15 | 16 | exclude = %w(Rwrapper.cpp RcppExports.cpp) 17 | $srcs = Dir["{#{ext},#{outliertree}}/*.{cc,cpp}"].reject { |f| exclude.include?(File.basename(f)) } 18 | $INCFLAGS += " -I#{outliertree}" 19 | $VPATH << outliertree 20 | 21 | create_makefile("outliertree/ext") 22 | -------------------------------------------------------------------------------- /lib/outliertree.rb: -------------------------------------------------------------------------------- 1 | # ext 2 | require "outliertree/ext" 3 | 4 | # stdlib 5 | require "etc" 6 | 7 | # modules 8 | require_relative "outliertree/dataset" 9 | require_relative "outliertree/model" 10 | require_relative "outliertree/result" 11 | require_relative "outliertree/version" 12 | 13 | module OutlierTree 14 | def self.new(**options) 15 | Model.new(**options) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /lib/outliertree/dataset.rb: -------------------------------------------------------------------------------- 1 | module OutlierTree 2 | class Dataset 3 | attr_reader :numeric_columns, :categorical_columns 4 | 5 | def initialize(data) 6 | @data = data 7 | 8 | if defined?(Rover::DataFrame) && data.is_a?(Rover::DataFrame) 9 | @vectors = data.vectors 10 | @numeric_columns, @categorical_columns = data.keys.partition { |k, v| ![:object, :bool].include?(data[k].type) } 11 | else 12 | @vectors = {} 13 | raise ArgumentError, "Array elements must be hashes" unless data.all? { |d| d.is_a?(Hash) } 14 | keys = data.flat_map(&:keys).uniq 15 | keys.each do |k| 16 | @vectors[k] = [] 17 | end 18 | data.each do |d| 19 | keys.each do |k| 20 | @vectors[k] << d[k] 21 | end 22 | end 23 | @numeric_columns, @categorical_columns = keys.partition { |k| @vectors[k].all? { |v| v.nil? || v.is_a?(Numeric) } } 24 | end 25 | end 26 | 27 | def [](k) 28 | @vectors[k] 29 | end 30 | 31 | def size 32 | @vectors.any? ? @vectors.values.first.size : 0 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/outliertree/model.rb: -------------------------------------------------------------------------------- 1 | module OutlierTree 2 | class Model 3 | def initialize( 4 | max_depth: 4, 5 | min_gain: 0.01, 6 | z_norm: 2.67, 7 | z_outlier: 8.0, 8 | pct_outliers: 0.01, 9 | min_size_numeric: 25, 10 | min_size_categ: 50, 11 | categ_split: "binarize", 12 | categ_outliers: "tail", 13 | numeric_split: "raw", 14 | follow_all: false, 15 | gain_as_pct: true, 16 | nthreads: -1 17 | ) 18 | # TODO validate values 19 | @max_depth = max_depth 20 | @min_gain = min_gain 21 | @z_norm = z_norm 22 | @z_outlier = z_outlier 23 | @pct_outliers = pct_outliers 24 | @min_size_numeric = min_size_numeric 25 | @min_size_categ = min_size_categ 26 | @categ_split = categ_split 27 | @categ_outliers = categ_outliers 28 | @numeric_split = numeric_split 29 | @follow_all = follow_all 30 | @gain_as_pct = gain_as_pct 31 | 32 | # etc module returns virtual cores 33 | nthreads = Etc.nprocessors if nthreads < 0 34 | @nthreads = nthreads 35 | end 36 | 37 | def fit(df) 38 | df = Dataset.new(df) 39 | prep_fit(df) 40 | options = data_options(df).merge(fit_options) 41 | @model_outputs = Ext.fit_outliers_models(options) 42 | end 43 | 44 | def outliers(df) 45 | raise "Not fit" unless @model_outputs 46 | 47 | df = Dataset.new(df) 48 | prep_predict(df) 49 | options = data_options(df).merge(nthreads: @nthreads) 50 | model_outputs = Ext.find_new_outliers(@model_outputs, options) 51 | 52 | Result.new( 53 | model_outputs: model_outputs, 54 | df: df, 55 | numeric_columns: @numeric_columns, 56 | categorical_columns: @categorical_columns, 57 | categories: @categories 58 | ).process 59 | end 60 | 61 | private 62 | 63 | def prep_fit(df) 64 | @numeric_columns = df.numeric_columns 65 | @categorical_columns = df.categorical_columns 66 | @categories = {} 67 | @categorical_columns.each do |k| 68 | @categories[k] = df[k].uniq.to_a.compact.map.with_index.to_h 69 | end 70 | end 71 | 72 | def prep_predict(df) 73 | # TODO handle column type mismatches 74 | (@numeric_columns + @categorical_columns).each do |k| 75 | raise ArgumentError, "Missing column: #{k}" unless df[k] 76 | end 77 | end 78 | 79 | def data_options(df) 80 | options = {} 81 | 82 | # numeric 83 | numeric_data = String.new 84 | @numeric_columns.each do |k| 85 | # more efficient for Rover 86 | numeric_data << (df[k].respond_to?(:to_numo) ? df[k].to_numo.cast_to(Numo::DFloat).to_binary : df[k].map { |v| v.nil? ? Float::NAN : v }.pack("d*")) 87 | end 88 | options[:numeric_data] = numeric_data 89 | options[:ncols_numeric] = @numeric_columns.size 90 | 91 | # categorical 92 | categorical_data = String.new 93 | ncat = String.new 94 | @categorical_columns.each do |k| 95 | categories = @categories[k] 96 | # for unseen values, set to categories.size 97 | categories_size = categories.size 98 | values = df[k].map { |v| v.nil? ? -1 : (categories[v] || categories_size) } 99 | # TODO make more efficient 100 | if values.any? { |v| v == categories_size } 101 | warn "[outliertree] Unseen values in column: #{k}" 102 | end 103 | # more efficient for Rover 104 | categorical_data << (values.respond_to?(:to_numo) ? values.to_numo.cast_to(Numo::Int32).to_binary : values.pack("i*")) 105 | ncat << [categories.size].pack("i") 106 | end 107 | options[:categorical_data] = categorical_data 108 | options[:ncols_categ] = @categorical_columns.size 109 | options[:ncat] = ncat 110 | 111 | # not supported yet 112 | options[:ordinal_data] = nil 113 | options[:ncols_ord] = 0 114 | options[:ncat_ord] = nil 115 | 116 | options[:nrows] = df.size 117 | options 118 | end 119 | 120 | def fit_options 121 | keys = %i( 122 | max_depth min_gain z_norm z_outlier pct_outliers 123 | min_size_numeric min_size_categ follow_all gain_as_pct nthreads 124 | ) 125 | options = {} 126 | keys.each do |k| 127 | options[k] = instance_variable_get("@#{k}") 128 | end 129 | options[:categ_as_bin] = @categ_split == "binarize" 130 | options[:ord_as_bin] = @categ_split == "binarize" 131 | options[:cat_bruteforce_subset] = @categ_split == "bruteforce" 132 | options[:categ_from_maj] = @categ_outliers == "majority" 133 | options[:take_mid] = @numeric_split == "mid" 134 | options 135 | end 136 | end 137 | end 138 | -------------------------------------------------------------------------------- /lib/outliertree/result.rb: -------------------------------------------------------------------------------- 1 | module OutlierTree 2 | class Result 3 | attr_reader :model_outputs, :df 4 | 5 | def initialize(model_outputs:, df:, numeric_columns:, categorical_columns:, categories:) 6 | @model_outputs = model_outputs 7 | @df = df 8 | @numeric_columns = numeric_columns 9 | @categorical_columns = categorical_columns 10 | @categories = categories 11 | end 12 | 13 | def process 14 | outliers = [] 15 | model_outputs.outlier_scores_final.each_with_index do |score, row| 16 | if score < 1 17 | outl_col = model_outputs.outlier_columns_final[row] 18 | outl_clust = model_outputs.outlier_clusters_final[row] 19 | outl_tree = model_outputs.outlier_trees_final[row] 20 | 21 | # column and value 22 | if outl_col < @numeric_columns.size 23 | column = @numeric_columns[outl_col] 24 | value = df[column][row] 25 | _decimals = model_outputs.outlier_decimals_distr[row] 26 | else 27 | column = @categorical_columns[outl_col - @numeric_columns.size] 28 | value = df[column][row] 29 | end 30 | 31 | # group statistics 32 | group_statistics = {} 33 | if outl_col < @numeric_columns.size 34 | cluster = model_outputs.all_clusters(outl_col, outl_clust) 35 | if value >= cluster.upper_lim 36 | group_statistics[:upper_thr] = cluster.display_lim_high 37 | group_statistics[:pct_below] = cluster.perc_below 38 | else 39 | group_statistics[:lower_thr] = cluster.display_lim_low 40 | group_statistics[:pct_above] = cluster.perc_above 41 | end 42 | group_statistics[:mean] = cluster.display_mean 43 | group_statistics[:sd] = cluster.display_sd 44 | group_statistics[:n_obs] = cluster.cluster_size 45 | else 46 | # TODO categorical stats 47 | end 48 | 49 | # conditions 50 | conditions = [] 51 | if cluster.column_type != :no_type 52 | conditions << add_condition(row, cluster.split_type, cluster) 53 | end 54 | 55 | # add conditions from tree branches 56 | curr_tree = outl_tree 57 | loop do 58 | break if curr_tree == 0 59 | 60 | tree = model_outputs.all_trees(outl_col, curr_tree) 61 | break if tree.parent_branch == :sub_trees 62 | 63 | parent_tree = tree.parent 64 | parent_cluster = model_outputs.all_trees(outl_col, parent_tree) 65 | 66 | if parent_cluster.all_branches.size > 0 67 | raise "Branch not supported yet. Please report an issue." 68 | else 69 | conditions << add_condition(row, tree.parent_branch, parent_cluster) 70 | end 71 | 72 | curr_tree = parent_tree 73 | end 74 | 75 | clean_conditions(conditions) 76 | 77 | outliers << { 78 | index: row, 79 | explanation: create_explanation(column, value, conditions, group_statistics), 80 | column: column, 81 | value: value, 82 | conditions: conditions, 83 | group_statistics: group_statistics 84 | # leave out for simplicity 85 | # score: score, 86 | # tree_depth: model_outputs.outlier_depth_final[row], 87 | # has_na_branch: model_outputs.all_clusters(outl_col, outl_clust).has_na_branch 88 | } 89 | end 90 | end 91 | outliers 92 | end 93 | 94 | private 95 | 96 | def add_condition(row, split_type, cluster) 97 | _coldecim = 0 98 | case cluster.column_type 99 | when :numeric 100 | cond_col = @numeric_columns[cluster.col_num] 101 | _coldecim = model_outputs.min_decimals_col[cluster.col_num] 102 | else 103 | cond_col = @categorical_columns[cluster.col_num] 104 | end 105 | colval = df[cond_col][row] 106 | 107 | case split_type 108 | when :greater, :less_or_equal 109 | colcond = split_type == :greater ? ">" : "<=" 110 | condval = cluster.split_point 111 | when :not_in_subset, :in_subset 112 | colcond = "in" 113 | condval = @categories[cond_col].keys.zip(cluster.split_subset) 114 | if split_type == :in_subset 115 | condval.select! { |k, v| v > 0 } 116 | else 117 | condval.select! { |k, v| v == 0 } 118 | end 119 | condval.map!(&:first) 120 | when :equal, :not_equal 121 | colcond = split_type == :equal ? "=" : "!=" 122 | condval = @categories[cond_col].keys[cluster.split_lev] 123 | else 124 | raise "Split type not supported yet: #{split_type}. Please report an issue." 125 | end 126 | 127 | { 128 | column: cond_col, 129 | comparison: colcond, 130 | to: condval, 131 | value: colval 132 | # leave out for simplicity 133 | # decimals: coldecim 134 | } 135 | end 136 | 137 | # remove overlapping conditions 138 | # could be more efficient, but should be few conditions 139 | def clean_conditions(conditions) 140 | conditions.reject! do |condition| 141 | conditions.any? do |other| 142 | if condition[:column] == other[:column] && condition[:comparison] == other[:comparison] 143 | case condition[:comparison] 144 | when ">" 145 | # not <= 146 | condition[:to] < other[:to] 147 | when "<=" 148 | condition[:to] > other[:to] 149 | end 150 | end 151 | end 152 | end 153 | end 154 | 155 | def create_explanation(column, value, conditions, group_statistics) 156 | explanation = String.new("#{column} (#{value}) ") 157 | 158 | looks = 159 | if group_statistics[:upper_thr] 160 | "high" 161 | elsif group_statistics[:lower_thr] 162 | "low" 163 | else 164 | "interesting" 165 | end 166 | explanation << "looks #{looks}" 167 | 168 | if conditions.any? 169 | explanation << " given" 170 | conditions.each_with_index do |condition, i| 171 | comparison = condition[:comparison] == "=" ? "is" : condition[:comparison] 172 | to = condition[:to] 173 | to = to.join(", ") if to.is_a?(Array) 174 | explanation << " #{condition[:column]} #{comparison} #{to}" 175 | 176 | # proper grammar 177 | if conditions.size > 1 178 | if i == conditions.size - 2 179 | explanation << " and" 180 | elsif i < conditions.size - 2 181 | explanation << "," 182 | end 183 | end 184 | end 185 | end 186 | 187 | explanation 188 | end 189 | end 190 | end 191 | -------------------------------------------------------------------------------- /lib/outliertree/version.rb: -------------------------------------------------------------------------------- 1 | module OutlierTree 2 | VERSION = "0.4.1" 3 | end 4 | -------------------------------------------------------------------------------- /outliertree.gemspec: -------------------------------------------------------------------------------- 1 | require_relative "lib/outliertree/version" 2 | 3 | Gem::Specification.new do |spec| 4 | spec.name = "outliertree" 5 | spec.version = OutlierTree::VERSION 6 | spec.summary = "Explainable outlier/anomaly detection for Ruby" 7 | spec.homepage = "https://github.com/ankane/outliertree-ruby" 8 | spec.license = "GPL-3.0-or-later" 9 | 10 | spec.author = "Andrew Kane" 11 | spec.email = "andrew@ankane.org" 12 | 13 | spec.files = Dir["*.{md,txt}", "{ext,lib}/**/*", "vendor/outliertree/{LICENSE,README.md}", "vendor/outliertree/src/**/*"] 14 | spec.require_path = "lib" 15 | spec.extensions = ["ext/outliertree/extconf.rb"] 16 | 17 | spec.required_ruby_version = ">= 3.1" 18 | 19 | spec.add_dependency "rice", ">= 4.3.3" 20 | end 21 | -------------------------------------------------------------------------------- /test/outliertree_test.rb: -------------------------------------------------------------------------------- 1 | require_relative "test_helper" 2 | 3 | class OutlierTreeTest < Minitest::Test 4 | def setup 5 | @train_data = nil 6 | @test_data = nil 7 | end 8 | 9 | def test_works 10 | model = OutlierTree.new 11 | model.fit(train_data) 12 | outliers = model.outliers(test_data) 13 | 14 | assert_equal 1, outliers.size 15 | 16 | outlier = outliers.first 17 | assert_equal 0, outlier[:index] 18 | assert_equal "numeric_col2 (-1000000.0) looks low", outlier[:explanation] 19 | assert_equal :numeric_col2, outlier[:column] 20 | assert_equal(-1000000, outlier[:value]) 21 | assert_equal [], outlier[:conditions] 22 | end 23 | 24 | def test_rover 25 | require "rover" 26 | 27 | train_df = Rover::DataFrame.new(train_data) 28 | test_df = Rover::DataFrame.new(test_data) 29 | 30 | model = OutlierTree.new 31 | model.fit(train_df) 32 | outliers = model.outliers(test_df) 33 | 34 | assert_equal 1, outliers.size 35 | 36 | outlier = outliers.first 37 | assert_equal 0, outlier[:index] 38 | assert_equal "numeric_col2 (-1000000.0) looks low", outlier[:explanation] 39 | assert_equal :numeric_col2, outlier[:column] 40 | assert_equal(-1000000, outlier[:value]) 41 | assert_equal [], outlier[:conditions] 42 | end 43 | 44 | def test_missing_values 45 | model = OutlierTree.new 46 | train_data[1][:numeric_col1] = nil 47 | train_data[3][:categ_col] = nil 48 | model.fit(train_data) 49 | outliers = model.outliers(test_data) 50 | assert_equal 1, outliers.size 51 | end 52 | 53 | def test_not_fit 54 | model = OutlierTree.new 55 | error = assert_raises do 56 | model.outliers(test_data) 57 | end 58 | assert_equal "Not fit", error.message 59 | end 60 | 61 | def test_predict_missing_column 62 | model = OutlierTree.new 63 | model.fit(train_data) 64 | test_data.each { |v| v.delete(:numeric_col2) } 65 | error = assert_raises(ArgumentError) do 66 | model.outliers(test_data) 67 | end 68 | assert_equal "Missing column: numeric_col2", error.message 69 | end 70 | 71 | def test_predict_unseen_value 72 | model = OutlierTree.new 73 | model.fit(train_data) 74 | test_data.last[:categ_col] = "categD" 75 | expected = "[outliertree] Unseen values in column: categ_col\n" 76 | assert_output(nil, expected) do 77 | model.outliers(test_data) 78 | end 79 | end 80 | 81 | def train_data 82 | @train_data ||= dataset("train") 83 | end 84 | 85 | def test_data 86 | @test_data ||= dataset("test") 87 | end 88 | 89 | def dataset(name) 90 | CSV.table("test/support/#{name}.csv").map(&:to_h) 91 | end 92 | end 93 | -------------------------------------------------------------------------------- /test/support/predict.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from outliertree import OutlierTree 3 | 4 | train_df = pd.read_csv('test/support/train.csv', float_precision='high') 5 | test_df = pd.read_csv('test/support/test.csv', float_precision='high') 6 | 7 | model = OutlierTree(nthreads=1) 8 | model.fit(train_df, outliers_print=0) 9 | 10 | # unseen values 11 | # test_df.loc[-2, 'categ_col'] = 'categD' 12 | # test_df.loc[-1, 'categ_col'] = 'categE' 13 | 14 | predictions = model.predict(test_df) 15 | outliers = predictions[predictions['outlier_score'].notnull()].to_dict('records') 16 | print(outliers) 17 | -------------------------------------------------------------------------------- /test/support/test.csv: -------------------------------------------------------------------------------- 1 | numeric_col1,numeric_col2,categ_col 2 | 0.6980320340722189,-1000000.0,categB 3 | -1.0571195357282195,0.8521920906787348,categA 4 | 0.5421572747513552,0.4478781579960474,categC 5 | 0.9194094892055672,3.1868904195943135,categB 6 | 0.05073417601741061,4.338416778859276,categB 7 | 1.2013533604724205,0.4896112961410629,categC 8 | -0.08601853396647267,1.8089870446099006,categC 9 | -0.1481365755111718,4.025323983936054,categB 10 | 0.13912627031251631,0.8803276825213846,categA 11 | -1.9029086257149541,1.1114909032058342,categA 12 | 2.3301420730356743,0.09020543324699237,categA 13 | -0.3062655343446691,4.761890205385033,categB 14 | -1.4560635733239236,1.5118584602124725,categB 15 | -2.0749051910263945,5.178721023846309,categC 16 | -0.2454532065443549,1.060420244715674,categC 17 | 0.0029789962527352106,0.03814634851681666,categB 18 | -0.2677463549117358,1.2697186803460674,categB 19 | -0.4037817443553943,1.1651242631038827,categB 20 | 0.15727179858227153,0.9385697991394971,categA 21 | 0.3389351501227822,0.11228586544828592,categA 22 | -1.1042314891489586,0.32945562871929607,categC 23 | 0.7030658993569658,1.4970678503295656,categB 24 | -0.32545970675632646,2.2087879070550542,categC 25 | -0.6397477676506512,0.7145737121278929,categC 26 | 0.6575762831237139,1.2577531503615538,categC 27 | 0.6161910397425115,0.0809702463863562,categB 28 | -1.023766702647537,0.9553024727719274,categC 29 | 0.0368526058222781,1.818948982387241,categA 30 | -1.2227894297141741,1.1907453272175161,categB 31 | -0.9851014203318917,0.8098143953821971,categB 32 | 1.118358502414252,1.9746466397196678,categB 33 | -0.7369548854409249,1.3677533537395679,categB 34 | -0.18820403485461892,0.30727178986277026,categA 35 | -1.2137726194435934,8.373599229415277,categB 36 | 0.5047001544392725,0.8526867905773534,categA 37 | -1.4922319016761136,1.019040689137395,categA 38 | 1.0456310471225272,0.6727557129722816,categA 39 | -0.5245465917804782,2.201606951265767,categA 40 | 1.4513981128271614,2.0755313323661437,categC 41 | 0.6957200104957912,1.8824940173256368,categB 42 | 1.6654320045628033,1.0433399214203134,categC 43 | -1.0620601826772573,0.8808720932255958,categA 44 | -0.7231270160002728,0.3307048131613828,categC 45 | 1.1052778757693549,4.785973275793242,categA 46 | 0.6829056589764245,1.6077453121591272,categA 47 | 2.4032925586467084,0.6964250103761771,categC 48 | 0.2639984687572734,1.4797936669390763,categB 49 | -0.07020333231944395,0.13270891749487504,categA 50 | -0.5917589593813345,2.5385335107578526,categB 51 | -0.08718268553989575,0.045872845307043345,categA 52 | 0.29053467793943966,0.259554475702208,categB 53 | -2.141184219164164,1.8482431229411385,categC 54 | 1.3051161982222024,0.3646662374198733,categA 55 | -0.18435066622863627,0.6622718786309458,categC 56 | 1.3615241343499593,1.049975231329909,categB 57 | 0.08755411832848671,0.15541813437663463,categB 58 | 0.9992951357145193,0.3649926437725557,categB 59 | 0.7951542597882889,2.1796638668956487,categB 60 | 0.14718403051810938,2.1789695630905808,categC 61 | 1.1306071265677138,0.7240532119211895,categC 62 | -0.9291974675009477,0.31072902808376485,categA 63 | -0.0913790494319576,2.123129904297673,categA 64 | 1.3990099849444007,0.6891219104561784,categC 65 | 1.2334636463474642,0.40562401191899466,categB 66 | -0.04489194835596777,2.250909690207088,categB 67 | -1.6811608130137858,0.22876311401803665,categA 68 | 0.4056633076405743,1.7126045111793278,categA 69 | -1.7863235075185215,1.8253443024245253,categC 70 | -0.10564964422971469,0.7398112705612694,categC 71 | 0.6487539089894154,0.17666306940876048,categB 72 | -1.2139642507017367,2.9754791336143995,categC 73 | -0.09330509775572464,0.06128948619368555,categA 74 | 1.0365559262857864,2.175947916901278,categA 75 | -0.9447165623212421,0.854124297069524,categC 76 | 1.0781913930304066,0.47617768650163844,categC 77 | -1.4446361364982787,1.0241690124635436,categB 78 | -0.05359123858199108,0.13674454864706553,categA 79 | 1.4872648570736096,0.5220599436426289,categC 80 | 0.01549697990323449,0.4406935390912177,categB 81 | -0.14954944944184198,0.16387990149191342,categA 82 | 0.21520007727738835,1.9412325470107756,categC 83 | -0.8868175830149869,0.1075937246023753,categC 84 | 0.8719291929987495,3.0601855742271877,categC 85 | -1.065829456070261,0.18249814982069487,categA 86 | 0.18618358991730377,0.5787555615906835,categB 87 | -0.7057853833118962,0.1748492110121784,categB 88 | -0.21642053762949862,2.114213249993586,categB 89 | 1.9425785616831623,0.6509507945254517,categB 90 | 0.05721831545373639,0.509415613151093,categC 91 | -2.1238980167894783,0.32223236417504236,categC 92 | -0.6586725436729843,0.05302240761451909,categC 93 | -0.829595820759661,0.14932938079041763,categB 94 | -2.243605045956253,1.5079577335746104,categB 95 | 0.4113499894421548,0.19607978938784523,categC 96 | 0.23671791375177653,0.8757212527927741,categB 97 | 0.8250249015064804,2.6328104220741397,categB 98 | -0.5779241638736375,0.1139123966902765,categC 99 | 0.6565854588112914,0.6026564552749478,categC 100 | 1.493118935456973,0.16959389853122117,categA 101 | 0.8122258814143298,0.36270857548443974,categB 102 | -------------------------------------------------------------------------------- /test/support/train.csv: -------------------------------------------------------------------------------- 1 | numeric_col1,numeric_col2,categ_col 2 | 1.6243453636632417,1.6456102936933177,categC 3 | -0.6117564136500754,0.4907953166133395,categC 4 | -0.5281717522634557,1.9917373381857357,categC 5 | -1.0729686221561705,1.3748467072093855,categA 6 | 0.8654076293246785,0.8124719305257668,categB 7 | -2.3015386968802827,0.14670953058065908,categC 8 | 1.74481176421648,0.06178784320291027,categB 9 | -0.7612069008951028,0.1293611922847273,categB 10 | 0.31903909605709857,0.04557481143281571,categB 11 | -0.2493703754774101,0.11372218731758917,categB 12 | 1.462107937044974,0.25580794437232146,categA 13 | -2.060140709497654,1.2482346680804435,categB 14 | -0.3224172040135075,0.8203375362446026,categC 15 | -0.38405435466841564,0.01263547258245401,categB 16 | 1.1337694423354374,0.07469583072816746,categA 17 | -1.0998912673140309,3.419656609731613,categA 18 | -0.17242820755043575,0.8395622685227498,categC 19 | -0.8778584179213718,0.2272685909298382,categC 20 | 0.04221374671559283,0.2907878832533142,categB 21 | 0.5828152137158222,1.3618978082536763,categA 22 | -1.1006191772129212,0.21744666084034464,categA 23 | 1.1447237098396141,0.8707413545128535,categA 24 | 0.9015907205927955,3.5072244223421856,categC 25 | 0.5024943389018682,1.8761990386319407,categC 26 | 0.9008559492644118,0.27423654885144294,categA 27 | -0.6837278591743331,0.6807636030850309,categB 28 | -0.12289022551864817,0.9674675024755492,categB 29 | -0.9357694342590688,1.765980030180328,categB 30 | -0.2678880796260159,0.1705408956566086,categC 31 | 0.530355466738186,0.018750906769109437,categB 32 | -0.691660751725309,0.07259450356908541,categB 33 | -0.39675352685597737,0.666203661088188,categA 34 | -0.6871727001195994,0.9322409165816796,categB 35 | -0.8452056414987196,0.8413025547367118,categC 36 | -0.671246130836819,0.3817911741784129,categA 37 | -0.01266459891890136,4.475559982275411,categA 38 | -1.1173103486352778,0.8668941307814301,categA 39 | 0.23441569781709215,0.47826352464999505,categC 40 | 1.6598021771098705,0.8006170729848269,categB 41 | 0.7420441605773356,1.3678040883352993,categB 42 | -0.19183555236161492,1.106340756850409,categB 43 | -0.8876289640848363,0.30777534040128457,categA 44 | -0.7471582937508376,0.06863740013342447,categA 45 | 1.6924546010277466,0.4621691160108877,categA 46 | 0.05080775477602897,0.99348907012124,categC 47 | -0.6369956465693534,0.2359426234981121,categB 48 | 0.19091548466746602,1.3973777707692607,categC 49 | 2.100255136478842,0.06885339695052854,categC 50 | 0.12015895248162915,0.30153099235619035,categA 51 | 0.6172031097074192,1.6334978643810734,categB 52 | 0.3001703199558275,0.21496990007857378,categA 53 | -0.35224984649351865,1.020154814635117,categB 54 | -1.1425181980221402,0.7437466296585225,categC 55 | -0.3493427224128775,2.587710042996679,categC 56 | -0.2088942333747781,0.30557014154718976,categA 57 | 0.5866231911821976,0.06823718282444959,categC 58 | 0.8389834138745049,1.3282744020516106,categC 59 | 0.9311020813035573,1.4791907863652922,categC 60 | 0.2855873252542588,2.3839670992491837,categC 61 | 0.8851411642707281,2.687836911034196,categA 62 | -0.7543979409966528,0.014049810956373594,categB 63 | 1.2528681552332879,0.26704591828457724,categC 64 | 0.5129298204180088,0.9591417548395782,categA 65 | -0.29809283510271567,2.9762497109557278,categA 66 | 0.48851814653749703,2.999260876699656,categB 67 | -0.07557171302105573,0.8134029443292796,categB 68 | 1.131629387451427,2.472263114408025,categB 69 | 1.5198168164221988,1.0260113192518963,categA 70 | 2.1855754065331614,0.49430896802793367,categC 71 | -1.3964963354881377,0.6655138562928072,categC 72 | -1.4441138054295894,0.9271254230315796,categC 73 | -0.5044658629464512,0.7975035816397283,categA 74 | 0.16003706944783047,2.606144908395195,categC 75 | 0.8761689211162249,2.5100206094259283,categA 76 | 0.31563494724160523,0.5023212436103023,categA 77 | -2.022201215824003,3.3039580213976407,categC 78 | -0.3062040126283718,0.19110683460178537,categB 79 | 0.8279746426072462,0.1350519989486968,categC 80 | 0.2300947353643834,0.1451172884305069,categC 81 | 0.7620111803120247,0.7045361203822831,categA 82 | -0.22232814261035927,0.02175984277310264,categC 83 | -0.20075806892999745,2.9559388632246053,categB 84 | 0.1865613909882843,1.7551313707518712,categC 85 | 0.4100516472082563,0.015132907784669669,categA 86 | 0.19829972012676975,0.19382295168222638,categC 87 | 0.11900864580745882,0.4035622811823979,categA 88 | -0.6706622862890306,0.140408522895853,categB 89 | 0.3775637863209194,1.658054225381088,categA 90 | 0.12182127099143693,0.4227180671780609,categC 91 | 1.1294839079119197,2.8152036954088007,categC 92 | 1.198917879901507,0.8723077703511628,categC 93 | 0.18515641748394385,2.1105771379615637,categB 94 | -0.3752849500901142,1.8626183724769767,categC 95 | -0.6387304074542224,2.3580166086376604,categC 96 | 0.4234943540641129,0.6159644340333974,categB 97 | 0.07734006834855942,0.7904222848093443,categA 98 | -0.3438536755710756,1.60248012980024,categB 99 | 0.04359685683424694,0.3364786290694512,categB 100 | -0.6200008439481293,0.6738417800368404,categC 101 | 1000000.0,0.9140689709285671,categC 102 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | Bundler.require(:default) 3 | require "minitest/autorun" 4 | require "minitest/pride" 5 | require "csv" 6 | --------------------------------------------------------------------------------