Learn how to apply testdriven development (TDD) to machinelearning algorithmsand catch mistakes that could sink your
291 80 6MB
English Pages xiv, 215 pages: illustrations; 24 cm [235] Year 2014;2015
Table of contents :
Table of Contents......Page 5
What to Expect from This Book......Page 11
How to Contact Me......Page 12
Conventions Used in This Book......Page 13
Safari® Books Online......Page 14
Acknowledgments......Page 15
Chapter 1. TestDriven Machine Learning......Page 17
TDD and the Scientific Method......Page 18
TDD Makes a Logical Proposition of Validity......Page 19
TDD and Scientific Method Work in Feedback Loops......Page 21
Underfitting......Page 22
Overfitting......Page 24
Mitigate Unstable Data with Seam Testing......Page 25
Check Fit by CrossValidating......Page 26
Reduce Overfitting Risk by Testing the Speed of Training......Page 28
Conclusion......Page 29
What Is Machine Learning?......Page 31
Unsupervised Learning......Page 32
What Can Machine Learning Accomplish?......Page 33
Mathematical Notation Used Throughout the Book......Page 34
Conclusion......Page 35
Chapter 3. KNearest Neighbors Classification......Page 37
House Happiness Based on a Neighborhood......Page 38
Guessing K......Page 41
Heuristics for Picking K......Page 42
What Makes a Neighbor “Near”?......Page 45
Minkowski Distance......Page 46
Mahalanobis Distance......Page 47
Determining Classes......Page 48
Beard and Glasses Detection Using KNN and OpenCV......Page 50
The Class Diagram......Page 51
Raw Image to Avatar......Page 52
The Face Class......Page 55
The Neighborhood Class......Page 58
Conclusion......Page 66
Using Bayes’s Theorem to Find Fraudulent Orders......Page 67
Conditional Probabilities......Page 68
Naive Bayesian Classifier......Page 70
Naivety in Bayesian Reasoning......Page 71
Pseudocount......Page 72
Spam Filter......Page 73
The Class Diagram......Page 74
Email Class......Page 75
Tokenization and Context......Page 77
The SpamTrainer......Page 79
Error Minimization Through CrossValidation......Page 86
Conclusion......Page 89
Tracking User Behavior Using State Machines......Page 91
Emissions/Observations of Underlying States......Page 93
Using Markov Chains Instead of a Finite State Machine......Page 95
Evaluation: ForwardBackward Algorithm......Page 96
Using User Behavior......Page 97
The Decoding Problem through the Viterbi Algorithm......Page 100
PartofSpeech Tagging with the Brown Corpus......Page 101
The Seam of Our PartofSpeech Tagger: CorpusParser......Page 102
Writing the PartofSpeech Tagger......Page 104
CrossValidating to Get Confidence in the Model......Page 112
Conclusion......Page 113
Solving the Loyalty Mapping Problem......Page 115
Derivation of SVM......Page 117
The Kernel Trick......Page 118
Soft Margins......Page 122
The Class Diagram......Page 124
Corpus Class......Page 125
Return a Unique Set of Words from the Corpus......Page 129
The CorpusSet Class......Page 130
The SentimentClassifier Class......Page 134
Conclusion......Page 139
History of Neural Networks......Page 141
What Is an Artificial Neural Network?......Page 142
Input Layer......Page 143
Hidden Layers......Page 144
Neurons......Page 145
Training Algorithms......Page 151
How Many Hidden Layers?......Page 155
Tolerance for Error and Max Epochs......Page 156
Using a Neural Network to Classify a Language......Page 157
Writing the Seam Test for Language......Page 159
CrossValidating Our Way to a Network Class......Page 162
WrapUp of Example......Page 166
Conclusion......Page 167
Chapter 8. Clustering......Page 169
User Cohorts......Page 170
The KMeans Algorithm......Page 172
Expectation Maximization (EM) Clustering......Page 173
Categorizing Music......Page 175
Gathering the Data......Page 176
Analyzing the Data with KMeans......Page 177
EM Clustering......Page 179
EM Jazz Clustering Results......Page 183
Conclusion......Page 184
Collaborative Filtering......Page 185
Linear Regression Applied to Collaborative Filtering......Page 187
Introducing Regularization, or Ridge Regression......Page 189
WrapUp of Theory......Page 191
The Tools We Will Need......Page 192
Reviewer......Page 195
Writing the Code to Figure Out Someone’s Preference......Page 197
Conclusion......Page 200
The Problem with the Curse of Dimensionality......Page 203
Feature Selection......Page 204
Feature Transformation......Page 207
Principal Component Analysis (PCA)......Page 210
Independent Component Analysis (ICA)......Page 211
Monitoring Machine Learning Algorithms......Page 213
Precision and Recall: Spam Filter......Page 214
Mean Squared Error......Page 216
The Wilds of Production Environments......Page 218
Conclusion......Page 219
Machine Learning Algorithms Revisited......Page 221
What’s Next for You?......Page 223
Index......Page 225
About the Author......Page 234
Thoughtful Machine Learning
Machinelearning algorithms often have tests baked in, but they can’t account for human errors in coding. Rather than blindly rely on machinelearning results as many researchers have, you can mitigate the risk of errors with TDD and write clean, stable machinelearning code. If you’re familiar with Ruby 2.1, you’re ready to start. ■■
Apply TDD to write and run tests before you start coding
■■
Learn the best uses and tradeoffs of eight machinelearning algorithms
■■
Use realworld examples to test each algorithm through engaging, handson exercises
■■
Understand the similarities between TDD and the scientific method for validating solutions
■■
Be aware of the risks of machine learning, such as underfitting and overfitting data
■■
Explore techniques for improving your machinelearning models or data extraction
is a very fascinating “ This read, and it is a great resource for developers interested in the science behind machine learning.
”
—Brad Ediger
author, Advanced Rails
is an awesome “ This book.”
—Starr Horne
cofounder, Honeybadger
pumped about “ Pretty [Matthew Kirk]’s Thoughtful Machine Learning book.
”
—James Edward Gray II
consultant, Gray Soft
Thoughtful Machine Learning
Learn how to apply testdriven development (TDD) to machinelearning algorithms—and catch mistakes that could sink your analysis. In this practical guide, author Matthew Kirk takes you through the principles of TDD and machine learning, and shows you how to apply TDD to several machinelearning algorithms, including Naive Bayesian classifiers and Neural Networks.
Matthew Kirk is the founder of Modulus 7, a data science and Ruby development consulting firm. Matthew speaks at conferences around the world about using machine learning and data science with Ruby.
US $39.99
Twitter: @oreillymedia facebook.com/oreilly
Kirk
PROGR AMMING/MACHINE LEARNING
Thoughtful Machine Learning A TESTDRIVEN APPROACH
CAN $41.99
ISBN: 9781449374068
Matthew Kirk
Thoughtful Machine Learning
Machinelearning algorithms often have tests baked in, but they can’t account for human errors in coding. Rather than blindly rely on machinelearning results as many researchers have, you can mitigate the risk of errors with TDD and write clean, stable machinelearning code. If you’re familiar with Ruby 2.1, you’re ready to start. ■■
Apply TDD to write and run tests before you start coding
■■
Learn the best uses and tradeoffs of eight machinelearning algorithms
■■
Use realworld examples to test each algorithm through engaging, handson exercises
■■
Understand the similarities between TDD and the scientific method for validating solutions
■■
Be aware of the risks of machine learning, such as underfitting and overfitting data
■■
Explore techniques for improving your machinelearning models or data extraction
is a very fascinating “ This read, and it is a great resource for developers interested in the science behind machine learning.
”
—Brad Ediger
author, Advanced Rails
is an awesome “ This book.”
—Starr Horne
cofounder, Honeybadger
pumped about “ Pretty [Matthew Kirk]’s Thoughtful Machine Learning book.
”
—James Edward Gray II
consultant, Gray Soft
Thoughtful Machine Learning
Learn how to apply testdriven development (TDD) to machinelearning algorithms—and catch mistakes that could sink your analysis. In this practical guide, author Matthew Kirk takes you through the principles of TDD and machine learning, and shows you how to apply TDD to several machinelearning algorithms, including Naive Bayesian classifiers and Neural Networks.
Matthew Kirk is the founder of Modulus 7, a data science and Ruby development consulting firm. Matthew speaks at conferences around the world about using machine learning and data science with Ruby.
US $39.99
Twitter: @oreillymedia facebook.com/oreilly
Kirk
PROGR AMMING/MACHINE LEARNING
Thoughtful Machine Learning A TESTDRIVEN APPROACH
CAN $41.99
ISBN: 9781449374068
Matthew Kirk
Thoughtful Machine Learning
Matthew Kirk
Thoughtful Machine Learning by Matthew Kirk Copyright © 2015 Itzy, Kickass.so. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 8009989938 or [email protected] .
Editors: Mike Loukides and Ann Spencer Production Editor: Melanie Yarbrough Copyeditor: Rachel Monaghan Proofreader: Jasmine Kwityn
Indexer: Ellen TroutmanZaig Interior Designer: David Futato Cover Designer: Ellie Volkhausen Illustrator: Rebecca Demarest
First Edition
October 2014:
Revision History for the First Edition 20140923:
First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781449374068 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Thoughtful Machine Learning, the cover image of a Eurasian eagleowl, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
9781449374068 [LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. TestDriven Machine Learning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 History of TestDriven Development TDD and the Scientific Method TDD Makes a Logical Proposition of Validity TDD Involves Writing Your Assumptions Down on Paper or in Code TDD and Scientific Method Work in Feedback Loops Risks with Machine Learning Unstable Data Underfitting Overfitting Unpredictable Future What to Test for to Reduce Risks Mitigate Unstable Data with Seam Testing Check Fit by CrossValidating Reduce Overfitting Risk by Testing the Speed of Training Monitor for Future Shifts with Precision and Recall Conclusion
2 2 3 5 5 6 6 6 8 9 9 9 10 12 13 13
2. A Quick Introduction to Machine Learning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 What Is Machine Learning? Supervised Learning Unsupervised Learning Reinforcement Learning What Can Machine Learning Accomplish? Mathematical Notation Used Throughout the Book Conclusion
15 16 16 17 17 18 13 iii
3. KNearest Neighbors Classification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 History of KNearest Neighbors Classification House Happiness Based on a Neighborhood How Do You Pick K? Guessing K Heuristics for Picking K Algorithms for Picking K What Makes a Neighbor “Near”? Minkowski Distance Mahalanobis Distance Determining Classes Beard and Glasses Detection Using KNN and OpenCV The Class Diagram Raw Image to Avatar The Face Class The Neighborhood Class Conclusion
22 22 25 25 26 29 29 30 31 32 34 35 36 39 42 50
4. Naive Bayesian Classification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Using Bayes’s Theorem to Find Fraudulent Orders Conditional Probabilities Inverse Conditional Probability (aka Bayes’s Theorem) Naive Bayesian Classifier The Chain Rule Naivety in Bayesian Reasoning Pseudocount Spam Filter The Class Diagram Data Source Email Class Tokenization and Context The SpamTrainer Error Minimization Through CrossValidation Conclusion
51 52 54 54 55 55 56 57 58 59 59 61 63 70 73
5. Hidden Markov Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Tracking User Behavior Using State Machines Emissions/Observations of Underlying States Simplification through the Markov Assumption Using Markov Chains Instead of a Finite State Machine Hidden Markov Model Evaluation: ForwardBackward Algorithm
iv

Table of Contents
75 77 79 79 80 80
Using User Behavior The Decoding Problem through the Viterbi Algorithm The Learning Problem PartofSpeech Tagging with the Brown Corpus The Seam of Our PartofSpeech Tagger: CorpusParser Writing the PartofSpeech Tagger CrossValidating to Get Confidence in the Model How to Make This Model Better Conclusion
81 84 85 85 86 88 96 97 97
6. Support Vector Machines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Solving the Loyalty Mapping Problem Derivation of SVM Nonlinear Data The Kernel Trick Soft Margins Using SVM to Determine Sentiment The Class Diagram Corpus Class Return a Unique Set of Words from the Corpus The CorpusSet Class The SentimentClassifier Class Improving Results Over Time Conclusion
99 101 102 102 106 108 108 109 113 114 118 123 123
7. Neural Networks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 History of Neural Networks What Is an Artificial Neural Network? Input Layer Hidden Layers Neurons Output Layer Training Algorithms Building Neural Networks How Many Hidden Layers? How Many Neurons for Each Layer? Tolerance for Error and Max Epochs Using a Neural Network to Classify a Language Writing the Seam Test for Language CrossValidating Our Way to a Network Class Tuning the Neural Network Convergence Testing
125 126 127 128 129 135 135 139 139 140 140 141 143 146 150 150
Table of Contents

v
Precision and Recall for Neural Networks WrapUp of Example Conclusion
150 150 151
8. Clustering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 User Cohorts KMeans Clustering The KMeans Algorithm The Downside of KMeans Clustering Expectation Maximization (EM) Clustering The Impossibility Theorem Categorizing Music Gathering the Data Analyzing the Data with KMeans EM Clustering EM Jazz Clustering Results Conclusion
154 156 156 157 157 159 159 160 161 163 167 168
9. Kernel Ridge Regression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Collaborative Filtering Linear Regression Applied to Collaborative Filtering Introducing Regularization, or Ridge Regression Kernel Ridge Regression WrapUp of Theory Collaborative Filtering with Beer Styles Data Set The Tools We Will Need Reviewer Writing the Code to Figure Out Someone’s Preference Collaborative Filtering with User Preferences Conclusion
169 171 173 175 175 176 176 176 179 181 184 184
10. Improving Models and Data Extraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 The Problem with the Curse of Dimensionality Feature Selection Feature Transformation Principal Component Analysis (PCA) Independent Component Analysis (ICA) Monitoring Machine Learning Algorithms Precision and Recall: Spam Filter The Confusion Matrix Mean Squared Error
vi

Table of Contents
187 188 191 194 195 197 198 200 200
The Wilds of Production Environments Conclusion
202 203
11. Putting It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Machine Learning Algorithms Revisited How to Use This Information for Solving Problems What’s Next for You?
205 207 207
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Table of Contents

vii
Preface
This book is about approaching tough problems. Machine learning is an amazing application of computation because it tackles problems that are straight out of science fiction. These algorithms can solve voice recognition, mapping, recommendations, and disease detection. The applications are endless, which is what makes machine learning so fascinating. This flexibility is also what makes machine learning daunting. It can solve many problems, but how do we know whether we’re solving the right problem, or actually solving it in the first place? On top of that sadly much of academic coding standards are lax. Up until this moment there hasn’t been a lot of talk about writing good quality code when it comes to machine learning and that is unfortunate. The ability for us to dis‐ seminate an idea across an entire industry is based on our ability to communicate it effectively. And if we write bad code, it’s doubtful a lot of people will listen. Writing this book is my answer to that problem. Teaching machine learning to people in an easier to approach way. This subject is tough, and it’s compounded by hard to read code, or ancient C implementations that make zero sense. While a lot of people will be confused as to why this book is written in Ruby instead of Python, it’s because writing tests in Ruby is a beautiful way of explaining your code. The entire book taking this test driven approach is about communication, and com‐ municating the beautiful world of Machine Learning.
What to Expect from This Book This book is not an exhaustive machine learning resource. For that I’d highly recom‐ mend Peter Flach’s Machine Learning: The Art and Science of Algorithms that Make Sense of Data (Cambridge University Press) or if you are mathematically inclined, Tom Mitchell’s Machine Learning series is top notch. There are also great tidbits from
ix
Artificial Intelligence: A Modern Approach, Third Edition by Stuart Russell and Peter Norvig (Prentice Hall). After reading this book you will not have a PhD in machine learning, but I hope to give you enough information to get working on real problems using data with machine learning. You should expect lots of examples of the approach to problems as well as how to use them at a fundamental level. You should also find yourself learning how to approach problems that are more fuzzy than the normal unit testing scenario.
How to Read This Book The best way to read this book is to find examples that excite you. Each chapter aims to be fairly contained, although at times they won’t be. My goal for this book is not to be purely theoretical but to introduce you to some examples of problems that machine learning can solve for you as well as some worked out samples of how I’d approach working with data. In most of the chapters, I try to introduce some business cases in the beginning then delve into a worked out example toward the end. This book is intended as a short read because I want you to focus on working with the code and thinking about these problems instead of getting steeped up in theory.
Who This Book Is For There are three main people I have written the book for: the developer, the CTO, and the business analyst. The developer already knows how to write code and is interested in learning more about the exciting world of machine learning. She has some background in working out problems in a computational context and may or may not write Ruby. The book is primarily focused on this persona but there is also the CTO and the business analyst. The CTO is someone who really wants to know how to utilize machine learning to improve his company. He might have heard of KMeans, KNearest Neighbors but hasn’t quite figured out how it’s applicable to him. The business analyst is similar except that she is less technically inclined. These two personas I wrote the start of every chapter for.
How to Contact Me I love receiving emails from people who either liked a presentation I gave or need help with a problem. Feel free to email me at [email protected] And to cement
x

Preface
this, I will gladly buy you a cup of coffee if you come to the Seattle area (and our schedules permit). If you’d like to view any of the code in this book, it’s free at GitHub.
Conventions Used in This Book The following typographical conventions are used in this book: Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program ele‐ ments such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold
Shows commands or other text that should be typed literally by the user. Constant width italic
Shows text that should be replaced with usersupplied values or by values deter‐ mined by context. This element signifies a tip or suggestion.
This element signifies a general note.
This element indicates a warning or caution.
This element indicates a warning of significant importance; read carefully.
Preface

xi
Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at http://github.com/thoughtfulml. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CDROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a signifi‐ cant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Thoughtful Machine Learning by Matthew Kirk (O’Reilly). Copyright 2015 Matthew Kirk, 9781449374068.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected]
Safari® Books Online Safari Books Online is an ondemand digital library that deliv‐ ers expert content in both book and video form from the world’s leading authors in technology and business. Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training. Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals. Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, AddisonWesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kauf‐ mann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGrawHill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.
xii

Preface
How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 8009989938 (in the United States or Canada) 7078290515 (international or local) 7078290104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/thoughtfulmachinelearning. To comment or ask technical questions about this book, send email to bookques‐ [email protected] For more information about our books, courses, conferences, and news, see our web‐ site at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments • Mike Loukides, who was intrigued by my idea about using testdriven develop‐ ment on machine learning code. • Ann Spencer, my editor, who over the many months of my writing the book, coached me through edits and gave great feedback to shape the book. I would like to thank all of the O’Reilly team, who helped make this book what it is, especially the following: My reviewers: • Brad Ediger, who was excited by my weird idea of writing a book on testdriven machine learning code, and gave great feedback on the first draft of the book. • Starr Horne, who offered great insight during the review process. Thanks also for the conversation on the conference circuit about machine learning, error report‐ ing, and more. • Aaron Sumner, who provided great feedback about the overall coding structure of the book.
Preface

xiii
My amazing coworkers and friends who offered guidance during the book writing process: Edward Carrel, JonMichael Deldin, Christopher Hobbs, Chris Kuttruff, Ste‐ fan Novak, Mike Perham, Max Spransy, Moxley Stratton, and Wafa Zouyed. This book would not be a reality without the consistent and pressing support of my family: • To my wife, Sophia, who has been the anchor to my dreams and helped me shape the idea of this book into a reality. • To my grandmother, Gail, who instilled a love of learning in me from an early age, and asked intently about the coffee book I was reading during a road trip (it was a book on Java). • To my parents, Jay and Carol, who taught me the most about dissecting systems and adding human emotion to them. • To my brother, Jacob, and nieces, Zoe and Darby, who are teaching me to relearn the world through a toddler’s mind. Lastly, I dedicate this book to science and the pursuit of knowledge.
xiv
 Preface
CHAPTER 1
TestDriven Machine Learning
A great scientist is a dreamer and a skeptic. In modern history, scientists have made exceptional breakthroughs like discovering gravity, going to the moon, and produc‐ ing the theory of relativity. All those scientists had something in common: they dreamt big. However, they didn’t accomplish their feats without testing and validating their work first. Although we aren’t in the company of Einstein and Newton these days, we are in the age of big data. With the rise of the information age, it has become increasingly important to find ways to manipulate that data into something meaningful—which is precisely the goal of data science and machine learning. Machine learning has been a subject of interest because of its ability to use informa‐ tion to solve complex problems like facial recognition or handwriting detection. Many times, machine learning algorithms do this by having tests baked in. Examples of these tests are formulating statistical hypotheses, establishing thresholds, and mini‐ mizing mean squared errors over time. Theoretically, machine learning algorithms have built a solid foundation. These algorithms have the ability to learn from past mistakes and minimize errors over time. However, as humans, we don’t have the same rate of effectiveness. The algorithms are capable of minimizing errors, but sometimes we may not point them toward mini‐ mizing the right errors, or we may make errors in our own code. Therefore, we need tests for addressing human error, as well as a way to document our progress. The most popular way of writing these tests is called testdriven development (TDD). This method of writing tests first has become popularized as a best practice for program‐ mers. However, it is a best practice that is sometimes not exercised in a development environment.
1
There are two good reasons to use testdriven development. One reason is that while TDD takes 15–35% more time in active development mode, it also has the ability to reduce bugs up to 90%. The second main reason to use TDD is for the benefit of doc‐ umenting how the code is intended to work. As code becomes more complex, the need for a specification increases—especially as people are making bigger decisions based on what comes out of the analysis. Harvard scholars Carmen Reinhart and Kenneth Rogoff wrote an economics paper stating that countries that took on debt of over 90% of their gross domestic product suffered sharp drops in economic growth. Paul Ryan cited this conclusion heavily in his presidential race. In 2013, three researchers from the University of Massachusetts found that the calculation was incorrect because it was missing a substantial number of countries from its analysis. Some examples aren’t as drastic, but this case demonstrates the potential blow to one’s academic reputation due to a single error in the statistical analysis. One mistake can cascade into many more—and this is the work of Harvard researchers who have been through a rigorous process of peer review and have years of experience in research. It can happen to anybody. Using TDD would have helped to mitigate the risk of making such an error, and would have saved these researchers from the embarrassment.
History of TestDriven Development In 1999, Kent Beck popularized TDD through his work with extreme programming. TDD’s power comes from the ability to first define our intentions and then satisfy those intentions. The practice of TDD involves writing a failing test, writing the code that makes it pass, and then refactoring the original code. Some people call it “redgreenrefactor” after the colors of many testing libraries. Red is writing a test that doesn’t work originally but documents what your goal is, while green involves making the code work so the test passes. Finally, you refactor the original code to work so that you are happy with its design. Testing has always been a mainstay in the traditional development practice, but TDD emphasizes testing first instead of testing near the end of a development cycle. In a waterfall model, acceptance tests are used and involve many people—usually end users, not programmers—after the code is actually written. This approach seems good until coverage becomes a factor. Many times, quality assurance professionals test only what they want to test and don’t get to everything underneath the surface.
TDD and the Scientific Method Part of the reason why TDD is so appealing is that it syncs well with people and their working style. The process of hypothesizing, testing, and theorizing makes it very similar to the scientific method. 2

Chapter 1: TestDriven Machine Learning
Science involves trial and error. Scientists come up with a hypothesis, test that hypothesis, and then combine their hypotheses into a theory. Hypothesize, test, and theorize could be called “redgreenrefactor” instead.
Just as with the scientific method, writing tests first works well with machine learning code. Most machine learning practitioners apply some form of the scientific method, and TDD forces you to write cleaner and more stable code. Beyond its similarity to the scientific method, though, there are three other reasons why TDD is really just a subset of the scientific method: making a logical proposition of validity, sharing results through documentation, and working in feedback loops. The beauty of testdriven development is that you can utilize it to experiment as well. Many times, we write tests first with the idea that we will eventually fix the error that is created by the initial test. But it doesn’t have to be that way: you can use tests to experiment with things that might not ever work. Using tests in this way is very useful for many problems that aren’t easily solvable.
TDD Makes a Logical Proposition of Validity When scientists use the scientific method, they are trying to solve a problem and prove that it is valid. Solving a problem requires creative guessing, but without justifi‐ cation it is just a belief. Knowledge, according to Plato, is a justified true belief and we need both a true belief and justification for that. To justify our beliefs, we need to construct a stable, logical proposition. In logic, there are two types of conditions to use for proposing whether something is true: necessary and sufficient conditions. Necessary conditions are those without which our hypothesis fails. For example, this could be a unanimous vote or a preflight checklist. The emphasis here is that all con‐ ditions must be satisfied to convince us that whatever we are testing is correct. Sufficient conditions, unlike necessary conditions, mean that there is enough evi‐ dence for an argument. For instance, thunder is sufficient evidence that lightning has happened because they go together, but thunder isn’t necessary for lightning to hap‐ pen. Many times sufficient conditions take the form of a statistical hypothesis. It might not be perfect, but it is sufficient enough to prove what we are testing. Together, necessary and sufficient conditions are what scientists use to make an argu‐ ment for the validity of their solutions. Both the scientific method and TDD use these religiously to make a set of arguments come together in a cohesive way. However, TDD and the Scientific Method

3
while the scientific method uses hypothesis testing and axioms, TDD uses integration and unit tests (see Table 11). Table 11. A comparison of TDD to the scientific method Scientific method Necessary conditions Axioms
TDD Pure functional testing
Sufficient conditions Statistical hypothesis testing Unit and integration testing
Example: Proof through axioms and functional tests Fermat famously conjectured in 1637 that “there are no positive integers a, b, and c that can satisfy the equation a^n + b^n = c^n for any integer value of n greater than two.” On the surface, this appears like a simple problem, and supposedly Fermat him‐ self said he had a proof. Except the proof was too big for the margin of the book he was working out of. For 358 years, this problem was toiled over. In 1995, Andrew Wiles solved it using Galois transformations and elliptic curves. His 100page proof was not elegant but was sound. Each section took a previous result and applied it to the next step. The 100 pages of proof were based on axioms or presumptions that had been proved before, much like a functional testing suite would have been done. In programming terms, all of those axioms and assertions that Andrew Wiles put into his proof could have been written as functional tests. These functional tests are just coded axioms and assertions, each step feeding into the next section. This vacuum of testing in most cases doesn’t exist in production. Many times the tests we are writing are scattershot assertions about the code. In many cases, we are testing the thunder, not the lightning, to use our earlier example (i.e., our testing focuses on sufficient conditions, not necessary conditions).
Example: Proof through sufficient conditions, unit tests, and integration tests Unlike pure mathematics, sufficient conditions are focused on just enough evidence to support a causality. An example is inflation. This mysterous force in economics has been studied since the 19th century. The problem with proving that inflation exists is that we cannot use axioms. Instead, we rely on the sufficient evidence from our observations to prove that infla‐ tion exists. Based on our experience looking at economic data and separating out fac‐ tors we know to be true, we have found that economies tend to grow over time. Sometimes they deflate as well. The existence of inflation can be proved purely on our previous observations, which are consistent.
4

Chapter 1: TestDriven Machine Learning
Sufficient conditions like this have an analog to integration tests. Integration tests aim to test the overarching behavior of a piece of code. Instead of monitoring little changes, integration tests will watch the entire program and see whether the intended behavior is still there. Likewise, if the economy were a program we could assert that inflation or deflation exists.
TDD Involves Writing Your Assumptions Down on Paper or in Code Academic institutions require professors to publish their research. While many com‐ plain that universities focus too much on publications, there’s a reason why: publica‐ tions are the way research becomes timeless. If professors decided to do their research in solitude and made exceptional breakthroughs but didn’t publish, that research would be worthless. Testdriven development is the same way: tests can be great in peer reviews as well as serving as a version of documentation. Many times, in fact, documentation isn’t nec‐ essary when TDD is used. Software is abstract and always changing, so if someone doesn’t document or test his code it will most likely be changed in the future. If there isn’t a test ensuring that the code operates a certain way, then when a new program‐ mer comes to work on the software she will probably change it.
TDD and Scientific Method Work in Feedback Loops Both the scientific method and TDD work in feedback loops. When someone makes a hypothesis and tests it, he finds out more information about the problem he’s inves‐ tigating. The same is true with TDD; someone makes a test for what he wants and then as he goes through writing code he has more information as to how to proceed. Overall, TDD is a type of scientific method. We make hypotheses, test them, and then revisit them. This is the same approach that TDD practitioners take with writing a test that fails first, finding the solution to it, and then refactoring that solution.
Example: Peer review Peer review is common across many fields and formats, whether they be academic journals, books, or programming. The reason editors are so valuable is because they are a third party to a piece of writing and can give objective feedback. The counter‐ part in the scientific community is peer reviewing journal articles. Testdriven development is different in that the third party is a program. When someone writes tests, the program codes the assumptions and requirements and is entirely objective. This feedback can be valuable for the programmer to test assump‐ tions before someone else looks at the code. It also helps with reducing bugs and fea‐ ture misses.
TDD and the Scientific Method

5
This doesn’t mitigate the inherent issues with machine learning or math models; rather, it just defines the process of tackling problems and finding a good enough sol‐ ution to them.
Risks with Machine Learning While the scientific method and TDD are a good start to the development process, there are still issues that we might come across. Someone can follow the scientific method and still have wrong results; TDD just helps us create better code and be more objective. The following sections will outline some of these more commonly encountered issues with machine learning: • Unstable data • Underfitting • Overfitting • Unpredictable future
Unstable Data Machine learning algorithms do their best to avoid unstable data by minimizing out‐ liers, but what if the errors were our own fault? If we are misrepresenting what is cor‐ rect data, then we will end up skewing our results. This is a real problem considering the amount of incorrect information we may have. For example, if an application programming interface (API) you are using changes from giving you 0 to 1 binary information to –1 to 1, then that could be detrimental to the output of the model. We might also have holes in a time series of data. With this instability, we need a way of testing for data issues to mitigate human error.
Underfitting Underfitting is when a model doesn’t take into account enough information to accu‐ rately model real life. For example, if we observed only two points on an exponential curve, we would probably assert that there is a linear relationship there (Figure 11). But there may not be a pattern, because there are only two points to reference.
6

Chapter 1: TestDriven Machine Learning
Figure 11. In the range of –1 to 1 a line will fit an exponential curve well Unfortunately, though, when you increase the range you won’t see nearly as clear results, and instead the error will drastically increase (Figure 12).
Risks with Machine Learning

7
Figure 12. In the range of 20 to 20 a linear line will not fit an exponential curve at all In statistics, there is a measure called power that denotes the probability of not find‐ ing a false negative. As power goes up, false negatives go down. However, what influ‐ ences this measure is the sample size. If our sample size is too small, we just don’t have enough information to come up with a good solution.
Overfitting While too little of a sample isn’t ideal, there is also some risk of overfitting data. Using the same exponential curve example, let’s say we have 300,00 data points. Overfitting the model would be building a function that has 300,000 operators in it, effectively memorizing the data. This is possible, but it wouldn’t perform very well if there were a new data point that was out of that sample.
8

Chapter 1: TestDriven Machine Learning
It seems that the best way to mitigate underfitting a model is to give it more informa‐ tion, but this actually can be a problem as well. More data can mean more noise and more problems. Using too much data and too complex of a model will yield some‐ thing that works for that particular data set and nothing else.
Unpredictable Future Machine learning is well suited for the unpredictable future, because most algorithms learn from new information. But as new information is found, it can also come in unstable forms, and new issues can arise that weren’t thought of before. We don’t know what we don’t know. When processing new information, it’s sometimes hard to tell whether our model is working.
What to Test for to Reduce Risks Given the fact that we have problems such as unstable data, underfitted models, over‐ fitted models, and uncertain future resiliency, what should we do? There are some general guidelines and techniques, known as heuristics, that we can write into tests to mitigate the risk of these issues arising.
Mitigate Unstable Data with Seam Testing In his book Working Effectively with Legacy Code (Prentice Hall), Michael Feathers introduces the concept of testing seams when interacting with legacy code. Seams are simply the points of integration between parts of a code base. In legacy code, many times we are given a piece of code where we don’t know what it does internally but can predict what will happen when we feed it something. Machine learning algo‐ rithms aren’t legacy code, but they are similar. As with legacy code, machine learning algorithms should be treated like a black box. Data will flow into a machine learning algorithm and flow out of the algorithm. We can test those two seams by unit testing our data inputs and outputs to make sure they are valid within our given tolerances.
Example: Seam testing a neural network Let’s say that you would like to test a neural network. You know that the data that is yielded to a neural network needs to be between 0 and 1 and that in your case you want the data to sum to 1. When data sums to 1, that means it is modeling a percent‐ age. For instance, if you have two widgets and three whirligigs, the array of data would be 2/5 widgets and 3/5 whirligigs. Because we want to make sure that we are feeding only information that is positive and adds up to 1, we’d write the following test in our test suite:
What to Test for to Reduce Risks

9
it 'needs to be between 0 and 1' do @weights = NeuralNetwork.weights @weights.each do point (0..1).must_include(point) end end it 'has data that sums up to 1' do @weights = NeuralNetwork.weights @weights.reduce(&:+).must_equal 1 end
Seam testing serves as a good way to define interfaces between pieces of code. While this is a trivial example, note that the more complex the data gets, the more important these seam tests are. As new programmers touch the code, they might not know all the intricacies that you do.
Check Fit by CrossValidating Crossvalidation is a method of splitting all of your data into two parts: training and validation (see Figure 13). The training data is used to build the machine learning model, whereas the validation data is used to validate that the model is doing what is expected. This increases our ability to find and determine the underlying errors in a model. Training is special to the machine learning world. Because machine learning algorithms aim to map previous observations to out‐ comes, training is essential. These algorithms learn from data that has been collected, so without an initial set to train on, the algo‐ rithm would be useless.
Swapping training with validation helps increase the number of tests. You would do this by splitting the data into two; the first time you’d use set 1 to train and set 2 to validate, and then you’d swap them for the second test. Depending on how much data you have, you could split the data into smaller sets and crossvalidate that way. If you have enough data, you could split crossvalidation into an indefinite amount of sets. In most cases, people decide to split validation and training data in half—one part to train the model and the other to validate that it works with real data. If, for instance, you are training a language model that tags many parts of speech using a Hidden Markov Model, you want to minimize the error of the model.
10

Chapter 1: TestDriven Machine Learning
Figure 13. Our real goal is to minimize the crossvalidated error or real error rate
Example: Crossvalidating a model From our trained model we might have a 5% error rate, but when we introduce data outside of the model, that error might skyrocket to something like 15%. That is why it’s important to use a data set that is separate; this is as essential to machine learning as doubleentry accounting is to accounting. For example: def compare(network, text_file) misses = 0 hits = 0 sentences.each do sentence if model.run(sentence).classification == sentence.classification hits += 1
What to Test for to Reduce Risks

11
else misses += 1 end end assert misses < (0.05 * (misses + hits)) end def test_first_half compare(first_data_set, second_data_set) end def test_second_half compare(second_data_set, first_data_set) end
This method of first splitting data into two sets eliminates common issues that might happen as a result of improper parameters on your machine learning model. It’s a great way of finding issues before they become a part of any code base.
Reduce Overfitting Risk by Testing the Speed of Training Occam’s Razor emphasizes simplicity when modeling data, and states that the simpler solution is the better one. This directly implies “don’t overfit your data.” The idea that the simpler solution is the better one has to do with how overfitted models generally just memorize the data given to them. If a simpler solution can be found, it will notice the patterns versus parsing out the previous data. A good proxy for complexity in a machine learning model is how fast it takes to train it. If you are testing different approaches to solving a problem and one takes 3 hours to train while the other takes 30 minutes, generally speaking the one that takes less time to train is probably better. The best approach would be to wrap a benchmark around the code to find out if it’s getting faster or slower over time. Many machine learning algorithms have max iterations built into them. In the case of neural networks, you might set a max epoch of 1,000 so that if the model isn’t trained within 1,000 iterations, it isn’t good enough. An epoch is just a measure of one itera‐ tion through all inputs going through the network.
Example: Benchmark testing To take it a step further, you can also use unit testing frameworks like MiniTest. This adds computational complexity and an IPS (iterations per second) benchmark test to your test suite so that the performance doesn’t degrade over time. For example: it 'should not run too much slower than last time' do bm = Benchmark.measure do model.run('sentence') end
12
 Chapter 1: TestDriven Machine Learning
bm.real.must_be < (time_to_run_last_time * 1.2) end
In this case, we don’t want the test to run more than 20% over what it did last time.
Monitor for Future Shifts with Precision and Recall Precision and recall are ways of monitoring the power of the machine learning imple‐ mentation. Precision is a metric that monitors the percentage of true positives. For example, a precision of 4/7 would mean that 4 were correct out of 7 yielded to the user. Recall is the ratio of true positives to true positive plus false negatives. Let’s say that we have 4 true positives and 9; in that case, recall would be 4/9. User input is needed to calculate precision and recall. This closes the learning loop and improves data over time due to information feeding back after being misclassi‐ fied. Netflix, for instance, illustrates this by displaying a star rating that it predicts you’d give a certain movie based on your watch history. If you don’t agree with it and rate it differently or indicate you’re not interested, Netflix feeds that back into its model for future predictions.
Conclusion Machine learning is a science and requires an objective approach to problems. Just like the scientific method, testdriven development can aid in solving a problem. The reason that TDD and the scientific method are so similar is because of these three shared characteristics: • Both propose that the solution is logical and valid. • Both share results through documentation and work over time. • Both work in feedback loops. But while the scientific method and testdriven development are similar, there are some issues specific to machine learning: • Unstable data • Underfitting • Overfitting • Unpredictable future Fortunately, these challenges can be mitigated through the heuristics shown in Table 12.
Conclusion

13
Table 12. Heuristics to mitigate machine learning risks Problem/risk
Heuristic
Unstable data
Seam testing
Underfitting
Crossvalidation
Overfitting
Benchmark testing (Occam’s Razor)
Unpredictable future Precision/recall tracking over time
The best part is that you can write and think about all of these heuristics before writ‐ ing actual code. Testdriven development, like the scientific method, is valuable as a way to approach machine learning problems.
14

Chapter 1: TestDriven Machine Learning
CHAPTER 2
A Quick Introduction to Machine Learning
You’ve picked up this book because you’re interested in machine learning. While you probably have an idea of what machine learning is, it’s a subject that is often defined in a somewhat vague way. In this quick introduction, we’ll go over what exactly machine learning is, as well as a general framework for thinking about machine learning algorithms.
What Is Machine Learning? Machine learning is the intersection between theoretically sound computer science and practically noisy data. Essentially, it’s about machines making sense out of data in much the same way that humans do. Machine learning is a type of artificial intelligence whereby an algorithm or method will extract patterns out of data. Generally speaking, there are a few problems machine learning tackles; these are listed in Table 21 and described in the subsec‐ tions that follow. Table 21. The problems of machine learning The problem
Machine learning category
Fitting some data to a function or function approximation Supervised learning Figuring out what the data is without any feedback
Unsupervised learning
Playing a game with rewards and payoffs
Reinforcement learning
15
Supervised Learning Supervised learning, or function approximation, is simply fitting data to a function of any variety. For instance, given the noisy data shown in Figure 21, you can fit a line that generally approximates it.
Figure 21. This shows a line fitted to some random data
Unsupervised Learning Unsupervised learning involves figuring out what makes the data special. For instance, if we were given many data points, we could group them by similarity (Figure 22), or perhaps determine which variables are better than others.
16

Chapter 2: A Quick Introduction to Machine Learning
Figure 22. Clustering is a common example of unsupervised learning
Reinforcement Learning Reinforcement learning involves figuring out how to play a multistage game with rewards and payoffs. Think of it as the algorithms that optimize the life of something. A common example of a reinforcement learning algorithm is a mouse trying to find cheese in a maze. For the most part, the mouse gets zero reward until it finally finds the cheese. We will discuss supervised and unsupervised learning in this book but skip reinforce‐ ment learning. In the final chapter, I include some resources that you can check out if you’d like to learn more about reinforcement learning.
What Can Machine Learning Accomplish? What makes machine learning unique is its ability to optimally figure things out. But each machine learning algorithm has quirks and tradeoffs. Some do better than oth‐ ers. This book covers quite a few algorithms, so Table 22 provides a matrix to help you navigate them and determine how useful each will be to you. Table 22. Machine learning algorithm matrix Algorithm
Type
Class
Restriction bias
Preference bias
KNearest Neighbor
Supervised learning
Instance based
Generally speaking, KNN is good for measuring distancebased approximations, but it suffers from the curse of dimensionality
Prefers problems that are distance based
Naive Bayesian Classification
Supervised learning
Probabilistic
Works on problems where the inputs are independent from each other
Prefers problems where the probability will always be greater than zero for each class
What Can Machine Learning Accomplish?

17
Algorithm
Type
Class
Restriction bias
Preference bias
Hidden Markov Models
Supervised/ unsupervised
Markovian
Generally works well for system information where the Markov assumption holds
Prefers time series data and memoryless information
Support Vector Machine
Supervised learning
Decision boundary
Works where there is a definite distinction between two classifications
Prefers binary classification problems
Neural Networks
Supervised learning
Nonlinear functional approximation
Has little restriction bias
Prefers binary inputs
Clustering
Unsupervised
Clustering
No restriction
Prefers data that is in groupings given some form of distance (Euclidean, Manhattan, or others)
(Kernel) Ridge Regression
Supervised
Regression
Has low restriction on problems it can solve
Prefers continuous variables
Filtering
Unsupervised
Feature transformation
No restriction
Prefer data to have lots of variables on which to filter
Refer to this matrix throughout the book to understand how these algorithms relate to one another. Machine learning is only as good as what it applies to, so let’s get to implementing some of these algorithms! Before we get started, you will need to install Ruby, which you can do at https://www.rubylang.org/en/. This book was tested using Ruby 2.1.2, but things do change rapidly in the Ruby community. All of those changes will be annotated in the coding resources, which are available on GitHub.
Mathematical Notation Used Throughout the Book This book uses mathematics to solve problems, but all of the examples are programmercentric. Throughout the book, I’ll use the mathematical notations shown in Table 23.
18
 Chapter 2: A Quick Introduction to Machine Learning
Table 23. Mathematical notations used in this book’s examples Symbol
How do you say it?
What does it do?
∑i2 = 0 x i
The sum of all x’s from x0 to x2
This is the same thing as x0 + x1 + … + x2.
x
The absolute value of x
This takes any value of x and makes it positive. So x=1 would equal 1, and x=1 would equal 1 as well.
The square root of 4
This is the opposite of 22.
zk =
Vector zk equals 0.5 and 0.5
This is a point on the xy plane and is denoted as a vector, which is a group of numerical points.
log2(2)
Log 2
This solves for i in 2i = 2.
P(A)
Probability of A
In many cases, this is the count of A divided by the total occurrences.
P(AB)
Probability of A given B
This is the probability of A and B divided by the probability of B.
{1,2,3} ∩ {1}
The intersection of set one and two This turns into a set called 1.
{1,2,3} ∪ {4,1}
The union of set one and two
This equates to {1,2,3,4}.
det(C)
The determinant of the matrix C
This will help determine whether a matrix is invertible or not.
a∝b
a is proportional to b
This means that ma˙ = b.
min f(x)
Minimize f(x)
This is an objective function to minimize the function f(x).
XT
Transpose of the matrix X
Take all elements of the matrix and switch the row with the column.
4
Conclusion This isn’t an exhaustive introduction to machine learning, but that’s OK. There’s always going to be a lot for us all to learn when it comes to this complex subject, but for the remainder of this book, this should serve us well in approaching these prob‐ lems.
Conclusion

19
CHAPTER 3
KNearest Neighbors Classification
You probably know someone who really likes a certain brand, such as a particular technology company or clothing manufacturer. Usually you can detect this by what the person wears, talks about, and interacts with. But what are some other ways we could determine brand affinity? For an ecommerce site, we could identify brand loyalty by looking at previous orders of similar users to see what they’ve bought. So, for instance, let’s assume that a user has a history of orders, each including two items, as shown in Figure 31.
Figure 31. User with a history of orders of multiple brands Based on his previous orders, we can see that this user buys a lot of Milan Clothing Supplies (not a real brand, but you get the picture). Out of the last five orders, he has bought five Milan Clothing Supplies shirts. Thus, we could say he has a certain affin‐ ity toward this company. Knowing this, if we pose the question of what brand this user is particularly interested in, Milan Clothing Supplies would be at the top. This general idea is known as the KNearest Neighbors (KNN) classification algo‐ rithm. In our case, K equals 5, and each order represents a vote on a brand. Whatever 21
brand gets the highest vote is our classification. This chapter will introduce and define the KNN classification as well as work through a code example that detects whether a face has glasses or facial hair. KNearest Neighbors classification is an instancebased supervised learning method that works well with distancesensitive data. It suffers from the curse of dimensionality and other problems with distancebased algorithms as we’ll discuss.
History of KNearest Neighbors Classification The KNN algorithm was originally introduced by Drs. Evelyn Fix, and J.L. Hodges Jr, PhD, in an unpublished technical report written for the U.S. Air Force School of Avi‐ ation Medicine. Fix and Hodges’ original research focused on splitting up classifica‐ tion problems into a few subproblems: • Distributions F and G are completely known. • Distributions F and G are completely known except for a few parameters. • F and G are unknown, except possibly for the existence of densities. Fix and Hodges pointed out that if you know the distributions of two classifications or you know the distribution minus some parameters, you can easily back out useful solutions. Therefore, they focused their work on the more difficult case of finding classifications among distributions that are unknown. What they came up with laid the groundwork for the KNN algorithm. This algorithm has been shown to have no worse than twice the Bayes error rate as data approaches infinity. This means that as entities are added to your data set, the rate of error will be no worse than a Bayes error rate. Also, being such a simple algo‐ rithm, KNN is easy to implement as a first stab at a classification problem, and is suf‐ ficient in many cases. One challenge, though, is how arbitrary KNN can seem. How do you pick K? How do you determine what is a neighbor and what isn’t? These are questions we’ll aim to answer in the next couple of sections.
House Happiness Based on a Neighborhood Imagine you are looking to buy a new house. You are considering two different houses and want to figure out whether the neighbors are happy or not. (Of course you don’t want to move into an unhappy neighborhood.) You go around asking homeowners whether they are happy where they are and collect the information shown in Table 31. 22

Chapter 3: KNearest Neighbors Classification
We’re going to use coordinate minutes because we want to make this specific to a small enough neighborhood.
Table 31. House happiness Latitude minutes Longitude minutes Happy? 56
2
Yes
3
20
No
18
1
Yes
20
14
No
30
30
Yes
35
35
Yes
The two houses we are interested in are at (10,10) and (40,40). So which house is happy and which one is not? One method of determining this would be to use the nearest neighbor and determine whether they are happy. “Nearest” in this sense is absolute distance, which is also known as the Euclidean distance. The Euclidean distance for a twodimensional point like those shown in Table 31 would be x1 − x2 2 + y1 − y2 2. In Ruby, this would look like the following: require 'matrix' # Euclidean distance between two vectors v1 and v2 # Note that Vector#magnitude is the same thing as the Euclidean distance # from (0,0,....) to the vector point. distance = >(v1, v2) { (v1  v2).magnitude } house_happiness = { Vector[56, 2] => 'Happy', Vector[3, 20] => 'Not Happy', Vector[18, 1] => 'Happy', Vector[20, 14] => 'Not Happy', Vector[30, 30] => 'Happy', Vector[35, 35] => 'Happy' }
House Happiness Based on a Neighborhood

23
house_1 = Vector[10, 10] house_2 = Vector[40, 40] find_nearest = >(house) { house_happiness.sort_by {point,v distance.(point, house) }.first } find_nearest.(house_1) #=> [Vector[20, 14], "Not Happy"] find_nearest.(house_2) #=> [Vector[35, 35], "Happy"]
Based on this reasoning, you can see that the nearest neighbor for the first house is not happy, whereas the second house’s neighbor is. But what if we increased the num‐ ber of neighbors we looked at? # Using same code from above in this as well find_nearest_with_k = >(house, k) { house_happiness.sort_by {point, v distance.(point, house) }.first(k) } find_nearest_with_k.(house_1, 3) #=> [[Vector[20, 14], "Not Happy"], [Vector[18, 1], "Happy"], [Vector[3, 20], "N ot Happy"]] find_nearest_with_k.(house_2, 3) #=> [[Vector[35, 35], "Happy"], [Vector[30, 30], "Happy"], [Vector[20, 14], "No t Happy"]]
Using more neighbors doesn’t change the classification! This is a good thing and increases our confidence in the classification. This method demonstrates the KNearest Neighbors classification. More or less, we take the nearest neighbors and use their attributes to come up with a score. In this case, we wanted to see whether one house would be happier than the other, but the data can really be anything. KNN is an excellent algorithm because it is so simple, as you’ve just seen. It is also extremely powerful. It can be used to classify or regress data (see the following side‐ bar).
24
 Chapter 3: KNearest Neighbors Classification
Classification and Regression Note that in the preceding scenario we are mainly looking for whether the house is happy; that is, instead of trying to value the happiness, we are simply checking whether it meets our criteria. This is called a classification problem, and it can take many forms. Many times classification problems are binary, meaning that they have only two pos‐ sible answers, such as good or bad, true or false, and right or wrong. A lot of prob‐ lems can be distilled into the binary category. On the other hand, we could look for a numerical value of happiness for the house, but that would be a regression problem. While we won’t cover regression problems in this chapter, we will return to them later when we talk about Kernel Ridge Regres‐ sions (i.e., fitting a function to calculate a continuous answer).
This chapter will cover quite a bit and is broken out into a few different concerns around using KNN. We will first discuss picking K, or the constant that determines your neighborhood. Then we’ll delve more into what “nearest” means and follow up with an example of facial classification using OpenCV.
How Do You Pick K? In the case of figuring out house happiness, we implicitly picked a K of 5. This works well when we are making a quick judgment based on a person’s most recent purcha‐ ses. But for bigger problems, we might not have the ability to guess. K in the KNearest Neighbors algorithm is an arbitrary number generally ranging from 1 to the number of data points. With that much of a range, you might think that it’s difficult to pick out the optimal K, but in practice it’s not really such a vast deci‐ sion. You have three primary options for choosing K: • Guessing • Using a heuristic • Optimizing using an algorithm
Guessing K Guessing is the easiest solution. In the case of classifying brands into groups, we just pick 11 as a good guess. We know that 11 orders is probably sufficient to get a good idea of how a person shops.
How Do You Pick K?

25
Many times when we are approaching a problem we can qualitatively come up with a good enough K to solve the problem, so guessing will work. If you want to be more scientific about it, however, there are a few heuristics that can help.
Heuristics for Picking K There are three heuristics that can help you determine an optimal K for a KNN algo‐ rithm: • Avoid an even K when there are only two classes to classify. • Choose a K that is greater or equal to the number of classes plus one. • Choose a K that is low enough to avoid noise.
Use coprime class and K combinations Picking coprime numbers of classes and K will ensure fewer ties. Coprime numbers are simply two numbers that don’t share any common divisors except for 1. So, for instance, 4 and 9 are coprime while 3 and 9 are not. Imagine you have two classes, good and bad. If we were to pick a K of 6, which is even, then we might end up having ties. Graphically it looks like Figure 32.
Figure 32. Tie with K=6 and two classes If you picked a K of 5 instead (Figure 33), there wouldn’t be a tie.
26

Chapter 3: KNearest Neighbors Classification
Figure 33. K=5 with two classes and no tie
Choose a K that is greater or equal to the number of classes + 1 Imagine there are three classes: lawful, chaotic, and neutral. A good heuristic is to pick a K of at least 3 because anything less will mean that there is no chance that each class will be represented. To illustrate, Figure 34 shows the case of K=2.
Figure 34. With K=2 there is no possibility that all three classes will be represented Note how there are only two classes that get the chance to be used. Again, this is why we need to use at least K=3. But based on what we found in the first heuristic, ties are not a good thing. So, really, instead of K=3, we should use K=4 (as shown in Figure 35).
How Do You Pick K?

27
Figure 35. With K set greater than the number of classes, there is a chance for all classes to be represented
Choose a K that is low enough to avoid noise As K increases, you eventually approach the size of the entire data set. If you were to pick the entire data set, you would select the most common class. To return to our brand affinity example, say you have 100 orders, as shown in Table 32. Table 32. Brands ordered Brand
Count
Widget Inc.
30
Bozo Group
23
Robots and Rockets 12 Ion 5
35
Total
100
If we were to set K=100, our answer will always be Ion 5 because Ion 5 is the distribu‐ tion (the most common class) of the order history. That is not really what we want; instead, we want to determine the most recent order affinity. More specifically, we want to minimize the amount of noise that comes into our classification. Without coming up with a specific algorithm for this, we can justify K being set to a much lower rate, like K=3 or K=11.
28

Chapter 3: KNearest Neighbors Classification
Algorithms for Picking K Picking K can be somewhat qualitative and nonscientific, and that’s why there are many algorithms showing how to optimize K over a given training set. There are many approaches to choosing K, ranging from genetic algorithms to brute force to grid searches. Many people assert that you should determine K based on domain knowledge that you have as the implementor. For instance, if you know that 5 is good enough, you can pick that. This problem where you are trying to minimize error based on an arbitrary K is known as a hill climbing problem. The idea is to iterate through a couple of possible Ks until you find a suitable error. The difficult part about finding a K using an algorithm like genetic algorithms or brute force is that as K increases, the complexity of the clas‐ sification also increases and slows down performance. In other words, as you increase K, the program actually gets slower. If you want to learn more about genetic algorithms applied to find‐ ing an optimal K, you can read more about it in Florian Nigsch et al.’s Journal of Chemical Information and Modeling article, “Melting Point Prediction Employing kNearest Neighbor Algorithms and Genetic Parameter Optimization”.
Personally, I think iterating twice through 1% of the population size is good enough. You should have a decent idea of what works and what doesn’t just by experimenting with different Ks.
What Makes a Neighbor “Near”? Imagine that you are sitting on the corner of a city block. How far is it from one cor‐ ner of the block to the opposite corner? The answer depends on your constraints: are you able to jump over fences on foot, or do you have to drive? If you were to drive you’d be traveling two times the length of the city block (Figure 36) whereas if you walked straight it’d be 2x2 where x is the length of a city block (Figure 37). Assuming that a city block is 250 feet (76.2 meters) long, we could say that by car the distance would be 500 feet (152.4 meters), whereas on foot, it’d be roughly 353.5 feet (107.75 meters).
What Makes a Neighbor “Near”?

29
Figure 36. Driving from point A to point B on a city block
Figure 37. Straight line between A and B You probably remember what we’ve just defined from geometry class. The Pythagor‐ ean theorem states that the length of a hypotenuse is a2 + b2. In modern mathematics terms, these are called metrics. They are a measure of how far points are from each other. We calculate these metrics using a distance function, which in the preceding example was the Taxicab distance function and Euclidean dis‐ tance function. There are many ways of measuring distance, and how you do so is essential to understanding how the KNN algorithm works because it is based on proximity of data. For the most part, Euclidean distances are commonly used and represent the shortest path between two points.
Minkowski Distance A generalization of Euclidean and Taxicab distances is called the Minkowski distance. To understand the Minkowski distance, let’s first look at what the Taxicab distance function looks like: 30

Chapter 3: KNearest Neighbors Classification
dtaxicab x, y = ∑ni = 1 xi − yi This function takes the absolute differences between all dimensions of the points x and y. Now let’s look at the Euclidean distance function: deuclid x, y = ∑ni = 1 xi − yi
2
1 2
Note that squaring something will always yield a positive number and that x = x . So we could rewrite this to be: deuclid x, y =
∑ni = 1
xi − yi
1 2 2
This is very similar to the preceding Taxicab distance function, and in fact Minkow‐ ski generalizes this to the following: d x, y =
∑ni = 1
xi − yi
1 p p
Introducing a new parameter p, we can build the Taxicab distance function using p=1 and the Euclidean distance function using p=2. This is intriguing because if we needed we could increase p as much as we need to. While we won’t get into the appli‐ cation of all versions of a Minkowski distance, you now have a foundation that you can opt to study more.
Mahalanobis Distance One problem with the Minkowski type distance functions is that they assume that data should be symmetric in nature—that is, that distance is the same on all sides. A lot of times, data is not spherical in nature or well suited for symmetric distances like the Minkowski distances. For example, in the case of Figure 38, we should take into consideration the ellipsoidal nature of the data. Instead of drawing a perfect cir‐ cle around the data like the one shown, we need to figure something out that is better suited for the data’s variability.
What Makes a Neighbor “Near”?

31
Figure 38. With squashed data, Minkowski distances don’t work as well The Mahalanobis distance function takes into consideration a volatility about each dimension (see Figure 39). So for each dimension of the data there is a certain vari‐ able si, which is the standard deviation of that set.
Figure 39. Using the Mahalanobis distance The formula for the Mahalanobis distance is as follows: d x, y = ∑ni = 1
xi − yi
2
s2i
As you can see, this is very similar to the Euclidean distance, except it takes into con‐ sideration the given standard deviation of the dimensions.
Determining Classes Classes can be quite arbitrary. Sometimes things aren’t as mutually exclusive as we first think. So a caveat with building KNN classification tools is that as the number of attributes that you are modeling increases, the number of classes also increases expo‐ nentially. For example, if we have two attribute colors, red and yellow, we end up with the classes red, yellow, and orange (see Figure 310).
32

Chapter 3: KNearest Neighbors Classification
Figure 310. Mix of two attributes Now if we add the attribute blue, we’ll have red, yellow, blue, green, burnt sienna, orange, and purple classes (see Figure 311). In the first case, our two attributes yiel‐ ded three classes, while in the second case our three attributes yielded seven classes.
Figure 311. Mix of three attributes The general case of combining attributes into mutually exclusive classes is n attributes the amount of class names will be: 2n – 1 where n is the amount of attributes you are adding.
Determining Classes

33
Unless there’s a strong argument for taking out mixed classes, this is an important distinction to make. If you have 4 attributes you can assume that there are 15 classes you need to take into consideration, so set your K to at least 16.
The Curse of Dimensionality The KNearest Neighbors algorithm has one downside, which is called the curse of dimensionality. This curse is that high dimensional data tends to be sparse and far apart. Imagine a shotgun blast where over time the pellets expand through the air. This problem is common in algorithms that are based on locality and the ability to determine how close something is. Figure 312 shows how a unit sphere (i.e., the distance from 0,0,0 to the edge is exactly 1), when shrunken onto a twodimensional plane, actually gets a slightly lessthanaverage distance to points. The opposite happens when it’s expanding into more dimensions.
Figure 312. Curse of dimensionality on a sphere There is no way of avoiding this with regards to the KNN algorithm itself; it has to be mitigated through other means, which we’ll be covering in Chapter 6. We will also discuss the curse of dimensionality in Chapters 9 and 10.
Beard and Glasses Detection Using KNN and OpenCV Suppose that we wanted to determine with a general accuracy whether someone had facial hair and whether he was wearing glasses. How would we do such a thing? We really don’t have a lot of information about the distribution of any of this data, so KNN is a good algorithm to use, along with some help from OpenCV (Open Com‐ puter Vision). First I’ll explain what the program’s class diagram looks like. From
34
 Chapter 3: KNearest Neighbors Classification
there, we’ll delve deeper into how to convert a raw image into an avatar. Then we’ll extract features from those avatar images. When we have enough features, we’ll then use KNN to build a neighborhood of faces that will help us determine attributes of input images.
Setup notes All of the code for this example can be found on GitHub. Because Ruby is constantly changing, the README file should include the most uptodate instructions on getting these code examples working. You will have to install imagemagick, OpenCV, and a recent Ruby version to get started.
The Class Diagram The general idea for this example is to take a raw image (Image), extract a smaller avatar image (Face), and then put all the Face classes inside of a Neighborhood com‐ prising faces that have been annotated with information. See Figure 313 for the class diagram.
Figure 313. Class diagram for our facial hair and glasses detector
Beard and Glasses Detection Using KNN and OpenCV

35
Raw Image to Avatar The Image class takes a raw image of a human and tries to find a face inside it. To accomplish this, we rely on OpenCV. What we want is something like the images shown in Figures 314 and 315.
Figure 314. Raw image that gets extracted
Figure 315. Extracted avatar from Haar classifier Knowing a bit about OpenCV, we realize that we can achieve this by using a Haarlike feature to extract what looks like a face. We use data provided by the OpenCV library and rely on its implementation to accomplish this. This data is freely available and not actually made by me. Instead, someone trained this data set with facial images to figure out what looks like a face and what doesn’t. It extracts some features on that face that were built by others.
OpenCV and HaarLike Features OpenCV is a tool that can extract faces out of a bigger image using a Haarlike fea‐ ture, which is just a rectangle around something we want to isolate. Based on previ‐ ous information, we know that faces, unlike background elements, generally share certain characteristics, so we can use Haarlike training information to determine the rectangle where the face exists. For more information on OpenCV, check out these resources: 36

Chapter 3: KNearest Neighbors Classification
• Mastering OpenCV Practical Computer Projects by Daniel Lélis Baggio (Packt Publishing) • The OpenCV documentation is great as well.
To determine whether our avatars are correct, we use pHash. Unlike MD5 or SHA1, which are cryptographic hashes, this perceptual hash uses hamming distances to find close matches. So even though the photo is slightly off, it’ll still be a duplicate. A hamming distance is the sum of differences in a string. For instance, if you have the strings apple and oople, the first two characters are different, so the hamming distance is 2. These dis‐ tances work only on equal size strings.
To begin, we need to test whether our face extraction method will return the same photo every time. We do that by asserting that the top photo extracts the second: # test/lib/image_spec.rb require 'spec_helper' describe Image do it 'tries to convert to a face avatar using haar classifier' do @image = Image.new('./test/fixtures/raw.jpg') @face = @image.to_face avatar1 = Phashion::Image.new("./test/fixtures/avatar.jpg") avatar2 = Phashion::Image.new(@face.filepath) assert avatar1.duplicate?(avatar2) end end
This will fail, as you can imagine, because @image.to_face does nothing and @face doesn’t have a filepath associated with it. To fill in the gaps, let’s try the following: # lib/image.rb # Stub out Face for now Face = Struct.new(:filepath) class Image HAAR_FILEPATH = './data/haarcascade_frontalface_alt.xml' FACE_DETECTOR = OpenCV::CvHaarClassifierCascade::load(HAAR_FILEPATH) attr_reader :filepath def initialize(filepath)
Beard and Glasses Detection Using KNN and OpenCV

37
@filepath = filepath end def self.write(filepath) yield filepath end def face_region @image = OpenCV::CvMat.load(@filepath, OpenCV::CV_LOAD_IMAGE_GRAYSCALE) FACE_DETECTOR.detect_objects(@image).first end def to_face name = File.basename(@filepath) outfile = File.expand_path("../../public/faces/avatar_#{name}", __FILE__) self.class.write(outfile) do image = MiniMagick::Image.open(@filepath) image.crop(crop_params) image.write(outfile) end Face.new(outfile) end def x_size face_region.bottom_right.x  face_region.top_left.x end def y_size face_region.bottom_right.y  face_region.top_left.y end def crop_params crop_params = JSON.parse(File.read('./test/fixtures/attributes.json')) }
44

Chapter 3: KNearest Neighbors Classification
n.attributes.must_equal expected end end
For each folder, the attributes should be parsed and brought into a hash like so: # lib/neighborhood.rb class Neighborhood # initialize # file_from_id # nearest_feature_ids def attributes attributes = {} @files.each do file att_name = File.join(File.dirname(file), 'attributes.json') attributes[att_name.split("/")[2]] = JSON.parse(File.read(att_name)) end attributes end end
But we’re still missing one piece—a hash containing the tally of the different classes (“Facial Hair No Glasses,” “Facial Hair Glasses,” “Glasses No Facial Hair,” “Glasses Facial Hair”). In our case, we’d like something like this: { 'glasses' => {false => 1, true => 0}, 'facial_hair' => {false => 1, true => 1} }
That way, we can see the votes (counts) for each class broken out. A test for this would look as follows: # test/lib/neighborhood.rb describe Neighborhood do it 'finds the nearest face which is itself' do files = ['./test/fixtures/avatar.jpg'] neighborhood = Neighborhood.new(files) descriptor_count = Face.new(files.first).descriptors.length attributes = JSON.parse(File.read('./test/fixtures/attributes.json')) expectation = { 'glasses' => { attributes.fetch('glasses') => descriptor_count, !attributes.fetch('glasses') => 0 }, 'facial_hair' => {
Beard and Glasses Detection Using KNN and OpenCV

45
attributes.fetch('facial_hair') => descriptor_count, !attributes.fetch('facial_hair') => 0 } } neighborhood.attributes_guess(files.first).must_equal expectation end it 'returns the proper face class' do file = './public/att_faces/s1/1.png' attrs = JSON.parse(File.read('./public/att_faces/s1/attributes.json')) expectation = {'glasses' => false, 'facial_hair' => false} attributes = %w[glasses facial_hair] Neighborhood.face_class(file, attributes).must_equal expectation end end
And filling in the pieces, we’d have this in our Neighborhood class: # lib/neighborhood.rb class Neighborhood # initialize # file_from_id # nearest_feature_ids # attributes def self.face_class(filename, subkeys) dir = File.dirname(filename) base = File.basename(filename, '.png') attributes_path = File.expand_path('../attributes.json', filename) json = JSON.parse(File.read(attributes_path)) h = nil if json.is_a?(Array) h = json.find do hh hh.fetch('ids').include?(base.to_i) end or raise "Cannot find #{base.to_i} inside of #{json} for file #{filename}" else h = json end h.select {k,v subkeys.include?(k) } end def attributes_guess(file, k = 4) ids = nearest_feature_ids(file, k)
46

Chapter 3: KNearest Neighbors Classification
votes = { 'glasses' => {false => 0, true => 0}, 'facial_hair' => {false => 0, true => 0} } ids.each do id resp = self.class.face_class(@ids[id], %w[glasses facial_hair]) resp.each do k,v votes[k][v] += 1 end end votes end end
Now we face the task of making this thing useful. You’ll notice that the default value for attributes_guess is K. We need to find that still.
Crossvalidation and finding K Now we need to actually train and build an optimal model and find our K. For that, we’re going to first fold our AT&T database into two pieces like so: # test/lib/neighborhood_spec.rb describe Neighborhood do let(:files) { Dir['./public/att_faces/**/*.png'] } let(:file_folds) do { 'fold1' => files.each_with_index.select {f, i i.even? }.map(&:first), 'fold2' => files.each_with_index.select {f, i i.odd? }.map(&:first) } end let(:neighborhoods) do { 'fold1' => Neighborhood.new(file_folds.fetch('fold1')), 'fold2' => Neighborhood.new(file_folds.fetch('fold2')) } end end
Next, we are going to want to build a test for each fold and crossvalidate to see what the errors look like. This isn’t a unit test at this point because we are doing some experimentation instead. Here is the code we’ll use: # test/lib/neighborhood_spec.rb describe Neighborhood do
Beard and Glasses Detection Using KNN and OpenCV

47
%w[fold1 fold2].each_with_index do fold, i other_fold = "fold#{(i + 1) % 2 + 1}" it "cross validates #{fold} against #{other_fold}" do (1..7).each do k_exp k = 2 ** k_exp  1 errors = 0 successes = 0 dist = measure_x_times(2) do file_folds.fetch(fold).each do vf face_class = Neighborhood.face_class(vf, %w[glasses facial_hair]) actual = neighborhoods.fetch(other_fold).attributes_guess(vf, k) face_class.each do k,v if actual[k][v] > actual[k][!v] successes += 1 else errors += 1 end end end end error_rate = errors / (errors + successes).to_f avg_time = dist.reduce(Rational(0,1)) do sum, bm sum += bm.real * Rational(1,2) end print "#{k}, #{error_rate}, #{avg_time}\n" end end end end
This prints out some useful information in finding our optimal K, as you can see in Figure 316 (values are shown in Tables 34 and 35). Table 34. Crossvalidation fold #1 K
Error rate Time to run
1
0.0
1.4284405
2
0.0
0.8799995
4
0.0575
1.3032545
8
0.1775
2.121337
16 0.2525
48

3.7583905
Chapter 3: KNearest Neighbors Classification
K
Error rate Time to run
32 0.255
8.555531
64 0.255
23.3080745
Table 35. Crossvalidation fold #2 K
Error rate Time to run
1
0.0
1.4773145
2
0.0
0.9168755
4
0.05
1.3097035
8
0.21
2.1183575
16 0.2475
3.890095
32 0.2475
8.6245775
64 0.2475
23.480187
Figure 316. Error rate and time (in seconds) to run over different Ks We now can make a better judgment as to which K to use. Instead of picking the obvious K = 1 or K = 2, we should use K = 4. The reason is because as new data comes in we want to be able to check all four classes and therefore make the model more robust to data changes in the future. Beard and Glasses Detection Using KNN and OpenCV

49
At this time, our code works!
Conclusion KNearest Neighbors is one of the best algorithms for classifying data sets. It is lazy and nonparametric. It also works fairly fast if you’re using something like a KD tree. As you saw in this chapter, KNN is well suited for any type of problem for which you want to model voting or determine whether things are close together. You also learned about the issues related with KNN, like the curse of dimensionality. When we built our tool to determine whether someone was wearing glasses or had facial hair, we learned quickly that if we looked at all pixels, things would break down, so we had to apply SURF to reduce the dimensions. Because it follows the Bayes error rate, however, for many problems KNN is a good, suitable tool as a starting point for determining whether you can solve anything at all.
50

Chapter 3: KNearest Neighbors Classification
CHAPTER 4
Naive Bayesian Classification
Remember email several years ago? You probably recall your inbox being full of spam messages ranging from Nigerian princes wanting to pawn off money to pharmaceuti‐ cal advertisements. It became such a major issue that we spent most of our time filter‐ ing spam. Nowadays, we spend a lot less time filtering spam than we used to, thanks to Gmail and tools like SpamAssassin. Using a method called a Naive Bayesian Classifier, such tools have been able to mitigate the influx of spam to our inboxes. This chapter will explore that topic as well as: • Bayes’s theorem • What a Naive Bayesian Classifier is and why it’s called “naive” • How to build a spam filter using a Naive Bayesian Classifier As noted in Chapter 2, a Naive Bayes Classifier is a supervised and probabalistic learning method. It does well with data in which the inputs are independent from one another. It also prefers problems where the probability of any attribute is greater than zero.
Using Bayes’s Theorem to Find Fraudulent Orders Imagine you’re running an online store and lately you’ve been overrun with fraudu‐ lent orders. You estimate that about 10% of all orders coming in are fraudulent. In other words, in 10% of orders, people are stealing from you. Now of course you want to mitigate this by reducing the fraudulent orders, but you are facing a conundrum.
51
Every month you receive at least 1,000 orders, and if you were to check every single one, you’d spend more money fighting fraud than the fraud was costing you in the first place. Assuming that it takes up to 60 seconds per order to determine whether it’s fraudulent or not, and a customer service representative costs around $15 per hour to hire, that totals 200 hours and $3,000 per year. Another way of approaching this problem would be to construct a probability that an order is over 50% fraudulent. In this case, we’d expect the number of orders we’d have to look at to be much lower. But this is where things become difficult, because the only thing we can determine is the probability that it’s fraudulent, which is 10%. Given that piece of information, we’d be back at square one looking at all orders because it’s more probable that an order is not fraudulent! Let’s say that we notice that fraudulent orders often use gift cards and multiple pro‐ motional codes. Using this knowledge, how would we determine what is fraudulent or not—namely, how would we calculate the probability of fraud given that the pur‐ chaser used a gift card? To answer for that, we first have to talk about conditional probabilities.
Conditional Probabilities Most people understand what we mean by the probability of something happening. For instance, the probability of an order being fraudulent is 10%. That’s pretty straightforward. But what about the probability of an order being fraudulent given that it used a gift card? To handle that more complicated case, we need something called a conditional probability, which is defined as follows: PA B =
P A∩B PB
Probability Symbols Generally speaking, writing P(E) means that you are looking at the probability of a given event. This event can be a lot of different things, including the event that A and B happened, the probability that A or B happened, or the probability of A given B happening in the past. Here we’ll cover how you’d notate each of these scenarios: A ∩ B could be called the and function, as it is the intersection of A and B. For instance, in Ruby it looks like this: a = [1,2,3] b = [1,4,5]
a & b #=> [1]
52
 Chapter 4: Naive Bayesian Classification
A ∪ B could be called the or function, as it is both A and B. For instance, in Ruby it looks like the following: a = [1,2,3] b = [1,4,5] a  b #=> [1,2,3,4,5]
Finally, the probability of A given B looks as follows in Ruby: a = [1,2,3] b = [1,4,5] total = 6.0 p_a_cap_b = (a & b).length / total p_b = b.length / total p_a_given_b = p_a_cap_b / p_b #=> 0.33
This definition basically says that the probability of A happening given that B hap‐ pened is the probability of A and B happening divided by the probability of B. Graph‐ ically, it looks something like Figure 41.
Figure 41. This shows how P(AB) sits between P(A and B) and P(B) In our fraud example, let’s say we want to measure the probability of fraud given that P Fraud ∩ Gi f tcard . Now an order used a gift card. This would be P Fraud Gi f tcard = P Gi f tcard this works if you know the actual probability of Fraud and Giftcard happening. At this point, we are up against the problem that we cannot calculate P(FraudGift‐ card) because that is hard to separate out. To solve this problem, we need to use a trick introduced by Bayes.
Using Bayes’s Theorem to Find Fraudulent Orders

53
Inverse Conditional Probability (aka Bayes’s Theorem) In the 1700s, Reverend Thomas Bayes came up with the original research that would become Bayes’s theorem. PierreSimon Laplace extended Bayes’s research to produce the beautiful result we know today. Bayes’s theorem is as follows: PB A =
PA BPB PA
This is because of the following: PB A =
P A∩B P B PB
PA
=
P A∩B PA
This is useful in our fraud example because we can effectively back out our result using other information. Using Bayes’s theorem, we would now calculate: P Fraud Gi f tcard =
P Gi f tcard Fraud P Fraud P Gi f tcard
Remember that the probability of fraud was 10%. Let’s say that the probability of gift card use is 10%, and based on our research the probability of gift card use in a frau‐ dulent order is 60%. So what is the probability that an order is fraudulent given that it uses a gift card? P Fraud Gi f tcard =
60 % 1˙0 % = 60 % 10 %
The beauty of this is that your work on measuring fraudulent orders is drastically reduced because all you have to look for is the orders with gift cards. Because the total number of orders is 1,000, and 100 of those are fraudulent, we will look at 60 of those fraudulent orders. Out of the remaining 900, 90 used gift cards, which brings the total we need to look at to 150! At this point, you’ll notice we reduced the orders needing fraud review from 1,000 to 40 (i.e., 4% of the total). But can we do better? What about introducing something like people using multiple promo codes or other information?
Naive Bayesian Classifier We’ve already solved the problem of finding fraudulent orders given that a gift card was used, but what about the problem of fraudulent orders given the fact that they 54
 Chapter 4: Naive Bayesian Classification
have gift cards, or multiple promo codes, or other features? How would we go about that? Namely, we want to solve the problem of P A B, C = ?. For this, we need a bit more information and something called the chain rule.
The Chain Rule If you think back to probability class, you might recall that the probability of A and B happening is the probability of B given A times the probability of A. Mathematically, this looks like P(A ∩ B) = P(B  A)P(A). This is assuming these events are not mutu‐ ally exclusive. Using something called a joint probability, this smaller result trans‐ forms into the chain rule. Joint probabilities are the probability that all the events will happen. We denote this by using ∩. The generic case of the chain rule is: P(A1, A2, …, An) = P(A1)P(A2  A1)P(A3  A1, A2) … P(An  A1, A2, …, An–1) This expanded version is useful in trying to solve our problem by feeding lots of information into our Bayesian probability estimates. But there is one problem: this can quickly evolve into a complex calculation using information we don’t have, so we make one big assumption and act naive.
Naivety in Bayesian Reasoning The chain rule is useful for solving potentially inclusive problems, but we don’t have the ability to calculate all of those probabilities. For instance, if we were to introduce multiple promos into our fraud example then we’d have the following to calculate: P Fraud Gi f tcard, Promos =
P Gi f tcard, Promos Fraud P Fraud P Gi f tcard, Promos
Let’s ignore the denominator for now, as it doesn’t depend on whether the order is fraudulent or not. At this point, we need to focus on finding the calculation for P(Giftcard, PromosFraud)P(Fraud). If we apply the chain rule, this is equivalent to P(Fraud, Giftcard, Promos). You can see this by the following: P(Fraud,Gift,Promo) = P(Fraud)P(Gift,PromoFraud) = P(Fraud)P(Gift Fraud)P(PromoFraud,Gift) Now at this point we have a conundrum: how do you measure the probability of a promo code given fraud and gift cards? While this is the correct probability, it really
Naive Bayesian Classifier

55
can be difficult to measure—especially with more features coming in. What if we were to be a tad naive and assume that we can get away with independence and just say that we don’t care about the interaction between promo codes and gift cards, just the interaction of each independently with fraud? In that case, our math would be much simpler: P(Fraud, Gift, Promo) = P(Fraud)P(Gift  Fraud)P(PromoFraud) This would be proportional to our numerator. And, to simplify things even more, we can assert that we’ll normalize later with some magical Z, which is the sum of all the probabilities of classes. So now our model becomes: P Fraud Gi f t, Promo =
1 P Fraud P Gi f t Fraud P Promo Fraud Z
To turn this into a classification problem, we simply determine which input—fraud or not fraud—yields the highest probability. See Table 41. Table 41. Probability of gift cards versus promos Fraud Not fraud Gift card present
60%
10%
Multiple promos used 50%
30%
Probability of class
90%
10%
At this point, you can use this information to determine whether an order is fraudu‐ lent based purely on whether it has a gift card present and whether it used multiple promos. The probability that an order is fraudulent given the use of gift cards and multiple promos is 62.5%. While we can’t exactly figure out how much savings this gives you in terms of the number of orders you must review, we know that we’re using better information and making a better judgment. There is one problem, though: what happens when the probability of using multiple promos given a fraudulent order is zero? A zero result can happen for several reasons, including that there just isn’t enough of a sample size. The way we solve this is by using something called a pseudocount.
Pseudocount There is one big challenge with a Naive Bayesian Classifier, and that is the introduc‐ tion of new information. For instance, let’s say we have a bunch of emails that are
56

Chapter 4: Naive Bayesian Classification
classified as spam or ham. We build our probabilities using all of this data, but then something bad happens: a new spammy word, fuzzbolt. Nowhere in our data did we see the word fuzzbolt, and so when we calculate the probability of spam given the word fuzzbolt, we get a probability of zero. This can have a zeroingout effect that will greatly skew results toward the data we have. Because a Naive Bayesian Classifier relies on multiplying all of the independent prob‐ abilities together to come up with a classification, if any of those probabilities are zero then our probability will be zero. Take, for instance, the email subject “Fuzzbolt: Prince of Nigeria.” Assuming we strip off of, we have the data shown in Table 42. Table 42. Probability of word given spam or ham Word
Spam Ham
Fuzzbolt 0
0
Prince
75%
15%
Nigeria
85%
10%
Now let’s assume we want to calculate a score for ham or spam. In both cases, the score would end up being zero because fuzzbolt isn’t present. At that point, because we have a tie, we’d just go with the more common situation, which is ham. This means that we have failed and classified something incorrectly due to one word not being recognized. There is an easy fix for that: pseudocount. When we go about calculating the probabil‐ ity, we add one to the count of the word. So, in other words, everything will end up being word_count + 1. This helps mitigate the zeroingout effect for now. In the case of our fraud detector, we would add one to each count to ensure that it is never zero. So in our preceding example, let’s say we have 3,000 words. We would give fuzzbolt a 1 score of 3000 . The other scores would change slightly, but this avoids the zeroingout problem.
Spam Filter The canonical machine learning example is building a spam filter. In this section, we will work up a simple spam filter using a Naive Bayesian Classifier and improve it by utilizing a 3gram tokenization model.
Spam Filter

57
Figure 42. Class diagram showing how emails get turned into a SpamTrainer As you have learned before, Naive Bayesian Classifiers can be easily calculated, and operate well under strongly independent conditions. In this example, we will cover the following: • What the classes look like interacting with each other • A good data source • A tokenization model • An objective to minimize our error • A way to improve over time
Setup notes All of the code we’re using for this example can be found on Git‐ Hub. Ruby is constantly changing, so the README file is the best place to get up to speed on running the examples. You will have to make sure libxml is installed.
The Class Diagram In our example, each email has an object that takes an .eml type text file that then tokenizes it into something the SpamTrainer can utilize for incoming email messages. See Figure 42 for the class diagram.
58

Chapter 4: Naive Bayesian Classification
Data Source There are numerous sources of data that we can use, but the best is raw email mes‐ sages marked as either spam or ham. For our purposes, we can use the CSDMC2010 SPAM corpus, which is available on SourceForge. This data set has 4,327 total messages, of which 2,949 are ham and 1,378 are spam. For our proof of concept, this should work well enough.
Email Class The Email class has one responsibility, which is to parse an incoming email message according to the RFC for emails. To handle this, we use the mail gem because there’s a lot of nuance in there. In our model, all we’re concerned with is subject and body. The cases we need to handle are HTML messages, plaintext, and multipart. Every‐ thing else we’ll just ignore. Building this class using testdriven development, let’s go through this step by step. Starting with the simple plaintext case, we’ll copy one of the example training files from our data set under data/TRAINING/TRAIN_00001.eml to ./test/fixtures/ plain.eml. This is a plaintext email and will work for our purposes. Note that the split between a message and header in an email is usually denoted by “\r\n\r\n”. Along with that header information is generally something like “Subject: A Subject goes here.” Using that, we can easily extract our test case, which is: require 'spec_helper' # test/lib/email_spec.rb describe Email do describe 'plaintext' do let(:plain_file) { './test/fixtures/plain.eml' } let(:plaintext) { File.read(plain_file) } let(:plain_email) { Email.new(plain_file) } it 'parses and stores the plain body' do body = plaintext.split("\n\n")[1..1].join("\n\n") plain_email.body.must_equal body end it 'parses the subject' do subject = plaintext.match(/^Subject: (.*)$/)[1] plain_email.subject.must_equal subject end end end
Spam Filter

59
Now instead of relying purely on regular expressions, we want to utilize a gem. We’ll use the mail gem, which will handle all of the nittygritty details. Making email work for this particular case, we have: require 'forwardable' # lib/email.rb class Email extend Forwardable def_delegators :@mail, :subject def initialize(filepath) @filepath = filepath @mail = Mail.read(filepath) end def body @mail.body.decoded end end
You’ll notice that we’re using def_delegators to delegate subject to the @mail object. This is just for simplicity’s sake. Now that we have captured the case of plaintext, we need to solve the case of HTML. For that, we want to capture only the inner_text. Knowing that regular expressions are useless for this, we need yet another gem: Nokogiri. Nokogiri will be able to do this for us easily. But first we need a test case, which looks something like this: # test/lib/email_spec.rb describe Email do describe 'html' do let(:html_file) { './test/fixtures/html.eml' } let(:html) { File.read(html_file) } let(:html_email) { Email.new(html_file) } it "parses and stores the html body's inner_text" do body = html.split("\n\n")[1..1].join("\n\n") html_email.body.must_equal Nokogiri::HTML.parse(body).inner_text end it "stores subject like plaintext does as well" do subject = html.match(/^Subject: (.*)$/)[1] html_email.subject.must_equal subject end end end
60

Chapter 4: Naive Bayesian Classification
As mentioned, we’re using Nokogiri to calculate the inner_text, and we’ll have to use it inside of the Email class as well. Now the problem is that we also need to detect the content_type. So we’ll add that in: # lib/email.rb require 'forwardable' class Email extend Forwardable def_delegators :@mail, :subject, :content_type def initialize(filepath) @filepath = filepath @mail = Mail.read(filepath) end def body single_body(@mail.body.decoded, content_type) end private def single_body(body, content_type) case content_type when 'text/html' Nokogiri::HTML.parse(body).inner_text when 'text/plain' body.to_s else '' end end end
At this point, we could add multipart processing as well, but I will leave that as an exercise that you can try out yourself. In the coding repository mentioned earlier in the chapter, you can see the multipart version. Now we have a working email parser, but we still have to deal with tokenization, or what to extract from the body and subject.
Tokenization and Context As Figure 43 shows, there are numerous ways to tokenize text, such as by stems, word frequencies, and words. In the case of spam, we are up against a tough problem because things are more contextual. The phrase Buy now sounds spammy, whereas Buy and now do not. Because we are building a Naive Bayesian Classifier, we are assuming that each individual token is contributing to the spamminess of the email.
Spam Filter

61
Figure 43. There are lots of ways of tokenizing text The goal of the tokenizer we’ll build is to extract words into a stream. Instead of returning an array, we want to yield the token as it happens so that we are keeping a low memory profile. Our tokenizer should also downcase all strings to keep them similar: # test/lib/tokenizer_spec.rb require 'spec_helper' describe Tokenizer do describe '1gram tokenization' do it 'downcases all words' do expectation = %w[this is all caps] Tokenizer.tokenize("THIS IS ALL CAPS") do token token.must_equal expectation.shift end end it 'uses the block if given' do expectation = %w[feep foop] Tokenizer.tokenize("feep foop") do token token.must_equal expectation.shift end end end end
As promised, we do two things in this tokenizer code. First, we lowercase all words. Second, instead of returning an array, we use a block. This is to mitigate memory constraints, as there is no need to build an array and return it. This makes it lazier. To make the subsequent tests work, though, we will have to fill in the skeleton for our tokenizer module like so:
62

Chapter 4: Naive Bayesian Classification
# lib/tokenizer.rb module Tokenizer extend self def tokenize(string, &block) current_word = '' return unless string.respond_to?(:scan) string.scan(/[azAZ09_\u0000]+/).each do token yield token.downcase end end end
Now that we have a way of parsing and tokenizing emails, we can move on to the Bayesian portion: the SpamTrainer.
The SpamTrainer We now need to build the SpamTrainer, which will accomplish three things: 1. Storing training data. 2. Building a Bayesian classifier. 3. Minimizing the false positive rate by testing.
Storing training data The first step we need to tackle is to store training data from a given set of email messages. In a production environment, you would pick something that has persis‐ tence. In our case, we will go with storing everything in one big hash. Remember that most machine learning algorithms have two steps: training and then computation. Our training step will consist of these substeps: 1. Storing a set of all categories 2. Storing unique word counts for each category 3. Storing the totals for each category So first we need to capture all of the category names; that test would look something like this: # test/lib/spam_trainer_spec.rb describe SpamTrainer do describe 'initialization' do let(:hash_test) do {'spam' => './filepath', 'ham' => './another', 'scram' => './another2'}
Spam Filter

63
end it 'allows you to pass in multiple categories' do st = SpamTrainer.new(hash_test) st.categories.sort.must_equal hash_test.keys.uniq.sort end end end
The solution is in the following code: # lib/spam_trainer.rb class SpamTrainer def initialize(training_files, n = 1) @categories = Set.new training_files.each do tf @categories 1, :negative => 1 }.fetch(@sentiment) end end
112

Chapter 6: Support Vector Machines
Return a Unique Set of Words from the Corpus Now we have one last step, which is to return the unique set of words from this train‐ ing file. The goal here is to return words that are located in the corpus so that they can then be zipped together in the CorpusSet class. Because in most cases we are assuming files are being brought in, we can use a StringIO object to act like a file instead, therefore mitigating the need to write temp files: # test/lib/corpus_spec.rb describe Corpus do let(:positive) { StringIO.new('loved movie!! loved') } let(:positive_corpus) { Corpus.new(positive, :positive) } it 'consumes a positive training set and unique set of words' do positive_corpus.words.must_equal Set.new(%w[loved movie]) end end
At this point, we need to implement the method #words on our Corpus. This method should return a set of words that have been passed in. This would probably look something like: # lib/corpus.rb class Corpus # initialize # tokenize # sentiment_code attr_reader :sentiment def words @words = begin set = Set.new @io.each_line do line Corpus.tokenize(line).each do word set 30000 ObjectSpace.memsize_of(sparse_array) #=> 302,672 bytes
This array has a length of 30,000! 29,990 of those are just 0s. Instead of storing all of those 0s, we can transform the array into a hash that stores only index relationships where the number is nonzero: sparse_hash = Hash.new(0) sparse_array.each_with_index do val, i if val.nonzero? sparse_hash[i] = val else # Skip end end sparse_hash.size #=> 10 ObjectSpace.memsize_of(sparse_hash) #=> 616 bytes
Notice the enormous reduction in size. We went from 30,000 to 10! Sparse vectors can be generalized to be used with matrices as well: require 'matrix' require 'objspace' matrix = Matrix.build(300, 100) do row, col if row % 3 == 0 && col % 300 == 0 1 else 0 end end matrix.row_size * matrix.column_size #=> 30000 # memsize_of doesn't work unless it's a C level object like an array ObjectSpace.memsize_of(matrix.to_a.flatten) #=> 312,968 bytes sparse_matrix = Hash.new(0) matrix.each_with_index do e, row, col if e.nonzero? sparse_matrix[[row, col]] = e else
116

Chapter 6: Support Vector Machines
# This is zero and therefore we skip it end end sparse_matrix.length #=> 100 ObjectSpace.memsize_of(sparse_matrix) #=> 5,144 bytes
Using a sparse vector is important for memory considerations and speed; there’s no need to store something that takes up more space than needed.
Using a sparse hash instead of a vector, let’s build a seam test that ensures that our sentiment analyzer receives the proper information from a CorpusSet. A test would look like this: # test/lib/corpus_set_spec.rb describe CorpusSet do it 'returns a set of sparse vectors to train on' do expected_ys = [1, 1] expected_xes = [[0,1], [2,3]] expected_xes.map! do x Libsvm::Node.features(Hash[x.map {i [i, 1]}]) end ys, xes = corpus_set.to_sparse_vectors ys.must_equal expected_ys xes.flatten.zip(expected_xes.flatten).each do x, xp x.value.must_equal xp.value x.index.must_equal xp.index end end end
To implement this, we would write the code as follows: # lib/corpus_set.rb class CorpusSet # initialize attr_reader :words def to_sparse_vectors calculate_sparse_vectors! [@yes, @xes] end private def calculate_sparse_vectors! return if @state == :calculated
Using SVM to Determine Sentiment

117
@yes = [] @xes = [] @corpora.each do corpus vectors = load_corpus(corpus) @xes.concat(vectors) @yes.concat([corpus.sentiment_code] * vectors.length) end @state = :calculated end def load_corpus(corpus) vectors = [] corpus.sentences do sentence vectors :positive, '.neg' => :negative } corpora = files.map { file Corpus.new(file, mapping.fetch(File.extname(fil e)) } corpus_set = CorpusSet.new(corpora) new(corpus_set) end end
Now that we’re at this junction, we still have a couple of decision to make: what library to use to build our SVM model, and where to find training data.
Library to handle Support Vector Machines: LibSVM When it comes to libraries to handle Support Vector Machines, most people tend to grab LibSVM. It has the longest track record, is written in C, and has many bindings, from Python to Java to Ruby. One caveat here, though, is that there are a few Ruby gems for LibSVM, and not all are superb. The gem rblibsvm—which is what we will use—supports sparse vectors and therefore is best suited for our problems. There are others out there that use swig adapters and unfortunately don’t support sparse matri‐ ces as well.
Using SVM to Determine Sentiment

119
Training data Up to this point, we haven’t talked about training data for our tool. We need some text that is mapped as either negative or positive. These data would be organized into lines and stored in files. There are many different sources of data, but what we’ll use is from GitHub. This is a set of data from Pang Lee about movie review sentiment. This is a highly specific data set and will work only for movie reviews from IMDb (the Internet Movie Database), but it is sufficient for our purposes. If you were to use this with any other program, most likely you would use a data set specific to what you were trying to solve. So, for instance, Twitter sentiment would come from actual tweets that were mapped to negative and positive. Keep in mind that it’s not too diffi‐ cult to build your own data set by creating a survey form and partitioning out work to Mechanical Turk by Amazon.
Crossvalidating with the movie review data Crossvalidation is the best way to ensure that our data is trained well and that our model works properly. The basic idea is to take a big data set, split it into two or more pieces, and then use one of those pieces of data as training while using the other to validate against it. In test form, it would look like this: # test/cross_validation_spec.rb describe 'Cross Validation' do include TestMacros def self.test_order :alpha end (15..15).each do exponent it "runs cross validation for C=#{2**exponent}" do neg = split_file("./config/rtpolaritydata/rtpolarity.neg") pos = split_file("./config/rtpolaritydata/rtpolarity.pos") classifier = SentimentClassifier.build([ neg.fetch(:training), pos.fetch(:training) ]) # find the minimum c = 2 ** exponent classifier.c = c n_er = validate(classifier, neg.fetch(:validation), :negative) p_er = validate(classifier, pos.fetch(:validation), :positive)
120

Chapter 6: Support Vector Machines
total = Rational(n_er.numerator + p_er.numerator, n_er.denominator + p_er.d enominator) skip("Total error rate for C=#{2 ** exponent} is: #{total.to_f}") end end end
We’re using skip and self.test_order here. The skip method is used to give us information but not to test anything per se. Because we are trying to find an optimal c, we are just experimenting using tests. Also notice that we override test_order here and set it to alpha. That is because the minitest by default uses random order, mean‐ ing that as we’re going through the series from –15 to 15 we will get data out of order. It is much easier to interpret results when you’re looking at them in order. Also notice that we have introduced two new methods, split_file and validate. These are in our test macros module as: # test/test_macros.rb module TestMacros def validate(classifier, file, sentiment) total = 0 misses = 0 File.open(file, 'rb').each_line do line if classifier.classify(line) != sentiment misses += 1 else end total += 1 end Rational(misses, total) end def split_file(filepath) ext = File.extname(filepath) validation = File.open("./test/fixtures/validation#{ext}", "wb") training = File.open("./test/fixtures/training#{ext}", "wb") counter = 0 File.open(filepath, 'rb').each_line do l if (counter) % 2 == 0 validation.write(l) else training.write(l) end counter += 1 end training.close
Using SVM to Determine Sentiment

121
validation.close end end
In this test, we iterate from 2 through –15 all the way up to 15. This will cover most of the territory we want. After the crossvalidation is done, we can pick the best C and use that for our model. Technically speaking, this is called a grid search, and it will attempt to find a good enough solution over a set of trial runs. Now we need to work on the backend of the SentimentClassifier. This is where we use LibSVM by building our model and making a tiny state machine: # lib/sentiment_classifier.rb class SentimentClassifier # build def initialize(corpus_set) @corpus_set = corpus_set @c = 2 ** 7 end def c=(cc) @c = cc @model = nil end def words @corpus_set.words end def classify(string) if trained? prediction = @model.predict(@corpus_set.sparse_vector(string)) present_answer(prediction) else @model = model classify(string) end end def trained? [email protected] end def model puts 'starting to get sparse vectors' y_vec, x_mat = @corpus_set.to_sparse_vectors prob = Libsvm::Problem.new parameter = Libsvm::SvmParameter.new parameter.cache_size = 1000
122

Chapter 6: Support Vector Machines
parameter.gamma = Rational(1, y_vec.length).to_f parameter.eps = 0.001 parameter.c = @c parameter.kernel_type = Libsvm::KernelType::LINEAR prob.set_examples(y_vec, x_mat) Libsvm::Model.train(prob, parameter) end end
Here’s where things get more interesting and we actually build the Support Vector Machine to work with the rest of the problem. As noted, we are using the LibSVM library, which is a standard. We first build our sparse_vectors, then load up a new LibSVM problem, and finally give it the default parameters. After running the crossvalidation, we see that the best C is 128, which happens to have a ~30% error rate.
Improving Results Over Time There are a few different ways of improving the 30% error rate, which involve a bit of experimentation: • Stripping out stop words • Improving tokenization • Using different polynomial kernels You could also try out a few other algorithms with the same data to see which one works better.
Conclusion The Support Vector Machines algorithm is very well suited for classifying two separa‐ ble classes. It can be modified to separate more than two classes and doesn’t suffer from the curse of dimensionality that KNearest Neighbors does. This chapter taught you how SVM can be used to separate loyal and disloyal customers, as well as how to assign sentiment to movie data.
Conclusion

123
CHAPTER 7
Neural Networks
Neural Networks (or Nets) are effective at mapping observed data to a function. Researchers have been able to use Neural Networks for things like handwriting detec‐ tion, computer vision, and speech recognition with breakthrough results. Essentially, Neural Nets are an effective way of learning from data and have a long history dating back to the 1800s. In this chapter, we’re going to discuss how the Neu‐ ral Networks algorithm came to be, what goes into it, and how it works, as well as a practical example of classifying languages based on character frequencies. The Neural Networks algorithm is excellent at function approxima‐ tion and supervised learning problems. It has little restrictions on what it can do and has proven quite successful in practice. How‐ ever, it is limited to operating on binary inputs and can present challenges from a complexity and speed standpoint.
History of Neural Networks When introduced, Neural Networks were about studying how the brain operates. Neurons in our brains work together in a network to process and make sense of inputs and stimuli. Alexander Bain and William James both proposed that brains operated in a network that could process lots of information. This network of neu‐ rons has the ability to recognize patterns and learn from previous data. For instance, if a child is shown a picture of eight dogs, she starts to understand what a dog looks like. This research was expanded to include a more artificial bent when Warren McCulloch and Walter Pitts invented threshold logic. Threshold logic combines binary information to determine logical truth. They suggested using something called
125
a step function, which attached a threshold to either accept or reject a summation of previous information. After many years of research, Neural Networks and threshold logic were combined to form what we call an Artificial Neural Network.
What Is an Artificial Neural Network? A Neural Network (see Figure 71) is a robust function that takes an arbitrary set of inputs and fits it to an arbitrary set of outputs that are binary. It is excellent at fuzzy matching, and building robust functions to match just about anything. In practice, Neural Networks are used in deep learning research to match images to features and much more.
Figure 71. A visual representation of a Neural Network Fuzzy matching is a general machine learning problem of trying to match inputs with outputs based on previous information.
What makes Neural Networks special is their use of a hidden layer of weighted func‐ tions called neurons, with which you can effectively build a network that maps a lot of other functions. Without a hidden layer of functions, Neural Networks would be just a set of simple weighted functions. Neural Networks are denoted by the number of neurons per layer. For example, if we have 20 neurons in our input layer, 10 in one hidden layer, and 5 in an output layer, it would be a 20105 network. If there is more than one hidden layer, then we would denote it as, say, 20775 (the two middle 7s are layers with 7 nodes apiece).
126

Chapter 7: Neural Networks
To summarize, then, Neural Networks comprise the following parts: • The input layer • The hidden layer(s) • Neurons • The output layer • The training algorithm Next, I’ll explain what each of these parts does and how it works.
Input Layer The input layer, shown in Figure 72, is the entry point of a Neural Network. It is the entry point for the inputs you are giving to the model. There are no neurons in this layer because its main purpose is to serve as a conduit to the hidden layer(s). The input type is important, as Neural Networks work with only two types: symmetric or standard.
Figure 72. The input layer of a Neural Net With training a Neural Network, we have observations and inputs. Taking the simple example of an exclusive or (also known as XOR), we have the truth table shown in Table 71.
What Is an Artificial Neural Network?

127
Table 71. XOR truth table Input A Input B Output false
false
false
false
true
true
true
false
true
true
true
false
In this case, we have four observations and two inputs, which could either be true or false. Neural Networks do not work off of true or false, though, and knowing how to code the input is key. We’ll need to translate these to either standard or symmetric inputs.
Standard inputs The standard range for input values is between 0 and 1. In our previous XOR exam‐ ple, we would code true as 1 and false as 0. This style of input has one downside: if your data is sparse, meaning full of 0s, it can skew results. Having a data set with lots of 0s means we risk the model breaking down. Only use standard inputs if you know that there isn’t sparse data.
Symmetric inputs Symmetric inputs avoids the issue with 0s. These are between –1 and 1. In our pre‐ ceding example, –1 would be false, and 1 would be true. This kind of input has the benefit of our model not breaking down because of the zeroingout effect. In addition to that, it puts less emphasis on the middle of a distri‐ bution of inputs. If we introduced a “maybe” into the XOR calculation, we could map that as 0 and ignore it. Inputs can be used in either the symmetric or standard format but need to be marked as such, as the way we calculate the output of neurons depends on this.
Hidden Layers Without hidden layers, Neural Networks would be a set of weighted linear combina‐ tions. In other words, Neural Networks have the ability to model nonlinear data because there are hidden layers (Figure 73).
128

Chapter 7: Neural Networks
Figure 73. The hidden layer of a Neural Network Each hidden layer contains a set of neurons (Figure 74), which then passes to the output layer.
Figure 74. Neurons of a Neural Network
Neurons Neurons are weighted linear combinations that are wrapped in an activation func‐ tion. The weighted linear combination (or sum) is a way of aggregating all of the pre‐ vious neurons’ data into one output for the next layer to consume as input. Activation functions, shown in Figure 75, serve as a way to normalize data so it’s either sym‐ metric or standard.
What Is an Artificial Neural Network?

129
Figure 75. Neurons wrapped in an activation function As a network is feeding information forward, it is aggregating previous inputs into weighted sums. We take the value y and compute the activated value based on an acti‐ vation function.
Activation functions As mentioned, activation functions, some of which are listed in Table 72, serve as a way to normalize data between either the standard or symmetric ranges. They also are differentiable, and need to be because of how we find weights in a training algo‐ rithm. Table 72. Common activation functions Name
Standard
Symmetric
Sigmoid
1 ˙ 1 + e−2sum
2 ˙ −1 1 + e−2sum
Cosine
cos sum + 0.5 2
cos(sum)
Sine
sin sum + 0.5 2
sin(sum)
1
2
2 esum
esum
Elliott
0 . 5s˙um + 0.5 1 + ∣ sum ∣
sum 1 + ∣ sum ∣
Linear
sum > 1 ? 1 : (sum < 0 : sum) sum > 1 ? 1 : (sum < 1 ? 1 : sum)
Gaussian
Threshold sum < 0 ? 0 : 1
130
 Chapter 7: Neural Networks
2
−1
sum < 0 ? 1 : 1
The big advantage of using activation functions is that they serve as a way of buffer‐ ing incoming values at each layer. This is useful because Neural Networks have a way of finding patterns and forgetting about the noise. There are two main categories for activation functions: sloped or periodic. In most cases, the sloped activation functions (shown in Figures 76 and 78) are a suitable default choice. The periodic functions (shown in Figures 77 and 79) are used for data that is random and is the cosine and sine.
Figure 76. Symmetric sloped activation functions
What Is an Artificial Neural Network?

131
Figure 77. Symmetric periodic activation functions
132
 Chapter 7: Neural Networks
Figure 78. Standard sloped activation functions
What Is an Artificial Neural Network?

133
Figure 79. Standard periodic activation functions Sigmoid is the default function to be used with neurons because of its ability to smooth out the decision. Elliott is a sigmoidal function that is quicker to compute, so it’s the choice I make. Cosine and sine waves are used when you are mapping some‐ thing that has a randomlooking process associated with it. In most cases, these trigo‐ nometric functions aren’t as useful to our problems. Neurons are where all of the work is done. They are a weighted sum of previous inputs put through an activation function that either bounds it to 0 to 1 or –1 to 1. In the case of a neuron where we have two inputs before it, the function for the neuron would be y = ϕ w1x˙ 1 + w2x˙ 2 , where ϕ is an activation function like sigmoid, and wi is weights determined by a training algorithm.
134

Chapter 7: Neural Networks
Output Layer The output layer is similar to the input layer except that it has neurons in it. This is where the data comes out of the model. Just as with the input layer, this data will either be symmetric or standard. Output layers decide how many neurons are output, which is a function of what we’re modeling (see Figure 710). In the case of a function that outputs whether a stop light is red, green, or yellow, we’d have three outputs (one for each color). Each of those outputs would contain an approximation for what we want.
Figure 710. The output layer of a Neural Network
Training Algorithms As mentioned, the weights for each neuron came from a training algorithm. There are many such algorithms, but the most common are: • Back Propagation • QuickProp • RProp All of these algorithms find optimal weights for each neuron. They do so through iterations, also known as epochs. For each epoch, a training algorithm goes through the entire Neural Network and compares it against what is expected. At this point, it learns from past miscalculations. These algorithms have one thing in common: they are trying to find the optimal solu‐ tion in a convex error surface. You can think of convex error surface as a bowl with a minimum value in it. Imagine that you are at the top of a hill and want to make it to a valley, but the valley is full of trees. You can’t see much in front of you, but you know that you want to get to the valley. You would do so by proceeding based on local inputs and guessing where you want to go next. This is known as the Gradient What Is an Artificial Neural Network?

135
Descent algorithm (i.e., determining minimum error by walking down a valley) and it is illustrated in Figure 711. The training algorithms do the same thing; they are look‐ ing to minimize error by using local information.
Figure 711. Gradient Descent algorithm in a nutshell
The delta rule While we could solve a massive amount of equations, it would be faster to iterate. Instead of trying to calculate the derivative of the error function with respect to the weight, we calculate a change in weight for each neuron’s weights. This is known as the delta rule, and it is as follows: δw ji = α t j − ϕ h j ϕ′ h j xi This states that the change in weight for the neuron j’s weight number i is: alpha * (expected  calculated) * derivative_of_calculated * input_at_i
alpha is the learning rate and is a small constant. This initial idea, though, is what
powers the idea behind the Back Propagation algorithm, or the general case of the delta rule.
Back Propagation Back Propagation is the simplest of the three algorithms that determine the weight of a neuron. You define error as (expected * actual)2 where expected is the expected out‐ put and actual is the calculated number from the neurons. We want to find where the derivative of that equals 0, which is the minimum:
136

Chapter 7: Neural Networks
� is the momentum factor and propels previous weight changes into our current weight change, whereas α is the learning rate.
Back Propagation has the disadvantage of taking many epochs to calculate. Up until 1988, researchers were struggling to train simple Neural Networks. Their research on how to improve this led to a whole new algorithm called QuickProp.
QuickProp Scott Fahlman introduced the QuickProp algorithm after he studied how to improve Back Propagation. He asserted that Back Propagation took too long to converge to a solution. He proposed that we instead take the biggest steps without overstepping the solution. Fahlman determined that there are two ways to improve Back Propagation: making the momentum and learning rate dynamic, and making use of a second derivative of the error with respect to each weight. In the first case, you could better optimize for each weight, and in the second case, you could utilize Newton’s method of approxi‐ mating functions. With QuickProp, the main difference from Back Propagation is that you keep a copy of the error derivative computed during the previous epoch, along with the difference between the current and previous values of this weight. To calculate a weight change at time t, use the following function: Δw t =
St S t−1 −S t
˙w t − 1 Δ
This carries the risk of changing the weights too much, so there is a new parameter for maximum growth. No weight is allowed to be greater in magnitude than the max‐ imum growth rate multiplied by the previous step for that weight.
RProp RProp is the most used algorithm because it converges fast. It was introduced by Martin Riedmiller in the 1990s and has had some improvements since then. It con‐ verges on a solution quickly due to its insight that the algorithm can update the weights many times through an epoch. Instead of calculating weight changes based on a formula, it uses only the sign for change as well as an increase factor and decrease factor.
What Is an Artificial Neural Network?

137
To see what this algorithm looks like in code, we need to define a few constants (or defaults). These are a way to make sure the algorithm doesn’t operate forever or become volatile. These defaults were taken from the FANN library. The basic algorithm was easier to explain in Ruby instead of writing out the partial derivatives. For ease of reading, note that I am not calculating the error gradi‐ ents (i.e., the change in error with respect to that specific weight term).
This code gives you an idea of how the RProp algorithm works using just pure Ruby code: neurons = 3 inputs = 4 delta_zero = 0.1 increase_factor = 1.2 decrease_factor = 0.5 delta_max = 50.0 delta_min = 0 max_epoch = 100 deltas = Array.new(inputs) { Array.new(neurons) { delta_zero }} last_gradient = Array.new(inputs) { Array.new(neurons) { 0.0 } }
sign = >(x) { if x > 0 1 elsif x < 0 1 else 0 end } weights = inputs.times.map {i rand(1.0..1.0) } 1.upto(max_epoch) do j weights.each_with_index do i, weight # Current gradient is derived from the change of each value at each layer gradient_momentum = last_gradient[i][j] * current_gradient[i][j] if gradient_momentum > 0 deltas[i][j] = [deltas[i][j] * increase_factor, delta_max].min change_weight = sign.(current_gradient[i][j]) * deltas[i][j] weights[i] = weight + change_weight last_gradient[i][j] = current_gradient[i][j]
138

Chapter 7: Neural Networks
elsif gradient_momentum < 0 deltas[i][j] = [deltas[i][j] * decrease_factor, delta_min].max last_gradient[i][j] = 0 else change_weight = sign.(current_gradient[i][j]) * deltas[i][j] weights[i] = weights[i] + change_weight last_gradient[i][j] = current_gradient[i][j] end end end
These are the fundamentals you need to understand to be able to build a Neural Net‐ work. Next, we’ll talk about how to do so, and what decisions we must make to build an effective one.
Building Neural Networks Before you begin building a Neural Network, you must answer the following ques‐ tions: • How many hidden layers should you use? • How many neurons per layer? • What is your tolerance for error?
How Many Hidden Layers? As noted earlier in this chapter, what makes Neural Nets unique is their usage of hid‐ den layers. If you took out hidden layers, you’d have a linear combination problem. You aren’t bound to use any number of hidden layers, but there are three heuristics that help: • Do not use more than two hidden layers; otherwise, you might overfit the data. With too many layers, the network starts to memorize the training data. Instead, we want it to find patterns. • One hidden layer will do the job of approximating a continuous mapping. This is the common case. Most neural networks have only one hidden layer in them. • Two hidden layers will be able to push past a continuous mapping. This is an uncommon case, but if you know that you don’t have a continuous mapping, you can use two hidden layers. There is no steadfast rule holding you to these heuristics for picking the number of hidden layers. It comes down to trying to minimize the risk of overfitting or underfit‐ ting your data.
Building Neural Networks

139
How Many Neurons for Each Layer? Neural Nets are excellent aggregators and terrible expanders. Neurons themselves are weighted sums of previous neurons, so they have a tendency to not expand out as well as they combine. If you think about it, a hidden layer of 2 that goes to an output layer of 30 would mean that for each output neuron, there would be two inputs. There just isn’t enough entropy or data to make a wellfitted model. This idea of emphasizing aggregation over expansion leads us to the next set of heu‐ ristics: • The number of hidden neurons should be between the number of inputs and the number of neurons at the output layer. • The number of hidden neurons should be twothirds the size of the input layer, plus the size of the output layer. • The number of hidden neurons should be less than twice the size of the input layer. This comes down to trial and error, though, as the number of hidden neurons will influence how well the model crossvalidates, as well as the convergence on a solu‐ tion. This is just a starting point.
Tolerance for Error and Max Epochs The tolerance for error gives us a time to stop training. We will never get to a perfect solution but rather converge on one. If you want an algorithm that performs well, then the error rate might be low, like 0.01%. But in most cases, that will take a long time to train due to its intolerance for error. Many start with an error tolerance of 1%. Through crossvalidation, this might need to be tightened even more. In Neural Network parlance, the tolerance is internal, is measured as a mean squared error, and defines a stopping place for the network. Neural Networks are trained over epochs, and this is set before the training algorithm even starts. If an algorithm is taking 10,000 iterations to get to a solution, then there might be a high risk for overtraining and creating a sensitive network. A starting point for training is 1,000 epochs or iterations to train over. This way, you can model some complexity without getting too carried away. Both max epochs and maximum error define our converging points. They serve as a way to signal when the training algorithm can stop and yield the Neural Network. At this point, we’ve learned enough to get our hands dirty and try a realworld example.
140

Chapter 7: Neural Networks
Using a Neural Network to Classify a Language Characters used in a language have a direct correlation with the language itself. Man‐ darin is recognizable due to its characters, because each character means a specific word. The same is true with many Latinbased languages, but in regards to letter fre‐ quency. If we look at the difference of “The quick brown fox jumped over the lazy dog” in English and its German equivalent, “Der schnelle braune Fuchs sprang über den fau‐ len Hund”, we’d get the frequency chart shown in Table 73. Table 73. Difference in frequency between English and German sentence a b c d e f g h i j k l m n o p q r s t u v w x y z ü English
1 1 1 2 4 1 1 2 1 1 1 1 1
1 4 1 1 2 0 2 2 1 1
1 1 1 0
German
3 2 2 3 7 2 1 3 0 0 0 3 0
6 0 1 0 4 2 0 4 0 0
0 0 1 1
Difference 2 1 1 1 3 1 0 1 1 1 1 2 1
5 4 0 1 2 2 2 2 1 1
1 1 0 1
There is a subtle difference between German and English. German uses quite a few more Ns, whereas English uses a lot of Os. If we wanted to expand this to a few more European languages, how would we do that? More specifically, how can we build a model to classify sentences written in English, Polish, German, Finnish, Swedish, or Norwegian? In this case, we’ll build a simple model to predict a language based on the frequency of the characters in the sentences. But before we start, we need to have some data. For that, we’ll use the most translated book in the world: the Bible. Let’s extract all the chapters out of Matthew and Acts. The approach we will take is to extract all the sentences out of these text files and cre‐ ate vectors of frequency normalized between 0 and 1. From that, we will train a net‐ work that will take those inputs and then match them to a vector of 6. The vector of 6 is defined as the index of the language equaling 1. If the language we are using to train is index 3, the vector would look like [0,0,0,1,0,0] (zerobased indexing).
Using a Neural Network to Classify a Language

141
Setup Notes All of the code we’re using for this example can be found on Git‐ Hub. Ruby is constantly changing, so the README file is the best place to get up to speed on running the examples.
Grabbing the Data If you want to grab the data, I wrote the following script to help you download it from Biblegateway.com: require 'nokogiri' require 'openuri' url = "http://www.biblegateway.com/passage/" languages = { 'English' => { 'version' => 'ESV', 'search' => ['Matthew', 'Acts'] }, 'Polish' => { 'version' => 'SZPL', 'search' => ['Ewangelia+według+św.+Mateusza', 'Dzieje+Apostolskie'] }, 'German' => { 'version' => 'HOF', 'search' => ['Matthaeus', 'Apostelgeschichte'] }, 'Finnish' => { 'version' => 'R1933', 'search' => ['Matteuksen', 'Teot'] }, 'Swedish' => { 'version' => 'SVL', 'search' => ['Matteus', 'Apostlagärningarna'] }, 'Norwegian' => { 'version' => 'DNB1930', 'search' => ['Matteus', 'Apostlenesgjerninge'] } } languages.each do language, search_pattern text = '' search_pattern['search'].each_with_index do search, i 1.upto(28).each do page puts "Querying #{language} #{search} chapter #{page}"
142

Chapter 7: Neural Networks
uri = [ url, URI.encode_www_form({ search: "#{URI.escape(search)}+#{page}", version: "#{search_pattern.fetch('version')}" }) ].join('?') puts uri doc = Nokogiri::HTML.parse(open(uri)) doc.css('.passage p').each do verse text += verse.inner_text.downcase.gsub(/[\d,;:\\\\"]/,'') end end File.open("#{language}_#{i}.txt", 'wb') {f f.write(text)} end end
This will download and store Acts and Matthew verses in multiple languages. Feel free to try more languages!
Writing the Seam Test for Language To take our training data, we need to build a class to parse that and interface with our Neural Network. For that, we will use the class name Language. The Language’s pur‐ pose is to take a file of text in a given language and load it into a distribution of char‐ acter frequencies. When needed, the Language will output a vector of these charac‐ ters, all summing up to 1. All of our inputs will be between 0 and 1. Our parameters are: • We want to make sure that our data is correct and sums to 1. • We don’t want characters like UTF8 spaces or punctuation entering our data. • We want to downcase all characters. A should be translated as a. Ä should also be ä. This helps us to make sure that our Language class, which takes a text file and outputs an array of hashes, is correct: # encoding: utf8 # test/lib/language_spec.rb require 'spec_helper' require 'stringio'
describe Language do let(:language_data) { » « › ‹ – „ /] SPACES = [" ", "\u00A0", "\n"] STOP_CHARACTERS = ['.', '?', '!'] def tokenize(blob) unless blob.respond_to?(:each_char) raise 'Must implement each_char on blob' end vectors = [] dist = Hash.new(0) characters = Set.new blob.each_char do char if STOP_CHARACTERS.include?(char) vectors @languages.length ) # Note that the library misspells Elliott @fann.set_activation_function_hidden(:elliot) end end
Now that we have the proper inputs and the proper outputs, the model is set up and we should be able to run the whole cross_validation_test.rb. But, of course, there is an error because we cannot run new data against the network. To address this, we need to build a function called #run. At this point, we have something that works and looks like this: # lib/network.rb require 'rubyfann' class Network # initialize # train! # code def run(sentence) if @trainer == :not_trained  @fann == :not_ran raise 'Must train first call method train!' else vectors, characters = Tokenizer.tokenize(sentence) output_vector = @fann.run(code(vectors.first)) @languages[output_vector.index(output_vector.max)] end end end
This is where things get interesting, as it appears that German, English, Swedish, and Norwegian are all failing our test. Because our code works, now we are at the stage where we tune our Neural Network based on unit tests. We have set our standards high, but we can reach them by tuning the network.
Using a Neural Network to Classify a Language

149
Tuning the Neural Network At this point, we change the activation function to the Elliott function, which improves the results by only failing Norwegian, Swedish, and German. English drop‐ ped out of the errors, but our epochs went up just a bit. Halving the internal error rate to 0.005 is our next step, which we do by changing the last argument for @fann.train_on_data to 0.005. Finally things work and we’ve achieved our goal. I’ll leave further tuning to you as an exercise in playing around with what works and what does not. You can try many different activation functions, as well as internal rates of decay or errors. The takeaway here is that with an initial test to base accuracy against, you can try many different avenues.
Convergence Testing Before continuing, you can reset the max epochs in the network class to have 20–50% over what you saw, just to make sure that over time things don’t start taking forever. In our case, I saw around 200 epochs for the model to train, so I’ll reset the max epochs to 300.
Precision and Recall for Neural Networks Going a step further, when we deploy this Neural Network code to a production envi‐ ronment, we need to close the information loop by introducing a precision and recall metric to track over time. This metric will be calculated from user input. We can measure precision and recall by asking in our user interface whether our pre‐ diction was correct. From this text, we can capture the blurb and the correct classifi‐ cation, and feed that back into our model the next time we train. To learn more about monitoring precision and recall, see Chapter 9. What we need to monitor the performance of this Neural Network in production is a metric for how many times a classification was run, as well as how many times it was wrong.
WrapUp of Example The Neural Networks algorithm is a fun way of mapping information and learning through iterations, and it works well for our case of mapping sentences to languages. Loading this code in an IRB session, I had fun trying out phrases like “meep moop,” which is classified as Norwegian! Feel free to play with the code.
150

Chapter 7: Neural Networks
Conclusion The Neural Networks algorithm is a powerful tool in a machine learning toolkit. Neural Networks serve as a way of mapping previous observations through a func‐ tional model. While they are touted as black box models, they can be understood with a little bit of mathematics and illustration. You can use Neural Networks for many things, like mapping letter frequencies to languages or handwriting detection. There are many problems being worked on right now with regards to this algorithm, and more indepth books on the topic as well. Anything written by Geoffrey Hinton is worth a read, namely Unsupervised Learning: Foundations of Neural Computation (Computational Neuroscience). This chapter introduced Neural Networks as an artificial version of our brain and explained how they work by summing up inputs using a weighted function. These weighted functions were then normalized within a certain range. Many algorithms exist to train these weight values, but the most prevalent is the RProp algorithm. Then we summed it all up with a practical example on mapping sentences to lan‐ guages.
Conclusion

151
CHAPTER 8
Clustering
If you’ve been to a library, you’ve seen clustering at work. The Dewey Decimal System is a form of clustering. Dewey came up with a system that would attach a number of increasing granularity and in doing so, revolutionized libraries. This idea of categorizing data points, or books, into groups is useful for organizing information. We don’t know what specific category they should belong to, so we just want to split the books into a set of categories. This type of problem is much different than what we’ve encountered before. All of the problems we have looked at thus far have attempted to figure out the best functional approximation to assign to a given data set and its labels. Now we are more con‐ cerned with the data itself, not the labels. As you will see in this chapter, clustering has one downside, which is that it doesn’t lend itself to use as well as other algorithms. This is called the impossibility theorem. In this chapter, we will talk about clustering in general as it applies to cohorts of users, and then introduce KMeans clustering and Expectation Maximization (EM) clustering. Finally, we’ll finish with an example about clustering jazz records into groups based on styles. The Clustering algorithm is an unsupervised learning problem and is great for grouping data. It can present issues, though, with appli‐ cation to problems; that is, it suffers from the impossibility theo‐ rem.
153
User Cohorts Grouping people into cohorts (clusters) makes a lot of business and marketing sense. For instance, your first customer is different from your ten thousandth customer or millionth customer. This process of defining users into cohorts is a common one. If we are to effectively split our customers into different buckets based on behavior and time of signup, then we can better serve them by diversifying our marketing strategy. The problem is that we don’t have a predefined label for customer cohorts. To solve this problem, you could look at what month and year each person became a cus‐ tomer. But that is making a big assumption about the time of first purchase being the defining factor that splits customers into groups. What if the time of first purchase had nothing to do with whether customers were in one cohort or the other? For example, they could have made only that one purchase or they may have made many since then. Instead, we should group users into cohorts based on what we do know about our users. For instance, let’s say we know when they signed up, the amount they’ve spent, and what their favorite color is. Over the last two years, we’ve had only 10 users sign up (I hope you’d have more than that over two years, but let’s keep this simple). Table 81shows the data we’ve collected about them over that time. Table 81. Data collected over two years User ID Signup date Money spent Favorite color 1
Jan 14
$40
N/A
2
Nov 3
$50
Orange
3
Jan 30
$53
Green
4
Oct 3
$100
Magenta
5
Feb 1
$0
Cyan
6
Dec 31
$0
Purple
7
Sep 3
$0
Mauve
8
Dec 31
$0
Yellow
9
Jan 13
$14
Blue
10
Jan 1
$50
Beige
154

Chapter 8: Clustering
Given this data, we want to define a mapping from each user to a cohort. Looking at these rows, you notice that the favorite colors are irrelevant data. This information does not provide a meaningful way of grouping our users into a cohort. That leaves us with Money spent and Signup date. There seems to be a group of users who spend money, and those who don’t. In the Signup date column, you’ll notice that there are a lot of users who sign up around the very beginning and end of the year as well as around September, October, or November. We now need to decide on the number of clusters we want to make. Because our data set is so small, we’ll just split it into two pieces. That means that we can split the cohorts into something like Table 82. Table 82. Manual cohort assignment to the original data set User Id Signup date (days to Jan 1) Money spent Cohort 1
Jan 14 (13)
$40
1
2
Nov 3 (59)
$50
2
3
Jan 30 (29)
$53
1
4
Oct 3 (90)
$100
2
5
Feb 1 (31)
$0
1
6
Dec 31 (1)
$0
1
7
Sep 3 (120)
$0
2
8
Dec 31 (1)
$0
1
9
Jan 13 (12)
$14
1
10
Jan 1 (0)
$50
1
We have divided the customers into two groups: group 1 contains seven customers, which we could call the “beginning of the year” signups, and group 2 contains the other three. What if we were to do this more algorithmically, though? In the next sections, we’ll touch on what KMeans clustering is, as well as introduce EM clustering in the theo‐ retical sense. Then we’ll wrap up this chapter with an example of how to categorize a record collection on a bookshelf.
User Cohorts

155
KMeans Clustering There are a lot of clustering algorithms, like Linkage clustering or DIANA, but one of the most common is KMeans clustering. Using a predefined K, which is the number of clusters we want to split the data into, KMeans will find the most optimal centroid of clusters. The nicest properties of KMeans clustering are that the clusters will be strict and spherical in nature, and it converges to a solution. Next, we will briefly talk about how KMeans Clustering works.
The KMeans Algorithm The KMeans algorithm starts with a base case. Pick K random points in the data set and define them to be centroids. Then, for each point, assign it to a cluster number that is closest to each different centroid. Now we have a clustering based on the origi‐ nal randomized centroid. But that is not exactly what we want to end with, so we update where the centroids are using a mean of the data. Then, we repeat until the centroids no longer move (see Figure 81).
Figure 81. KMeans is very circular Some possible ways of calculating the distance in KMeans (which we talked about in Chapter 3) are: Manhattan distance dmanhattan x, y = ∑ni = 1 xi − yi 156

Chapter 8: Clustering
Euclidean distance deuclid x, y = ∑ni = 1 xi − yi
2
Minkowski distance d x, y = ∑ni = 1 xi − yi
1 p p
Mahalanobis distance
d x, y =
∑ni = 1
xi − y i
2
s2i
The Downside of KMeans Clustering The downside of KMeans clustering is that everything must have a hard boundary. This means that a data point can be in only one cluster; it can’t straddle the line between the two. On top of that, KMeans prefers spherical data, as most of the time the Euclidean distance is being used. The downsides are obvious when you look at a graphic like Figure 81 where the data in the middle could really go either direction, to cluster 1 or 2.
Expectation Maximization (EM) Clustering Instead of focusing on finding a centroid and then data points that relate to it, Expectation Maximization (EM) clustering focuses on solving a different problem. Let’s say that you want to split your data points into two sections, either cluster 1 or 2. You want a good guess as to whether the data is in either cluster, but don’t care if there’s some fuzziness. Instead of getting an assignment, we really want a probability that the data point is in each cluster. Unlike KMeans clustering, which focuses on making definite boundaries between clusters, EM clustering is robust to data points that might be in either cluster. This can be quite useful for classifying data that doesn’t have a definite boundary. To start with EM clustering, we make a vector called zk = , which holds the probability of whether the row vector k is in either cluster. Through iterations, we find out something more like zk = . Imagine you have a set of data points and instead of circling different clusters like before we assign a shade to each one.
Expectation Maximization (EM) Clustering

157
The darker it is, the more Cluster 2 it becomes, while the lighter grays become Cluster 1, as shown in Figure 82.
Figure 82. EM clustering shows how clusters can actually be much softer The EM algorithm is split into two pieces, the expectation step and the maximization step. The expectation step is where the probabilities get calculated given how the data cur‐ rently looks and the value of our initial zk. We calculate the log likelihood function with respect to the conditional distribution of Z given X under the current estimate of the parameters of Theta (t): Q θ ∥ θt = EZ ∥ X,θ logL θ; X, Z t
Next, the maximization step is finding the parameter θ that maximizes the probability of θ given θt: θt = arg maxθ Q θ ∥ θt
The disadvantage of EM clustering is that it does not converge necessarily and can falter when you’re mapping data with singular covariances. We will delve into more of the issues related to EM clustering in the example section. First, though, we need to talk about one feature that all clustering algorithms have in common: the impossibility theorem.
158

Chapter 8: Clustering
The Impossibility Theorem There is no such thing as a free lunch, and clustering is no exception. The benefit we get out of clustering to magically map data points to particular groupings comes at a cost, as was laid out by Jon Kleinberg, who coined the impossibility theorem. The theorem states that you can never have more than two of the following attributes: • Richness • Scale invariance • Consistency Richness is the notion that there exists a distance function that will yield all different types of partitions. What this means intuitively is that a clustering algorithm has the ability to create all types of mappings from data points to cluster assignments. Scale invariance is simple to understand. Imagine that you were building a rocketship and started calculating everything in kilometers until your boss said that you need to use miles instead. There wouldn’t be a problem switching; you just need to divide by a constant on all your measurements. It is scale invariant. Basically, if the numbers are all multiplied by 20, then the same cluster should happen. Consistency is more subtle. Similar to scale invariance, if we shrank the distance between points inside a cluster and expanded them, the cluster should yield the same result. At this point, you probably understand that clustering isn’t as good as you might have thought initially. It has a lot of issues, and consistency is definitely one that should be called out. For our purposes, KMeans and EM clustering satisfy richness and scale invariance, but not consistency. This fact makes testing clustering just about impossible. The only way we really can test is by anecdote and example. But that is OK for analysis pur‐ poses. In the next section, we will analyze jazz music using KMeans and EM clustering.
Categorizing Music Music has a deep history of recordings as well as composed pieces. You could have an entire degree and study musicology just to be able to effectively categorize these sheets of music. The ways we can split music into categories are endless. Personally, I split my own record collection by artist name. But artists will often collaborate. Other people prefer to categorize based on genre. But what about the fact that genres are broad? Take jazz,
The Impossibility Theorem

159
for instance; according to the Montreux Jazz Festival, jazz is anything you can improv over. How can we effectively build a library of music where we can split up our collection into similar pieces of work?
Setup Notes All of the code we’re using for this example can be found on Git‐ Hub. In this section, we will first determine where we will get our data from, what sort of attributes we can extract on, and what we can validate based on. We will also discuss why clustering sounds great in theory but in practice doesn’t give us much—except for clusters, that is. Ruby is constantly changing, so the README file is the best place to get up to speed on running the examples.
Let’s approach this by using KMeans and EM clustering. By the end, we will have a soft clustering of music pieces that we can utilize to build a taxonomy of music. We will not be using a testdriven approach to writing a clusterer, because clustering is a problem that doesn’t lend itself well to hypothesis testing. This is a very important point. On the surface, clustering seems like a great solution to all problems, but in reality, it doesn’t work well for actually testing our assumptions. Remember from our earlier discussion of the impossibility theorem that we can’t have a clustering algorithm that is consistent, rich, and scale invariant (it can only ever be two of these at most). In many ways, clustering is a data analysis tool, but it’s not something we should use to solve problems that we want to control.
Gathering the Data There is a massive amount of music data from the 1100s through today. We have MP3s, CDs, vinyl records, and written music. Without trying to classify the entire world of music, let’s determine a small subsection that we can use. I don’t want to be involved in any copyright suits, so we will only use public information on albums. This would be artist, song name, genre (if available), as well as characteristics that we can find on the music. To achieve this, we will access the plethora of information con‐ tained on Discogs.com, which contains lots of XML data dumps of records and songs.
160

Chapter 8: Clustering
Also, because we aren’t trying to cluster the entire data set of albums in existence, let’s just focus on jazz. Most people would agree that jazz is a genre that is hard to really classify into any category. It could be fusion, it could be steel drums, and so on. So to get our data set, I downloaded the best jazz albums according to the website. The data goes back to 1940 and well into the 2000s. In total, I was able to download about 1,200 unique records. All great albums! But that isn’t enough information. On top of that, I annotated the information by using the Discogs API to determine the style of jazz in each. After annotating the original data set, I found that there are 128 unique styles associ‐ ated with jazz (at least according to Discogs). They range from aboriginal to vocal.
Analyzing the Data with KMeans Like we did with the KNearest Neighbors algorithm, we need to figure out an opti‐ mal K. Unfortunately, with clustering there really isn’t much we can test except to simply see whether we can split the data into two different clusters. But let’s say that we want to fit all of our records on a bookshelf and we have 25 slots. We could run a clustering of all of our data using K = 25. Doing so requires little code because we have the ai4r gem to rely on: # lib/kmeans_clusterer.rb require 'csv' require 'ai4r' data = [] artists = [] CSV.foreach('./annotated_jazz_albums.csv', :headers => true) do row @headers = row.headers[2..1] artists @headers) clusterer = Ai4r::Clusterers::KMeans.new clusterer.build(ds, 25) CSV.open('./clustered_kmeans.csv', 'wb') do csv csv @s * Matrix.identity(@width) } end @partitions = [] end end
At this point, we have set up all of our base case code. We have @k, which is the num‐ ber of clusters; @data, the data we pass in that we want to cluster; @labels, an array of the probability that the row is in each cluster; @classes, which holds on to an array of
Categorizing Music

163
means and covariances that tells us where the distribution of data is. And finally, there’s @partitions, which is the assignments of each data row to cluster index. Now we need to build our expectation step, which is to figure out the probability of each data row in each cluster. To do this, we need to write a new method, expect: # lib/em_clusterer.rb class EMClusterer # initialize # setup_cluster! def expect @classes.each_with_index do klass, i puts "Expectation for class #{i}" inv_cov = if klass[:covariance].regular? klass[:covariance].inv else puts "Applying shrinkage" (klass[:covariance]  (0.0001 * Matrix.identity(@width))).inv end d = Math::sqrt(klass[:covariance].det) @data.row_vectors.each_with_index do row, j rel = row  klass[:means] p = d * Math::exp(0.5 * fast_product(rel, inv_cov)) @labels[j][i] = p end end @labels = @labels.map.each_with_index do probabilities, i sum = probabilities.inject(&:+) @partitions[i] = probabilities.index(probabilities.max) if sum.zero? probabilities.map { 1.0 / @k } else probabilities.map {p p / sum.to_f } end end end def fast_product(rel, inv_cov) sum = 0 inv_cov.column_count.times do j local_sum = 0 (0 ... rel.size).each do k
164

Chapter 8: Clustering
local_sum += rel[k] * inv_cov[k, j] end sum += local_sum * rel[j] end sum end end
The first part iterates through all classes, which holds on to the means and covarian‐ ces of each cluster. From there, we want to find the inverse covariance matrix as well as the determinant of the covariance. For each row, we calculate a value that is pro‐ portional to the probability that the row is in a cluster: −
pi j = det C e
1 x − μi C−1 x j − μi 2 j
This is effectively a Gaussian distance metric to help us determine how far outside the mean our data is. Let’s say that the row vector is exactly the mean. That would mean that this would reduce to pij = det(C), which is just the determinant of the covariance matrix. This is actually the highest value you can get out of this function. If, for instance, the row vector was far away from the mean vector, then pij would become smaller and smaller due to the exponentiation and negative fraction in the front. The nice thing is that this is proportional to the Gaussian probability that the row vector is in the mean. Because this is proportional (not equal), we end up normaliz‐ ing to sum to 1. You’ll notice one last thing here: the introduction of the fast_product method. This is because the Matrix library in Ruby is slow and builds Array within Array, which is memory inefficient. In this case, things won’t change, so we optimized things for that. Now we can move on to the maximization step: # lib/em_clusterer.rb class EMClusterer # initialize # setup_cluster! # expect # fast_product def maximize @classes.each_with_index do klass, i puts "Maximizing for class #{i}" sum = Array.new(@width) { 0 } num = 0
Categorizing Music

165
@data.each_with_index do row, j p = @labels[j][i] @width.times do k sum[k] += p * @data[j,k] end num += p end mean = sum.map {s s / num } covariance = Matrix.zero(@width, @width) @data.row_vectors.each_with_index do row, j p = @labels[j][i] rel = row  Vector[*mean] covariance += Matrix.build(@width, @width) do m,n rel[m] * rel[n] * p end end covariance = (1.0 / num) * covariance @classes[i][:means] = Vector[*mean] @classes[i][:covariance] = covariance end end end
Again, here we are iterating over the clusters called @classes. We first make an array called sum, which holds on to the weighted sum of the data happening. From there, we normalize to build a weighted mean. To calculate the covariance matrix, we start with a zero matrix that is square and the width of our clusters. We then iterate through all row_vectors and incrementally add on the weighted difference of the row and the mean for each combination of the matrix. Again, at this point, we normalize and store. Now we can get down to actually using this. To do that, we add two convenience methods that help in querying the data: # lib/em_clusterer.rb class EMClusterer # initialize # setup_cluster! # expect # fast_product # maximize def cluster(iterations = 5)
166

Chapter 8: Clustering
iterations.times do i puts "Iteration #{i}" expect_maximize end end def expect_maximize expect maximize end end
EM Jazz Clustering Results Back to our results using EM clustering with our jazz music. To actually perform the analysis, we run the following script: data = [] artists = [] CSV.foreach('./annotated_jazz_albums.csv', :headers => true) do row @headers = row.headers[2..1] artists Throws an error conditioned = Matrix[[1,2], [2,1]] (conditioned.transpose * conditioned).inverse * conditioned.transpose * y #=> Ma trix[[(1/1)], [(0/1)]]
What this shows you is that if, for instance, you have a lot of variables like the first ill_conditioned problem, then there is no suitable way to solve this problem using linear regression because there aren’t enough data points to find the optimal mini‐ mized least squares. The inner matrix will become singular when there are more fea‐ tures than there are data points. But let’s think about this problem a little more.
Introducing Regularization, or Ridge Regression Our ill_conditioned regression problem is recapped in Table 91. Table 91. Ill_conditioned problem Y X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 1 1
2
3
4
5
6
7
8
9
10
2 10 9
8
7
6
5
4
3
2
1
Even without any algorithm to solve this, you can see that a lot of the data here is pretty useless. What we are looking for is a function that will yield 1 in the first case and 2 in the second case, so we probably want to find information that is 2× as big in the second case and 1× in the first. That means that columns like X_1, X_2, X_5, X_6, X_9, and X_10 are probably useless. So let’s take those out and look again (Table 92).
Introducing Regularization, or Ridge Regression

173
Table 92. Ill_conditioned problem with fewer columns Y X3 X4 X7 X8 1 3
4
7
8
2 8
7
4
3
As a matter of fact, X_7 and X_8 are not really needed either, so let’s forget about them as well. That leaves us with X_3 and X_4. Now we can actually solve this: require 'matrix' y = Matrix[[1],[2]] simplified = Matrix[[3,4], [8,7]] betas = (simplified.transpose * simplified).inverse * simplified.transpose * y # => Matrix[[(1/11)], [(2/11)]]
This solves the problem! All we did was get rid of the extraneous variables that didn’t make a difference. But the question is, can we make this more algorithmic instead of just anecdotal? Yes, we can—by using something called a Kernel Ridge Regression, or Regularized Regression. The basic idea is to introduce a ridge parameter, which will help address the problems with ill_conditioned we have seen before: require 'matrix' y = Matrix[[1],[2]] ill_conditioned = Matrix[[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]] shrinkage = 0.0001 left_half = ill_conditioned.transpose * ill_conditioned + shrinkage * Matrix.ide ntity(ill_conditioned.column_size) left_half.singular? #=> false betas = left_half.inverse * ill_conditioned.transpose * y betas.transpose * ill_conditioned.row(0) #=> Vector[1.0000000549097194] betas.transpose * ill_conditioned.row(1) #=> Vector[1.9999994492261521]
As you can see, adding in this shrinkage factor has helped us overcome the problem of singular matrices, and we can actually solve this illposed problem easily. But there is still one snag we need to deal with: nonlinearity.
174

Chapter 9: Kernel Ridge Regression
Kernel Ridge Regression As you may recall from Chapter 6, we introduced kernels that would take nonlinear data and transform it into a new feature space where all of a sudden it was linear. Ker‐ nels are a powerful tool and well suited for problems in which the data is nonlinear. To refresh your memory, here are the kernel functions we discussed in Chapter 6: Homogenous polynomial d
K xi, x j = xTi x j
Heterogeneous polynomial K xi, x j = xTi x j + c
d
Radial basis function −
K xi, x j = e
2 ∥ ∥xi − x j ∥ ∥2 2 * σ2
The important thing to know here is that we don’t actually have to calculate a lot of this, as much of the time these functions are in addition to the part of the regression where XXT. Basically, without getting too deep into the mathematics, we can replace that term with K, which is a kernel representing a new, nonlinear space. So now our equation looks similar: y = yT K + λI
−1
κ
K i j = f xi, x j κi = f xi, x′
This is simply a change. You can just add these functions into the preceding function, and that will effectively turn the regression from linear to having a kernel—very much like how we used them in Chapter 6.
WrapUp of Theory The Kernel Ridge Regressions algorithm can be useful for finding simple functions to map an illposed problem. In the next section, we’ll see how to actually use this algo‐ rithm to recommend beer styles to people based on user preferences expressed in reviews. Kernel Ridge Regression

175
Collaborative Filtering with Beer Styles Remember our collaborative filtering with beer drinkers from earlier in the chapter? What if we were to apply the same to an actual data set—namely, beer styles? All of the reviewers completed reviews as to whether they liked the taste, appearance, and other attributes of different selections.
Data Set This data set has beer styles, beers, breweries, reviewers, and reviews. There are 1,586,615 reviews, 62,260 unique beers, 33,388 reviewers, 5,743 breweries, and 104 unique beer styles. Until this point, we’ve been loading everything into memory and analyzing this way, but this data set is large so a better approach is to load this info into some sort of database.
Setup Notes All of the code we’re using for this example can be found on Git‐ Hub. Ruby is constantly changing, so the README file is the best place to get up to speed on running the examples.
Why Regression for Collaborative Filtering? If you have ever read any other books on machine learning or data science, most likely you haven’t heard of regression being used for collaborative filtering. In most cases, people will build what is called matrix factorization to find recommendations. We use regression in this example because it is well suited for determining the linear combination of factors that will identify what someone wants. The beauty is that you can use this to figure out someone’s preferences. So, for instance, in beer reviews, we can figure out whether someone likes alcohol more than palate. While we can do that with matrix factorization as well, this is slightly different.
The Tools We Will Need To accomplish our collaborative filtering on beer styles, we need to build some tables into Postgres. I think Sequel is much easier to use than ActiveRecord for small projects like this, so we’ll use that. First, let’s define some tables to use and some models. We will need tables for beers, reviews, reviewers, breweries, reviews, and beer styles.
176

Chapter 9: Kernel Ridge Regression
To start out, let’s create our file bootstrap, which will make sure the tables exist and everything migrates correctly: # script/load_db.rb # Note that this doesn't have to be postgres—you can use sqlite or mysql, too DB = Sequel.connect('postgres://localhost/beer_reviews') DB.create_table? :beers do primary_key :id Integer :beer_style_id, :index => true Integer :brewery_id, :index => true String :name Float :abv end
Beers have a beer_style_id, name, abv, and brewery_id. We want to make this fairly spread out, so a beer_style_id is a foreign key to beer_styles that we will make. abv is simply alcohol by volume, and lastly, brewery_id is a foreign key to breweries. Next, let’s build our breweries as well as everything else: # script/load_db.rb # DB # create_table :beers DB.create_table? :breweries do primary_key :id String :name end DB.create_table? :reviewers do primary_key :id String :name end DB.create_table? :reviews do primary_key :id Integer :reviewer_id, :index => true Integer :beer_id, :index => true Float :overall Float :aroma Float :appearance Float :palate Float :taste end DB.create_table? :beer_styles do primary_key :id String :name, :index => true end
Collaborative Filtering with Beer Styles

177
Now we need to load the information, which we can do through the following script: # script/load_db.rb # # # # # #
DB create_table create_table create_table create_table create_table
:beers :breweries :reviewers :reviews :beer_styles
require 'csv' require 'set' # brewery_id,brewery_name,review_time,review_overall,review_aroma,review_appeara nce,review_profilename,beer_style,review_palate,review_tast,beer_name,beer_abv,b eer_beerid breweries = {} reviewers = {} beer_styles = {} if !File.exists?('./beer_reviews/beer_reviews.csv') system('bzip2 cd ./beer_reviews/beer_reviews.csv.bz2 > ./beer_reviews/beer_re views.csv') or die end CSV.foreach('./beer_reviews/beer_reviews.csv', :headers => true) do line puts line if !breweries.has_key?(line.fetch('brewery_name')) b = Brewery.create(:name => line.fetch('brewery_name')) breweries[line.fetch('brewery_name')] = b.id end if !reviewers.has_key?(line.fetch('review_profilename')) r = Reviewer.create(:name => line.fetch('review_profilename')) reviewers[line.fetch('review_profilename')] = r.id end if !beer_styles.has_key?(line.fetch('beer_style')) bs = BeerStyle.create(:name => line.fetch('beer_style')) beer_styles[line.fetch('beer_style')] = bs.id end beer = Beer.create({ :beer_style_id => beer_styles.fetch(line.fetch('beer_style')), :name => line.fetch('beer_name'), :abv => line.fetch('beer_abv'), :brewery_id => breweries.fetch(line.fetch('brewery_name')) }) Review.create({ :reviewer_id => reviewers.fetch(line.fetch('review_profilename')), :beer_id => beer.id,
178

Chapter 9: Kernel Ridge Regression
:overall => line.fetch('review_overall'), :aroma => line.fetch('review_aroma'), :appearance => line.fetch('review_appearance'), :palate => line.fetch('review_palate'), :taste => line.fetch('review_taste') }) end
Now that we have loaded our data, we can move on to testing and building our rec‐ ommendation algorithm using Ridge Regression.
Reviewer Our first step is to quickly put together associations between all models, and to do that we write the following: # lib/models/reviewer.rb class Reviewer < Sequel::Model one_to_many :reviews one_to_many :user_preferences end # lib/models/brewery.rb class Brewery < Sequel::Model one_to_many :beers end # lib/models/beer_style.rb class BeerStyle < Sequel::Model one_to_many :beers end # lib/models/review.rb class Review < Sequel::Model many_to_one :reviewer end # lib/models/user_preference.rb class UserPreference < Sequel::Model many_to_one :reviewer many_to_one :beer_style end
At this point, we need to build a test for two different scenarios. The first is that for each rated style we want to assign a nonzero constant. The second is that we want the highest slope to be the most favorite style, and we want the smallest slope to be the least liked beer style.
Collaborative Filtering with Beer Styles

179
To test for a correct calculation, we write the following: # test/lib/models/reviewer_spec.rb describe Reviewer do let(:reviewer) { Reviewer.find(:id => 3) } it 'calculates a preference for a user correctly' do pref = reviewer.preference reviewed_styles = reviewer.reviews.map {r r.beer.beer_style_id } pref.each_with_index do r,i if reviewed_styles.include?(i + 1) r.wont_equal 0 else r.must_equal 0 end end end end
Let’s just assume that you have loaded Reviewer into the database and that you can pick a random reviewer to test, like id = 3. This test will make sure that there is only 0s for styles not rated, and a nonzero constant for rated styles. From here, we can test the real question, which is whether we can come up with a ranking for beer style likes. We do this by writing the following test: # test/lib/models/reviewer_spec.rb describe Reviewer do let (:reviewer) { Reviewer.find(:id => 3) } # Test from above it 'gives the highest rated beer_style the highest constant' do pref = reviewer.preference most_liked = pref.index(pref.max) + 1 least_liked = pref.index(pref.select(&:nonzero?).min) + 1 reviews = {} reviewer.reviews.each do r reviews[r.beer.beer_style_id] = [] reviews[r.beer.beer_style_id] review_ratings.fetch(least_liked)
180
 Chapter 9: Kernel Ridge Regression
best_fit = review_ratings.max_by(&:last) worst_fit = review_ratings.min_by(&:last) assert best_fit.first == most_liked  best_fit.last == review_ratings[most_ liked] assert worst_fit.first == least_liked  worst_fit.last == review_ratings[le ast_liked] end end
Now, of course, we need to write the actual code to make this work.
Writing the Code to Figure Out Someone’s Preference The problem we are trying to solve with these two tests is really finding a linear com‐ bination of beer styles to average ratings overall. There are around 104 beer styles, and most users won’t review that often. Therefore, we will probably have singular matrices, and regression won’t work right out of the box. So instead, we have to build an algorithm that will shrink the matrix enough so that it can invert. We do this by exponentially increasing the shrinkage parameter until it works. You will notice that I am using the NMatrix library, which is a subset of NArray. This is purely for speed. Unfortunately, the Matrix library in Ruby is slow and inefficient, so to do lots of calculations we have to utilize NMatrix. There are downsides to this library too, though, which are that NMatrix is really just a simple hack on top of NAr‐ ray and doesn’t have features like determinants or other neat tools. So I created a class called MatrixDeterminance that takes a matrix and calculates its determinant: # lib/matrix_determinance.rb require 'narray' require 'nmatrix' class MatrixDeterminance def initialize(matrix) @matrix = matrix end def determinant raise "Must be square" unless square? size = @matrix.sizes[1] last = size  1 a = @matrix.to_a no_pivot = Proc.new{ return 0 } sign = +1 pivot = 1.0 size.times do k previous_pivot = pivot if (pivot = a[k][k].to_f).zero?
Collaborative Filtering with Beer Styles

181
switch = (k+1 ... size).find(no_pivot) {row a[row][k] != 0 } a[switch], a[k] = a[k], a[switch] pivot = a[k][k] sign = sign end (k+1).upto(last) do i ai = a[i] (k+1).upto(last) do j ai[j] = (pivot * ai[j]  ai[k] * a[k][j]) / previous_pivot end end end sign * pivot end def singular? determinant == 0 end def square? @matrix.sizes[0] == @matrix.sizes[1] end def regular? !singular? end end
The way to use this is simple: just initialize a new MatrixDeterminance object and you can calculate whether that matrix is singular or regular and what the determinant is. Now we can calculate the user’s preference using a ridge regression: # lib/models/reviewer.rb class Reviewer < Sequel::Model one_to_many :reviews one_to_many :user_preferences IDENTITY = NMatrix[ *Array.new(104) { i Array.new(104) { j (i == j) ? 1.0 : 0.0 } } ] def preference @max_beer_id = BeerStyle.count return [] if reviews.empty?
182

Chapter 9: Kernel Ridge Regression
rows = [] overall = [] context = DB.fetch(