Web数据挖掘(Web Data Mining)第二版


Data-Centric Systems and Applications M.J. Carey S. Ceri Editorial Board P. Bernstein U. Dayal C. Faloutsos J.C. Freytag G. Gardarin W. Jonker V. Krishnamurthy M.-A. Neimat P. Valduriez G. Weikum K.-Y. Whang J. Widom Series Editors 123 Bing Liu Web Data Exploring Hyperlinks, Contents, and Usage Data Second Edition Mining Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com) The use of general descriptive names, registered names, trademarks, etc. in this publication does not laws and regulations and therefore free for general use. This work is subject to copyright. All rights are reserved, whether the whole or part of the material is in its current version, and permission for use must always be obtained from Springer. Violations are Springer London New York concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, liable to prosecution under the German Copyright Law. University of Illinois, Chicago Bing Liu Chicago, IL 60607-7053 USA Department of Computer Science liub@cs.uic.edu ISBN 978-3-642-19459-7 e-ISBN 978-3-642-19460-3 imply, even in the absence of a specific statement, that such names are exempt from the relevant protective DOI 10.1007/978-3-642-19460-3 Library of Congress Control Number: 2011932320 reproduction on microfilm or in any other way, and storage in data banks. Duplication of this publication ACM Codes: H.2, H.3, I.2, I.5, E.5 © Springer-Verlag Berlin Heidelberg 2007, 2011 Cover design: KünkelLopka GmbH or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, Heidelberg Dordrecht 851 S. Morgan St. To my parents, my wife Yue and children Shelley and Kate Preface The rapid growth of the Web in the past two decades has made it the larg- est publicly accessible data source in the world. Web mining aims to dis- cover useful information or knowledge from Web hyperlinks, page con- tents, and usage logs. Based on the primary kinds of data used in the mining process, Web mining tasks can be categorized into three main types: Web structure mining, Web content mining and Web usage mining. Web structure mining discovers knowledge from hyperlinks, which repre- sent the structure of the Web. Web content mining extracts useful informa- tion/knowledge from Web page contents. Web usage mining mines user activity patterns from usage logs and other forms of logs of user interac- tions with Web systems. Since the publication of the first edition at the end of 2006, there have been some important advances in several areas. To re- flect these advances, new materials have been added to most chapters. The major changes are in Chapter 11 and Chapter 12, which have been re- written and significantly expanded. When the first edition was written, opinion mining (Chapter 11) was still in its infancy. Since then, the re- search community has gained a much better understanding of the problem and has proposed many novel techniques to solve various aspects of the problem. To include the latest developments for the Web usage mining chapter (Chapter 12), the topics of recommender systems and collaborative filtering, query log mining, and computational advertising have been added. This new edition is thus considerably longer, from a total of 532 pages in the first edition to a total of 622 pages in this second edition. The goal of the book is to present the above Web data mining tasks and their core mining algorithms. The book is intended to be a text with a comprehensive coverage, and therefore, for each topic, sufficient details are given so that readers can gain a reasonably complete knowledge of its algorithms or techniques without referring to any external materials. Five of the chapters - partially supervised learning, structured data extraction, information integration, opinion mining and sentiment analysis, and Web usage mining - make this book unique. These topics are not covered by ex- isting books, but yet are essential to Web data mining. Traditional Web mining topics such as search, crawling and resource discovery, and social network analysis are also covered in detail in this book. Although the book is entitled Web Data Mining, it also includes the main topics of data mining and information retrieval since Web mining uses their algorithms and techniques extensively. The data mining part mainly consists of chapters on association rules and sequential patterns, supervised learning (or classification), and unsupervised learning (or clus- tering), which are the three fundamental data mining tasks. The advanced topic of partially (semi-) supervised learning is included as well. For in- formation retrieval, its core topics that are crucial to Web mining are de- scribed. The book is thus naturally divided into two parts. The first part, which consists of Chapters 2–5, covers data mining foundations. The sec- ond part, which consists of Chapters 6–12, covers Web specific mining. Two main principles have guided the writing of this book. First, the ba- sic content of the book should be accessible to undergraduate students, and yet there should be sufficient in-depth materials for graduate students who plan to pursue Ph.D. degrees in Web data mining or related areas. Few as- sumptions are made in the book regarding the prerequisite knowledge of readers. One with a basic understanding of algorithms and probability con- cepts should have no problem with this book. Second, the book should ex- amine the Web mining technology from a practical point of view. This is important because most Web mining tasks have immediate real-world ap- plications. In the past few years, I was fortunate to have worked directly or indirectly with many researchers and engineers in several search engine companies, e-commerce companies, opinion mining and sentiment analy- sis companies, and also traditional companies that are interested in exploit- ing the information on the Web in their businesses. During the process, I gained practical experiences and first-hand knowledge of real-world prob- lems. I try to pass those non-confidential pieces of information and knowl- edge along in the book. The book, thus, has a good balance of theory and practice. I hope that it will not only be a learning text for students, but also a valuable source of information/knowledge and ideas for Web mining re- searchers and practitioners. Acknowledgements Many researchers have assisted me technically in writing this book. With- out their help, the book might never have become reality. My deepest thanks go to Filippo Menczer, Bamshad Mobasher, and Olfa Nasraoui, who were so kind to have helped write two essential chapters of the book. They are all experts in their respective fields. Filippo wrote the entire chapter on Web crawling. Bamshad and Olfa wrote all sections of the chapter on Web usage mining except only the recommender systems sec- VIII Preface Preface IX tion for which they also helped. I am also very grateful to Wee Sun Lee, who helped a great deal in the writing of Chapter 5 on partially supervised learning. Jian Pei helped with the writing of the PrefixSpan algorithm in Chapter 2, and checked the MS-PS algorithm. Eduard Dragut assisted with the writing of the last section of Chapter 10 and also read the chapter many times. Yuanlin Zhang gave many great suggestions on Chapter 9. Simon Funk, Yehuda Koren, Wee Sun Lee, Jing Peng, Arkadiusz Paterek, and Domonkos Tikk helped with the recommender systems section in Chapter 12. I am indebted to all of them. Many other researchers have also assisted in various ways. Yang Dai and Rudy Setiono helped with Support Vector Machines (SVM). Chris Ding helped with social network analysis. Clement Yu and ChengXiang Zhai read Chapter 6. Amy Langville read Chapter 7. Kevin C.-C. Chang, Ji-Rong Wen, and Clement Yu helped with many aspects of Chapter 10. Justin Zobel helped clarify some issues related to index compression, and Ion Muslea helped clarify some issues on wrapper induction. Divy Agrawal, Yunbo Cao, Charles Elkan, Edward Fox, Jing Jiang, Hang Li, Xiaoli Li, Ruihua Song, Zhaohui Tan, Dell Zhang, Xin Zhao and Zijian Zheng helped check various chapters or sections. I am very grateful. Discussions with many researchers also helped shape the book: Amir Ashkenazi, Imran Aziz, Shenghua Bao, Roberto Bayardo, Wendell Baker, Ling Bao, Jeffrey Benkler, Brian Davison, AnHai Doan, Byron Dom, Juliana Freire, Michael Gamon, Robert Grossman, Natalie Glance, Jiawei Han, Meichun Hsu, Wynne Hsu, Ronny Kohavi, Birgit König, David D. Lewis, Ian McAllister, Wei-Ying Ma, Marco Maggini, Llew Mason, Kamel Nigan, Julian Qian, Yan Qu, Thomas M. Tirpak, Andrew Tomkins, Alexander Tuzhilin, Weimin Xiao, Gu Xu, Philip S. Yu, Mohammed Zaki, Yuri Zelenkov, and Daniel Zeng. My former and current students, Gao Cong, Xiaowen Ding, Murthy Ga- napathibhotla, Minqing Hu, Nitin Jindal, Xin Li, Yiming Ma, Arjun Muk- herjee, Quang Qiu (visiting student from Zhejiang University), William Underwood, Yanhong Zhai, Zhongwu Zhai (visiting student from Tsinghua University), Lei Zhang, and Kaidi Zhao contributed many research ideas over the years and/or checked many algorithms and made numerous cor- rections. Most chapters of the book have been used in my graduate classes at the University of Illinois at Chicago. I thank the students in these classes for implementing several algorithms. Their questions helped me improve and, in some cases, correct the algorithms. It is not possible to list all their names. Here, I would particularly like to thank John Castano, Hari Prasad Divyakotti, Islam Ismailov, Suhyuk Park, Cynthia Kersey, Po-Hsiu Lin, Srikanth Tadikonda, Makio Tamura, Ravikanth Turlapati, Guillermo Vazquez, Haisheng Wang, and Chad Williams for pointing out errors in texts, examples or algorithms. Michael Bombyk from DePaul University also found several typing errors. It was a pleasure working with the helpful staff at Springer. I thank my editor Ralf Gerstner who asked me in early 2005 whether I was interested in writing a book on Web mining. It has been a wonderful experience working with him since. I also thank my copyeditor Mike Nugent for helping me improve the presentation, and my production editor Michael Reinfarth for guiding me through the final production process. Two anonymous reviewers also gave me many insightful comments. The Department of Computer Science at the University of Illinois at Chicago provided computing resources and a supportive environment for this project. Finally, I thank my parents, brother and sister for their constant sup- ports and encouragements. My greatest gratitude goes to my own family: Yue, Shelley and Kate. They have helped me in so many ways. Despite their young ages, Shelley and Kate actually read many parts of the book and caught numerous typing errors. My wife has taken care of almost eve- rything at home and put up with me and the long hours that I have spent on this book. I dedicate this book to them. Bing Liu X Preface Table of Contents 1. Introduction ·································································· 1 1.1. What is the World Wide Web? ··································· 1 1.2. A Brief History of the Web and the Internet ············· 2 1.3. Web Data Mining ························································ 4 1.3.1. What is Data Mining? ·············································· 6 1.3.2. What is Web Mining? ·············································· 7 1.4. Summary of Chapters ················································ 8 1.5. How to Read this Book ············································ 11 Bibliographic Notes ··························································· 12 Bibliography ······································································· 13 Part I: Data Mining Foundations 2. Association Rules and Sequential Patterns ············ 17 2.1. Basic Concepts of Association Rules ····················· 17 2.2. Apriori Algorithm ······················································· 20 2.2.1. Frequent Itemset Generation ······························· 20 2.2.2 Association Rule Generation ······························· 24 2.3. Data Formats for Association Rule Mining ············· 26 2.4. Mining with Multiple Minimum Supports ················· 26 2.4.1 Extended Model ····················································· 28 2.4.2. Mining Algorithm ··················································· 30 2.4.3. Rule Generation ···················································· 35 2.5. Mining Class Association Rules ······························ 36 2.5.1. Problem Definition ················································ 36 2.5.2. Mining Algorithm ··················································· 38 2.5.3. Mining with Multiple Minimum Supports ············ 41 2.6. Basic Concepts of Sequential Patterns ··················· 41 2.7. Mining Sequential Patterns Based on GSP·············· 43 2.7.1. GSP Algorithm ······················································· 43 2.7.2. Mining with Multiple Minimum Supports ············ 45 2.8. Mining Sequential Patterns Based on PrefixSpan··· 49 2.8.1. PrefixSpan Algorithm ··········································· 50 2.8.2. Mining with Multiple Minimum Supports ············ 52 2.9. Generating Rules from Sequential Patterns ············ 53 2.9.1. Sequential Rules ··················································· 54 2.9.2. Label Sequential Rules ········································· 54 2.9.3. Class Sequential Rules ········································· 55 Bibliographic Notes ··························································· 56 Bibliography ······································································· 58 3. Supervised Learning ·················································· 63 3.1. Basic Concepts ······························································· 63 3.2. Decision Tree Induction ················································ 67 3.2.1. Learning Algorithm ··············································· 70 3.2.2. Impurity Function ·················································· 71 3.2.3. Handling of Continuous Attributes ······················ 75 3.2.4. Some Other Issues ················································ 76 3.3. Classifier Evaluation ······················································ 79 3.3.1. Evaluation Methods ·············································· 79 3.3.2. Precision, Recall, F-score and Breakeven Point 81 3.3.3. Receiver Operating Characteristic Curve ··········· 83 3.3.4. Lift Curve ······························································· 86 3.4. Rule Induction ·································································· 87 3.4.1. Sequential Covering ············································· 87 3.4.2. Rule Learning: Learn-One-Rule Function ············ 90 3.4.3. Discussion ····························································· 93 3.5. Classification Based on Associations ······················· 93 3.5.1. Classification Using Class Association Rules ··· 94 3.5.2. Class Association Rules as Features ·················· 98 3.5.3. Classification Using Normal Association Rules 99 3.6. Naïve Bayesian Classification ···································· 3.7. Naïve Bayesian Text Classification ·························· 103 XII Table of Contents 100 ·· ·· Table of Contents XIII 3.7.1. Probabilistic Framework ···································· 104 3.7.2. Naïve Bayesian Model ········································ 105 3.7.3. Discussion ··························································· 108 3.8. Support Vector Machines ··········································· 109 3.8.1. Linear SVM: Separable Case ······························ 111 3.8.2. Linear SVM: Non-Separable Case ······················ 117 3.8.3. Nonlinear SVM: Kernel Functions ····················· 120 3.9. K-Nearest Neighbor Learning ···································· 124 3.10. Ensemble of Classifiers ··············································· 12 3.10.1. Bagging ································································ 126 3.10.2. Boosting ······························································· 126 Bibliographic Notes ······························································· 12 Bibliography ····································································· 129 4. Unsupervised Learning ············································ 133 4.1. Basic Concepts ························································ 133 4.2. K-means Clustering ················································· 136 4.2.1. K-means Algorithm ············································· 136 4.2.2. Disk Version of the K-means Algorithm ············ 139 4.2.3. Strengths and Weaknesses ································ 140 4.3. Representation of Clusters ········································· 144 4.3.1. Common Ways of Representing Clusters ········· 145 4.3.2 Clusters of Arbitrary Shapes ······························ 146 4.4. Hierarchical Clustering ················································ 147 4.4.1. Single-Link Method ············································· 149 4.4.2. Complete-Link Method ······································· 149 4.4.3. Average-Link Method ·········································· 150 4.4.4. Strengths and Weaknesses ································ 150 4.5. Distance Functions ······················································· 151 4.5.1. Numeric Attributes ·············································· 151 4.5.2. Binary and Nominal Attributes ··························· 152 4.5.3. Text Documents ·················································· 154 4.6. Data Standardization ···················································· 155 4.7. Handling of Mixed Attributes ······································ 157 4.8. Which Clustering Algorithm to Use? ······················· 159 4.9. Cluster Evaluation ························································ 159 4.10. Discovering Holes and Data Regions ······················ 162 6 8 Bibliographic Notes ······························································· 165 Bibliography ····································································· 166 5. Partially Supervised Learning ································· 171 5.1. Learning from Labeled and Unlabeled Examples · 171 5.1.1. EM Algorithm with Naïve Bayesian 5.1.2. Co-Training ·························································· 176 5.1.3. Self-Training ························································ 178 5.1.4. Transductive Support Vector Machines ············ 179 5.1.5. Graph-Based Methods ········································ 180 5.1.6. Discussion ··························································· 183 5.2. Learning from Positive and Unlabeled Examples · 184 5.2.1. Applications of PU Learning ······························ 185 5.2.2. Theoretical Foundation ······································ 187 5.2.3. Building Classifiers: Two-Step Approach ········· 190 5.2.4. Building Classifiers: Biased-SVM ······················ 197 5.2.5. Building Classifiers: Probability Estimation ····· 199 5.2.6. Discussion ··························································· 201 Appendix: Derivation of EM for Naïve Bayesian Bibliographic Notes ······························································· 204 Bibliography ····································································· 206 Part II: Web Mining 6. Information Retrieval and Web Search ·················· 211 6.1. Basic Concepts of Information Retrieval ················ 212 6.2. Information Retrieval Models ·································· 215 6.2.1. Boolean Model ····················································· 216 6.2.2. Vector Space Model ············································ 21 6.2.3. Statistical Language Model ································ 219 6.3. Relevance Feedback ···················································· 220 6.4. Evaluation Measures ···················································· 223 XIV Table of Contents Classification Classification 7 173 ······················································· 202······················································· ·· Table of Contents XV 6.5. Text and Web Page Pre-Processing ························· 227 6.5.1. Stopword Removal ·············································· 227 6.5.2. Stemming ····························································· 228 6.5.3. Other Pre-Processing Tasks for Text ················ 228 6.5.4. Web Page Pre-Processing ·································· 229 6.5.5. Duplicate Detection ············································· 231 6.6. Inverted Index and Its Compression ························ 232 6.6.1. Inverted Index ······················································ 232 6.6.2. Search Using an Inverted Index ························· 234 6.6.3. Index Construction ············································· 235 6.6.4. Index Compression ············································· 23 6.7. Latent Semantic Indexing ··········································· 24 6.7.1. Singular Value Decomposition ·························· 243 6.7.2. Query and Retrieval ············································ 24 6.7.3. An Example ·························································· 24 6.7.4. Discussion ··························································· 249 6.8. Web Search ···································································· 2 6.9. Meta-Search: Combining Multiple Rankings ········· 25 6.9.1. Combination Using Similarity Scores ··············· 254 6.9.2. Combination Using Rank Positions ·················· 255 6.10. Web Spamming ····························································· 257 6.10.1. Content Spamming ············································· 258 6.10.2. Link Spamming ··················································· 259 6.10.3. Hiding Techniques ·············································· 26 6.10.4. Combating Spam ················································· 26 Bibliographic Notes ······························································· 263 Bibliography ····································································· 264 7. Social Network Analysis ·········································· 269 7.1. Social Network Analysis ············································· 270 7.1.1 Centrality ······························································ 270 7.1.2 Prestige ································································ 273 7.2. Co-Citation and Bibliographic Coupling ················· 275 7.2.1. Co-Citation ··························································· 276 7.2.2. Bibliographic Coupling ······································· 277 7.3. PageRank ········································································ 277 7.3.1. PageRank Algorithm ··········································· 278 7.3.2. Strengths and Weaknesses of PageRank·········· 285 6 2 5 6 49 2 0 1 XVI Table of Contents 7.3.3. Timed PageRank and Recency Search ·············· 286 7.4. HITS ·················································································· 288 7.4.1. HITS Algorithm ···················································· 289 7.4.2. Finding Other Eigenvectors ······························· 291 7.4.3. Relationships with Co-Citation and Coupling ····································· 292 7.4.4. Strengths and Weaknesses of HITS ·················· 293 7.5. Community Discovery ················································· 294 7.5.1. Problem Definition ·············································· 295 7.5.2. Bipartite Core Communities ······························· 29 7.5.3. Maximum Flow Communities ····························· 298 7.5.4. Email Communities Based on Betweenness ···· 301 7.5.5. Overlapping Communities of Named Entities ·· 30 Bibliographic Notes ································································ 304 Bibliography ····································································· 305 8. Web Crawling ···························································· 311 8.1. A Basic Crawler Algorithm ····································· 312 8.1.1. Breadth-First Crawlers ······································· 313 8.1.2. Preferential Crawlers ·········································· 314 8.2. Implementation Issues ················································· 315 8.2.1. Fetching ······························································· 315 8.2.2. Parsing ································································· 316 8.2.3. Stopword Removal and Stemming ···················· 318 8.2.4. Link Extraction and Canonicalization ··············· 318 8.2.5. Spider Traps ························································ 320 8.2.6. Page Repository ·················································· 321 8.2.7. Concurrency ························································ 322 8.3. Universal Crawlers ························································ 323 8.3.1. Scalability ···························································· 324 8.3.2. Coverage vs Freshness vs Importance ············· 326 8.4. Focused Crawlers ························································· 327 8.5. Topical Crawlers ··························································· 330 8.5.1. Topical Locality and Cues ·································· 332 8.5.2. Best-First Variations ··········································· 338 8.5.3. Adaptation ··························································· 341 8.6. Evaluation ······································································· 348 8.7. Crawler Ethics and Conflicts ······································ 353 Bibliographic 7 3 . . Table of Contents XVII 8.8. Some New Developments ··········································· 356 Bibliographic Notes ·························································· 358 Bibliography ····································································· 359 9. Structured Data Extraction: Wrapper Generation · 363 9.1 Preliminaries ·································································· 364 9.1.1. Two Types of Data Rich Pages ·························· 364 9.1.2. Data Model ··························································· 366 9.1.3. HTML Mark-Up Encoding of Data Instances ····· 368 9.2. Wrapper Induction ························································ 370 9.2.1. Extraction from a Page ······································· 370 9.2.2. Learning Extraction Rules ·································· 373 9.2.3. Identifying Informative Examples ······················ 377 9.2.4. Wrapper Maintenance ········································· 378 9.3. Instance-Based Wrapper Learning ··························· 378 9.4. Automatic Wrapper Generation: Problems ············ 381 9.4.1. Two Extraction Problems ··································· 382 9.4.2. Patterns as Regular Expressions ······················ 383 9.5. String Matching and Tree Matching ························· 384 9.5.1. String Edit Distance ············································ 384 9.5.2. Tree Matching ······················································ 386 9.6. Multiple Alignment ························································ 390 9.6.1. Center Star Method ············································· 390 9.6.2. Partial Tree Alignment ········································ 391 9.7. Building DOM Trees ····················································· 396 9.8. Extraction Based on a Single List Page: Flat Data Records ························································· 397 9.8.1. Two Observations about Data Records ············ 398 9.8.2. Mining Data Regions ··········································· 399 9.8.3. Identifying Data Records in Data Regions ········ 404 9.8.4. Data Item Alignment and Extraction ·················· 405 9.8.5. Making Use of Visual Information ······················ 406 9.8.6. Some Other Techniques ····································· 406 9.9. Extraction Based on a Single List Page: Nested Data Records ··················································· 407 9.10. Extraction Based on Multiple Pages ························ 413 9.10.1. Using Techniques in Previous Sections ··········· 413 XVIII Table of Contents 9.10.2. RoadRunner Algorithm ······································· 414 9.11. Some Other Issues ······················································· 415 9.11.1. Extraction from Other Pages ······························ 416 9.11.2. Disjunction or Optional ······································ 416 9.11.3. A Set Type or a Tuple Type ································ 417 9.11.4. Labeling and Integration ····································· 418 9.11.5. Domain Specific Extraction ································ 418 9.12. Discussion ······································································ 419 Bibliographic Notes ································································ 419 Bibliography ····································································· 421 10. Information Integration ············································ 425 10.1. Introduction to Schema Matching ··························· 426 10.2. Pre-Processing for Schema Matching ···················· 428 10.3. Schema-Level ············································· 429 10.3.1. Linguistic Approaches ······································ 429 10.3.2. Constraint Based Approaches ························· 430 10.4. Domain and Instance-Level Matching ···················· 431 10.5. Combining Similarities ··············································· 434 10.6. 1:m Match ······································································ 435 10.7. Some Other Issues ······················································ 436 10.7.1. Reuse of Previous Match Results····················· 436 10.7.2. Matching a Large Number of Schemas ··········· 437 10.7.3 Schema Match Results ····································· 437 10.7.4 User Interactions ··············································· 438 10.8. Integration of Web Query Interfaces ······················· 438 10.8.1. A Clustering Based Approach ·························· 441 10.8.2. A Correlation Based Approach ························· 444 10.8.3. An Instance Based Approach ··························· 447 10.9. Constructing a Unified Global Query Interface ···· 450 10.9.1. Structural Appropriateness and the Merge Algorithm ················································ 45 10.9.2. Lexical Appropriateness ··································· 45 10.9.3. Instance Appropriateness ································ 45 Bibliographic Notes ································································ 454 Bibliography ····································································· 455 1 3 4 Matching Table of Contents XIX 11. Opinion Mining and Sentiment Analysis ···················· 459 11.1. The Problem of Opinion Mining ······························· 460 11.1.1. Problem Definitions ··········································· 460 11.1.2. Aspect-Based Opinion Summary ····················· 467 11.2. Document Sentiment Classification ······················· 469 11.2.1. Classification Based on Supervised Learning 470 11.2.2. Classification Based on Unsupervised 7 11.3. Sentence Subjectivity and Sentiment Classification 474 11.4. Opinion Lexicon Expansion ································· 477 11.5. Aspect-Based Opinion Mining ································· 480 11.5.1. Aspect Sentiment Classification ······················ 481 11.5.2. Basic Rules of Opinions ··································· 483 11.5.3. Aspect Extraction ·············································· 486 11.5.4. Simultaneous Opinion Lexicon Expansion and Aspect Extraction ······································· 490 11.6. Mining Comparative Opinions ·································· 493 11.6.1. Problem Definitions ··········································· 493 11.6.2. Identification of Comparative Sentences ········ 495 11.6.3. Identification of Preferred Entities ··················· 496 11.7. Some Other Problems ················································ 498 11.8. Opinion Search and Retrieval ·································· 503 11.9. Opinion Spam Detection ············································ 506 11.9.1. Types of Spam and Spammers ························ 506 11.9.2. Hiding Techniques ············································ 508 11.9.3. Spam Detection Based on Supervised Learning 509 11.9.4. Spam Detection Based on Abnormal Behaviors 511 11.9.5. Group Spam Detection······································· 513 11.10. Utility of Reviews ························································· 514 Bibliographic Notes ································································ 515 Bibliography ····································································· 517 12. Web Usage Mining ······························································ 527 12.1. Data Collection and Pre-Processing ······················· 528 Learning 4 2······················································· ····· ······························································· ······················································· ······ ······················································ ····· · XX Table of Contents 12.1.1. Sources and Types of Data ······························ 530 12.1.2. Key Elements of Web Usage Data Pre-Processing ·················································· 533 12.2. Data Modeling for Web Usage Mining ···················· 540 12.3. Discovery and Analysis of Web Usage Patterns · 544 12.3.1. Session and Visitor Analysis ···························· 544 12.3.2. Cluster Analysis and Visitor Segmentation ···· 545 12.3.3. Association and Correlation Analysis ············· 5 12.3.4. Analysis of Sequential and Navigational 550 1 2.3.5. Classification and Prediction ased on Web User Transactions ············································· 554 12.4. Recommender Systems and Collaborative 12.4.1. The Recommendation ···················· 556 12.4.2. Content-Based Recommendation ···················· 557 12.4.3. Collaborative Filtering: K-Nearest Neighbor 559 12.4.4. Collaborative Filtering: Using Association 12.4.5. Collaborative Filtering: Matrix Factorization ··· 565 12.5. Query Log Mining ························································ 571 12.5.1. Data Sources, Characteristics, and Challenge 573 12.5.2. Query Log Data Preparation ····························· 57 12.5.3. Query Log Data Models ···································· 577 12.5.4. Query Log Feature Extraction ·························· 582 12.5.5. Query Log Mining Applications ························ 583 12.5.6. Query Log Mining Methods ······························ 586 12.6. Computational Advertising ······································· 589 12.7. Discussion and Outlook ············································ 593 Bibliographic Notes ································································ 593 B ibliography ····································································· 59 Subject Index ················································································· 605 s·· 4 4 49 b ······················································· ········Patterns ··········································································Filtering 555 (KNN) ······················································· ············ 5··································································· 61Rules Problem ··· 1 Introduction When you read this book, you, without doubt, already know what the World Wide Web is and have used it extensively. The World Wide Web (or the Web for short) has impacted almost every aspect of our lives. It is the biggest and most widely known information source that is easily acces- sible and searchable. It consists of billions of interconnected documents (called Web pages) which are authored by millions of people. Since its in- ception, the Web has dramatically changed our information seeking behav- ior. Before the Web, finding information meant asking a friend or an ex- pert, or buying/borrowing a book to read. However, with the Web, everything is just a few clicks away from the comfort of our homes or of- fices. We can not only find needed information on the Web, but also easily share our information and knowledge with others. The Web has also become an important channel for conducting busi- nesses. We can buy almost anything from online stores without needing to go to a physical shop. The Web also provides a convenient means for us to communicate with each other, to express our views and opinions, and to discuss with people from anywhere in the world. The Web is truly a vir- tual society. In this first chapter, we introduce the Web, its history, and the topics that we will study in this book. 1.1 What is the World Wide Web? The World Wide Web is officially defined as a “wide-area hypermedia in- formation retrieval initiative aiming to give universal access to a large uni- verse of documents.” In simpler terms, the Web is an Internet-based computer network that allows users of one computer to access information stored on another through the world-wide network called the Internet. The Web's implementation follows a standard client-server model. In this model, a user relies on a program (called the client) to connect to a remote machine (called the server) where the data is stored. Navigating through the Web is done by means of a client program called the browser, e.g., Netscape, Internet Explorer, Firefox, Chrome, etc. Web browsers work by sending requests to remote servers for information and then inter- B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, 1 Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_1, © Springer-Verlag Berlin Heidelberg 2011 2 1 Introduction preting the returned documents written in HTML and laying out the text and graphics on the user’s computer screen on the client side. The operation of the Web relies on the structure of its hypertext docu- ments. Hypertext allows Web page authors to link their documents to other related documents residing on computers anywhere in the world. To view these documents, one simply follows the links (called hyperlinks). The idea of hypertext was invented by Ted Nelson in 1965 [14], who also created the well known hypertext system Xanadu (http://xanadu.com/). Hypertext that also allows other media (e.g., image, audio and video files) is called hypermedia. 1.2 A Brief History of the Web and the Internet Creation of the Web: The Web was invented in 1989 by Tim Berners- Lee, who, at that time, worked at CERN (Centre European pour la Recher- che Nucleaire, or European Laboratory for Particle Physics) in Switzer- land. He coined the term “World Wide Web,” wrote the first World Wide Web server, httpd, and the first client program (a browser and editor), “WorldWideWeb.” It began in March 1989 when Tim Berners-Lee submitted a proposal ti- tled “Information Management: A Proposal” to his superiors at CERN. In the proposal, he discussed the disadvantages of hierarchical information organization and outlined the advantages of a hypertext-based system. The proposal called for a simple protocol that could request information stored in remote computer systems through networks, and for a scheme by which information could be exchanged in a common format and documents of individuals could be linked by hyperlinks to other documents. It also pro- posed methods for reading text and graphics using the display technology at CERN at that time. The proposal essentially outlined a distributed hy- pertext system, which is the basic architecture of the Web. Initially, the proposal did not receive the needed support. However, in 1990, Berners-Lee re-circulated the proposal and received the support to begin the work. With this project, Berners-Lee and his team at CERN laid the foundation for the future development of the Web as a distributed hy- pertext system. They introduced their server and browser, the protocol used for communication between clients and the server, the HyperText Transfer Protocol (HTTP), the HyperText Markup Language (HTML) used for authoring Web documents, and the Universal Resource Locator (URL). And so it began. 1.2 A Brief History of the Web and the Internet 3 Mosaic and Netscape Browsers: The next significant event in the de- velopment of the Web was the arrival of Mosaic. In February of 1993, Marc Andreesen from the University of Illinois’ NCSA (National Center for Supercomputing Applications) and his team released the first "Mosaic for X" graphical Web browser for UNIX. A few months later, different versions of Mosaic were released for Macintosh and Windows operating systems. This was an important event. For the first time, a Web client, with a consistent and simple point-and-click graphical user interface, was im- plemented for the three most popular operating systems available at the time. It soon made big splashes outside the academic circle where it had begun. In mid-1994, Silicon Graphics founder Jim Clark collaborated with Marc Andreessen, and they founded the company Mosaic Communications (later renamed as Netscape Communications). Within a few months, the Netscape browser was released to the public, which started the explosive growth of the Web. The Internet Explorer from Microsoft entered the market in August, 1995 and began to challenge Netscape. The creation of the World Wide Web by Tim Berners-Lee followed by the release of the Mosaic browser are often regarded as the two most sig- nificant contributing factors to the success and popularity of the Web. Internet: The Web would not be possible without the Internet, which provides the communication network for the Web to function. The Inter- net started with the computer network ARPANET in the Cold War era. It was produced as the result of a project in the United States aiming at main- taining control over its missiles and bombers after a nuclear attack. It was supported by the Advanced Research Projects Agency (ARPA), which was part of the Department of Defense in the United States. The first ARPANET connections were made in 1969, and in 1972, it was demon- strated at the First International Conference on Computers and Communi- cation, held in Washington D.C. At the conference, ARPA scientists linked together computers from 40 different locations. In 1973, Vinton Cerf and Bob Kahn started to develop the protocol later to be called TCP/IP (Transmission Control Protocol/Internet Proto- col). In the next year, they published the paper “Transmission Control Pro- tocol”, which marked the beginning of TCP/IP. This new protocol allowed diverse computer networks to interconnect and communicate with each other. In subsequent years, many networks were built, and many compet- ing techniques and protocols were proposed and developed. However, ARPANET was still the backbone to the entire system. During the period, the network scene was chaotic. In 1982, the TCP/IP was finally adopted, and the Internet, which is a connected set of networks using the TCP/IP protocol, was born. 4 1 Introduction Search Engines: With information being shared worldwide, there was a need for individuals to find information in an orderly and efficient manner. Thus began the development of search engines. The search system Excite was introduced in 1993 by six Stanford University students. EINet Galaxy was established in 1994 as part of the MCC Research Consortium at the University of Texas. Jerry Yang and David Filo created Yahoo! in 1994, which started out as a listing of their favorite Web sites, and offered direc- tory search. In subsequent years, many search systems emerged, e.g., Ly- cos, Inforseek, AltaVista, Inktomi, Ask Jeeves, Northernlight, etc. Google was launched in 1998 by Sergey Brin and Larry Page based on their research project at Stanford University. Microsoft started to commit to search in 2003, and launched the MSN search engine in spring 2005 (which is now called Bing). Yahoo! provided a general search capability in 2004 after it purchased Inktomi in 2003. W3C (The World Wide Web Consortium): W3C was formed in the December of 1994 by MIT and CERN as an international organization to lead the development of the Web. W3C's main objective was “to promote standards for the evolution of the Web and interoperability between WWW products by producing specifications and reference software.” The first International Conference on World Wide Web (WWW) was also held in 1994, which has been a yearly event ever since. From 1995 to 2001, the growth of the Web boomed. Investors saw commercial opportunities and became involved. Numerous businesses started on the Web, which led to irrational developments. Finally, the bub- ble burst in 2001. However, the development of the Web was not stopped, but has only become more rational since. 1.3 Web Data Mining The rapid growth of the Web in the last decade makes it the largest pub- licly accessible data source in the world. The Web has many unique char- acteristics, which make mining useful information and knowledge a fasci- nating and challenging task. Let us review some of these characteristics. 1. The amount of data/information on the Web is huge and still growing. The coverage of the information is also very wide and diverse. One can find information on almost anything on the Web. 2. Data of all types exist on the Web, e.g., structured tables, semi- structured pages, unstructured texts, and multimedia files (images, audios, and videos). 1.3 Web Data Mining 5 3. Information on the Web is heterogeneous. Due to diverse authorships of Web pages, multiple pages may present the same or similar information using completely different words and/or formats. This makes integra- tion of information from multiple pages a challenging problem. 4. A significant amount of information on the Web is linked. Hyperlinks exist among Web pages within a site and across different sites. Within a site, hyperlinks serve as an information organization mechanism. Across different sites, hyperlinks represent implicit conveyance of au- thority to the target pages. That is, those pages that are linked (or pointed) to by many other pages are usually high quality pages or au- thoritative pages simply because many people trust them. 5. The information on the Web is noisy. The noise comes from two main sources. First, a typical Web page contains many pieces of information, e.g., the main content of the page, navigation links, advertisements, copyright notices, privacy policies, etc. For a particular application, only part of the information is useful. The rest is considered noise. To perform fine-grained Web information analysis and data mining, the noise should be removed. Second, due to the fact that the Web does not have quality control of information, i.e., one can write almost anything that one likes, a large amount of information on the Web is of low qual- ity, erroneous, or even misleading. 6. The Web is also about businesses and commerce. All commercial Web sites allow people to perform useful operations at their sites, e.g., to purchase products, to pay bills, and to fill in forms. To support such ap- plications, the Web site needs to provide many types of automated ser- vices, e.g., recommendation services using recommender systems. 7. The Web is dynamic. Information on the Web changes constantly. Keeping up with the change and monitoring the change are important issues for many applications. 8. The Web is a virtual society. It is not just about data, information and services, but also about interactions among people, organizations and automated systems. One can communicate with people anywhere in the world easily and instantly, and also express one’s views and opinions on anything in Internet forums, blogs, review sites and social network sites. Such information offers new types of data that enable many new mining tasks, e.g., opinion mining and social network analysis. All these characteristics present both challenges and opportunities for min- ing and discovery of information and knowledge from the Web. In this book, we focus only on mining textual data. For mining of images, videos and audios, please refer to [15, 26]. To explore information mining on the Web, it is necessary to know data 6 1 Introduction mining, which has been applied in many Web mining tasks. However, Web mining is not entirely an application of data mining. Due to the rich- ness and diversity of information and other Web specific characteristics discussed above, Web mining has developed many of its own algorithms. 1.3.1 What is Data Mining? Data mining is also called knowledge discovery in databases (KDD). It is commonly defined as the process of discovering useful patterns or knowledge from data sources, e.g., databases, texts, images, the Web, etc. The patterns must be valid, potentially useful, and understandable. Data mining is a multi-disciplinary field involving machine learning, statistics, databases, artificial intelligence, information retrieval, and visualization. There are many data mining tasks. Some of the common ones are su- pervised learning (or classification), unsupervised learning (or cluster- ing), association rule mining, and sequential pattern mining. We will study all of them in this book. A data mining application usually starts with an understanding of the application domain by data analysts (data miners), who then identify suitable data sources and the target data. With the data, data mining can be performed, which is usually carried out in three main steps:  Pre-processing: The raw data is usually not suitable for mining due to various reasons. It may need to be cleaned to remove noises or abnor- malities. The data may also be too large and/or involve many irrelevant attributes, which call for data reduction through sampling and attribute or feature selection. Details about data pre-processing can be found in any standard data mining textbook.  Data mining: The processed data is then fed to a data mining algorithm which will produce patterns or knowledge.  Post-processing: In many applications, not all discovered patterns are useful. This step identifies those useful ones for applications. Various evaluation and visualization techniques are used to make the decision. The whole process (also called the data mining process) is almost always iterative. It usually takes many rounds to achieve the final satisfactory re- sult, which is then incorporated into real-world operational tasks. Traditional data mining uses structured data stored in relational tables, spread sheets, or flat files in the tabular form. With the growth of the Web and text documents, Web mining and text mining are becoming increas- ingly important and popular. Web mining is the focus of this book. 1.3 Web Data Mining 7 1.3.2 What is Web Mining? Web mining aims to discover useful information or knowledge from the Web hyperlink structure, page content, and usage data. Although Web mining uses many data mining techniques, as mentioned above it is not purely an application of traditional data mining techniques due to the het- erogeneity and semi-structured or unstructured nature of the Web data. Many new mining tasks and algorithms were invented in the past decade. Based on the primary kinds of data used in the mining process, Web min- ing tasks can be categorized into three types: Web structure mining, Web content mining and Web usage mining.  Web structure mining: Web structure mining discovers useful knowl- edge from hyperlinks (or links for short), which represent the structure of the Web. For example, from the links, we can discover important Web pages, which is a key technology used in search engines. We can also discover communities of users who share common interests. Tradi- tional data mining does not perform such tasks because there is usually no link structure in a relational table.  Web content mining: Web content mining extracts or mines useful in- formation or knowledge from Web page contents. For example, we can automatically classify and cluster Web pages according to their topics. These tasks are similar to those in traditional data mining. However, we can also discover patterns in Web pages to extract useful data such as descriptions of products, postings of forums, etc., for many purposes. Furthermore, we can mine customer reviews and forum postings to dis- cover consumer opinions. These are not traditional data mining tasks.  Web usage mining: Web usage mining refers to the discovery of user access patterns from Web usage logs, which record every click made by each user. Web usage mining applies many data mining algorithms. One of the key issues in Web usage mining is the pre-processing of click- stream data in usage logs in order to produce the right data for mining. In this book, we will study all these three types of mining. However, due to the richness and diversity of information on the Web, there are a large number of Web mining tasks. We will not be able to cover them all. We will only focus on some important tasks and their fundamental algorithms. The Web mining process is similar to the data mining process. The dif- ference is usually in the data collection. In traditional data mining, the data is often already collected and stored in a data warehouse. For Web mining, data collection can be a substantial task, especially for Web structure and content mining, which involves crawling a large number of target Web pages. We will devote a whole chapter to crawling. 8 1 Introduction Once the data is collected, we go through the same three-step process: data pre-processing, Web data mining and post-processing. However, the techniques used for each step can be quite different from those used in tra- ditional data mining. 1.4 Summary of Chapters This book consists of two main parts. The first part, which includes Chaps. 2–5, covers the major topics of data mining. The second part, which com- prises the rest of the chapters, covers Web mining (including a chapter on Web search). In the Web mining part, Chaps. 7 and 8 are on Web structure mining, which are closely related to Web search (Chap. 6). Since it is dif- ficult to draw a boundary between Web search and Web mining, Web search and mining are put together. Chaps 9–11 are on Web content min- ing, and Chap. 12 is on Web usage mining. Below we give a brief intro- duction to each chapter. Chapter 2 – Association Rules and Sequential Patterns: This chapter studies two important data mining models that have been used in many Web mining tasks, especially in Web usage and content mining. Associa- tion rule mining finds sets of data items that occur together frequently. Se- quential pattern mining finds sets of data items that occur together fre- quently in some sequences. Clearly, they can be used to find regularities in the Web data. For example, in Web usage mining, association rule mining can be used to find users’ visit and purchase patterns, and sequential pat- tern mining can be used to find users’ navigation patterns. Chapter 3 – Supervised Learning: Supervised learning is perhaps the most frequently used mining/learning technique in both practical data min- ing and Web mining. It is also called classification, which aims to learn a classification function (called a classifier) from data that are labeled with pre-defined classes or categories. The resulting classifier is then applied to classify future data instances into these classes. Due to the fact that the data instances used for learning (called the training data) are labeled with pre-defined classes, the method is called supervised learning. Chapter 4 – Unsupervised Learning: In unsupervised learning, the data used for learning has no pre-defined classes. The learning algorithm has to find the hidden structures or regularities in the data. One of the key unsu- pervised learning techniques is clustering, which organizes data instances into groups or clusters according to their similarities (or differences). Clustering is widely used in Web mining. For example, we can cluster 1.4 Summary of Chapters 9 Web pages into groups, where each group may represent a particular topic. We can also cluster documents into a hierarchy of clusters, which may rep- resent a topic hierarchy. Chapter 5 – Partially Supervised Learning: Supervised learning re- quires a large number of labeled data instances to learn an accurate classi- fier. Labeling, which is often done manually, is labor intensive and time consuming. To reduce the manual labeling effort, learning from labeled and unlabeled examples (or LU learning) was proposed to use a small set of labeled examples (data instances) and a large set of unlabeled exam- ples for learning. This model is also called semi-supervised learning. Another learning model that we will study is called learning from posi- tive and unlabeled examples (or PU learning), which is for two-class classifications (the two classes are often called the positive and negative classes). However, there are no labeled negative examples for learning. This model is useful in many situations. For example, we have a set of Web mining papers and we want to identify other Web mining papers in a research paper repository which contains all kinds of papers. The set of Web mining papers can be treated as the positive data, and the papers in the research repository can be treated as the unlabeled data. Chapter 6 – Information Retrieval and Web Search: Search is probably the largest application on the Web. It has its root in information retrieval (or IR for short), which is a field of study that helps the user find needed information from a large collection of text documents. Given a query (e.g., a set of keywords), which expresses the user’s information need, an IR system finds a set of documents that is relevant to the query from its un- derlying collection. This is also how a Web search engine works. Web search brings IR to a new height. It applies some IR techniques, but also presents a host of interesting problems due to special characteris- tics of the Web data. First of all, Web pages are not the same as plain text documents because they are semi-structured and contain hyperlinks. Thus, new methods have been designed to produce better Web IR (or search) systems. Another major issue is efficiency. Document collections used in traditional IR systems are not large, but the number of pages on the Web is huge. For example, Google claimed that it indexed more than 8 billion pages when the first edition of this book was written. Web users demand very fast responses. No matter how accurate a retrieval algorithm is, if the retrieval cannot be done extremely efficiently, few people will use it. In the chapter, several other search related issues will also be discussed. Chapter 7 – Social Network Analysis: Hyperlinks are a special feature of the Web, which link Web pages to form a huge network. They have been 10 1 Introduction exploited for many purposes, especially for Web search. Google’s early success was largely attributed to its hyperlink-based ranking algorithm called PageRank, which was originated from social network analysis [24]. In this chapter, we will first introduce some main concepts of social network analysis and then describe two most well known Web hyperlink analysis algorithms, PageRank and HITS. In addition, we will also study several community finding algorithms. When Web pages link to one an- other, they form Web communities, which are groups of content creators that share some common interests. Communities not only manifest in hy- perlinks, but also in other contexts such as emails, Web page contents, and friendship networks on social networking sites. Chapter 8 – Web Crawling: A Web crawler is a program that automati- cally traverses the Web’s hyperlink structure and downloads each linked page to a local storage. Crawling is often the first step of Web mining or building a Web search engine. Although conceptually easy, implementing a practical crawler is by no means simple. Due to efficiency and many other concerns, it involves a great deal of engineering. There are two main types of crawlers: universal crawlers and topic crawlers. A universal crawler downloads all pages irrespective of their contents, while a topic crawler downloads only pages of certain topics. The difficulty in topic crawling is how to recognize such pages. We will study several techniques for this purpose. Chapter 9 – Structured Data Extraction: Wrapper Generation: A large number of pages on the Web contain structured data, which are usu- ally data records retrieved from underlying databases and displayed in Web pages following some fixed templates. Structured data often represent their host pages’ essential information, e.g., lists of products and services. Extracting such data allows one to provide value added services, e.g., comparative shopping and meta-search. There are two main approaches to extraction. One is the supervised approach, which uses supervised learning to learn data extraction rules. The other is the unsupervised pattern discov- ery approach, which finds repeated patterns (hidden templates) in Web pages for data extraction. Chapter 10 – Information Integration: Due to diverse authorships of the Web, different Web sites typically use different words or terms to express the same or similar information. In order to make use of the data or infor- mation extracted from multiple sites to provide value added services, we need to semantically integrate the data/information from these sites in or- der to produce a consistent and coherent database. Intuitively, integration means (1) to match columns in different data tables that contain the same 1.5 How to Read this Book 11 type of information (e.g., product names) and (2) to match data values that are semantically the same but expressed differently at different sites. Chapter 11 – Opinion Mining and Sentiment Analysis: Apart from structured data, the Web also contains a huge amount of unstructured text. Analyzing such text is also of great importance. It is perhaps even more important than extracting structured data because of the sheer volume of valuable information of almost any imaginable types contained in it. This chapter focuses only on mining people’s opinions and sentiments ex- pressed in product reviews, forum discussions and blogs. The task is not only technically challenging, but also very useful in practice because busi- nesses and organizations always want to know consumer opinions on their products and services. Chapter 12 – Web Usage Mining: Web usage mining aims to study user clicks and their applications to e-commerce and business intelligence. The objective is to capture and model behavioral patterns and profiles of us- ers who interact with a Web site. Such patterns can be used to better un- derstand the behaviors of different user segments, to improve the organiza- tion and structure of the site, and to create personalized experiences for users by providing dynamic suggestions of products and services using re- commender systems. This chapter also covers the important topics of query log mining and computational advertising, which have emerged as active research areas in recent years. 1.5 How to Read this Book This book is a textbook although two chapters are mainly contributed by three other researchers. The contents of the two chapters have been care- fully edited and integrated into the common framework of the whole book. The book is suitable for both graduate students and senior undergraduate students in the fields of computer science, information science, engineer- ing, statistics, and social sciences. It can also be used as a reference by re- searchers and practitioners who are interested in or are working in the field of Web mining, data mining or text mining. As mentioned earlier, the book is divided into two parts. Part I (Chaps. 2–5) covers the major topics of data mining. Text classification and clus- tering are included in this part as well. Part II, which includes the rest of the chapters, covers Web mining (and search). In general, all chapters in Part II require some techniques in Part I. Within each part, the dependency is minimal except Chap. 5, which needs several techniques from Chap. 4. 12 1 Introduction To Instructors: This book can be used as a class text for a one-semester course on Web data mining. In this case, there are two possibilities. If the students already have a data mining or machine learning background, the chapters in Part I can be skipped. If the students do not have any data min- ing background, I recommend covering some selected sections from each chapter of Part I before going to Part II. The chapters in Part II can be cov- ered in any sequence. You can also select a subset of the chapters accord- ing to your needs. The book may also be used as a class text for an introductory course on data mining where Web mining concepts and techniques are introduced. In this case, I recommend first covering all the chapters in Part I and then se- lectively covering some chapters or sections from each chapter in Part II depending on needs. It is usually a good idea to cover some sections of Chaps. 6 and 7 as search engines fascinate most students. I also recom- mend including one or two lectures on data pre-processing for data mining since the topic is important for practical data mining applications but is not covered in this book. You can find teaching materials on data pre-processing from most introductory data mining books. Supporting Materials: Updates to chapters and teaching materials, in- cluding lecture slides, data sets, implemented algorithms, and other re- sources, are available at http://www.springer.com/3-540-37881-2. Bibliographic Notes The W3C Web site (http://www.w3.org/) is the most authoritative resource site for information on Web developments, standards and guidelines. The history of the Web and hypertext, and Tim Berners-Lee’s original proposal can all be found there. Many other sites also contain information about the history of the Web, the Internet and search engines, e.g., http://www.elsop. com/wrc/h_web.htm, http://www.zeltser.com/web-history/, http://www.isoc. org/internet/history/, http://www.livinginternet.com, http://www.w3c.rl.ac.uk/ primers/history/origins.htm and http://searchenginewatch.com/. There are some earlier introductory texts on Web mining, e.g., those by Baldi et al. [1] and Chakrabarti [3]. There are also several application ori- ented books, e.g., those by Linoff and Berry [12], and Thuraisingham [22], and edited volumes by Zaiane et al. [26], Scime [19], and Zhong et al. [27]. On data mining, there are many textbooks, e.g., those by Duda et al. [4], Dunham [5], Han and Kamber [8], Hand et al. [9], Larose [11], Langley [10], Mitchell [13], Roiger and Geatz [17], Tan et al. [20], and Witten and Frank [25]. Application oriented books include those by Berry and Linoff Bibliography 13 [2], Pyle [16], Rud [18], and Tang and MacLennan [21]. Several edited volumes exist as well, e.g., those by Fayyad et al. [6], Grossman et al. [7], and Wang et al. [23]. Latest research results on Web mining can be found in a large number of conferences and journals (too many to list) due to the interdisciplinary nature of the field. All the journals and conferences related to the Web technology, information retrieval, data mining, databases, artificial intelli- gence, natural language processing, and machine learning may contain Web mining related papers. Bibliography 1. Baldi, P., P. Frasconi, and P. Smyth. Modeling the Internet and the Web: Probabilistic methods and algorithms. 2003: John Wiley & Sons Inc. 2. Berry, M. and G. Linoff. Data mining techniques: for marketing, sales, and customer relationship management. 2004: Wiley New York. 3. Chakrabarti, S. Mining the Web: discovering knowledge from hypertext data. 2003: Morgan Kaufmann Publishers. 4. Duda, R., P. Hart, and D. Stork. Pattern classification. 2001: John Wiley & Sons Inc. 5. Dunham, M. Data mining: Introductory and advanced topics. 2002: Pearson Education. 6. Fayyad, U., G. Piatetsky-Shapiro, P. Smyth, and R. Uthurusamy. Advances in knowledge discovery and data mining. 1996: MIT Press. 7. Grossman, R., C. Kamath, P. Kegelmeyer, V. Kumar, and R. Namburu. Data mining for scientific and engineering applications. 2001: Springer. 8. Han, J. and M. Kamber. Data mining: concepts and techniques. 2006: Morgan Kaufmann Publishers. 9. Hand, D., H. Mannila, and P. Smyth. Principles of data mining. 2001: MIT Press. 10. Langley, P. Elements of machine learning. 1996: Morgan Kaufmann Publishers. 11. Larose, D.T. Discovering Knowledge in Data: an Introduction to Data Mining. 2004: John Wiley. 12. Linoff, G. and M. Berry. Mining the web: transforming customer data into customer value. 2002: John Wiley & Sons, Inc. 13. Mitchell, T. Machine Learning. 1997: McGraw Hill. 14. Nelson, T. A file structure for the complex, the changing and the indeterminate. In Proceedings of ACM National Conference, 1965. 15. Perner, P. Data mining on multimedia data. 2002: Springer. 16. Pyle, D. Business modeling and data mining. 2003: Morgan Kaufmann Publishers. 14 1 Introduction 17. Roiger, R. and M. Geatz. Data mining: a tutorial-based primer. 2003: Addison Wesley Boston. 18. Rud, O.P. Data Mining Cookbook. 2003: John Wiley & Sons. 19. Scime, A. Web Mining: applications and techniques. 2005: Idea Group Publishers. 20. Tan, P., M. Steinbach, and V. Kumar. Introduction to data mining. 2006: Pearson Addison Wesley Boston. 21. Tang, Z. and J. Maclennan. Data mining with SQL Server 2005. 2005: Wiley Publishing, Inc. 22. Thuraisingham, B.M. Web Data Mining and Applications in Business Intelligence and Counter-Terrorism. 2003: CRC Press. 23. Wang, J., M. Zaki, H. Toivonen, and D. Shasha. Data mining in bioinformatics. 2005: Springer Verlag. 24. Wasserman, S. and K. Faust. Social Network Analysis. 1994: Cambridge University Press. 25. Witten, I. and E. Frank. Data Mining: Practical machine learning tools and techniques. 2005: Morgan Kaufmann Publishers. 26. Zaiane, O., S. Simoff, and C. Djeraba. Mining multimedia and complex data. 2003: Springer. 27. Zhong, N., Y. Yao, and J. Liu. Web Intelligence. 2003: Springer. Part I Data Mining Foundations 2 Association Rules and Sequential Patterns Association rules are an important class of regularities in data. Mining of association rules is a fundamental data mining task. It is perhaps the most important model invented and extensively studied by the database and data mining community. Its objective is to find all co-occurrence relationships, called associations, among data items. Since it was first introduced in 1993 by Agrawal et al. [2], it has attracted a great deal of attention. Many efficient algorithms, extensions and applications have been reported. The classic application of association rule mining is the market basket data analysis, which aims to discover how items purchased by customers in a supermarket (or a store) are associated. An example association rule is Cheese  Beer [support = 10%, confidence = 80%]. The rule says that 10% customers buy Cheese and Beer together, and those who buy Cheese also buy Beer 80% of the time. Support and confi- dence are two measures of rule strength, which we will define later. This mining model is in fact very general and can be used in many ap- plications. For example, in the context of the Web and text documents, it can be used to find word co-occurrence relationships and Web usage pat- terns as we will see in later chapters. Association rule mining, however, does not consider the sequence in which the items are purchased. Sequential pattern mining takes care of that. An example of a sequential pattern is “5% of customers buy bed first, then mattress and then pillows”. The items are not purchased at the same time, but one after another. Such patterns are useful in Web usage mining for analyzing clickstreams in server logs. They are also useful for finding language or linguistic patterns from natural language texts. 2.1 Basic Concepts of Association Rules The problem of mining association rules can be stated as follows: Let I = {i1, i2, …, im} be a set of items. Let T = (t1, t2, …, tn) be a set of transac- tions (the database), where each transaction ti is a set of items such that ti  I. An association rule is an implication of the form, B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, 17 Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_2, © Springer-Verlag Berlin Heidelberg 2011 18 2 Association Rules and Sequential Patterns X  Y, where X  I, Y  I, and X  Y = . X (or Y) is a set of items, called an itemset. Example 1: We want to analyze how the items sold in a supermarket are related to one another. I is the set of all items sold in the supermarket. A transaction is simply a set of items purchased in a basket by a customer. For example, a transaction may be: {Beef, Chicken, Cheese}, which means that a customer purchased three items in a basket, Beef, Chicken, and Cheese. An association rule may be: Beef, Chicken  Cheese, where {Beef, Chicken} is X and {Cheese} is Y. For simplicity, brackets “{” and “}” are usually omitted in transactions and rules. ▀ A transaction ti  T is said to contain an itemset X if X is a subset of ti (we also say that the itemset X covers ti). The support count of X in T (denoted by X.count) is the number of transactions in T that contain X. The strength of a rule is measured by its support and confidence. Support: The support of a rule, X  Y, is the percentage of transactions in T that contains X  Y, and can be seen as an estimate of the probability, Pr(XY). The rule support thus determines how frequent the rule is ap- plicable in the transaction set T. Let n be the number of transactions in T. The support of the rule X  Y is computed as follows: .). ( n countYXsupport  (1) Support is a useful measure because if it is too low, the rule may just oc- cur due to chance. Furthermore, in a business environment, a rule cover- ing too few cases (or transactions) may not be useful because it does not make business sense to act on such a rule (not profitable). Confidence: The confidence of a rule, X  Y, is the percentage of transac- tions in T that contain X also contain Y. It can be seen as an estimate of the conditional probability, Pr(Y | X). It is computed as follows: .. ). ( countX countYXconfidence  (2) Confidence thus determines the predictability of the rule. If the confi- dence of a rule is too low, one cannot reliably infer or predict Y from X. A rule with low predictability is of limited use. 2.1 Basic Concepts of Association Rules 19 Objective: Given a transaction set T, the problem of mining association rules is to discover all association rules in T that have support and confi- dence greater than or equal to the user-specified minimum support (de- noted by minsup) and minimum confidence (denoted by minconf). The keyword here is “all”, i.e., association rule mining is complete. Previ- ous methods for rule mining typically generate only a subset of rules based on various heuristics (see Chap. 3). Example 2: Fig. 2.1 shows a set of seven transactions. Each transaction ti is a set of items purchased in a basket in a store by a customer. The set I is the set of all items sold in the store. t1: Beef, Chicken, Milk t2: Beef, Cheese t3: Cheese, Boots t4: Beef, Chicken, Cheese t5: Beef, Chicken, Clothes, Cheese, Milk t6: Chicken, Clothes, Milk t7: Chicken, Milk, Clothes Fig. 2.1. An example of a transaction set Given the user-specified minsup = 30% and minconf = 80%, the following association rule (sup is the support, and conf is the confidence) Chicken, Clothes  Milk [sup = 3/7, conf = 3/3] is valid as its support is 42.86% (> 30%) and its confidence is 100% (> 80%). The rule below is also valid, whose consequent has two items: Clothes  Milk, Chicken [sup = 3/7, conf = 3/3]. Clearly, more association rules can be discovered, as we will see later. ▀ We note that the data representation in the transaction form of Fig. 2.1 is a simplistic view of shopping baskets. For example, the quantity and price of each item are not considered in the model. We also note that a text document or even a sentence in a single docu- ment can be treated as a transaction without considering word sequence and the number of occurrences of each word. Hence, given a set of docu- ments or a set of sentences, we can find word co-occurrence relations. A large number of association rule mining algorithms have been re- ported in the literature, which have different mining efficiencies. Their re- sulting sets of rules are, however, all the same based on the definition of association rules. That is, given a transaction data set T, a minimum sup- port and a minimum confidence, the set of association rules existing in T is 20 2 Association Rules and Sequential Patterns uniquely determined. Any algorithm should find the same set of rules al- though their computational efficiencies and memory requirements may be different. The best known mining algorithm is the Apriori algorithm pro- posed in [3], which we study next. 2.2 Apriori Algorithm The Apriori algorithm works in two steps: 1. Generate all frequent itemsets: A frequent itemset is an itemset that has transaction support above minsup. 2. Generate all confident association rules from the frequent itemsets: A confident association rule is a rule with confidence above minconf. We call the number of items in an itemset its size, and an itemset of size k a k-itemset. Following Example 2 above, {Chicken, Clothes, Milk} is a fre- quent 3-itemset as its support is 3/7 (minsup = 30%). From the itemset, we can generate the following three association rules (minconf = 80%): Rule 1: Chicken, Clothes  Milk [sup = 3/7, conf = 3/3] Rule 2: Clothes, Milk  Chicken [sup = 3/7, conf = 3/3] Rule 3: Clothes  Milk, Chicken [sup = 3/7, conf = 3/3]. Below, we discuss the two steps in turn. 2.2.1 Frequent Itemset Generation The Apriori algorithm relies on the apriori or downward closure property to efficiently generate all frequent itemsets. Downward Closure Property: If an itemset has minimum support, then every non-empty subset of this itemset also has minimum support. The idea is simple because if a transaction contains a set of items X, then it must contain any non-empty subset of X. This property and the minsup threshold prune a large number of itemsets that cannot be frequent. To ensure efficient itemset generation, the algorithm assumes that the items in I are sorted in lexicographic order (a total order). The order is used throughout the algorithm in each itemset. We use the notation {w[1], w[2], …, w[k]} to represent a k-itemset w consisting of items w[1], w[2], …, w[k], where w[1] < w[2] < … < w[k] according to the total order. The Apriori algorithm for frequent itemset generation, which is given in Fig. 2.2, is based on level-wise search. It generates all frequent itemsets 2.2 Apriori Algorithm 21 by making multiple passes over the data. In the first pass, it counts the sup- ports of individual items (line 1) and determines whether each of them is frequent (line 2). F1 is the set of frequent 1-itemsets. In each subsequent pass k, there are three steps: 1. It starts with the seed set of itemsets Fk1 found to be frequent in the (k1)-th pass. It uses this seed set to generate candidate itemsets Ck (line 4), which are possible frequent itemsets. This is done using the candidate-gen() function. 2. The transaction database is then scanned and the actual support of each candidate itemset c in Ck is counted (lines 5–10). Note that we do not need to load the whole data into memory before processing. Instead, at Algorithm Apriori(T) 1 C1  init-pass(T); // the first pass over T 2 F1  {f | f  C1, f.count/n  minsup}; // n is the no. of transactions in T 3 for (k = 2; Fk1  ; k++) do // subsequent passes over T 4 Ck  candidate-gen(Fk1); 5 for each transaction t  T do // scan the data once 6 for each candidate c  Ck do 7 if c is contained in t then 8 c.count++; 9 endfor 10 endfor 11 Fk  {c  Ck | c.count/n  minsup} 12 endfor 13 return F  ∪k Fk; Fig. 2.2. The Apriori algorithm for generating frequent itemsets Function candidate-gen(Fk1) 1 Ck  ; // initialize the set of candidates 2 forall f1, f2  Fk1 // find all pairs of frequent itemsets 3 with f1 = {i1, … , ik2, ik1} // that differ only in the last item 4 and f2 = {i1, … , ik2, i’k1} 5 and ik1 < i’k1 do // according to the lexicographic order 6 c  {i1, …, ik1, i’k1}; // join the two itemsets f1 and f2 7 Ck  Ck  {c}; // add the new itemset c to the candidates 8 for each (k1)-subset s of c do 9 if (s  Fk1) then 10 delete c from Ck; // delete c from the candidates 11 endfor 12 endfor 13 return Ck; // return the generated candidates Fig. 2.3. The candidate-gen function 22 2 Association Rules and Sequential Patterns any time, only one transaction resides in memory. This is a very impor- tant feature of the algorithm. It makes the algorithm scalable to huge da- ta sets, which cannot be loaded into memory. 3. At the end of the pass or scan, it determines which of the candidate itemsets are actually frequent (line 11). The final output of the algorithm is the set F of all frequent itemsets (line 13). The candidate-gen() function is discussed below. Candidate-gen function: The candidate generation function is given in Fig. 2.3. It consists of two steps, the join step and the pruning step. Join step (lines 2–6 in Fig. 2.3): This step joins two frequent (k1)- itemsets to produce a possible candidate c (line 6). The two frequent itemsets f1 and f2 have exactly the same items except the last one (lines 3–5). c is added to the set of candidates Ck (line 7). Pruning step (lines 8–11 in Fig. 2.3): A candidate c from the join step may not be a final candidate. This step determines whether all the k1 sub- sets (there are k of them) of c are in Fk1. If anyone of them is not in Fk1, c cannot be frequent according to the downward closure property, and is thus deleted from Ck. The correctness of the candidate-gen() function is easy to show (see [3]). Here, we use an example to illustrate the working of the function. Example 3: Let the set of frequent itemsets at level 3 be F3 = {{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {1, 3, 5}, {2, 3, 4}}. For simplicity, we use numbers to represent items. The join step (which generates candidates for level 4) will produce two candidate itemsets, {1, 2, 3, 4} and {1, 3, 4, 5}. {1, 2, 3, 4} is generated by joining the first and the second itemsets in F3 as their first and second items are the same respec- tively. {1, 3, 4, 5} is generated by joining {1, 3, 4} and {1, 3, 5}. After the pruning step, we have only: C4 = {{1, 2, 3, 4}} because {1, 4, 5} is not in F3 and thus {1, 3, 4, 5} cannot be frequent. Example 4: Let us see a complete running example of the Apriori algo- rithm based on the transactions in Fig. 2.1. We use minsup = 30%. F1: {{Beef}:4, {Cheese}:4, {Chicken}:5, {Clothes}:3, {Milk}:4} Note: the number after each frequent itemset is the support count of the itemset, i.e., the number of transactions containing the itemset. A mini- mum support count of 3 is sufficient because the support of 3/7 is greater than 30%, where 7 is the total number of transactions. 2.2 Apriori Algorithm 23 C2: {{Beef, Cheese}, {Beef, Chicken}, {Beef, Clothes}, {Beef, Milk}, {Cheese, Chicken}, {Cheese, Clothes}, {Cheese, Milk}, {Chicken, Clothes}, {Chicken, Milk}, {Clothes, Milk}} F2: {{Beef, Chicken}:3, {Beef, Cheese}:3, {Chicken, Clothes}:3, {Chicken, Milk}:4, {Clothes, Milk}:3} C3: {{Chicken, Clothes, Milk}} Note: {Beef, Cheese, Chicken} is also produced in line 6 of Fig. 2.3. However, {Cheese, Chicken} is not in F2, and thus the itemset {Beef, Cheese, Chicken} is not included in C3. F3: {{Chicken, Clothes, Milk}:3}. ▀ Finally, some remarks about the Apriori algorithm are in order:  Theoretically, this is an exponential algorithm. Let the number of items in I be m. The space of all itemsets is O(2m) because each item may or may not be in an itemset. However, the mining algorithm exploits the sparseness of the data and the high minimum support value to make the mining possible and efficient. The sparseness of the data in the context of market basket analysis means that the store sells a lot of items, but each shopper only purchases a few of them.  The algorithm can scale up to large data sets as it does not load the en- tire data into the memory. It only scans the data K times, where K is the size of the largest itemset. In practice, K is often small (e.g., < 10). This scale-up property is very important in practice because many real-world data sets are so large that they cannot be loaded into the main memory.  The algorithm is based on level-wise search. It has the flexibility to stop at any level. This is useful in practice because in many applications, long frequent itemsets or rules are not needed as they are hard to use.  As mentioned earlier, once a transaction set T, a minsup and a minconf are given, the set of frequent itemsets that can be found in T is uniquely determined. Any algorithm should find the same set of frequent item- sets. This property about association rule mining does not hold for many other data mining tasks, e.g., classification or clustering, for which dif- ferent algorithms may produce very different results.  The main problem with association rule mining is that it often produces a huge number of itemsets (and rules), tens of thousands, or more, which makes it hard for the user to analyze them to find those useful ones. This is called the interestingness problem. Researchers have pro- posed several methods to tackle this problem (see Bibliographic Notes). An efficient implementation of the Apriori algorithm involves sophisti- cated data structures and programming techniques, which are beyond the 24 2 Association Rules and Sequential Patterns scope of this book. Apart from the Apriori algorithm, there is a large num- ber of other algorithms, e.g., FP-growth [17] and many others. 2.2.2 Association Rule Generation In many applications, frequent itemsets are already useful and sufficient. Then, we do not need to generate association rules. In applications where rules are desired, we use frequent itemsets to generate all association rules. Compared with frequent itemset generation, rule generation is relatively simple. To generate rules for every frequent itemset f, we use all non- empty subsets of f. For each such subset , we output a rule of the form (f  )  , if ,).( . minconfcountf countfconfidence   (3) where f.count (or (f).count) is the support count of f (or (f  )). The support of the rule is f.count/n, where n is the number of transactions in the transaction set T. All the support counts needed for confidence computa- tion are available because if f is frequent, then any of its non-empty subsets is also frequent and its support count has been recorded in the mining process. Thus, no data scan is needed in rule generation. This exhaustive rule generation strategy is, however, inefficient. To de- sign an efficient algorithm, we observe that the support count of f in the above confidence computation does not change as  changes. It follows that for a rule (f  )   to hold, all rules of the form (f  sub)  sub must also hold, where sub is a non-empty subset of , because the support count of (f  sub) must be less than or equal to the support count of (f  ). For example, given an itemset {A, B, C, D}, if the rule (A, B  C, D) holds, then the rules (A, B, C  D) and (A, B, D  C) must also hold. Thus, for a given frequent itemset f, if a rule with consequent  holds, then so do rules with consequents that are subsets of . This is similar to the downward closure property that, if an itemset is frequent, then so are all its subsets. Therefore, from the frequent itemset f, we first generate all rules with one item in the consequent. We then use the consequents of these rules and the function candidate-gen() (Fig. 2.3) to generate all pos- sible consequents with two items that can appear in a rule, and so on. An algorithm using this idea is given in Fig. 2.4. Note that all 1-item conse- quent rules (rules with one item in the consequent) are first generated in line 2 of the function genRules(). The confidence is computed using (3). 2.2 Apriori Algorithm 25 Example 5: We again use transactions in Fig. 2.1, minsup = 30% and minconf = 80%. The frequent itemsets are as follows (see Example 4): F1: {{Beef}:4, {Cheese}:4, {Chicken}:5, {Clothes}:3, {Milk}:4} F2: {{Beef, Cheese}:3, {Beef, Chicken}:3, {Chicken, Clothes}:3, {Chicken, Milk}:4, {Clothes, Milk}:3} F3: {{Chicken, Clothes, Milk}:3}. We use only the itemset in F3 to generate rules (generating rules from each itemset in F2 can be done in the same way). The itemset in F3 generates the following possible 1-item consequent rules: Rule 1: Chicken, Clothes  Milk [sup = 3/7, conf = 3/3] Rule 2: Chicken, Milk  Clothes [sup = 3/7, conf = 3/4] Rule 3: Clothes, Milk  Chicken [sup = 3/7, conf = 3/3]. Due to the minconf requirement, only Rule 1 and Rule 3 are output in line 2 of the algorithm genRules(). Thus, H1 = {{Chicken}, {Milk}}. The function ap-genRules() is then called. Line 2 of ap-genRules() produces H2 = {{Chicken, Milk}}. The following rule is then generated: Rule 4: Clothes  Milk, Chicken [sup = 3/7, conf = 3/3]. Algorithm genRules(F) // F is the set of all frequent itemsets 1 for each frequent k-itemset fk in F, k  2 do 2 output every 1-item consequent rule of fk with confidence  minconf and support  fk.count / n // n is the total number of transactions in T 3 H1 {consequents of all 1-item consequent rules derived from fk above}; 4 ap-genRules(fk, H1); 5 endfor Procedure ap-genRules(fk, Hm) // Hm is the set of m-item consequents 1 if (k > m + 1) AND (Hm  ) then 2 Hm+1  candidate-gen(Hm); 3 for each hm+1 in Hm+1 do 4 conf  fk.count / (fk  hm+1).count; 5 if (conf  minconf) then 6 output the rule (fk  hm+1)  hm+1 with confidence = conf and support = fk.count / n; // n is the total number of transactions in T 7 else 8 delete hm+1 from Hm+1; 9 endfor 10 ap-genRules(fk, Hm+1); 11 endif Fig. 2.4. The association rule generation algorithm 26 2 Association Rules and Sequential Patterns Thus, three association rules are generated from the frequent itemset {Chicken, Clothes, Milk} in F3, namely Rule 1, Rule 3 and Rule 4. ▀ 2.3 Data Formats for Association Rule Mining So far, we have used only transaction data for mining association rules. Market basket data sets are naturally of this format. Text documents can be seen as transaction data as well. Each document is a transaction, and each distinctive word is an item. Duplicate words are removed. However, mining can also be performed on relational tables. We just need to convert a table data set to a transaction data set, which is fairly straightforward if each attribute in the table takes categorical values. We simply change each value to an attribute–value pair. Example 6: The table data in Fig. 2.5(A) can be converted to the transac- tion data in Fig. 2.5(B). Each attribute–value pair is considered an item. Using only values is not sufficient in the transaction form because different attributes may have the same values. For example, without including at- tribute names, value a’s for Attribute1 and Attribute2 are not distinguish- able. After the conversion, Fig. 2.5(B) can be used in mining. ▀ If an attribute takes numerical values, it becomes complex. We need to first discretize its value range into intervals, and treat each interval as a ca- tegorical value. For example, an attribute’s value range is from 1–100. We may want to divide it into 5 equal-sized intervals, 1–20, 21–40, 41–60, 61– 80, and 81–100. Each interval is then treated as a categorical value. Discre- tization can be done manually based on expert knowledge or automati- cally. There are several existing algorithms [14, 40]. A point to note is that for a table data set, the join step of the candidate generation function (Fig. 2.3) needs to be slightly modified in order to en- sure that it does not join two itemsets to produce a candidate itemset con- taining two items from the same attribute. Clearly, we can also convert a transaction data set to a table data set us- ing a binary representation and treating each item in I as an attribute. If a transaction contains an item, its attribute value is 1, and 0 otherwise. 2.4 Mining with Multiple Minimum Supports The key element that makes association rule mining practical is the minsup threshold. It is used to prune the search space and to limit the number of frequent itemsets and rules generated. However, using only a single min- 2.4 Mining with Multiple Minimum Supports 27 sup implicitly assumes that all items in the data are of the same nature and/or have similar frequencies in the database. This is often not the case in real-life applications. In many applications, some items appear very fre- quently in the data, while some other items rarely appear. If the frequen- cies of items vary a great deal, we will encounter two problems [23]: 1. If the minsup is set too high, we will not find rules that involve infre- quent items or rare items in the data. 2. In order to find rules that involve both frequent and rare items, we have to set the minsup very low. However, this may cause combinatorial ex- plosion and make mining impossible because those frequent items will be associated with one another in all possible ways. Let us use an example to illustrate the above problem with a very low min- sup, which will actually introduce another problem. Example 7: In a supermarket transaction data set, in order to find rules in- volving those infrequently purchased items such as FoodProcessor and CookingPan (they generate more profits per item), we need to set the min- sup very low. Let us use only frequent itemsets in this example as they are generated first and rules are produced from them. They are also the source of all the problems. Now assume we set a very low minsup of 0.005%. We find the following meaningful frequent itemset: {FoodProcessor, CookingPan} [sup = 0.006%]. However, this low minsup may also cause the following two meaningless itemsets being discovered: f1: {Bread, Cheese, Egg, Bagel, Milk, Sugar, Butter} [sup = 0.007%], f2: {Bread, Egg, Milk, CookingPan} [sup = 0.006%]. Knowing that 0.007% of the customers buy the seven items in f1 together is useless because all these items are so frequently purchased in a supermar- Attribute1 Attribute2 Atribute3 a a x b n y (A) Table data t1: (Attribute1, a), (Attribute2, a), (Attribute3, x) t2: (Attribute1, b), (Attribute2, n), (Attribute3, y) (B) Transaction data Fig. 2.5. From a table data set to a transaction data set 28 2 Association Rules and Sequential Patterns ket. Worst still, they will almost certainly cause combinatorial explosion! For itemsets involving such items to be useful, their supports have to be much higher. Similarly, knowing that 0.006% of the customers buy the four items in f2 together is also meaningless because Bread, Egg and Milk are purchased on almost every grocery shopping trip. ▀ This dilemma is called the rare item problem. Using a single minsup for the whole data set is inadequate because it cannot capture the inherent natures and/or frequency differences of items in the database. By the na- tures of items we mean that some items, by nature, appear more frequently than others. For example, in a supermarket, people buy FoodProcessor and CookingPan much less frequently than Bread and Milk. The situation is the same for online stores. In general, those durable and/or expensive goods are bought less often, but each of them generates more profit. It is thus im- portant to capture rules involving less frequent items. However, we must do so without allowing frequent items to produce too many meaningless rules with very low supports and cause combinatorial explosion [23]. One common solution to this problem is to partition the data into several smaller blocks (subsets), each of which contains only items of similar fre- quencies. Mining is then done separately for each block using a different minsup. This approach is, however, not satisfactory because itemsets or rules that involve items across different blocks will not be found. A better solution is to allow the user to specify multiple minimum sup- ports, i.e., to specify a different minimum item support (MIS) to each item. Thus, different itemsets need to satisfy different minimum supports depending on what items are in the itemsets. This model thus enables us to achieve our objective of finding itemsets involving rare items without causing frequent items to generate too many meaningless itemsets. This method helps solve the problem of f1. To deal with the problem of f2, we prevent itemsets that contain both very frequent items and very rare items from being generated. A constraint will be introduced to realize this. An interesting by-product of this extended model is that it enables the user to easily instruct the algorithm to generate only itemsets that contain certain items but not itemsets that contain only the other items. This can be done by setting the MIS values to more than 100% (e.g., 101%) for these other items. This capability is very useful in practice because in many ap- plications the user is only interested in certain types of itemsets or rules. 2.4.1 Extended Model To allow multiple minimum supports, the original model in Sect. 2.1 needs to be extended. In the extended model, the minimum support of a rule is 2.4 Mining with Multiple Minimum Supports 29 expressed in terms of minimum item supports (MIS) of the items that appear in the rule. That is, each item in the data can have a MIS value spe- cified by the user. By providing different MIS values for different items, the user effectively expresses different support requirements for different rules. It seems that specifying a MIS value for each item is a difficult task. This is not so as we will see at the end of Sect. 2.4.2. Let MIS(i) be the MIS value of item i. The minimum support of a rule R is the lowest MIS value among the items in the rule. That is, a rule R, i1, i2, …, ik  ik+1, …, ir, satisfies its minimum support if the rule’s actual support in the data is greater than or equal to: min(MIS(i1), MIS(i2), …, MIS(ir)). Minimum item supports thus enable us to achieve the goal of having higher minimum supports for rules that involve only frequent items, and having lower minimum supports for rules that involve less frequent items. Example 8: Consider the set of items in a data set, {Bread, Shoes, Clothes}. The user-specified MIS values are as follows: MIS(Bread) = 2% MIS(Clothes) = 0.2% MIS(Shoes) = 0.1%. The following rule doesn’t satisfy its minimum support: Clothes  Bread [sup = 0.15%, conf = 70%]. This is so because min(MIS(Bread), MIS(Clothes)) = 0.2%. The following rule satisfies its minimum support: Clothes  Shoes [sup = 0.15%, conf = 70%]. because min(MIS(Clothes), MIS(Shoes)) = 0.1%. ▀ As we explained earlier, the downward closure property holds the key to pruning in the Apriori algorithm. However, in the new model, if we use the Apriori algorithm to find all frequent itemsets, the downward closure property no longer holds. Example 9: Consider the four items 1, 2, 3 and 4 in a data set. Their min- imum item supports are: MIS(1) = 10% MIS(2) = 20% MIS(3) = 5% MIS(4) = 6%. If we find that itemset {1, 2} has a support of 9% at level 2, then it does not satisfy either MIS(1) or MIS(2). Using the Apriori algorithm, this itemset is discarded since it is not frequent. Then, the potentially frequent itemsets {1, 2, 3} and {1, 2, 4} will not be generated for level 3. Clearly, itemsets {1, 30 2 Association Rules and Sequential Patterns 2, 3} and {1, 2, 4} may be frequent because MIS(3) is only 5% and MIS(4) is 6%. It is thus wrong to discard {1, 2}. However, if we do not discard {1, 2}, the downward closure property is lost. ▀ Below, we present an algorithm to solve this problem. The essential idea is to sort the items according to their MIS values in ascending order to avoid the problem. Note that MIS values prevent low support itemsets involving only fre- quent items from being generated because their individual MIS values are all high. To prevent very frequent items and very rare items from appear- ing in the same itemset, we introduce the support difference constraint. Let sup(i) be the actual support of item i in the data. For each itemset s, the support difference constraint is as follows: maxis{sup(i)}  minis{sup(i)} ≤ , where 0 ≤  ≤ 1 is the user-specified maximum support difference, and it is the same for all itemsets. The constraint basically limits the difference between the largest and the smallest actual supports of items in itemset s to . This constraint can reduce the number of itemsets generated dramati- cally, and it does not affect the downward closure property. 2.4.2 Mining Algorithm The new algorithm generalizes the Apriori algorithm for finding frequent itemsets. We call the algorithm, MS-Apriori. When there is only one MIS value (for all items), it reduces to the Apriori algorithm. Like Apriori, MS-Apriori is also based on level-wise search. It generates all frequent itemsets by making multiple passes over the data. However, there is an exception in the second pass as we will see later. The key operation in the new algorithm is the sorting of the items in I in ascending order of their MIS values. This order is fixed and used in all subsequent operations of the algorithm. The items in each itemset follow this order. For example, in Example 9 of the four items 1, 2, 3 and 4 and their given MIS values, the items are sorted as follows: 3, 4, 1, 2. This or- der helps solve the problem identified above. Let Fk denote the set of frequent k-itemsets. Each itemset w is of the fol- lowing form, {w[1], w[2], …, w[k]}, which consists of items, w[1], w[2], …, w[k], where MIS(w[1])  MIS(w[2])  …  MIS(w[k]). The algorithm MS-Apriori is given in Fig. 2.6. Line 1 performs the sorting on I according to the MIS value of each item (stored in MS). Line 2 makes the first pass over the data using the function init-pass(), which takes two arguments, the 2.4 Mining with Multiple Minimum Supports 31 data set T and the sorted items M, to produce the seeds L for generating candidate itemsets of length 2, i.e., C2. init-pass() has two steps: 1. It first scans the data once to record the support count of each item. 2. It then follows the sorted order to find the first item i in M that meets MIS(i). i is inserted into L. For each subsequent item j in M after i, if j.count/n  MIS(i), then j is also inserted into L, where j.count is the support count of j, and n is the total number of transactions in T. Frequent 1-itemsets (F1) are obtained from L (line 3). It is easy to show that all frequent 1-itemsets are in F1. Example 10: Let us follow Example 9 and the given MIS values for the four items. Assume our data set has 100 transactions (not limited to the four items). The first pass over the data gives us the following support counts: {3}.count = 6, {4}.count = 3, {1}.count = 9 and {2}.count = 25. Then, L = {3, 1, 2}, and F1 = {{3}, {2}}. Item 4 is not in L because 4.count/n < MIS(3) (= 5%), and {1} is not in F1 because 1.count / n < MIS(1) (= 10%). ▀ For each subsequent pass (or data scan), say pass k, the algorithm per- forms three operations. Algorithm MS-Apriori(T, MS, )// MS stores all MIS values 1 M  sort(I, MS); // according to MIS(i)’s stored in MS 2 L  init-pass(M, T); // make the first pass over T 3 F1  {{l} | l  L, l.count/n  MIS(l)}; // n is the size of T 4 for (k = 2; Fk1  ; k++) do 5 if k = 2 then 6 Ck  level2-candidate-gen(L, ) // k = 2 7 else Ck  MScandidate-gen(Fk1, ) 8 endif; 9 for each transaction t  T do 10 for each candidate c  Ck do 11 if c is contained in t then // c is a subset of t 12 c.count++ 13 if c – {c[1]} is contained in t then // c without the first item 14 (c – {c[1]}).count++ 15 endfor 16 endfor 17 Fk  {c  Ck | c.count/n  MIS(c[1])} 18 endfor 19 return F  ∪k Fk; Fig. 2.6. The MS-Apriori algorithm 32 2 Association Rules and Sequential Patterns 1. The frequent itemsets in Fk1 found in the (k–1)th pass are used to gen- erate the candidates Ck using the MScandidate-gen() function (line 7). However, there is a special case, i.e., when k = 2 (line 6), for which the candidate generation function is different, i.e., level2-candidate-gen(). 2. It then scans the data and updates various support counts of the candi- dates in Ck (line 9–16). For each candidate c, we need to update its sup- port count (lines 11–12) and also the support count of c without the first item (lines 13–14), i.e., c – {c[1]}, which is used in rule generation and will be discussed in Sect. 2.4.3. If rule generation is not required, lines 13 and 14 can be deleted. 3. The frequent itemsets (Fk) for the pass are identified in line 17. We present candidate generation functions level2-candidate-gen() and MScandidate-gen() below. Level2-candidate-gen function: It takes an argument L, and returns a su- perset of the set of all frequent 2-itemsets. The algorithm is given in Fig. 2.7. Note that in line 5, we use |sup(h)  sup(l)| ≤  because sup(l) may not be lower than sup(h), although MIS(l) ≤ MIS(h). Example 11: Let us continue with Example 10. We set  = 10%. Recall the MIS values of the four items are (in Example 9): MIS(1) = 10% MIS(2) = 20% MIS(3) = 5% MIS(4) = 6%. The level2-candidate-gen() function in Fig. 2.7 produces C2 = {{3, 1}}. {1, 2} is not a candidate because the support count of item 1 is only 9 (or 9%), less than MIS(1) (= 10%). Hence, {1, 2} cannot be frequent. {3, 2} is not a candidate because sup(3) = 6% and sup(2) = 25% and their difference is greater than  = 10% ▀ Note that we must use L rather than F1 because F1 does not contain those items that may satisfy the MIS of an earlier item (in the sorted order) but not the MIS of itself, e.g., item 1 in the above example. Using L, the prob- lem discussed in Sect. 2.4.1 is solved for C2. MScandidate-gen function: The algorithm is given in Fig. 2.8, which is similar to the candidate-gen function in the Apriori algorithm. It also has two steps, the join step and the pruning step. The join step (lines 2–6) is the same as that in the candidate-gen() function. The pruning step (lines 8– 12) is, however, different. For each (k-1)-subset s of c, if s is not in Fk1, c can be deleted from Ck. However, there is an exception, which is when s does not include c[1] 2.4 Mining with Multiple Minimum Supports 33 (there is only one such s). That is, the first item of c, which has the lowest MIS value, is not in s. Even if s is not in Fk1, we cannot delete c because we cannot be sure that s does not satisfy MIS(c[1]), although we know that it does not satisfy MIS(c[2]), unless MIS(c[2]) = MIS(c[1]) (line 9). Example 12: Let F3 = {{1, 2, 3}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, {1, 4, 6}, {2, 3, 5}}. Items in each itemset are in the sorted order. The join step produces (we ignore the support difference constraint here) {1, 2, 3, 5}, {1, 3, 4, 5} and {1, 4, 5, 6}. The pruning step deletes {1, 4, 5, 6} because {1, 5, 6} is not in F3. We are then left with C4 = {{1, 2, 3, 5}, {1, 3, 4, 5}}. {1, 3, 4, 5} is not deleted al- though {3, 4, 5} is not in F3 because the minimum support of {3, 4, 5} is MIS(3), which may be higher than MIS(1). Although {3, 4, 5} does not sat- isfy MIS(3), we cannot be sure that it does not satisfy MIS(1). However, if MIS(3) = MIS(1), then {1, 3, 4, 5} can also be deleted. ▀ Function level2-candidate-gen(L, ) 1 C2  ; // initialize the set of candidates 2 for each item l in L in the same order do 3 if l.count/n  MIS(l) then 4 for each item h in L that is after l do 5 if h.count/n  MIS(l) and |sup(h)  sup(l)| ≤  then 6 C2  C2  {{l, h}}; // insert the candidate {l, h} into C2 Fig. 2.7. The level2-candidate-gen function Function MScandidate-gen(Fk1, ) 1 Ck  ; // initialize the set of candidates 2 forall f1, f2  Fk // find all pairs of frequent itemsets 3 with f1 = {i1, … , ik2, ik1} // that differ only in the last item 4 and f2 = {i1, … , ik2, i’k1} 5 and ik-1 < i’k1 and |sup(ik-1)  sup(i’k1)| ≤  do 6 c  {i1, …, ik1, i’k1}; // join the two itemsets f1 and f2 7 Ck  Ck  {c}; // insert the candidate itemset c into Ck 8 for each (k1)-subset s of c do 9 if (c[1]  s) or (MIS(c[2]) = MIS(c[1])) then 10 if (s  Fk1) then 11 delete c from Ck; // delete c from the set of candidates 12 endfor 13 endfor 14 return Ck; // return the generated candidates Fig. 2.8. The MScandidate-gen function 34 2 Association Rules and Sequential Patterns The problem discussed in Sect. 2.4.1 is solved for Ck (k > 2) because, due to the sorting, we do not need to extend a frequent (k1)-itemset with any item that has a lower MIS value. Let us see a complete example. Example 13: Given the following seven transactions, Beef, Bread Bread, Clothes Bread, Clothes, Milk Cheese, Boots Beef, Bread, Cheese, Shoes Beef, Bread, Cheese, Milk Bread, Milk, Clothes and MIS(Milk) = 50%, MIS(Bread) = 70%, and 25% for all other items. Again, the support difference constraint is not used. The following fre- quent itemsets are produced: F1 = {{Beef}, {Cheese}, {Clothes}, {Bread}} F2 = {{Beef, Cheese}, {Beef, Bread}, {Cheese, Bread} {Clothes, Bread}, {Clothes, Milk}} F3 = {{Beef, Cheese, Bread}, {Clothes, Milk, Bread}}. ▀ To conclude this sub-section, let us further discuss two important issues: 1. Specify MIS values for items: This is usually done in two ways:  Assign a MIS value to each item according to its actual sup- port/frequency in the data set T. For example, if the actual support of item i in T is sup(i), then the MIS value for i may be computed with sup(i), where  is a parameter (0    1) and is the same for all items in T.  Group items into clusters (or blocks). Items in each cluster have simi- lar frequencies. All items in the same cluster are given the same MIS value. We should note that in the extended model frequent itemsets involving items from different clusters will be found. 2. Generate itemsets that must contain certain items: As mentioned earlier, the extended model enables the user to instruct the algorithm to generate itemsets that must contain certain items, or not to generate any itemsets consisting of only the other items. Let us see an example. Example 14: Given the data set in Example 13, if we want to generate frequent itemsets that must contain at least one item in {Boots, Bread, Cheese, Milk, Shoes}, or not to generate itemsets involving only Beef and/or Clothes, we can simply set MIS(Beef) = 101%, and MIS(Clothes) = 101% 2.4 Mining with Multiple Minimum Supports 35 Then the algorithm will not generate the itemsets, {Beef}, {Clothes} and {Beef, Clothes}. However, it will still generate such frequent item- sets as {Cheese, Beef} and {Cheese, Bread, Beef}. ▀ In many applications, this feature comes quite handy because the user is often only interested in certain types of itemsets or rules. 2.4.3 Rule Generation Association rules are generated using frequent itemsets. In the case of a single minsup, if f is a frequent itemset and fsub is a subset of f, then fsub must also be a frequent itemset. All their support counts are computed and recorded by the Apriori algorithm. Then, the confidence of each possible rule can be easily calculated without seeing the data again. However, in the case of MS-Apriori, if we only record the support count of each frequent itemset, it is not sufficient. Let us see why. Example 15: Recall in Example 8, we have MIS(Bread) = 2% MIS(Clothes) = 0.2% MIS(Shoes) = 0.1%. If the actual support for the itemset {Clothes, Bread} is 0.15%, and for the itemset {Shoes, Clothes, Bread} is 0.12%, according to MS-Apriori, {Clothes, Bread} is not a frequent itemset since its support is less than MIS(Clothes). However, {Shoes, Clothes, Bread} is a frequent itemset as its actual support is greater than min(MIS(Shoes), MIS(Clothes), MIS(Bread)) = MIS(Shoes)). We now have a problem in computing the confidence of the rule, Clothes, Bread  Shoes because the itemset {Clothes, Bread} is not a frequent itemset and thus its support count is not recorded. In fact, we may not be able to compute the confidences of the following rules either: Clothes  Shoes, Bread Bread  Shoes, Clothes because {Clothes} and {Bread} may not be frequent. ▀ Lemma: The above problem may occur only when the item that has the lowest MIS value in the itemset is in the consequent of the rule (which may have multiple items). We call this problem the head-item problem. Proof by contradiction: Let f be a frequent itemset, and a  f be the item with the lowest MIS value in f (a is called the head item). Thus, f uses 36 2 Association Rules and Sequential Patterns MIS(a) as its minsup. We want to form a rule, X  Y, where X, Y  f, X  Y = f and X  Y = . Our examples above already show that the head-item problem may occur when a  Y. Now assume that the problem can also occur when a  X. Since a  X and X  f, a must have the lowest MIS value in X and X must be a frequent itemset, which is ensured by the MS- Apriori algorithm. Hence, the support count of X is recorded. Since f is a frequent itemset and its support count is also recorded, then we can com- pute the confidence of X  Y. This contradicts our assumption. ▀ The lemma indicates that we need to record the support count of f – {a}. This is achieved by lines 13–14 in MS-Apriori (Fig. 2.6). All problems in Example 15 are solved. A similar rule generation function as genRules() in Apriori can be designed to generate rules with multiple minimum supports. 2.5 Mining Class Association Rules The mining models studied so far do not use any targets. That is, any item can appear as a consequent or condition of a rule. However, in some appli- cations, the user is interested in only rules with some fixed target items on the right-hand side [22]. For example, the user has a collection of text doc- uments from some topics (target items), and he/she wants to know what words are correlated with each topic. In [25], a data mining system based entirely on such rules, called class association rules, is reported, which has been in use in Motorola for many different applications since 2006. In the Web environment, class association rules are also useful because many types of Web data are in the form of transactions, e.g., search queries is- sued by users and pages clicked by visitors. Such applications often have target items, e.g., advertisements. Web sites want to know how user activi- ties are related to advertisements that the users may view or click (see Chap. 12). This touches the issue of classification or prediction, which we will study in the next chapter where we will see that such rules can be used either directly for classification or indirectly as features for classification. 2.5.1 Problem Definition Let T be a transaction data set consisting of n transactions. Each transac- tion is labeled with a class y. Let I be the set of all items in T, Y be the set of all class labels (or target items) and I  Y = . A class association rule (CAR) is an implication of the form X  y, where X  I, and y  Y. 2.5 Mining Class Association Rules 37 The definitions of support and confidence are the same as those for nor- mal association rules. In general, a class association rule is different from a normal association rule in two ways: 1. The consequent of a CAR has only a single item, while the consequent of a normal association rule can have any number of items. 2. The consequent y of a CAR can only be from the class label set Y, i.e., y  Y. No item from I can appear as the consequent, and no class label can appear as a rule condition. In contrast, a normal association rule can have any item as a condition or a consequent. Objective: The problem of mining CARs is to generate the complete set of CARs that satisfies the user-specified minimum support (minsup) and min- imum confidence (minconf) constraints. Example 16: Fig. 2.9 shows a data set which has seven text documents. Each document is a transaction and consists of a set of keywords. Each transaction is also labeled with a topic class (education or sport). I = {Student, Teach, School, City, Game, Baseball, Basketball, Team, Coach, Player, Spectator} Y = {Education, Sport}. Transactions Class doc 1: Student, Teach, School : Education doc 2: Student, School : Education doc 3: Teach, School, City, Game : Education doc 4: Baseball, Basketball : Sport doc 5: Basketball, Player, Spectator : Sport doc 6: Baseball, Coach, Game, Team : Sport doc 7: Basketball, Team, City, Game : Sport Fig. 2.9. An example of a data set for mining class association rules Let minsup = 20% and minconf = 60%. The following are two examples of class association rules: Student, School  Education [sup= 2/7, conf = 2/2] Game  Sport [sup= 2/7, conf = 2/3]. ▀ A question that one may ask is: can we mine the data by simply using the Apriori algorithm and then perform a post-processing of the resulting rules to select only those class association rules? In principle, the answer is yes because CARs are a special type of association rules. However, in practice this is often difficult or even impossible because of combinatorial explo- sion, i.e., the number of rules generated in this way can be huge. 38 2 Association Rules and Sequential Patterns 2.5.2 Mining Algorithm Unlike normal association rules, CARs can be mined directly in a single step. The key operation is to find all ruleitems that have support above minsup. A ruleitem is of the form: (condset, y), where condset  I is a set of items, and y  Y is a class label. The support count of a condset (called condsupCount) is the number of transactions in T that contain the condset. The support count of a ruleitem (called rule- supCount) is the number of transactions in T that contain the condset and are labeled with class y. Each ruleitem basically represents a rule: condset  y, whose support is (rulesupCount / n), where n is the total number of trans- actions in T, and whose confidence is (rulesupCount / condsupCount). Ruleitems that satisfy the minsup are called frequent ruleitems, while the rest are called infrequent ruleitems. For example, ({Student, School}, Education) is a ruleitem in T of Fig. 2.9. The support count of the condset {Student, School} is 2, and the support count of the ruleitem is also 2. Then the support of the ruleitem is 2/7 (= 28.6%), and the confidence of the rule- item is 100%. If minsup = 10%, then the ruleitem satisfies the minsup threshold. We say that it is frequent. If minconf = 80%, then the ruleitem satisfies the minconf threshold. We say that the ruleitem is confident. We thus have the class association rule: Student, School  Education [sup= 2/7, conf = 2/2]. The rule generation algorithm, called CAR-Apriori, is given in Fig. 2.10, which is based on the Apriori algorithm. Like the Apriori algorithm, CAR-Apriori generates all the frequent ruleitems by making multiple passes over the data. In the first pass, it computes the support count of each 1-ruleitem (containing only one item in its condset) (line 1). The set of all 1-candidate ruleitems considered is: C1 = {({i}, y) | i  I, and y  Y}, which basically associates each item in I (or in the transaction data set T) with every class label. Line 2 determines whether the candidate 1- ruleitems are frequent. From frequent 1-ruleitems, we generate 1-condition CARs (rules with only one condition) (line 3). In a subsequent pass, say k, it starts with the seed set of (k1)-ruleitems found to be frequent in the (k1)-th pass, and uses this seed set to generate new possibly frequent k- ruleitems, called candidate k-ruleitems (Ck in line 5). The actual support 2.5 Mining Class Association Rules 39 counts, both condsupCount and rulesupCount, are updated during the scan of the data (lines 6–13) for each candidate k-ruleitem. At the end of the da- ta scan, it determines which of the candidate k-ruleitems in Ck are actually frequent (line 14). From the frequent k-ruleitems, line 15 generates k- condition CARs (class association rules with k conditions). One interesting note about ruleitem generation is that if a ruleitem/rule has a confidence of 100%, then extending the ruleitem with more condi- tions (adding items to its condset) will also result in rules with 100% con- fidence although their supports may drop with additional items. In some applications, we may consider these subsequent rules redundant because additional conditions do not provide any more information. Then, we should not extend such ruleitems in candidate generation for the next level, which can reduce the number of generated rules substantially. If desired, redundancy handling can be added in the CAR-Apriori algorithm easily. The CARcandidate-gen() function is very similar to the candidate-gen() function in the Apriori algorithm, and it is thus omitted. The only differ- ence is that in CARcandidate-gen() ruleitems with the same class are joined by joining their condsets. Example 17: Let us work on a complete example using our data in Fig. 2.9. We set minsup = 20%, and minconf = 60%. F1: { ({School}, Education):(3, 3), ({Student}, Education):(2, 2), ({Teach}, Education):(2, 2), ({Baseball}, Sport):(2, 2), Algorithm CAR-Apriori(T) 1 C1  init-pass(T); // the first pass over T 2 F1  {f | f  C1, f. rulesupCount / n  minsup}; 3 CAR1  {f | f  F1, f.rulesupCount / f.condsupCount  minconf}; 4 for (k = 2; Fk1  ; k++) do 5 Ck  CARcandidate-gen(Fk1); 6 for each transaction t  T do 7 for each candidate c  Ck do 8 if c.condset is contained in t then // c is a subset of t 9 c.condsupCount++; 10 if t.class = c.class then 11 c.rulesupCount++ 12 endfor 13 end-for 14 Fk  {c  Ck | c.rulesupCount / n  minsup}; 15 CARk  {f | f  Fk, f.rulesupCount / f.condsupCount  minconf}; 16 endfor 17 return CAR  ∪k CARk; Fig. 2.10. The CAR-Apriori algorithm 40 2 Association Rules and Sequential Patterns ({Basketball}, Sport):(3, 3), ({Game}, Sport):(3, 2), ({Team}, Sport):(2, 2)} Note: The two numbers within the parentheses after each ruleitem are its condSupCount and ruleSupCount respectively. CAR1: School  Education [sup = 3/7, conf = 3/3] Student  Education [sup = 2/7, conf = 2/2] Teach  Education [sup = 2/7, conf = 2/2] Baseball  Sport [sup = 2/7, conf = 2/2] Basketball  Sport [sup = 3/7, conf = 3/3] Game  Sport [sup = 2/7, conf = 2/3] Team  Sport [sup = 2/7, conf = 2/2] Note: We do not deal with rule redundancy in this example. C2: { ({School, Student}, Education), ({School, Teach}, Education), ({Student, Teach}, Education), ({Baseball, Basketball}, Sport), ({Baseball, Game}, Sport), ({Baseball, Team}, Sport), ({Basketball, Game}, Sport), ({Basketball, Team}, Sport), ({Game, Team}, Sport)} F2: { ({School, Student}, Education):(2, 2), ({School, Teach}, Education):(2, 2), ({Game, Team}, Sport):(2, 2)} CAR2: School, Student  Education [sup = 2/7, conf = 2/2] School, Teach  Education [sup = 2/7, conf = 2/2] Game, Team  Sport [sup = 2/7, conf = 2/2] ▀ We note that for many applications involving target items, the data sets used are relational tables. They need to be converted to transaction forms before mining. We can use the method in Sect. 2.3 for the purpose. Example 18: In Fig. 2.11(A), the data set has three data attributes and a class attribute with two possible values, positive and negative. It is con- verted to the transaction data in Fig. 2.11(B). Notice that for each class, we only use its original value. There is no need to attach the attribute “Class” Attribute1 Attribute2 Atribute3 Class a a x positive b n y negative (A) Table data t1: (Attribute1, a), (Attribute2, a), (Attribute3, x) : Positive t2: (Attribute1, b), (Attribute2, n), (Attribute3, y) : negative (B) Transaction data Fig. 2.11. Converting a table data set (A) to a transaction data set (B) 2.6 Basic Concepts of Sequential Patterns 41 because there is no ambiguity. As discussed in Sect. 2.3, for each numeric attribute, its value range needs to be discretized into intervals either manu- ally or automatically before conversion and rule mining. There are many discretization algorithms. Interested readers are referred to [14]. ▀ 2.5.3 Mining with Multiple Minimum Supports The concept of mining with multiple minimum supports discussed in Sect. 2.4 can be incorporated in class association rule mining in two ways: 1. Multiple minimum class supports: The user can specify different min- imum supports for different classes. For example, the user has a data set with two classes, Yes and No. Based on the application requirement, he/she may want all rules of class Yes to have the minimum support of 5% and all rules of class No to have the minimum support of 20%. 2. Multiple minimum item supports: The user can specify a minimum item support for every item (either a class item/label or a non-class item). This is more general and is similar to normal association rule mining discussed in Sect. 2.4. For both approaches, similar mining algorithms to that given in Sect. 2.4 can be devised. The support difference constraint in Sect. 2.4.1 can be in- corporated as well. Like normal association rule mining with multiple min- imum supports, by setting minimum class and/or item supports to more than 100% for some items, the user effectively instructs the algorithm not to generate rules involving only these items. Finally, although we have discussed only multiple minimum supports so far, we can easily use different minimum confidences for different classes as well, which provides an additional flexibility in applications. 2.6 Basic Concepts of Sequential Patterns Association rule mining does not consider the order of transactions. How- ever, in many applications such orderings are significant. For example, in market basket analysis, it is interesting to know whether people buy some items in sequence, e.g., buying bed first and then buying bed sheets some time later. In Web usage mining, it is useful to find navigational patterns in a Web site from sequences of page visits of users (see Chap. 12). In text mining, considering the ordering of words in a sentence is vital for finding linguistic or language patterns (see Chap. 11). For these applications, asso- ciation rules will not be appropriate. Sequential patterns are needed. Be- 42 2 Association Rules and Sequential Patterns low, we define the problem of mining sequential patterns and introduce the main concepts involved. Let I = {i1, i2, …, im} be a set of items. A sequence is an ordered list of itemsets. Recall an itemset X is a non-empty set of items X  I. We denote a sequence s by a1a2…ar, where ai is an itemset, which is also called an element of s. We denote an element (or an itemset) of a sequence by {x1, x2, …, xk}, where xj  I is an item. We assume without loss of generality that items in an element of a sequence are in lexicographic order. An item can occur only once in an element of a sequence, but can occur multiple times in different elements. The size of a sequence is the number of ele- ments (or itemsets) in the sequence. The length of a sequence is the num- ber of items in the sequence. A sequence of length k is called a k-sequence. If an item occurs multiple times in different elements of a sequence, each occurrence contributes to the value of k. A sequence s1 = a1a2…ar is a subsequence of another sequence s2 = b1b2…bv, or s2 is a supersequence of s1, if there exist integers 1 ≤ j1 < j2 < … < jr1 < jr  v such that a1  bj1, a2  bj2, …, ar  bjr. We also say that s2 contains s1. Example 19: Let I = {1, 2, 3, 4, 5, 6, 7, 8, 9}. The sequence {3}{4, 5}{8} is contained in (or is a subsequence of) {6} {3, 7}{9}{4, 5, 8}{3, 8} because {3}  {3, 7}, {4, 5}  {4, 5, 8}, and {8}  {3, 8}. However, {3}{8} is not con- tained in {3, 8} or vice versa. The size of the sequence {3}{4, 5}{8} is 3, and the length of the sequence is 4. ▀ Objective: Given a set S of input data sequences (or sequence database), the problem of mining sequential patterns is to find all sequences that have a user-specified minimum support. Each such sequence is called a frequent sequence, or a sequential pattern. The support for a se- quence is the fraction of total data sequences in S that contains this se- quence. Example 20: We use the market basket analysis as an example. Each se- quence in this context represents an ordered list of transactions of a par- ticular customer. A transaction is a set of items that the customer pur- chased at a time (called the transaction time). Then transactions in the sequence are ordered by increasing transaction time. Table 2.1 shows a transaction database which is already sorted according to customer ID (the major key) and transaction time (the minor key). Table 2.2 gives the data sequences (also called customer sequences). Table 2.3 gives the output sequential patterns with the minimum support of 25%, i.e., two customers. ▀ 2.7 Mining Sequential Patterns Based on GSP 43 Table 2.1. A set of transactions sorted by customer ID and transaction time Customer ID Transaction Time Transaction (items bought) 1 July 20, 2005 30 1 July 25, 2005 90 2 July 9, 2005 10, 20 2 July 14, 2005 30 2 July 20, 2005 10, 40, 60, 70 3 July 25, 2005 30, 50, 70, 80 4 July 25, 2005 30 4 July 29, 2005 30, 40, 70, 80 4 August 2, 2005 90 5 July 12, 2005 90 Table 2.2. The sequence database produced from the transactions in Table 2.1. Customer ID Data Sequence 1 {30} {90} 2 {10, 20} {30} {10, 40, 60, 70} 3 {30, 50, 70, 80} 4 {30} {30, 40, 70, 80} {90} 5 {90} Table 2.3. The final output sequential patterns Sequential Patterns with Support  25% 1-sequences {30}, {40}, {70}, {80}, {90} 2-sequences {30} {40}, {30} {70}, {30}, {90}, {30, 70}, {30, 80}, {40, 70}, {70, 80} 3-sequences {30} {40, 70}, {30, 70, 80} 2.7 Mining Sequential Patterns Based on GSP This section describes two algorithms for mining sequential patterns based on the GSP algorithm in [41]: the original GSP, which uses a single mini- mum support, and MS-GSP, which uses multiple minimum supports. 2.7.1 GSP Algorithm GSP works in almost the same way as the Apriori algorithm. We still use Fk to store the set of all frequent k-sequences, and Ck to store the set of all 44 2 Association Rules and Sequential Patterns candidate k-sequences. The algorithm is given in Fig. 2.12. The main dif- ference is in the candidate generation, candidate-gen-SPM(), which is giv- en in Fig. 2.13. We use an example to illustrate the function. Example 21: Table 2.4 shows F3, and C4 after the join and prune steps. In the join step, the sequence {1, 2}{4} joins with {2}{4, 5} to produce {1, 2}{4, 5}, and joins with {2}{4}{6} to produce {1, 2}{4} {6}. The other se- quences cannot be joined. For instance, {1}{4, 5} does not join with any sequence since there is no sequence of the form {4, 5}{x} or {4, 5, x}. In the prune step, {1, 2}{4} {6} is removed since {1}{4} {6} is not in F3. ▀ Algorithm GSP(S) 1 C1  init-pass(S); // the first pass over S 2 F1  {{f}| f  C1, f.count/n  minsup}; // n is the number of sequences in S 3 for (k = 2; Fk1  ; k++) do // subsequent passes over S 4 Ck  candidate-gen-SPM(Fk1); 5 for each data sequence s  S do // scan the data once 6 for each candidate c  Ck do 7 if c is contained in s then 8 c.count++; // increment the support count 9 endfor 10 endfor 11 Fk  {c  Ck | c.count/n  minsup} 12 endfor 13 return F  ∪k Fk; Fig. 2.12. The GSP Algorithm for generating sequential patterns Function candidate-gen-SPM(Fk1) // SPM: Sequential Pattern Mining 1. Join step. Candidate sequences are generated by joining Fk1 with Fk1. A se- quence s1 joins with s2 if the subsequence obtained by dropping the first item of s1 is the same as the subsequence obtained by dropping the last item of s2. The candidate sequence generated by joining s1 with s2 is the sequence s1 ex- tended with the last item in s2. There are two cases:  the added item forms a separate element if it was a separate element in s2, and is appended at the end of s1 in the merged sequence, and  the added item is part of the last element of s1 in the merged sequence oth- erwise. When joining F1 with F1, we need to add the item in s2 both as part of an itemset and as a separate element. That is, joining {x} with {y} gives us both {x, y} and {x}{y}. Note that x and y in {x, y} are ordered. 2. Prune step. A candidate sequence is pruned if any one of its (k1)- subsequences is infrequent (without minimum support). Fig. 2.13. The candidate-gen-SPM function 2.7 Mining Sequential Patterns Based on GSP 45 Table 2.4. Candidate generation: an example Candidate 4-sequences Frequent 3-sequences after joining after pruning {1, 2} {4} {1, 2} {4, 5} {1, 2} {4, 5} {1, 2} {5} {1, 2} {4} {6} {1} {4, 5} {1, 4} {6} {2} {4, 5} {2} {4} {6} 2.7.2 Mining with Multiple Minimum Supports As in association rule mining, using a single minimum support in sequen- tial pattern mining is also a limitation for many applications because some items appear very frequently in the data, while some others appear rarely. Example 22: One of the Web mining tasks is to mine comparative sen- tences such as “the picture quality of camera X is better than that of cam- era Y.” from product reviews, forum postings and blogs (see Chap. 11). Such a sentence usually contains a comparative indicator word, e.g., better in the above sentence. We want to discover linguistic patterns involving a set of given comparative indicators, e.g., better, more, less, ahead, win, superior, etc. Some of these indicators (e.g., more and better) appear very frequently in natural language sentences, while some others (e.g., win and ahead) appear rarely. In order to find patterns that contain such rare indi- cators, we have to use a very low minsup. However, this causes patterns involving frequent indicators to generate a huge number of spurious pat- terns. Moreover, we need a way to tell the algorithm that we want only patterns that contain at least one comparative indicator. Using GSP with a single minsup is no longer appropriate. The multiple minimum supports model solves both problems nicely. ▀ We again use the concept of minimum item supports (MIS). The user is allowed to assign each item a MIS value. By providing different MIS values for different items, the user essentially expresses different support requirements for different sequential patterns. To ease the task of specify- ing many MIS values by the user, the same strategies as those for mining association rules can also be applied here (see Sect. 2.4.2). Let MIS(i) be the MIS value of item i. The minimum support of a se- quential pattern P is the lowest MIS value among the items in the pattern. Let the set of items in P be: i1, i2, …, ir. The minimum support for P is: 46 2 Association Rules and Sequential Patterns minsup(P) = min(MIS(i1), MIS(i2), …, MIS(ir)). The new algorithm, called MS-GSP, is given in Fig. 2.14. It generalizes the GSP algorithm in Fig. 2.12. Like GSP, MS-GSP is also based on level- wise search. Line 1 sorts the items in ascending order according to their MIS values stored in MS. Line 2 makes the first pass over the sequence da- ta using the function init-pass(), which performs the same function as that in MS-Apriori to produce the seeds set L for generating the set of candi- date sequences of length 2, i.e., C2. Frequent 1-sequences (F1) are obtained from L (line 3). For each subsequent pass, the algorithm works similarly to MS-Apriori. The function level2-candidate-gen-SPM() can be designed based on lev- el2-candidate-gen in MS-Apriori and the join step in Fig. 2.13. MScandi- date-gen-SPM() is, however, complex, which we will discuss shortly. In line 13, c.minMISItem gives the item that has the lowest MIS value in the candidate sequence c. Unlike that in MS-Apriori, where the first item in each itemset has the lowest MIS value, in sequential pattern mining the item with the lowest MIS value may appear anywhere in a sequence. Simi- lar to those in MS-Apriori, lines 13 and 14 are used to ensure that all se- quential rules can be generated after MS-GSP without scanning the origi- nal data. Note that in traditional sequential pattern mining, sequential rules are not defined. We will define several types in Sect. 2.9. Algorithm MS-GSP(S, MS) // MS stores all MIS values 1 M  sort(I, MS); // according to MIS(i)’s stored in MS 2 L  init-pass(M, S); // make the first pass over S 3 F1  {{l} | l  L, l.count/n  MIS(l)}; // n is the size of S 4 for (k = 2; Fk1  ; k++) do 5 if k = 2 then 6 Ck  level2-candidate-gen-SPM(L) 7 else Ck  MScandidate-gen-SPM(Fk1) 8 endif 9 for each data sequence s  S do 10 for each candidate c  Ck do 11 if c is contained in s then 12 c.count++ 13 if c’ is contained in s, where c’ is c after an occurrence of c.minMISItem is removed from c then 14 c.rest.count++ // c.rest: c without c.minMISItem 15 endfor 16 endfor 17 Fk  {c  Ck | c.count/n  MIS(c.minMISItem)} 18 endfor 19 return F  ∪k Fk; Fig. 2.14. The MS-GSP algorithm 2.7 Mining Sequential Patterns Based on GSP 47 Let us now discuss MScandidate-gen-SPM(). In MS-Apriori, the order- ing of items is not important and thus we put the item with the lowest MIS value in each itemset as the first item of the itemset, which simplifies the join step. However, for sequential pattern mining, we cannot artificially put the item with the lowest MIS value as the first item in a sequence be- cause the ordering of items is significant. This causes problems for joining. Example 23: Assume we have a sequence s1 = {1, 2}{4} in F3, from which we want to generate candidate sequences for the next level. Suppose that item 1 has the lowest MIS value in s1. We use the candidate generation function in Fig. 2.13. Assume also that the sequence s2 = {2}{4, 5} is not in F3 because its minimum support is not satisfied. Then we will not gen- erate the candidate {1, 2}{4, 5}. However, {1, 2}{4, 5} can be frequent be- cause items 2, 4, and 5 may have higher MIS values than item 1. ▀ To deal with this problem, let us make an observation. The problem on- ly occurs when the first item in the sequence s1 or the last item in the se- quence s2 is the only item with the lowest MIS value, i.e., no other item in s1 (or s2) has the same lowest MIS value. If the item (say x) with the lowest MIS value is not the first item in s1, then s2 must contain x, and the candi- date generation function in Fig. 2.13 will still be applicable. The same rea- soning goes for the last item of s2. Thus, we only need special treatment for these two cases. Let us see how to deal with the first case, i.e., the first item is the only item with the lowest MIS value. We use an example to develop the idea. Assume we have the frequent 3-sequence of s1 = {1, 2}{4}. Based on the algorithm in Fig. 2.13, s1 may be extended to generate two possible candi- dates using {2}{4}{x} and {2}{4, x} c1 = {1, 2}{4}{x} and c2 = {1, 2}{4, x}, where x is an item. However, {2}{4}{x} and {2}{4, x} may not be frequent because items 2, 4, and x may have higher MIS values than item 1, but we still need to generate c1 and c2 because they can be frequent. A different join strategy is thus needed. We observe that for c1 to be frequent, the subsequence s2 = {1}{4}{x} must be frequent. Then, we can use s1 and s2 to generate c1. c2 can be gen- erated in a similar manner with s2 = {1}{4, x}. s2 is basically the subse- quence of c1 (or c2) without the second item. Here we assume that the MIS value of x is higher than item 1. Otherwise, it falls into the second case. Let us see the same problem for the case where the last item has the on- ly lowest MIS value. Again, we use an example to illustrate. Assume we have the frequent 3-sequence s2 = {3, 5}{1}. It can be extended to produce two possible candidates based on the algorithm in Fig. 2.13, 48 2 Association Rules and Sequential Patterns c1 = {x}{3, 5}{1}, and c2 = {x, 3, 5}{1}. For c1 to be frequent, the subsequence s1 = {x}{3}{1} has to be frequent (we assume that the MIS value of x is higher than that of item 1). Thus, we can use s1 and s2 to generate c1. c2 can be generated with s1 = {x, 3}{1}. s1 is basically the subsequence of c1 (or c2) without the second last item. The MScandidate-gen-SPM() function is given in Fig. 2.15, which is self-explanatory. Some special treatments are needed for 2-sequences be- cause the same s1 (or s2) may generate two candidate sequences. We use two examples to show the working of the function. Example 24: Consider the items 1, 2, 3, 4, 5, and 6 with their MIS values, MIS(1) = 0.03 MIS(2) = 0.05 MIS(3) = 0.03 MIS(4) = 0.07 MIS(5) = 0.08 MIS(6) = 0.09. Function MScandidate-gen-SPM(Fk1) 1 Join Step. Candidate sequences are generated by joining Fk1 with Fk1. 2 if the MIS value of the first item in a sequence (denoted by s1) is less than (<) the MIS value of every other item in s1 then // s1 and s2 can be equal Sequence s1 joins with s2 if (1) the subsequences obtained by dropping the second item of s1 and the last item of s2 are the same, and (2) the MIS val- ue of the last item of s2 is greater than that of the first item of s1. Candidate sequences are generated by extending s1 with the last item of s2:  if the last item l in s2 is a separate element then {l} is appended at the end of s1 as a separate element to form a candi- date sequence c1. if (the length and the size of s1 are both 2) AND (the last item of s2 is greater than the last item of s1) then // maintain lexicographic order l is added at the end of the last element of s1 to form another candi- date sequence c2.  else if ((the length of s1 is 2 and the size of s1 is 1) AND (the last item of s2 is greater than the last item of s1)) OR (the length of s1 is greater than 2) then the last item in s2 is added at the end of the last element of s1 to form the candidate sequence c2. 3 elseif the MIS value of the last item in a sequence (denoted by s2) is less than (<) the MIS value of every other item in s2 then A similar method to the one above can be used in the reverse order. 4 else use the Join Step in Fig. 2.13 5 Prune step: A candidate sequence is pruned if any one of its (k1)- subsequences is infrequent (without minimum support) except the subse- quence that does not contain the item with strictly the lowest MIS value. Fig. 2.15. The MScandidate-gen-SPM function 2.8 Mining Sequential Patterns Based on PrefixSpan 49 The data set has 100 sequences. The following frequent 3-sequences are in F3 with their actual support counts attached after “:”: (a). {1}{4}{5}:4 (b). {1}{4}{6}:5 (c). {1}{5}{6}:6 (d). {1}{5, 6}:5 (e). {1}{6}{3}:4 (f). {6}{3}{6}:9 (g). {5, 6}{3}:5 (h). {5}{4}{3}:4 (i). {4}{5}{3}:7. For sequence (a) (= s1), item 1 has the lowest MIS value. It cannot join with sequence (b) because condition (1) in Fig. 2.15 is not satisfied. How- ever, (a) can join with (c) to produce the candidate sequence, {1}{4}{5}{6}. (a) can also join with (d) to produce {1}{4}{5, 6}. (b) can join with (e) to produce {1}{4}{6}{3}, which is pruned subsequently because {1}{4}{3} is infrequent. (d) and (e) can be joined to give {1}{5, 6}{3}, but it is pruned because {1}{5}{3} does not exist. (e) can join with (f) to produce {1}{6}{3}{6} which is done in line 4 because both item 1 and item 3 in (e) have the same MIS value. However, it is pruned because {1}{3}{6} is in- frequent. We do not join (d) and (g), although they can be joined based on the algorithm in Fig. 2.13, because the first item of (d) has the lowest MIS value and we use a different join method for such sequences. Now we look at 3-sequences whose last item has strictly the lowest MIS value. (i) (= s1) can join with (h) (= s2) to produce {4}{5}{4}{3}. However, it is pruned because {4}{4}{3} is not in F3. ▀ Example 25: Now we consider generating candidates from frequent 2- sequences, which is special as we noted earlier. We use the same items and MIS values in Example 24. The following frequent 2-sequences are in F2 with their actual support counts attached after “:”: (a). {1}{5}:6 (b). {1}{6}:7 (c) {5}{4}:8 (d). {1, 5}:6 (e). {1, 6}:6. (a) can join with (b) to produce both {1}{5}{6} and {1}{5, 6}. (b) can join with (d) to produce {1, 5}{6}. (e) can join with (a) to produce {1, 6}{5}. Clearly, there are other joins. Again, (a) will not join with (c). ▀ Note that the support difference constraint in Sect. 2.4.1 can also be included. We omitted it to simplify the algorithm as it is already complex. Also, the user can instruct the algorithm to generate only certain sequential patterns or not to generate others by setting the MIS values suitably. 2.8 Mining Sequential Patterns Based on PrefixSpan We now introduce another sequential pattern mining algorithm, called Pre- fixSpan [33], which does not generate candidates. Different from the GSP 50 2 Association Rules and Sequential Patterns algorithm [41], which can be regarded as performing breadth-first search to find all sequential patterns, PrefixSpan performs depth-first search. 2.8.1 PrefixSpan Algorithm It is easy to introduce the original PrefixSpan algorithm using an example. Example 26: Consider again mining sequential patterns from Table 2.2 with minsup = 25%. PrefixSpan first sorts all items in each element (or itemset) as shown in the table. Then, by one scan of the sequence database, it finds all frequent items, i.e., 30, 40, 70, 80 and 90. The corresponding length one sequential patterns are {30}, {40}, {70}, {80} and {90}. We notice that the complete set of sequential patterns can actually be divided into five mutually exclusive subsets: the subset with prefix {30}, the subset with prefix {40}, the subset with prefix {70}, the subset with prefix {80}, and the subset with prefix {90}. We only need to find the five subsets one by one. To find sequential patterns having prefix {30}, the algorithm extends the prefix by adding items to it one at a time. To add the next item x, there are two possibilities, i.e., x joining the last itemset of the prefix (i.e., {30, x}) and x forming a separate itemset (i.e., {30}{x}). PrefixSpan performs the task by first forming the {30}-projected database and then finding all the cases of the two types in the projected database. The projected database is produced as follows: If a sequence contains item 30, then the suffix fol- lowing the first 30 is extracted as a sequence in the projected database. Furthermore, since infrequent items cannot appear in a sequential pattern, all infrequent items are removed from the projection. The first sequence in our example, {30}{90}, is projected to {90}. The second sequence, {10, 20}{30}{10, 40, 60, 70}, is projected to {40, 70}, where the infrequent items 10 and 60 are removed. The third sequence {30, 50, 70, 80} is pro- jected to {_, 70, 80}, where the infrequent item 50 is removed. Note that the underline symbol “_” in this projection denotes that the items (only 30 in this case) in the last itemset of the prefix are in the same itemset as items 50, 70 and 80 in the sequence. The fourth sequence is projected to {30, 40, 70, 80}{90}. The projection of the last sequence is empty since it does not contain item 30. The final projected database for prefix {30} contains the following sequences: {90}, {40, 70}, {_, 70, 80}, and {30, 40, 70, 80}{90} By scanning the projected database once, PrefixSpan finds all possible one item extensions to the prefix, i.e., all x’s for {30, x} and all x’s for {30}{x}. Let us discuss the details. 2.8 Mining Sequential Patterns Based on PrefixSpan 51 Find All Frequent Patterns of the Form {30, x}: Two templates {_, x} and {30, x} are used to match each projected sequence to accumulate the support count for each possible x (here x matches any item). If in the same sequence multiple matches are found with the same x, they are only counted once. Note that in general, the second template should use the last itemset in the prefix rather than only its last item. In our example, they are the same because there is only one item in the last itemset of the prefix. Find All Frequent Patterns of the Form {30}{x}: In this case, x’s are frequent items in the projected database that are not in the same itemset as the last item of the prefix. Let us continue with our example. It is easy to check that both items 70 and 80 are in the same itemset as 30. That is, we have two frequent se- quences {30, 70} and {30, 80}. The support count of {30, 70} is 2 based on the projected database; one from the projected sequence {_, 70, 80} (a {_, x} match) and one from the projected sequence {30, 40, 70, 80}{90} (a {30, x} match). In both cases, the x’s are the same, i.e., 70. Similarly, the support count of {30, 80} is 2 as well and thus frequent. It is also easy to check that items 40, 70, and 90 are also frequent but not in the same itemset as 30. Thus, {30}{40}, {30}{70}, and {30}{90} are three sequential patterns. The set of sequential patterns having prefix {30} can be further divided into five mutually exclusive subsets: the ones with prefixes {30, 70}, {30, 80}, {30}{40}, {30}{70}, and {30}{90}. We can recursively find the five subsets by forming their corresponding projected databases. For example, to find sequential patterns having prefix {30}{40}, we can form the {30}{40}-projected database containing pro- jections {_, 70} and {_, 70, 80}{90}. Template {_, x} has two matches and in both cases x is 70. Thus, {30}{40, 70} is output as a sequential pat- tern. Since there is no other frequent item in this projected database, the prefix cannot grow longer. The depth-first search returns from this branch. After completing the mining of the {30}-projected database, we find all sequential patterns with prefix {30}, i.e., {30}, {30}{40}, {30}{40, 70}, {30}{70}, {30}{90}, {30, 70}, {30, 80} and {30, 70, 80} By forming and mining the {40}-, {70}-, {80}- and {90}-projected databases, the remaining sequential patterns can be found. ▀ The pseudo code of PrefixSpan can be found in [33]. Comparing to the breadth-first search of GSP, the key advantage of PrefixSpan is that it does not generate any candidates. It only counts the frequency of local items. With a low minimum support, a huge number of candidates can be gener- ated by GSP, which can cause memory and computational problems. 52 2 Association Rules and Sequential Patterns 2.8.2 Mining with Multiple Minimum Supports The PrefixSpan algorithm can be adapted to mine with multiple minimum supports. Again, let MIS(i) be the user-specified minimum item support of item i. Let  be the user-specified support difference threshold in the support difference constraint (Sect. 2.4.1), i.e., |sup(i) – sup(j)|  , where i and j are items in the same sequential pattern, and sup(x) is the ac- tual support of item x in the sequence database S. PrefixSpan can be modi- fied as follows. We call the modified algorithm MS-PS. 1. Find every item i whose actual support in the sequence database S is at least MIS(i). i is called a frequent item. 2. Sort all the discovered frequent items in ascending order according to their MIS values. Let i1, …, iu be the frequent items in the sorted order. 3. For each item ik in the above sorted order, (i) identify all the data sequences in S that contain ik and at the same time remove every item j in each sequence that does not satisfy |sup(j) – sup(ik)| ≤ . The resulting set of sequences is denoted by Sk. Note that we are not using ik as the prefix to project the database S. (ii) call the function r-PrefixSpan(ik, Sk, count(MIS(ik))) (restricted Pre- fixSpan), which finds all sequential patterns that contain ik, i.e., no pattern that does not contain ik should be generated. r-PrefixSpan() uses count(MIS(ik)) (the minimum support count in terms of the number of sequences) as the only minimum support for mining in Sk. The sequence count is easier to use than the MIS value in percent- age, but they are equivalent. Once the complete set of such patterns is found from Sk, All occurrences of ik are removed from S. r-PrefixSpan() is almost the same as PrefixSpan with one important differ- ence. During each recursive call, either the prefix or every sequence in the projected database must contain ik because, as we stated above, this func- tion finds only those frequent sequences that contain ik. Another minor dif- ference is that the support difference constraint needs to be checked during each projection as sup(ik) may not be the lowest in the pattern. Example 27: Consider mining sequential patterns from Table 2.5. Let MIS(20) = 30% (3 sequences in minimum support count), MIS(30) = 20% (2 sequences), MIS(40) = 30% (3 sequences), and the MIS values for the rest of the items be 15% (2 sequences). We ignore the support difference constraint as it is simple. In step 1, we find three frequent items, 20, 30 and 40. After sorting in step 2, we have (30, 20, 40). We then go to step 3. In the first iteration of step 3, we work on i1 = 30. Step 3(i) gives us the second, fourth and sixth sequences in Table 2.5, i.e., 2.9 Generating Rules from Sequential Patterns 53 S1 = {{40}{30}{40, 60}, {30}{20, 40}{40, 100}, {40}{30}{110}}. We then run r-PrefixSpan(30, S1, 2) in step 3(ii). The frequent items in S1 are 30, and 40. They both have the support of 3 sequences. The length one frequent sequence is only {30}. {40} is not included because we re- quire that every frequent sequence must contain 30. We next find frequent sequences having prefix {30}. The database S1 is projected to give {40} and {40}{40}. 20, 60 and 100 have been removed because their supports in S1 are less than the required support for item 30 (i.e., 2 sequences). For the same reason, the projection of {40}{30}{110} is empty. Thus, we find a length two frequent sequence {30}{40}. In this case, there is no item in the same itemset as 30 to form a frequent sequence of the form {30, x}. Next, we find frequent sequences with prefix {40}. We again project S1, which gives us only {30}{40} and {30}. {40, 100} is not included be- cause it does not contain 30. This projection gives us another length two frequent sequence {40}{30}. The first iteration of step 3 ends. In the second iteration of step 3, we work on i2 = 20. Step 3(i) gives us the first, fourth, fifth and seventh sequences in Table 2.5 with item 30 re- moved, S2 = {{20, 50}, {20, 40}{40, 100}, {20, 40}{10}, {20}{80}{70}}. It is easy to see that only item 20 is frequent, and thus only a length one frequent sequence is generated, {20}. In the third iteration of step 3, we work on i3 = 40. We can verify that again only one frequent sequence, i.e., {40}, is found. The final set of sequential patterns generated from the sequence data- base in Table 2.5 is {{30}, {20}, {40}, {40}{30}, {30}{40}}. ▀ 2.9 Generating Rules from Sequential Patterns In classic sequential pattern mining, no rules are generated. It is, however, possible to define and generate many types of rules. This section intro- Table 2.5. An example of a sequence database Sequence ID Data Sequence 1 {20, 50} 2 {40}{30}{40, 60} 3 {40, 90, 120} 4 {30}{20, 40}{40, 100} 5 {20, 40}{10} 6 {40}{30}{110} 7 {20}{80}{70} 54 2 Association Rules and Sequential Patterns duces only three types, sequential rules, label sequential rules and class sequential rules, which have been used in Web usage mining and Web content mining (see Chaps. 11 and 12). 2.9.1 Sequential Rules A sequential rule (SR) is an implication of the form, X  Y, where Y is a sequence and X is a proper subsequence of Y, i.e., X is a subsequence of Y and the length Y is greater than the length of X. The support of a sequen- tial rule, X  Y, in a sequence database S is the fraction of sequences in S that contain Y. The confidence of a sequential rule, X  Y, in S is the pro- portion of sequences in S that contain X also contain Y. Given a minimum support and a minimum confidence, according to the downward closure property, all the rules can be generated from frequent sequences without going to the original sequence data. Let us see an ex- ample of a sequential rule found from the data sequences in Table 2.6. Table 2.6. An example of a sequence database for mining sequential rules Data Sequence 1 {1}{3}{5}{7, 8, 9} 2 {1}{3}{6}{7, 8} 3 {1, 6}{7} 4 {1}{3}{5, 6} 5 {1}{3}{4} Example 28: Given the sequence database in Table 2.6, the minimum support of 30% and the minimum confidence of 60%, one of the sequential rules found is the following, {1}{7}  {1}{3}{7, 8} [sup = 2/5, conf = 2/3] Data sequences 1, 2 and 3 contain {1}{7}, and data sequences 1 and 2 con- tain {1}{3}{7, 8}. ▀ If multiple minimum supports are used, we can employ the results of multiple minimum support pattern mining to generate all the rules. 2.9.2 Label Sequential Rules Sequential rules may not be restrictive enough in some applications. We introduce a special kind of sequential rules called label sequential rules. A label sequential rule (LSR) is of the form, X  Y, where Y is a sequence 2.9 Generating Rules from Sequential Patterns 55 and X is a sequence produced from Y by replacing some of its items with wildcards. A wildcard is denoted by an “*” which matches any item. These replaced items are usually very important and are called labels. The labels are a small subset of all the items in the data. Example 29: Given the sequence database in Table 2.6, the minimum support of 30% and the minimum confidence of 60%, one of the label se- quential rules found is the following, {1}{*}{7, *}  {1}{3}{7, 8} [sup = 2/5, conf = 2/2]. Notice the confidence change compared to the rule in Example 28. The supports of the two rules are the same. In this case, data sequences 1 and 2 contain {1}{*}{7, *}, and they also contain {1}{3}{7, 8}. Items 3 and 8 are labels. ▀ LSRs are useful because in some applications we need to predict the la- bels in an input sequence, e.g., items 3 and 8 above. The confidence of the rule simply gives us the estimated probability that the two “*”s are 3 and 8 given that an input sequence contains {1}{*}{7, *}. We will see an applica- tion of LSRs in Chap. 11, where we want to predict whether a word in a comparative sentence is an entity (e.g., a product name), which is a label. Note that due to the use of wildcards, frequent sequences alone are not sufficient for computing rule confidences. Scanning the data is needed. Notice also that the same pattern may appear in a data sequence multiple times. Rule confidences thus can be defined in different ways according to application needs. The wildcards may also be restricted to match only cer- tain types of items to make the label prediction meaningful and unambigu- ous (see some examples in Chap. 11). 2.9.3 Class Sequential Rules Class sequential rules (CSR) are analogous to class association rules (CAR). Let S be a set of data sequences. Each sequence is also labeled with a class y. Let I be the set of all items in S, and Y be the set of all class labels, I  Y = . Thus, the input data D for mining is represented with {(s1, y1), (s2, y2), …, (sn, yn)}, where si is a sequence in S and yi  Y is its class. A class sequential rule (CSR) is of the form X  y, where X is a sequence, and y  Y. A data instance (si, yi) is said to cover a CSR, X  y, if X is a subsequence of si. A data instance (si, yi) is said to satisfy a CSR if X is a subsequence of si and yi = y. 56 2 Association Rules and Sequential Patterns Example 30: Table 2.7 gives an example of a sequence database with five data sequences and two classes, c1 and c2. Using the minimum support of 30% and the minimum confidence of 60%, one of the discovered CSRs is: {1}{3}{7, 8}  c1 [sup = 2/5, conf = 2/3]. Data sequences 1 and 2 satisfy the rule, and data sequences 1, 2 and 5 cov- er the rule. ▀ Table 2.7. An example of a sequence database for mining CSRs Data Sequence Class 1 {1}{3}{5}{7, 8, 9} c1 2 {1}{3}{6}{7, 8} c1 3 {1, 6}{9} c2 4 {3}{5, 6} c2 5 {1}{3}{4}{7, 8} c2 As in class association rule mining, we can modify the GSP and Prefix- Span algorithms to produce algorithms for mining all CSRs. Similarly, we can also use multiple minimum class supports and/or multiple minimum item supports as in class association rule mining. Bibliographic Notes Association rule mining was introduced in 1993 by Agrawal et al. [2]. Since then, thousands of research papers have been published on the topic. This short chapter only introduces some basics, and it, by no means, does justice to the huge body of literature in the area. The bibliographic notes here should help you explore further. Since given a data set, a minimum support and a minimum confidence, the solution (the set of frequent itemsets or the set of rules) is determined and unique, most papers improve the mining efficiency. The most well- known algorithm is the Apriori algorithm proposed by Agrawal and Sri- kant [3], which has been described in this chapter. Another important algo- rithm is FP-growth proposed by Han et al. [17]. The algorithm com- presses the data and stores it in memory using a frequent pattern tree. It then mines all frequent itemsets without candidate generation. Other nota- ble general algorithms include those by Agarwal et al. [1], Mannila et al. [26], Park et al. [31], Zaki et al. [55], etc. An efficiency comparison of var- ious algorithms was reported by Zheng et al. [56]. Apart from performance improvements, several variations of the origi- nal model were also proposed. Srikant and Agrawal [39], and Han and Fu Bibliographic Notes 57 [15] proposed two algorithms to mine generalized association rules or multi-level association rules. Liu et al. [23] extended the original model to take multiple minimum supports, which was also studied by Wang et al. [47], Seno and Karypis [37], Xiong et al. [51], etc. Srikant et al. [42] proposed to mine association rules with item constraints. The model re- stricts the rules that should be generated. Ng et al. [28] generalized the idea, which was followed by many subsequent papers on the topic of con- strained rule mining. It is well known that association rule mining often generates a huge number of frequent itemsets and rules. Bayardo [6] and Lin and Kedem [21] introduced the problem of mining maximal frequent itemsets, which are itemsets with no frequent supersets. Improved algorithms are reported in many papers, e.g., [1, 10]. Since maximal pattern mining only finds longest patterns, the support information of their subsets, which are obvi- ously also frequent, is not found. As a result, association rules cannot be generated. The next significant development was the mining of closed fre- quent itemsets studied by Pasquier et al. [32], Zaki and Hsiao [54], and Wang et al. [46]. Closed itemsets are better than maximal frequent itemsets because closed frequent itemsets provide a lossless concise representation of all frequent itemsets. Other developments on association rules include class association rules by Liu et al. [22] and emerging patterns (similar to class association rules) by Dong and Li [13], implication rules by Brin et al. [8], cyclic as- sociation rules by Ozden et al. [29], periodic patterns by Yang et al. [52], negative association rules by Savasere [36] and Wu et al. [50], weighted association rules by Wang et al. [48], association rules with numerical variables by Webb [49], high-performance rule mining by Buehrer et al. [9], incremental rule mining by Cheung et al. [11], inte- grating mining with database systems by Sarawagi et al. [35], sampling for rule mining by Toivonen [44], and many others. Cong et al. [12] in- troduced association rule mining from bioinformatics data, which typically have a very large number of attributes (more than ten thousands) but only a very small number of records or transactions (e.g., less than 100). Another major research area of association rules is the interestingness of the discovered rules. Since an association rule miner often generates a huge number of rules, it is very difficult, if not impossible, for human us- ers to inspect them in order to find those truly interesting or useful rules. Researchers have proposed many techniques to help users identify such rules, e.g., [7, 20, 24, 25, 30, 34, 38, 43]. There are also several data min- ing query languages [16, 18, 27, 45]. A deployed data mining system that uses some of these ideas, class association rules, and OLAP is reported in [25], which has been in production use since 2006. 58 2 Association Rules and Sequential Patterns Regarding sequential pattern mining, the first algorithm was proposed by Agrawal and Srikant [4], which was a direct application of the Apriori algorithm. Improvements were made subsequently by several researchers, e.g., Ayres et al. [5], Pei et al. [33], Srikant and Agrawal [41], Zaki [53], etc. The MS-GSP and MS-PS algorithms for mining sequential patterns with multiple minimum supports and the support difference constraint are introduced in this book. Label and class sequential rules have been used in [19] for mining comparative sentences from text documents. The literature on association rule mining and sequential pattern mining is extensive. There are several publicly available implementations of algorithms for mining frequent itemsets, maximal frequent itemsets, closed frequent item- sets, and sequential patterns from various research groups, most notably from those of Jiawei Han, Johnanne Gehrke, and Mohammed Zaki. There were also two workshops dedicated to frequent itemset mining organized by Roberto Bayardo, Bart Goethals, and Mohammed J. Zaki, which re- ported many efficient implementations. The workshop Web sites are http://fimi.cs.helsinki.fi/fimi03/ and http://fimi.cs.helsinki.fi/fimi04/. Bibliography 1. Agarwal, R., C. Aggarwal, and V. Prasad. A tree projection algorithm for generation of frequent item sets. Journal of Parallel and Distributed Computing, 2001, 61(3): p. 350-371. 2. Agrawal, R., T. Imieliski, and A. Swami. Mining association rules between sets of items in large databases. In Proceedings of ACM SIGMOD International Conference on Management of Data (SIGMOD-1993), 1993. 3. Agrawal, R. and R. Srikant. Fast algorithms for mining association rules. In Proceedings of International Conference on Very Large Data Bases (VLDB- 1994), 1994. 4. Agrawal, R. and R. Srikant. Mining sequential patterns. In Proceedings of IEEE International Conference on Data Engingeering (ICDE-1995), 1995. 5. Ayres, J., J. Flannick, J. Gehrke, and T. Yiu. Sequential pattern mining using a bitmap representation. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2002), 2002. 6. Bayardo Jr, R. Efficiently mining long patterns from databases. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-1998), 1998. 7. Bayardo Jr, R. and R. Agrawal. Mining the most interesting rules. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 8. Brin, S., R. Motwani, J. Ullman, and S. Tsur. Dynamic itemset counting and implication rules for market basket data. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-1997), 1997. Bibliography 59 9. Buehrer, G., S. Parthasarathy, and A. Ghoting. Out-of-core frequent pattern mining on a commodity PC. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2006), 2006. 10. Burdick, D., M. Calimlim, J. Flannick, J. Gehrke, and T. Yiu. Mafia: A maximal frequent itemset algorithm. Knowledge and Data Engineering, IEEE Transactions on, 2005, 17(11): p. 1490-1504. 11. Cheung, D., S. Lee, and B. Kao. A general incremental technique for maintaining discovered association rules. In Proceedings of the Fifth International Conference on Database Systems for Advanced Applications (DASFAA-1997), 1997. 12. Cong, G., K. Tan, A. Tung, and X. Xu. Mining top-k covering rule groups for gene expression data. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-2005), 2005. 13. Dong, G. and J. Li. Efficient mining of emerging patterns: Discovering trends and differences. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 14. Dougherty, J., R. Kohavi, and M. Sahami. Supervised and unsupervised discretization of continuous features. In Proceedings of International Conference on Machine Learning (ICML-1995), 1995. 15. Han, J. and Y. Fu. Discovery of Multi-Level Association Rules from Large Databases. In Proceedings of International Conference on Very Large Data Bases (VLDB-1995), 1995. 16. Han, J., Y. Fu, W. Wang, K. Koperski, and O. Zaiane. DMQL: A data mining query language for relational databases. In Proceedings of 1996 ACM SIGMOD workshop on Research Issues in Data Mining and Knowledge Discovery (DMKD-1996), 1996. 17. Han, J., J. Pei, and Y. Yin. Mining frequent patterns without candidate generation. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-2000), 2000. 18. Imielinski, T. and A. Virmani. MSQL: A query language for database mining. Data Mining and Knowledge Discovery, 1999, 3(4): p. 373-408. 19. Jindal, N. and B. Liu. Mining comparative sentences and relations. In Proceedings of National Conf. on Artificial Intelligence (AAAI-2006), 2006. 20. Klemettinen, M., H. Mannila, P. Ronkainen, H. Toivonen, and A. Verkamo. Finding interesting rules from large sets of discovered association rules. In Proceedings of ACM International Conference on Information and Knowledge Management (CIKM-1994), 1994. 21. Lin, D. and Z. Kedem. Pincer-search: A new algorithm for discovering the maximum frequent set. International Conference on Extended Database Technology (EDBT-1998), 1998: p. 103-119. 22. Liu, B., W. Hsu, and Y. Ma. Integrating classification and association rule mining. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1998), 1998. 23. Liu, B., W. Hsu, and Y. Ma. Mining association rules with multiple minimum supports. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 60 2 Association Rules and Sequential Patterns 24. Liu, B., W. Hsu, and Y. Ma. Pruning and summarizing the discovered associations. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 25. Liu, B., K. Zhao, J. Benkler, and W. Xiao. Rule interestingness analysis using OLAP operations. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2006), 2006. 26. Mannila, H., H. Toivonen, and A. Verkamo. Efficient algorithms for discovering association rules. In Proceedings of Knowledge Discovery in Databases (KDD'94), 1994. 27. Meo, R., G. Psaila, and S. Ceri. A new SQL-like operator for mining association rules. In Proceedings of International Conference on Very Large Data Bases (VLDB-1996), 1996. 28. Ng, R., L. Lakshmanan, J. Han, and A. Pang. Exploratory mining and pruning optimizations of constrained associations rules. ACM SIGMOD Record, 1998, 27(2): p. 13-24. 29. Ozden, B., S. Ramaswamy, and A. Silberschatz. Cyclic association rules. In Proceedings of IEEE International Conference on Data Engingeering (ICDE- 2002), 2002. 30. Padmanabhan, B. and A. Tuzhilin. Small is beautiful: discovering the minimal set of unexpected patterns. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD- 2000), 2000. 31. Park, J., M. Chen, and P. Yu. An effective hash-based algorithm for mining association rules. ACM SIGMOD Record, 1995, 24(2): p. 175-186. 32. Pasquier, N., Y. Bastide, R. Taouil, and L. Lakhal. Discovering frequent closed itemsets for association rules. Database Theory—ICDT’99, 1999: p. 398-416. 33. Pei, J., J. Han, B. Mortazavi-Asl, H. Pinto, Q. Chen, U. Dayal, and M. Hsu. PrefixSpan: Mining sequential patterns efficiently by prefix-projected pattern growth. In Proceedings of IEEE International Conference on Data Engingeering (ICDE-2001), 2001. 34. Piatetsky-Shapiro, G. Discovery, analysis, and presentation of strong rules. Knowledge discovery in databases, 1991. 35. Sarawagi, S., S. Thomas, and R. Agrawal. Integrating association rule mining with relational database systems: Alternatives and implications. Data Mining and Knowledge Discovery, 2000, 4(2): p. 89-125. 36. Savasere, A., E. Omiecinski, and S. Navathe. Mining for strong negative associations in a large database of customer transactions. In Proceedings of IEEE International Conference on Data Engingeering (ICDE-1998), 1998. 37. Seno, M. and G. Karypis. Finding frequent patterns using length-decreasing support constraints. Data Mining and Knowledge Discovery, 2005, 10(3): p. 197-228. 38. Silberschatz, A. and A. Tuzhilin. What makes patterns interesting in knowledge discovery systems. IEEE Transactions on Knowledge and Data Engineering, 1996, 8(6): p. 970-974. Bibliography 61 39. Srikant, R. and R. Agrawal. Mining generalized association rules. Future Generation Computer Systems, 1997, 13(2-3): p. 161-180. 40. Srikant, R. and R. Agrawal. Mining quantitative association rules in large relational tables. ACM SIGMOD Record, 1996, 25(2): p. 1-12. 41. Srikant, R. and R. Agrawal. Mining sequential patterns: Generalizations and performance improvements. Advances in Database Technology—EDBT'96, 1996: p. 1-17. 42. Srikant, R., Q. Vu, and R. Agrawal. Mining association rules with item constraints. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1997), 1997. 43. Tan, P., V. Kumar, and J. Srivastava. Selecting the right interestingness measure for association patterns. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD- 2002), 2002. 44. Toivonen, H. Sampling large databases for association rules. In Proceedings of International Conference on Very Large Data Bases (VLDB-1996), 1996. 45. Tuzhilin, A. and B. Liu. Querying multiple sets of discovered rules. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2002), 2002. 46. Wang, J., J. Han, and J. Pei. Closet+: Searching for the best strategies for mining frequent closed itemsets. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD- 2003), 2003. 47. Wang, K., Y. He, and J. Han. Mining frequent itemsets using support constraints. In Proceedings of International Conference on Very Large Data Bases (VLDB-2000), 2000. 48. Wang, W., J. Yang, and P. Yu. WAR: weighted association rules for item intensities. Knowledge and Information systems, 2004, 6(2): p. 203-229. 49. Webb, G. Discovering associations with numeric variables. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2001), 2001. 50. Wu, X., C. Zhang, and S. Zhang. Efficient mining of both positive and negative association rules. ACM Transactions on Information Systems (TOIS), 2004, 22(3): p. 381-405. 51. Xiong, H., P. Tan, and V. Kumar. Mining strong affinity association patterns in data sets with skewed support distribution. In Proceedings of IEEE International Conference on Data Mining (ICDM-2003), 2003. 52. Yang, J., W. Wang, and P. Yu. Mining surprising periodic patterns. Data Mining and Knowledge Discovery, 2004, 9(2): p. 189-216. 53. Zaki, M. SPADE: An efficient algorithm for mining frequent sequences. Machine Learning, 2001, 42(1): p. 31-60. 54. Zaki, M. and C. Hsiao. CHARM: An efficient algorithm for closed association rule mining. In Proceedings of SIAM International Conference on Data Mining (SDM-2002), 2002. 62 2 Association Rules and Sequential Patterns 55. Zaki, M., S. Parthasarathy, M. Ogihara, and W. Li. New algorithms for fast discovery of association rules. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1997), 1997. 56. Zheng, Z., R. Kohavi, and L. Mason. Real world performance of association rule algorithms. In ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2001), 2001. 3 Supervised Learning Supervised learning has been a great success in real-world applications. It is used in almost every domain, including text and Web domains. Super- vised learning is also called classification or inductive learning in ma- chine learning. This type of learning is analogous to human learning from past experiences to gain new knowledge in order to improve our ability to perform real-world tasks. However, since computers do not have “experi- ences”, machine learning learns from data, which are collected in the past and represent past experiences in some real-world applications. There are several types of supervised learning tasks. In this chapter, we focus on one particular type, namely, learning a target function that can be used to predict the values of a discrete class attribute. This type of learning has been the focus of the machine learning research and is perhaps also the most widely used learning paradigm in practice. This chapter introduces a number of such supervised learning techniques. They are used in almost every Web mining application. We will see their uses from Chaps. 6–12. 3.1 Basic Concepts A data set used in the learning task consists of a set of data records, which are described by a set of attributes A = {A1, A2, …, A|A|}, where |A| denotes the number of attributes or the size of the set A. The data set also has a special target attribute C, which is called the class attribute. In our subse- quent discussions, we consider C separately from attributes in A due to its special status, i.e., we assume that C is not in A. The class attribute C has a set of discrete values, i.e., C = {c1, c2, …, c|C|}, where |C| is the number of classes and |C|  2. A class value is also called a class label. A data set for learning is simply a relational table. Each data record describes a piece of “past experience”. In the machine learning and data mining literature, a da- ta record is also called an example, an instance, a case or a vector. A data set basically consists of a set of examples or instances. Given a data set D, the objective of learning is to produce a classifica- tion/prediction function to relate values of attributes in A and classes in C. The function can be used to predict the class values/labels of the future B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_3, © Springer-Verlag Berlin Heidelberg 2011 63 64 3 Supervised Learning data. The function is also called a classification model, a predictive mod- el or simply a classifier. We will use these terms interchangeably in this book. It should be noted that the function/model can be in any form, e.g., a decision tree, a set of rules, a Bayesian model or a hyperplane. Example 1: Table 3.1 shows a small loan application data set. It has four attributes. The first attribute is Age, which has three possible values, young, middle and old. The second attribute is Has_Job, which indicates whether an applicant has a job. Its possible values are true (has a job) and false (does not have a job). The third attribute is Own_house, which shows whether an applicant owns a house. The fourth attribute is Credit_rating, which has three possible values, fair, good and excellent. The last column is the Class attribute, which shows whether each loan application was ap- proved (denoted by Yes) or not (denoted by No) in the past. Table 3.1. A loan application data set ID Age Has_job Own_house Credit_rating Class 1 young false false fair No 2 young false false good No 3 young true false good Yes 4 young true true fair Yes 5 young false false fair No 6 middle false false fair No 7 middle false false good No 8 middle true true good Yes 9 middle false true excellent Yes 10 middle false true excellent Yes 11 old false true excellent Yes 12 old false true good Yes 13 old true false good Yes 14 old true false excellent Yes 15 old false false fair No We want to learn a classification model from this data set that can be used to classify future loan applications. That is, when a new customer comes into the bank to apply for a loan, after inputting his/her age, whether he/she has a job, whether he/she owns a house, and his/her credit rating, the classification model should predict whether his/her loan application should be approved. ▀ Our learning task is called supervised learning because the class labels (e.g., Yes and No values of the class attribute in Table 3.1) are provided in 3.1 Basic Concepts 65 the data. It is as if some teacher tells us the classes. This is in contrast to the unsupervised learning, where the classes are not known and the learn- ing algorithm needs to automatically generate classes. Unsupervised learn- ing is the topic of the next chapter. The data set used for learning is called the training data (or the train- ing set). After a model is learned or built from the training data by a learning algorithm, it is evaluated using a set of test data (or unseen da- ta) to assess the model accuracy. It is important to note that the test data is not used in learning the classi- fication model. The examples in the test data usually also have class labels. That is why the test data can be used to assess the accuracy of the learned model because we can check whether the class predicted for each test case by the model is the same as the actual class of the test case. In order to learn and also to test, the available data (which has classes) for learning is usually split into two disjoint subsets, the training set (for learning) and the test set (for testing). We will discuss this further in Sect. 3.3. The accuracy of a classification model on a test set is defined as: , cases test ofnumber Total tionsclassificacorrect ofNumber Accuracy (1) where a correct classification means that the learned model predicts the same class as the original class of the test case. There are also other meas- ures that can be used. We will discuss them in Sect. 3.3. We pause here to raises two important questions: 1. What do we mean by learning by a computer system? 2. What is the relationship between the training and the test data? We answer the first question first. Given a data set D representing past “experiences”, a task T and a performance measure M, a computer system is said to learn from the data to perform the task T if after learning the sys- tem’s performance on the task T improves as measured by M. In other words, the learned model or knowledge helps the system to perform the task better as compared to no learning. Learning is the process of building the model or extracting the knowledge. We use the data set in Example 1 to explain the idea. The task is to pre- dict whether a loan application should be approved. The performance measure M is the accuracy in Equation (1). With the data set in Table 3.1, if there is no learning, all we can do is to guess randomly or to simply take the majority class (which is the Yes class). Suppose we use the majority class and announce that every future instance or case belongs to the class Yes. If the future data are drawn from the same distribution as the existing training data in Table 3.1, the estimated classification/prediction accuracy 66 3 Supervised Learning on the future data is 9/15 = 0.6 as there are 9 Yes class examples out of the total of 15 examples in Table 3.1. The question is: can we do better with learning? If the learned model can indeed improve the accuracy, then the learning is said to be effective. The second question in fact touches the fundamental assumption of machine learning, especially the theoretical study of machine learning. The assumption is that the distribution of training examples is identical to the distribution of test examples (including future unseen examples). In practical applications, this assumption is often violated to a certain degree. Strong violations will clearly result in poor classification accuracy, which is quite intuitive because if the test data behave very differently from the training data then the learned model will not perform well on the test data. To achieve good accuracy on the test data, training examples must be suf- ficiently representative of the test data. We now illustrate the steps of learning in Fig. 3.1 based on the preced- ing discussions. In step 1, a learning algorithm uses the training data to generate a classification model. This step is also called the training step or training phase. In step 2, the learned model is tested using the test set to obtain the classification accuracy. This step is called the testing step or testing phase. If the accuracy of the learned model on the test data is satis- factory, the model can be used in real-world tasks to predict classes of new cases (which do not have classes). If the accuracy is not satisfactory, we need to go back and choose a different learning algorithm and/or do some further processing of the data (this step is called data pre-processing, not shown in the figure). A practical learning task typically involves many it- erations of these steps before a satisfactory model is built. It is also possi- ble that we are unable to build a satisfactory model due to a high degree of randomness in the data or limitations of current learning algorithms. Fig. 3.1. The basic learning process: training and testing From the next section onward, we study several supervised learning al- gorithms, except Sect. 3.3, which focuses on model/classifier evaluation. We note that throughout the chapter we assume that the training and test data are available for learning. However, in many text and Web page re- lated learning tasks, this is not true. Usually, we need to collect raw data, Learning algorithm model Accuracy Test data Training data Step 1: Training Step 2: Testing 3.2 Decision Tree Induction 67 design attributes and compute attribute values from the raw data. The rea- son is that the raw data in text and Web applications are often not suitable for learning either because their formats are not right or because there are no obvious attributes in the raw text documents or Web pages. 3.2 Decision Tree Induction Decision tree learning is one of the most widely used techniques for classi- fication. Its classification accuracy is competitive with other learning me- thods, and it is very efficient. The learned classification model is repre- sented as a tree, called a decision tree. The techniques presented in this section are based on the C4.5 system from Quinlan [49]. Example 2: Fig. 3.2 shows a possible decision tree learnt from the data in Table 3.1. The tree has two types of nodes, decision nodes (which are in- ternal nodes) and leaf nodes. A decision node specifies some test (i.e., asks a question) on a single attribute. A leaf node indicates a class. Fig. 3.2. A decision tree for the data in Table 3.1 The root node of the decision tree in Fig. 3.2 is Age, which basically asks the question: what is the age of the applicant? It has three possible an- swers or outcomes, which are the three possible values of Age. These three values form three tree branches/edges. The other internal nodes have the same meaning. Each leaf node gives a class value (Yes or No). (x/y) below each class means that x out of y training examples that reach this leaf node have the class of the leaf. For instance, the class of the left most leaf node is Yes. Two training examples (examples 3 and 4 in Table 3.1) reach here and both of them are of class Yes. ▀ To use the decision tree in testing, we traverse the tree top-down ac- cording to the attribute values of the given test instance until we reach a leaf node. The class of the leaf is the predicted class of the test instance. Age? Has_job? Own_house? Credit_rating? Young middle old true false Yes No (2/2) (3/3) true false Yes No (3/3) (2/2) fair good excellent No Yes Yes (1/1) (2/2) (2/2) 68 3 Supervised Learning Example 3: We use the tree to predict the class of the following new in- stance, which describes a new loan applicant. Age Has_job Own_house Credit-rating Class young false false good ? Going through the decision tree, we find that the predicted class is No as we reach the second leaf node from the left. ▀ A decision tree is constructed by partitioning the training data so that the resulting subsets are as pure as possible. A pure subset is one that con- tains only training examples of a single class. If we apply all the training data in Table 3.1 on the tree in Fig. 3.2, we will see that the training exam- ples reaching each leaf node form a subset of examples that have the same class as the class of the leaf. In fact, we can see that from the x and y val- ues in (x/y). We will discuss the decision tree building algorithm in Sect. 3.2.1. An interesting question is: Is the tree in Fig. 3.2 unique for the data in Table 3.1? The answer is no. In fact, there are many possible trees that can be learned from the data. For example, Fig. 3.3 gives another decision tree, which is much smaller and is also able to partition the training data per- fectly according to their classes. Fig. 3.3. A smaller tree for the data set in Table 3.1 In practice, one wants to have a small and accurate tree for many rea- sons. A smaller tree is more general and also tends to be more accurate (we will discuss this later). It is also easier to understand by human users. In many applications, the user understanding of the classifier is important. For example, in some medical applications, doctors want to understand the model that classifies whether a person has a particular disease. It is not sat- isfactory to simply produce a classification because without understanding why the decision is made the doctor may not trust the system and/or does not gain useful knowledge. It is useful to note that in both Fig. 3.2 and Fig. 3.3, the training exam- Has_job? Own_house? true false Yes No (3/3) (6/6) true false Yes (6/6) 3.2 Decision Tree Induction 69 ples that reach each leaf node all have the same class (see the values of (x/y) at each leaf node). However, for most real-life data sets, this is usu- ally not the case. That is, the examples that reach a particular leaf node are not of the same class, i.e., x  y. The value of x/y is, in fact, the confidence (conf) value used in association rule mining, and x is the support count. This suggests that a decision tree can be converted to a set of if-then rules. Yes, indeed. The conversion is done as follows: Each path from the root to a leaf forms a rule. All the decision nodes along the path form the condi- tions of the rule and the leaf node or the class forms the consequent. For each rule, a support and confidence can be attached. Note that in most classification systems, these two values are not provided. We add them here to see the connection of association rules and decision trees. Example 4: The tree in Fig. 3.3 generates three rules. “,” means “and”. Own_house = true  Class =Yes [sup=6/15, conf=6/6] Own_house = false, Has_job = true  Class = Yes [sup=3/15, conf=3/3] Own_house = false, Has_job = false  Class = No [sup=6/15, conf=6/6]. We can see that these rules are of the same format as association rules. However, the rules above are only a small subset of the rules that can be found in the data of Table 3.1. For instance, the decision tree in Fig. 3.3 does not find the following rule: Age = young, Has_job = false  Class = No [sup=3/15, conf=3/3]. Thus, we say that a decision tree only finds a subset of rules that exist in data, which is sufficient for classification. The objective of association rule mining is to find all rules subject to some minimum support and minimum confidence constraints. Thus, the two methods have different objectives. We will discuss these issues again in Sect. 3.5 when we show that associa- tion rules can be used for classification as well, which is obvious. An interesting and important property of a decision tree and its resulting set of rules is that the tree paths or the rules are mutually exclusive and exhaustive. This means that every data instance is covered by a single rule (a tree path) and a single rule only. By covering a data instance, we mean that the instance satisfies the conditions of the rule. We also say that a decision tree generalizes the data as a tree is a small- er (more compact) description of the data, i.e., it captures the key regulari- ties in the data. Then, the problem becomes building the best tree that is small and accurate. It turns out that finding the best tree that models the data is a NP-complete problem [26]. All existing algorithms use heuristic methods for tree building. Below, we study one of the most successful techniques. 70 3 Supervised Learning 3.2.1 Learning Algorithm As indicated earlier, a decision tree T simply partitions the training data set D into disjoint subsets so that each subset is as pure as possible (of the same class). The learning of a tree is typically done using the divide-and- conquer strategy that recursively partitions the data to produce the tree. At the beginning, all the examples are at the root. As the tree grows, the ex- amples are sub-divided recursively. A decision tree learning algorithm is given in Fig. 3.4. For now, we assume that every attribute in D takes dis- crete values. This assumption is not necessary as we will see later. The stopping criteria of the recursion are in lines 1–4 in Fig. 3.4. The algorithm stops when all the training examples in the current data are of the same class, or when every attribute has been used along the current tree Algorithm decisionTree(D, A, T) 1 if D contains only training examples of the same class cj  C then 2 make T a leaf node labeled with class cj; 3 elseif A =  then 4 make T a leaf node labeled with cj, which is the most frequent class in D 5 else // D contains examples belonging to a mixture of classes. We select a single 6 // attribute to partition D into subsets so that each subset is purer 7 p0 = impurityEval-1(D); 8 for each attribute Ai  A (={A1, A2, …, Ak}) do 9 pi = impurityEval-2(Ai, D) 10 endfor 11 Select Ag  {A1, A2, …, Ak} that gives the biggest impurity reduction, computed using p0 – pi; 12 if p0 – pg < threshold then // Ag does not significantly reduce impurity p0 13 make T a leaf node labeled with cj, the most frequent class in D. 14 else // Ag is able to reduce impurity p0 15 Make T a decision node on Ag; 16 Let the possible values of Ag be v1, v2, …, vm. Partition D into m disjoint subsets D1, D2, …, Dm based on the m values of Ag. 17 for each Dj in {D1, D2, …, Dm} do 18 if Dj   then 19 create a branch (edge) node Tj for vj as a child node of T; 20 decisionTree(Dj, A{Ag}, Tj) // Ag is removed 21 endif 22 endfor 23 endif 24 endif Fig. 3.4. A decision tree learning algorithm 3.2 Decision Tree Induction 71 path. In tree learning, each successive recursion chooses the best attribute to partition the data at the current node according to the values of the at- tribute. The best attribute is selected based on a function that aims to mi- nimize the impurity after the partitioning (lines 7–11). In other words, it maximizes the purity. The key in decision tree learning is thus the choice of the impurity function, which is used in lines 7, 9 and 11 in Fig. 3.4. The recursive recall of the algorithm is in line 20, which takes the subset of training examples at the node for further partitioning to extend the tree. This is a greedy algorithm with no backtracking. Once a node is created, it will not be revised or revisited no matter what happens subsequently. 3.2.2 Impurity Function Before presenting the impurity function, we use an example to show what the impurity function aims to do intuitively. Example 5: Fig. 3.5 shows two possible root nodes for the data in Table 3.1. Fig. 3.5. Two possible root nodes or two possible attributes for the root node Fig. 3.5(A) uses Age as the root node, and Fig. 3.5(B) uses Own_house as the root node. Their possible values (or outcomes) are the branches. At each branch, we listed the number of training examples of each class (No or Yes) that land or reach there. Fig. 3.5(B) is obviously a better choice for the root. From a prediction or classification point of view, Fig. 3.5(B) makes fewer mistakes than Fig. 3.5(A). In Fig. 3.5(B), when Own_house = true every example has the class Yes. When Own_house = false, if we take majority class (the most frequent class), which is No, we make three mis- takes/errors. If we look at Fig. 3.5(A), the situation is worse. If we take the majority class for each branch, we make five mistakes (marked in bold). Thus, we say that the impurity of the tree in Fig. 3.5(A) is higher than the tree in Fig. 3.5(B). To learn a decision tree, we prefer Own_house to Age to be the root node. Instead of counting the number of mistakes or errors, C4.5 uses a more principled approach to perform this evaluation on every attribute in order to choose the best attribute to build the tree. ▀ No: 0 No: 6 Yes: 6 Yes: 3 (B) Own_house? true false No: 3 No: 2 No: 1 Yes: 2 Yes: 3 Yes: 4 (A) Age? Young middle old 72 3 Supervised Learning The most popular impurity functions used for decision tree learning are information gain and information gain ratio, which are used in C4.5 as two options. Let us first discuss information gain, which can be extended slightly to produce information gain ratio. The information gain measure is based on the entropy function from in- formation theory [55]: ,1)Pr( )Pr(log)Pr()( || 1 || 1 2       C j j j C j j c ccDentropy (2) where Pr(cj) is the probability of class cj in data set D, which is the number of examples of class cj in D divided by the total number of examples in D. In the entropy computation, we define 0log0 = 0. The unit of entropy is bit. Let us use an example to get a feeling of what this function does. Example 6: Assume we have a data set D with only two classes, positive and negative. Let us see the entropy values for three different compositions of positive and negative examples: 1. The data set D has 50% positive examples (Pr(positive) = 0.5) and 50% negative examples (Pr(negative) = 0.5). .15.0log5.05.0log5.0)( 22 Dentropy 2. The data set D has 20% positive examples (Pr(positive) = 0.2) and 80% negative examples (Pr(negative) = 0.8). .722.08.0log8.02.0log2.0)( 22 Dentropy 3. The data set D has 100% positive examples (Pr(positive) = 1) and no negative examples, (Pr(negative) = 0). .00log01log1)( 22 Dentropy We can see a trend: When the data becomes purer and purer, the entropy value becomes smaller and smaller. In fact, it can be shown that for this binary case (two classes), when Pr(positive) = 0.5 and Pr(negative) = 0.5 the entropy has the maximum value, i.e., 1 bit. When all the data in D be- long to one class the entropy has the minimum value, 0 bit. ▀ It is clear that the entropy measures the amount of impurity or disorder in the data. That is exactly what we need in decision tree learning. We now describe the information gain measure, which uses the entropy function. 3.2 Decision Tree Induction 73 Information Gain The idea is the following: 1. Given a data set D, we first use the entropy function (Equation 2) to compute the impurity value of D, which is entropy(D). The impuri- tyEval-1 function in line 7 of Fig. 3.4 performs this task. 2. Then, we want to know which attribute can reduce the impurity most if it is used to partition D. To find out, every attribute is evaluated (lines 8–10 in Fig. 3.4). Let the number of possible values of the attribute Ai be v. If we are going to use Ai to partition the data D, we will divide D into v disjoint subsets D1, D2, …, Dv. The entropy after the partition is .)(|| ||)( 1    v j j j A DentropyD DDentropy i (3) The impurityEval-2 function in line 9 of Fig. 3.4 performs this task. 3. The information gain of attribute Ai is computed with: ).()(),( DentropyDentropyADgain iAi  (4) Clearly, the gain criterion measures the reduction in impurity or disorder. The gain measure is used in line 11 of Fig. 3.4, which chooses attribute Ag resulting in the largest reduction in impurity. If the gain of Ag is too small, the algorithm stops for the branch (line 12). Normally a threshold is used here. If choosing Ag is able to reduce impurity significantly, Ag is em- ployed to partition the data to extend the tree further, and so on (lines 15– 21 in Fig. 3.4). The process goes on recursively by building sub-trees using D1, D2, …, Dm (line 20). For subsequent tree extensions, we do not need Ag any more, as all training examples in each branch has the same Ag value. Example 7: Let us compute the gain values for attributes Age, Own_house and Credit_Rating using the whole data set D in Table 3.1, i.e., we evaluate for the root node of a decision tree. First, we compute the entropy of D. Since D has 6 No class training ex- amples, and 9 Yes class training examples, we have .971.015 9log15 9 15 6log15 6)( 22 Dentropy We then try Age, which partitions the data into 3 subsets (as Age has three possible values) D1 (with Age=young), D2 (with Age=middle), and D3 (with Age=old). Each subset has five training examples. In Fig. 3.5, we al- so see the number of No class examples and the number of Yes examples in each subset (or in each branch). 74 3 Supervised Learning .888.0722.015 5971.015 5971.015 5 )(15 5)(15 5)(15 5)( 321   DentropyDentropyDentropyDentropy Age Likewise, we compute for Own_house, which partitions D into two sub- sets, D1 (with Own_house=true) and D2 (with Own_house=false). .551.0 918.015 9015 6 )(15 9)(15 6)( 21_   DentropyDentropyDentropy houseOwn Similarly, we obtain entropyHas_job(D) = 0.647, and entropyCredit_rating(D) = 0.608. The gains for the attributes are: gain(D, Age) = 0.971  0.888 = 0.083 gain(D, Own_house) = 0.971  0.551 = 0.420 gain(D, Has_job) = 0.971  0.647 = 0.324 gain(D, Credit_rating) = 0.971  0.608 = 0.363. Own_house is the best attribute for the root node. Fig. 3.5(B) shows the root node using Own_house. Since the left branch has only one class (Yes) of data, it results in a leaf node (line 1 in Fig. 3.4). For Own_house = false, further extension is needed. The process is the same as above, but we only use the subset of the data with Own_house = false, i.e., D2. ▀ Information Gain Ratio The gain criterion tends to favor attributes with many possible values. An extreme situation is that the data contain an ID attribute that is an identifi- cation of each example. If we consider using this ID attribute to partition the data, each training example will form a subset and has only one class, which results in entropyID(D) = 0. So the gain by using this attribute is maximal. From a prediction point of review, such a partition is useless. Gain ratio (Equation 5) remedies this bias by normalizing the gain us- ing the entropy of the data with respect to the values of the attribute. Our previous entropy computations are done with respect to the class attribute:           s j jj i i D D D D ADgainADgainRatio 1 || ||log|| || ),(),( 2 (5) where s is the number of possible values of Ai, and Dj is the subset of data 3.2 Decision Tree Induction 75 that has the jth value of Ai. |Dj|/|D| corresponds to the probability of Equa- tion (2). Using Equation (5), we simply choose the attribute with the high- est gainRatio value to extend the tree. This method works because if Ai has too many values the denominator will be large. For instance, in our above example of the ID attribute, the denominator will be log2|D|. The denominator is called the split info in C4.5. One note is that the split info can be 0 or very small. Some heuristic solutions can be devised to deal with it (see [49]). 3.2.3 Handling of Continuous Attributes It seems that the decision tree algorithm can only handle discrete attrib- utes. In fact, continuous attributes can be dealt with easily as well. In a real life data set, there are often both discrete attributes and continuous attrib- utes. Handling both types in an algorithm is an important advantage. To apply the decision tree building method, we can divide the value range of attribute Ai into intervals at a particular tree node. Each interval can then be considered a discrete value. Based on the intervals, gain or gainRatio is evaluated in the same way as in the discrete case. Clearly, we can divide Ai into any number of intervals at a tree node. However, two in- tervals are usually sufficient. This binary split is used in C4.5. We need to find a threshold value for the division. Clearly, we should choose the threshold that maximizes the gain (or gainRatio). We need to examine all possible thresholds. This is not a prob- lem because although for a continuous attribute Ai the number of possible values that it can take is infinite, the number of actual values that appear in the data is always finite. Let the set of distinctive values of attribute Ai that occur in the data be {v1, v2, …, vr}, which are sorted in ascending order. Clearly, any threshold value lying between vi and vi+1 will have the same effect of dividing the training examples into those whose value of attribute Ai lies in {v1, v2, …, vi} and those whose value lies in {vi+1, vi+2, …, vr}. There are thus only r1 possible splits on Ai, which can all be evaluated. The threshold value can be the middle point between vi and vi+1, or just on the “right side” of value vi, which results in two intervals Ai  vi and Ai > vi. This latter approach is used in C4.5. The advantage of this approach is that the values appearing in the tree actually occur in the data. The thresh- old value that maximizes the gain (gainRatio) value is selected. We can modify the algorithm in Fig. 3.4 (lines 8–11) easily to accommodate this computation so that both discrete and continuous attributes are considered. A change to line 20 of the algorithm in Fig. 3.4 is also needed. For a continuous attribute, we do not remove attribute Ag because an interval can 76 3 Supervised Learning be further split recursively in subsequent tree extensions. Thus, the same continuous attribute may appear multiple times in a tree path (see Example 9), which does not happen for a discrete attribute. From a geometric point of view, a decision tree built with only continu- ous attributes represents a partitioning of the data space. A series of splits from the root node to a leaf node represents a hyper-rectangle. Each side of the hyper-rectangle is an axis-parallel hyperplane. Example 8: The hyper-rectangular regions in Fig. 3.6(A), which partitions the space, are produced by the decision tree in Fig. 3.6(B). There are two classes in the data, represented by empty circles and filled rectangles. ▀ Fig. 3.6. A partitioning of the data space and its corresponding decision tree Handling of continuous (numeric) attributes has an impact on the effi- ciency of the decision tree algorithm. With only discrete attributes the al- gorithm grows linearly with the size of the data set D. However, sorting of a continuous attribute takes |D|log|D| time, which can dominate the tree learning process. Sorting is important as it ensures that gain or gainRatio can be computed in one pass of the data. 3.2.4 Some Other Issues We now discuss several other issues in decision tree learning. Tree Pruning and Overfitting: A decision tree algorithm recursively par- titions the data until there is no impurity or there is no attribute left. This process may result in trees that are very deep and many tree leaves may cover very few training examples. If we use such a tree to predict the train- ing set, the accuracy will be very high. However, when it is used to clas- sify unseen test set, the accuracy may be very low. The learning is thus not effective, i.e., the decision tree does not generalize the data well. This X Y 2.6 2.5 2 0 2 3 4 X Y  2 > 2 Y  2 > 2 X  3 > 3 X Y  4 > 4  2.5 > 2.5  2.6 > 2.6 (A) A partition of the data space (B). The decision tree 3.2 Decision Tree Induction 77 phenomenon is called overfitting. More specifically, we say that a classi- fier f1 overfits the data if there is another classifier f2 such that f1 achieves a higher accuracy on the training data than f2, but a lower accuracy on the unseen test data than f2 [45]. Overfitting is usually caused by noise in the data, i.e., wrong class val- ues/labels and/or wrong values of attributes, but it may also be due to the complexity and randomness of the application domain. These problems cause the decision tree algorithm to refine the tree by extending it to very deep using many attributes. To reduce overfitting in the context of decision tree learning, we per- form pruning of the tree, i.e., to delete some branches or sub-trees and re- place them with leaves of majority classes. There are two main methods to do this, stopping early in tree building (which is also called pre-pruning) and pruning the tree after it is built (which is called post-pruning). Post- pruning has been shown more effective. Early-stopping can be dangerous because it is not clear what will happen if the tree is extended further (without stopping). Post-pruning is more effective because after we have extended the tree to the fullest, it becomes clearer which branches/sub- trees may not be useful (overfit the data). The general idea of post-pruning is to estimate the error of each tree node. If the estimated error for a node is less than the estimated error of its extended sub-tree, then the sub-tree is pruned. Most existing tree learning algorithms take this approach. See [49] for a technique called the pessimistic error based pruning. Example 9: In Fig. 3.6(B), the sub-tree representing the rectangular region X  2, Y > 2.5, Y  2.6 in Fig. 3.6(A) is very likely to be overfitting. The region is very small and contains only a single data point, which may be an error (or noise) in the data collection. If it is pruned, we obtain Fig. 3.7(A) and (B). ▀ Fig. 3.7. The data space partition and the decision tree after pruning X Y 2.6 2.5 2 0 2 3 4 X Y  2 > 2  2 > 2 X  3 > 3 X  4 > 4 (A) A partition of the data space (B). The decision tree 78 3 Supervised Learning Another common approach to pruning is to use a separate set of data called the validation set, which is not used in training and neither in test- ing. After a tree is built, it is used to classify the validation set. Then, we can find the errors at each node on the validation set. This enables us to know what to prune based on the errors at each node. Rule Pruning: We noted earlier that a decision tree can be converted to a set of rules. In fact, C4.5 also prunes the rules to simplify them and to re- duce overfitting. First, the tree (C4.5 uses the unpruned tree) is converted to a set of rules in the way discussed in Example 4. Rule pruning is then performed by removing some conditions to make the rules shorter and fewer (after pruning some rules may become redundant). In most cases, pruning results in a more accurate rule set as shorter rules are less likely to overfit the training data. Pruning is also called generalization as it makes rules more general (with fewer conditions). A rule with more conditions is more specific than a rule with fewer conditions. Example 10: The sub-tree below X  2 in Fig. 3.6(B) produces these rules: Rule 1: X  2, Y > 2.5, Y > 2.6  Rule 2: X  2, Y > 2.5, Y  2.6  O Rule 3: X  2, Y  2.5  Note that Y > 2.5 in Rule 1 is not useful because of Y > 2.6, and thus Rule 1 should be Rule 1: X  2, Y > 2.6  In pruning, we may be able to delete the conditions Y > 2.6 from Rule 1 to produce: X  2  Then Rule 2 and Rule 3 become redundant and can be removed. ▀ A useful point to note is that after pruning the resulting set of rules may no longer be mutually exclusive and exhaustive. There may be data points that satisfy the conditions of more than one rule, and if inaccurate rules are discarded, of no rules. An ordering of the rules is thus needed to ensure that when classifying a test case only one rule will be applied to de- termine the class of the test case. To deal with the situation that a test case does not satisfy the conditions of any rule, a default class is used, which is usually the majority class. Handling Missing Attribute Values: In many practical data sets, some at- tribute values are missing or not available due to various reasons. There are many ways to deal with the problem. For example, we can fill each 3.3 Classifier Evaluation 79 missing value with the special value “unknown” or the most frequent value of the attribute if the attribute is discrete. If the attribute is continuous, use the mean of the attribute for each missing value. The decision tree algorithm in C4.5 takes another approach. At a tree node, it distributes the training example with missing value for the attrib- ute to each branch of the tree proportionally according to the distribution of the training examples that have values for the attribute. Handling Skewed Class Distribution: In many applications, the propor- tions of data for different classes can be very different. For instance, in a data set of intrusion detection in computer networks, the proportion of in- trusion cases is extremely small (< 1%) compared with normal cases. Di- rectly applying the decision tree algorithm for classification or prediction of intrusions is usually not effective. The resulting decision tree often con- sists of a single leaf node “normal”, which is useless for intrusion detec- tion. One way to deal with the problem is to over sample the intrusion ex- amples to increase its proportion. Another solution is to rank the new cases according to how likely they may be intrusions. The human users can then investigate the top ranked cases. 3.3 Classifier Evaluation After a classifier is constructed, it needs to be evaluated for accuracy. Ef- fective evaluation is crucial because without knowing the approximate ac- curacy of a classifier, it cannot be used in real-world tasks. There are many ways to evaluate a classifier, and there are also many measures. The main measure is the classification accuracy (Equation 1), which is the number of correctly classified instances in the test set divided by the total number of instances in the test set. Some researchers also use the error rate, which is 1 – accuracy. Clearly, if we have several classifi- ers, the one with the highest accuracy is preferred. Statistical significance tests may be used to check whether one classifier’s accuracy is signifi- cantly better than that of another given the same training and test data sets. Below, we first present several common methods for classifier evaluation, and then introduce some other evaluation measures. 3.3.1 Evaluation Methods Holdout Set: The available data D is divided into two disjoint subsets, the training set Dtrain and the test set Dtest, D = Dtrain  Dtest and Dtrain  Dtest = . The test set is also called the holdout set. This method is mainly used 80 3 Supervised Learning when the data set D is large. Note that the examples in the original data set D are all labeled with classes. As we discussed earlier, the training set is used for learning a classifier and the test set is used for evaluating the classifier. The training set should not be used in the evaluation as the classifier is biased toward the training set. That is, the classifier may overfit the training data, which results in very high accuracy on the training set but low accuracy on the test set. Us- ing the unseen test set gives an unbiased estimate of the classification ac- curacy. As for what percentage of the data should be used for training and what percentage for testing, it depends on the data set size. 50–50 and two thirds for training and one third for testing are commonly used. To partition D into training and test sets, we can use a few approaches: 1. We randomly sample a set of training examples from D for learning and use the rest for testing. 2. If the data is collected over time, then we can use the earlier part of the data for training/learning and the later part of the data for testing. In many applications, this is a more suitable approach because when the classifier is used in the real-world the data are from the future. This ap- proach thus better reflects the dynamic aspects of applications. Multiple Random Sampling: When the available data set is small, using the above methods can be unreliable because the test set would be too small to be representative. One approach to deal with the problem is to perform the above random sampling n times. Each time a different training set and a different test set are produced. This produces n accuracies. The final estimated accuracy on the data is the average of the n accuracies. Cross-Validation: When the data set is small, the n-fold cross-validation method is very commonly used. In this method, the available data is parti- tioned into n equal-size disjoint subsets. Each subset is then used as the test set and the remaining n1 subsets are combined as the training set to learn a classifier. This procedure is then run n times, which gives n accura- cies. The final estimated accuracy of learning from this data set is the aver- age of the n accuracies. 10-fold and 5-fold cross-validations are often used. A special case of cross-validation is the leave-one-out cross-validation. In this method, each fold of the cross validation has only a single test ex- ample and all the rest of the data is used in training. That is, if the original data has m examples, then this is m-fold cross-validation. This method is normally used when the available data is very small. It is not efficient for a large data set as m classifiers need to be built. In Sect. 3.2.4, we mentioned that a validation set can be used to prune a decision tree or a set of rules. If a validation set is employed for that pur- 3.3 Classifier Evaluation 81 pose, it should not be used in testing. In that case, the available data is di- vided into three subsets, a training set, a validation set and a test set. Apart from using a validation set to help tree or rule pruning, a validation set is also used frequently to estimate parameters in learning algorithms. In such cases, the values that give the best accuracy on the validation set are used as the final values of the parameters. Cross-validation can be used for pa- rameter estimating as well. Then a separate validation set is not needed. Instead, the whole training set is used in cross-validation. 3.3.2 Precision, Recall, F-score and Breakeven Point In some applications, we are only interested in one class. This is particu- larly true for text and Web applications. For example, we may be inter- ested in only the documents or web pages of a particular topic. Also, in classification involving skewed or highly imbalanced data, e.g., network intrusion and financial fraud detection, we are typically interested in only the minority class. The class that the user is interested in is commonly called the positive class, and the rest negative classes (the negative classes may be combined into one negative class). Accuracy is not a suitable measure in such cases because we may achieve a very high accuracy, but may not identify a single intrusion. For instance, 99% of the cases are normal in an intrusion detection data set. Then a classifier can achieve 99% accuracy (without doing anything) by simply classifying every test case as “not intrusion”. This is, however, useless. Precision and recall are more suitable in such applications because they measure how precise and how complete the classification is on the positive class. It is convenient to introduce these measures using a confusion ma- trix (Table 3.2). A confusion matrix contains information about actual and predicted results given by a classifier. Table 3.2. Confusion matrix of a classifier Classified positive Classified negative Actual positive TP FN Actual negative FP TN where TP: the number of correct classifications of the positive examples (true positive) FN: the number of incorrect classifications of positive examples (false negative) FP: the number of incorrect classifications of negative examples (false positive) TN: the number of correct classifications of negative examples (true negative) Based on the confusion matrix, the precision (p) and recall (r) of the posi- tive class are defined as follows: 82 3 Supervised Learning . . FNTP TP rFPTP TPp  (6) In words, precision p is the number of correctly classified positive ex- amples divided by the total number of examples that are classified as posi- tive. Recall r is the number of correctly classified positive examples di- vided by the total number of actual positive examples in the test set. The intuitive meanings of these two measures are quite obvious. However, it is hard to compare classifiers based on two measures, which are not functionally related. For a test set, the precision may be very high but the recall can be very low, and vice versa. Example 11: A test data set has 100 positive examples and 1000 negative examples. After classification using a classifier, we have the following confusion matrix (Table 3.3), Table 3.3. Confusion matrix of a classifier Classified positive Classified negative Actual positive 1 99 Actual negative 0 1000 This confusion matrix gives the precision p = 100% and the recall r = 1% because we only classified one positive example correctly and classified no negative examples wrongly. ▀ Although in theory precision and recall are not related, in practice high precision is achieved almost always at the expense of recall and high recall is achieved at the expense of precision. In an application, which measure is more important depends on the nature of the application. If we need a sin- gle measure to compare different classifiers, the F-score is often used: .2 rp prF  (7) The F-score (also called the F1-score) is the harmonic mean of precision and recall. .11 2 rp F   (8) The harmonic mean of two numbers tends to be closer to the smaller of the two. Thus, for the F-score to be high, both p and r must be high. There is also another measure, called precision and recall breakeven point, which is used in the information retrieval community. The break- 3.3 Classifier Evaluation 83 even point is when the precision and the recall are equal. This measure as- sumes that the test cases can be ranked by the classifier based on their like- lihoods of being positive. For instance, in decision tree classification, we can use the confidence of each leaf node as the value to rank test cases. Example 12: We have the following ranking of 20 test documents. 1 represents the highest rank and 20 represents the lowest rank. “+” (“”) represents an actual positive (negative) document. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + + +  +  +  + +   +    +   + Assume that the test set has 10 positive examples. At rank 1: p = 1/1 = 100% r = 1/10 = 10% At rank 2: p = 2/2 = 100% r = 2/10 = 20% … … … At rank 9: p = 6/9 = 66.7% r = 6/10 = 60% At rank 10: p = 7/10 = 70% r = 7/10 = 70% The breakeven point is p = r = 70%. Note that interpolation is needed if such a point cannot be found. ▀ 3.3.3 Receiver Operating Characteristic Curve A receiver operating characteristic (ROC) curve is a plot of the true posi- tive rate against the false positive rate. It is also commonly used to evalu- ate classification results on the positive class in two-class classification problems. The classifier needs to rank the test cases according to their like- lihoods of belonging to the positive class with the most likely positive case ranked at the top. The true positive rate (TPR) is defined as the fraction of actual positive cases that are correctly classified, .FNTP TPTPR  (9) The false positive rate (FPR) is defined as the fraction of actual negative cases that are classified to the positive class, .FPTN FPFPR  (10) TPR is basically the recall of the positive class and is also called sensitiv- ity in statistics. There is also another measure in statistics called specific- ity, which is the true negative rate (TNR), or the recall of the negative class. TNR is defined as follows: 84 3 Supervised Learning .FPTN TNTNR  (11) From Equations (10) and (11), we can see the following relationship, .1 yspecificitFPR  (12) Fig. 3.8 shows the ROC curves of two example classifiers (C1 and C2) on the same test data. Each curve starts from (0, 0) and ends at (1, 1). (0, 0) represents the situation where every test case is classified as negative, and (1, 1) represents the situation where every test case is classified as positive. This is the case because we can treat the classification result as a ranking of the test cases in the positive class, and we can partition the ranked list at any point into two parts with the upper part assigned to the positive class and the lower part assigned to the negative class. We will see shortly that an ROC curve is drawn based on such partitions. In Fig. 3.8, we also see the main diagonal line, which represents random guessing, i.e., predicting each case to be positive with a fixed probability. In this case, it is clear that for every FPR value, TPR has the same value, i.e., TPR = FPT. For classifier evaluation using the ROC curves in Fig. 3.8, we want to know which classifier is better. The answer is that when FPR is less than 0.43, C1 is better, and when FPR is greater than 0.43, C2 is better. However, sometimes this is not a satisfactory answer because we cannot say any one of the classifiers is strictly better than the other. For an overall comparison, researchers often use the area under the ROC curve (AUC). If the AUC value for a classifier Ci is greater than that of another classifier Cj, it is said that Ci is better than Cj. If a classifier is perfect, its AUC value is 1. If a classifier makes all random guesses, its AUC value is 0.5. C2 C1 Fig. 3.8. ROC curves for two classifiers (C1 and C2) on the same data 3.3 Classifier Evaluation 85 Let us now describe how to draw an ROC curve given the classification result as a ranking of test cases. The ranking is obtained by sorting the test cases in decreasing order of the classifier’s output values (e.g., posterior probabilities). We then partition the rank list into two subsets (or parts) at every test case and regard every test case in the upper part (with higher classifier output value) as a positive case and every test case in the lower part as a negative case. For each such partition, we compute a pair of TPR and FPR values. When the upper part is empty, we obtain the point (0, 0) on the ROC and when the lower part is empty, we obtain the point (1, 1). Finally, we simply connect the adjacent points. Example 13: We have 10 test cases. A classifier has been built, and it has ranked the 10 test cases as shown in the second row of Table 3.4 (the num- bers in row 1 are the rank positions, with 1 being the highest rank and 10 the lowest). The second row shows the actual class of each test case. “+” means that the test case is from the positive class, and “–” means that it is from the negative class. All the results needed for drawing the ROC curve are shown in rows 3–8 in Table 3.4. The ROC curve is given in Fig. 3.9. Table 3.4. Computations for drawing an ROC curve Rank 1 2 3 4 5 6 7 8 9 10 Actual class + + – – + – – + – – TP 0 1 2 2 2 3 3 3 4 4 4 FP 0 0 0 1 2 2 3 4 4 5 6 TN 6 6 6 5 4 4 3 2 2 1 0 FN 4 3 2 2 2 1 1 1 0 0 0 TPR 0 0.25 0.5 0.5 0.5 0.75 0.75 0.75 1 1 1 FPR 0 0 0 0.17 0.33 0.33 0.50 0.67 0.67 0.83 1 Fig. 3.9. ROC curve for the data shown in Table 3.4 ▀ 86 3 Supervised Learning 3.3.4 Lift Curve The lift curve (also called the lift chart) is similar to the ROC curve. It is also for evaluation of two-class classification tasks, where the positive class is the target of interest and usually the rare class. It is often used in direct marketing applications to link classification results to costs and prof- its. For example, a mail order company wants to send promotional materi- als to potential customers to sell an expensive watch. Since printing and postage cost money, the company needs to build a classifier to identify likely buyers, and only sends the promotional materials to them. The ques- tion is how many should be sent. To make the decision, the company needs to balance the cost and profit (if a watch is sold, the company makes a cer- tain profit, but to send each letter there is a fixed cost). The lift curve pro- vides a nice tool to enable the marketer to make the decision. Like an ROC curve, to draw a lift curve, the classifier needs to produce a ranking of the test cases according to their likelihoods of belonging to the positive class with the most likely positive case ranked at the top. After the ranking, the test cases are divided into N equal-sized bins (N is usually 10 – 20). The actual positive cases in each bin are then counted. A lift curve is drawn with the x-axis being the percentages of test data (or bins) and the y- axis being the percentages of cumulative positive cases from the first bin to the current bin. A lift curve usually also includes a line (called the base- line) along the main diagonal [from (0, 0) to (100, 100)] which represents the situation where the positive cases in the test set are uniformly (or ran- domly) distributed in the N bins (no learning), i.e., each bin contains 100/N percent of the positive cases. If the lift curve is above this baseline, learn- ing is said to be effective. The greater the area between the lift curve and the baseline, the better the classifier. Example 14: A company wants to send promotional materials to potential buyers to sell an expensive brand of watches. It builds a classification model and tests it on a test data of 10,000 people (test cases) that they col- lected in the past. After classification and ranking, it decides to divide the test data into 10 bins with each bin containing 10% of the test cases or 1,000 cases. Out of the 1,000 cases in each bin, there are a certain number of positive cases (e.g., past buyers). The detailed results are listed in Table 3.5, which includes the number (#) of positive cases and the percentage (%) of positive cases in each bin, and the cumulative percentage for that bin. The cumulative percentages are used in drawing the lift curve which is given in Fig. 3.10. We can see that the lift curve is way above the baseline, which means that the learning is highly effective. Suppose printing and postage cost $1.00 for each letter, and the sale of each watch makes $100 (assuming that each buyer only buys one watch). 3.4 Rule Induction 87 If the company wants to send promotional letters to 3000 people, it will make $36,000, i.e., $100 × (210 + 120 + 60)  $3,000 = $36,000 Table 3.5. Classification results for the 10 bins Bin 1 2 3 4 5 6 7 8 9 10 # of test cases 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 # of positive cases 210 120 60 40 22 18 12 7 6 5 % of positive cases 42.0% 24.0% 12% 8% 4.4% 3.6% 2.4% 1.4% 1.2% 1.0% % cumulative 42.0% 66.0% 78.0% 86.0% 90.4% 94.0% 96.4% 97.8% 99.0% 100.0% Fig. 3.10. Lift curve for the data shown in Table 3.5 ▀ 3.4 Rule Induction In Sect. 3.2, we showed that a decision tree can be converted to a set of rules. Clearly, the set of rules can be used for classification as the tree. A natural question is whether it is possible to learn classification rules di- rectly. The answer is yes. The process of learning such rules is called rule induction or rule learning. We study two approaches in the section. 3.4.1 Sequential Covering Most rule induction systems use a learning strategy called sequential cov- ering. A rule-based classifier built with this strategy typically consists of a list of rules, which is also called a decision list [51]. In the list, the order- ing of the rules is significant. 88 3 Supervised Learning The basic idea of sequential covering is to learn a list of rules sequen- tially, one at a time, to cover the training data. After each rule is learned, the training examples covered by the rule are removed. Only the remaining data are used to find subsequent rules. Recall that a rule covers an example if the example satisfies the conditions of the rule. We study two specific algorithms based on this general strategy. The first algorithm is based on the CN2 system [9], and the second algorithm is based on the ideas in FOIL [50], I-REP [21], REP [7], and RIPPER [106] systems. Many ideas are also taken from [45]. Algorithm 1 (Ordered Rules) This algorithm learns each rule without pre-fixing a class. That is, in each iteration, a rule of any class may be found. Thus rules of different classes may intermix in the final rule list. The ordering of rules is important. This algorithm is given in Fig. 3.11. D is the training data. RuleList is the list of rules, which is initialized to empty set (line 1). Rule is the best rule found in each iteration. The function learn-one-rule-1() learns the Rule (lines 2 and 6). The stopping criteria for the while-loop can be of various kinds. Here we use D =  or Rule is NULL (a rule is not learned). Once a rule is learned from the data, it is inserted into RuleList at the end (line 4). All the training examples that are covered by the rule are removed from the data (line 5). The remaining data is used to find the next rule and so on. After rule learning ends, a default class is inserted at the end of RuleList. This is because there may still be some training examples that are not cov- ered by any rule as no good rule can be found from them, or because some test cases may not be covered by any rule and thus cannot be classified. The final list of rules is as follows: (13) where ri is a rule. Algorithm 2 (Ordered Classes) This algorithm learns all rules for each class together. After rule learning for one class is completed, it moves to the next class. Thus all rules for each class appear together in the rule list. The sequence of rules for each class is unimportant, but the rule subsets for different classes are ordered. Typically, the algorithm finds rules for the least frequent class first, then the second least frequent class and so on. This ensures that some rules are learned for rare classes. Otherwise, they may be dominated by frequent classes and end up with no rules if considered after frequent classes. 3.4 Rule Induction 89 The algorithm is given in Fig. 3.12. The data set D is split into two sub- sets, Pos and Neg, where Pos contains all the examples of class c from D, and Neg the rest of the examples in D (line 3). c is the class that the algo- rithm is working on now. Two stopping conditions for rule learning of each class are in line 4 and line 6. The other parts of the algorithm are quite similar to those of the first algorithm in Fig. 3.11. Both learn-one- rule-1() and learn-one-rule-2() functions are described in Sect. 3.4.2. Use of Rules for Classification To use a list of rules for classification is straightforward. For a test case, we simply try each rule in the list sequentially. The class of the first rule Algorithm sequential-covering-1(D) 1 RuleList  ; 2 Rule  learn-one-rule-1(D); 3 while Rule is not NULL AND D   do 4 RuleList  insert Rule at the end of RuleList; 5 Remove from D the examples covered by Rule; 6 Rule  learn-one-rule-1(D) 7 endwhile 8 insert a default class c at the end of RuleList, where c is the majority class in D; 9 return RuleList Fig. 3.11. The first rule learning algorithm based on sequential covering Algorithm sequential-covering-2(D, C) 1 RuleList  ; // empty rule set at the beginning 2 for each class c  C do 3 prepare data (Pos, Neg), where Pos contains all the examples of class c from D, and Neg contains the rest of the examples in D; 4 while Pos   do 5 Rule  learn-one-rule-2(Pos, Neg, c); 6 if Rule is NULL then 7 exit-while-loop 8 else RuleList  insert Rule at the end of RuleList; 9 Remove examples covered by Rule from (Pos, Neg) 10 endif 11 endwhile 12 endfor 13 return RuleList Fig. 3.12. The second rule learning algorithm based on sequential covering 90 3 Supervised Learning that covers this test case is assigned as the class of the test case. Clearly, if no rule applies to the test case, the default class is used. 3.4.2 Rule Learning: Learn-One-Rule Function We now present the function learn-one-rule(), which works as follows: It starts with an empty set of conditions. In the first iteration, one condition is added. In order to find the best condition to add, all possible conditions are tried, which form candidate rules. A condition is of the form Ai op v, where Ai is an attribute and v is a value of Ai. We also called it an attrib- ute-value pair. For a discrete attribute, op is “=”. For a continuous attrib- ute, op  {>, }. The algorithm evaluates all the candidates to find the best one (the rest are discarded). After the first best condition is added, it tries to add the second condition and so on in the same fashion until some stop- ping condition is satisfied. Note that we omit the rule class here because it is implied, i.e., the majority class of the data covered by the conditions. This is a heuristic and greedy algorithm in that after a condition is add- ed, it will not be changed or removed through backtracking. Ideally, we would want to try all possible combinations of attributes and values. How- ever, this is not practical as the number of possibilities grows exponen- tially. Hence, in practice, the above greedy algorithm is used. However, in- stead of keeping only the best set of conditions, we can improve the function a little by keeping k best sets of conditions (k > 1) in each itera- tion. This is called the beam search (k beams), which ensures that a larger space is explored. Below, we present two specific implementations of the algorithm, namely learn-one-rule-1() and learn-one-rule-2(). learn-one- rule-1() is used in the sequential-covering-1 algorithm, and learn-one-rule- 2() is used in the sequential-covering-2 algorithm. Learn-One-Rule-1 This function uses beam search (Fig. 3.13). The number of beams is k. BestCond stores the conditions of the rule to be returned. The class is omit- ted as it is the majority class of the data covered by BestCond. candidate- CondSet stores the current best condition sets (which are the frontier beams) and its size is less than or equal to k. Each condition set contains a set of conditions connected by “and” (conjunction). newCandidateCondSet stores all the new candidate condition sets after adding each attribute-value pair (a possible condition) to every candidate in candidateCondSet (lines 5–11). Lines 13–17 update the BestCond. Specifically, an evaluation func- tion is used to assess whether each new candidate condition set is better than the existing best condition set BestCond (line 14). If so, it replaces the 3.4 Rule Induction 91 current BestCond (line 15). Line 18 updates candidateCondSet, which se- lects k new best condition sets (new beams). Once the final BestCond is found, it is evaluated to see if it is signifi- cantly better than without any condition () using a threshold (line 20). If yes, a rule will be formed using BestCond and the most frequent (or the majority) class of the data covered by BestCond (line 21). If not, NULL is returned to indicate that no significant rule is found. Function learn-one-rule-1(D) 1 BestCond  ; // rule with no condition. 2 candidateCondSet  {BestCond}; 3 attributeValuePairs  the set of all attribute-value pairs in D of the form (Ai op v), where Ai is an attribute and v is a value or an interval; 4 while candidateCondSet   do 5 newCandidateCondSet  ; 6 for each candidate cond in candidateCondSet do 7 for each attribute-value pair a in attributeValuePairs do 8 newCond  cond  {a}; 9 newCandidateCondSet  newCandidateCondSet  {newCond} 10 endfor 11 endfor 12 remove duplicates and inconsistencies, e.g., {Ai = v1, Ai = v2}; 13 for each candidate newCond in newCandidateCondSet do 14 if evaluation(newCond, D) > evaluation(BestCond, D) then 15 BestCond  newCond 16 endif 17 endfor 18 candidateCondSet  the k best members of newCandidateCondSet according to the results of the evaluation function; 19 endwhile 20 if evaluation(BestCond, D) – evaluation(, D) > threshold then 21 return the rule: “BestCond  c” where is c the majority class of the data covered by BestCond 22 else return NULL 23 endif Fig. 3.13. The learn-one-rule-1 function Function evaluation(BestCond, D) 1 D  the subset of training examples in D covered by BestCond; 2   || 1 2 )Pr(log)Pr()'(C j jj ccDentropy ; 3 return – entropy(D’) // since entropy measures impurity. Fig. 3.14. The entropy based evaluation function 92 3 Supervised Learning The evaluation() function (Fig. 3.14) uses the entropy function as in the decision tree learning. Other evaluation functions are possible too. Note that when BestCond = , it covers every example in D, i.e., D = D. Learn-One-Rule-2 In the learn-one-rule-2() function (Fig. 3.14), a rule is first generated and then it is pruned. This method starts by splitting the positive and negative training data Pos and Neg, into growing and pruning sets. The growing sets, GrowPos and GrowNeg, are used to generate a rule, called BestRule. The pruning sets, PrunePos and PruneNeg are used to prune the rule be- cause BestRule may overfit the data. Note that PrunePos and PruneNeg are actually validation sets discussed in Sects. 3.2.4 and 3.3.1. growRule() function: growRule() generates a rule (called BestRule) by repeatedly adding a condition to its condition set that maximizes an evaluation function until the rule covers only some positive examples in GrowPos but no negative examples in GrowNeg. This is basically the same as lines 4–17 in Fig. 3.13, but without beam search (i.e., only the best rule is kept in each iteration). Let the current partially developed rule be R: R: av1, .., avk  class where each avj is a condition (an attribute-value pair). By adding a new condition avk+1, we obtain the rule R+: av1, .., avk, avk+1 class. The evalu- ation function for R+ is the following information gain criterion (which is different from the gain function used in decision tree learning):        00 0 11 1 1 22 loglog),( np p np ppRRgain (14) where p0 (respectively, n0) is the number of positive (negative) examples covered by R in Pos (Neg), and p1 (n1) is the number of positive (negative) examples covered by R+ in Pos (Neg). The GrowRule() function simply re- Function learn-one-rule-2(Pos, Neg, class) 1 split (Pos, Neg) into (GrowPos, GrowNeg) and (PrunePos, PruneNeg) 2 BestRule  GrowRule(GrowPos, GrowNeg, class) // grow a new rule 3 BestRule  PruneRule(BestRule, PrunePos, PruneNeg) // prune the rule 4 if the error rate of BestRule on (PrunePos, PruneNeg) exceeds 50% then 5 return NULL 6 endif 7 return BestRule Fig. 3.15. The learn-one-rule-2() function 3.5 Classification Based on Associations 93 turns the rule R+ that maximizes the gain. PruneRule() function: To prune a rule, we consider deleting every subset of conditions from the BestRule, and choose the deletion that maximizes: ,),,( np npPruneNegPrunePosBestRulev   (15) where p (respectively n) is the number of examples in PrunePos (Prune- Neg) covered by the current rule (after a deletion). 3.4.3 Discussion Separate-and-Conquer vs. Divide-and-Conquer: Decision tree learning is said to use the divide-and-conquer strategy. At each step, all attributes are evaluated and one is selected to partition/divide the data into m disjoint subsets, where m is the number of values of the attribute. Rule induction discussed in this section is said to use the separate-and-conquer strategy, which evaluates all attribute-value pairs (conditions) (which are much lar- ger in number than the number of attributes) and selects only one. Thus, each step of divide-and-conquer expands m rules, while each step of sepa- rate-and-conquer expands only one rule. Due to both effects, the separate- and-conquer strategy is much slower than the divide-and-conquer strategy. Rule Understandability: If-then rules are easy to understand by human users. However, a word of caution about rules generated by sequential covering is in order. Such rules can be misleading because the covered da- ta are removed after each rule is generated. Thus the rules in the rule list are not independent of each other. A rule r may be of high quality in the context of the data D from which r was generated. However, it may be a weak rule with a very low accuracy (confidence) in the context of the whole data set D (D  D) because many training examples that can be covered by r have already been removed by rules generated before r. If you want to understand the rules and possibly use them in some real-world tasks, you should be aware of this fact. 3.5 Classification Based on Associations In Sect. 3.2, we showed that a decision tree can be converted to a set of rules, and in Sect. 3.4, we saw that a set of rules may also be found directly for classification. It is thus only natural to expect that association rules, in particular class association rules (CAR), may be used for classification 94 3 Supervised Learning too. Yes, indeed! In fact, normal association rules can be employed for classification as well as we will see in Sect. 3.5.3. CBA, which stands for Classification Based on Associations, is the first reported system that uses association rules for classification [39]. Classifiers built using association rules are often called associative classifiers. In this section, we describe three approaches to employing association rules for classification: 1. Using class association rules for classification directly. 2. Using class association rules as features or attributes. 3. Using normal (or classic) association rules for classification. The first two approaches can be applied to tabular data or transactional data. The last approach is usually employed for transactional data only. All these methods are useful in the Web environment as many types of Web data are in the form of transactions, e.g., search queries issued by users, and Web pages clicked by visitors. Transactional data sets are difficult to handle by traditional classification techniques, but are very natural for as- sociation rules. Below, we describe the three approaches in turn. We should note that various sequential rules can be used for classification in similar ways as well if sequential data sets are involved. 3.5.1 Classification Using Class Association Rules Recall that a class association rule (CAR) is an association rule with only a class label on the right-hand side of the rule as its consequent (Sect. 2.5). For instance, from the data in Table 3.1, the following rule can be found: Own_house = false, Has_job = true  Class = Yes [sup=3/15, conf=3/3], which was also a rule from the decision tree in Fig. 3.3. In fact, there is no difference between rules from a decision tree (or a rule induction system) and CARs if we consider only categorical (or discrete) attributes (more on this later). The differences are in the mining processes and the final rule sets. CAR mining finds all rules in data that satisfy the user-specified min- imum support (minsup) and minimum confidence (minconf) constraints. A decision tree or a rule induction system finds only a subset of the rules (expressed as a tree or a list of rules) for classification. Example 15: Recall that the decision tree in Fig. 3.3 gives the following three rules: Own_house = true  Class =Yes [sup=6/15, conf=6/6] Own_house = false, Has_job = true  Class=Yes [sup=3/15, conf=3/3] Own_house = false, Has_job = false  Class=No [sup=6/15, conf=6/6]. 3.5 Classification Based on Associations 95 However, there are many other rules that exist in data, e.g., Age = young, Has_job = true  Class=Yes [sup=2/15, conf=2/2] Age = young, Has_job = false  Class=No [sup=3/15, conf=3/3] Credit_rating = fair  Class=No [sup=4/15, conf=4/5] and many more, if we use minsup = 2/15 = 13.3% and minconf = 70%. ▀ In many cases, rules that are not in the decision tree (or a rule list) may be able to perform classification more accurately. Empirical comparisons reported by several researchers show that classification using CARs can perform more accurately on many data sets than decision trees and rule in- duction systems (see Bibliographic Notes for references). The complete set of rules from CAR mining is also beneficial from a rule usage point of view. In some applications, the user wants to act on some interesting rules. For example, in an application for finding causes of product problems, more rules are preferred to fewer rules because with more rules, the user is more likely to find rules that indicate causes of the problems. Such rules may not be generated by a decision tree or a rule in- duction system. A deployed data mining system based on CARs is re- ported in [41]. We should, however, also bear in mind of the following: 1. Decision tree learning and rule induction do not use the minsup or min- conf constraint. Thus, some rules that they find can have very low sup- ports, which, of course, are likely to be pruned because the chance that they overfit the training data is high. Although we can set a low minsup for CAR mining, it may cause combinatorial explosion. In practice, in addition to minsup and minconf, a limit on the total number of rules to be generated may be used to further control the CAR generation proc- ess. When the number of generated rules reaches the limit, the algorithm stops. However, with this limit, we may not be able to generate long rules (with many conditions). Recall that the Apriori algorithm works in a level-wise fashion, i.e., short rules are generated before long rules. In some applications, this might not be an issue as short rules are often pre- ferred and are sufficient for classification or for action. Long rules nor- mally have very low supports and tend to overfit the data. However, in some other applications, long rules can be useful. 2. CAR mining does not use continuous (numeric) attributes, while deci- sion trees deal with continuous attributes naturally. Rule induction can use continuous attributes as well. There is still no satisfactory method to deal with such attributes directly in association rule mining. Fortunately, many attribute discretization algorithms exist that can automatically dis- cretize the value range of a continuous attribute into suitable intervals [16, 19], which are then considered as discrete values. 96 3 Supervised Learning Mining Class Association Rules for Classification There are many techniques that use CARs to build classifiers. Before de- scribing them, let us first discuss some issues related to CAR mining for classification. Since a CAR mining algorithm has been discussed in Sect. 2.5, we will not repeat it here. Rule Pruning: CAR rules are highly redundant, and many of them are not statistically significant (which can cause overfitting). Rule pruning is thus needed. The idea of pruning CARs is basically the same as that in decision tree building or rule induction. Thus, we will not discuss it further (see [36, 39] for some of the pruning methods). Multiple Minimum Class Supports: As discussed in Sect. 2.5.3, a single minsup is inadequate for mining CARs because many practical classifica- tion data sets have uneven class distributions, i.e., some classes cover a large proportion of the data, while others cover only a very small propor- tion (which are called rare or infrequent classes). Example 16: Suppose we have a dataset with two classes, Y and N. 99% of the data belong to the Y class, and only 1% of the data belong to the N class. If we set minsup = 1.5%, we will not find any rule for class N. To solve the problem, we need to lower down the minsup. Suppose we set minsup = 0.2%. Then, we may find a huge number of overfitting rules for class Y because minsup = 0.2% is too low for class Y. ▀ Multiple minimum class supports can be applied to deal with the prob- lem. We can assign a different minimum class support minsupi for each class ci, i.e., all the rules of class ci must satisfy minsupi. Alternatively, we can provide one single total minsup, denoted by t_minsup, which is then distributed to each class according to the class distribution: minsupi = t_minsup  sup(ci) (16) where sup(ci) is the support of class ci in training data. The formula gives frequent classes higher minsups and infrequent classes lower minsups. Parameter Selection: The parameters used in CAR mining are the mini- mum supports and the minimum confidences. Note that a different mini- mum confidence may also be used for each class. However, minimum con- fidences do not affect the classification much because classifiers tend to use high confidence rules. One minimum confidence is sufficient as long as it is not set too high. To determine the best minsupi for each class ci, we can try a range of values to build classifiers and then use a validation set to select the final value. Cross-validation may be used as well. 3.5 Classification Based on Associations 97 Data Formats: The algorithm for CAR mining given in Sect. 2.5.2 is for mining transaction data sets. However, many classification data sets are in the table format. As we discussed in Sect. 2.3, a tabular data set can be eas- ily converted to a transaction data set. Classifier Building After all CAR rules are found, a classifier is built using the rules. There are many existing approaches, which can be grouped into three categories. Use the Strongest Rule: This is perhaps the simplest strategy. It simply uses CARs directly for classification. For each test instance, it finds the strongest rule that covers the instance. Recall that a rule covers an instance if the instance satisfies the conditions of the rule. The class of the strongest rule is then assigned as the class of the test instance. The strength of a rule can be measured in various ways, e.g., based on confidence, 2 test, or a combination of both support and confidence values. Select a Subset of the Rules to Build a Classifier: The representative me- thod of this category is the one used in the CBA system. The method is similar to the sequential covering method, but applied to class association rules with additional enhancements as discussed above. Let the set of all discovered CARs be S. Let the training data set be D. The basic idea is to select a subset L ( S) of high confidence rules to cov- er the training data D. The set of selected rules, including a default class, is then used as the classifier. The selection of rules is based on a total order defined on the rules in S. Definition: Given two rules, ri and rj, ri ; rj (also called ri precedes rj or ri has a higher precedence than rj) if 1. the confidence of ri is greater than that of rj, or 2. their confidences are the same, but the support of ri is greater than that of rj, or 3. both the confidences and supports of ri and rj are the same, but ri is generated earlier than rj. A CBA classifier L is of the form: L = where ri  S, ra ; rb if b > a. In classifying a test case, the first rule that satisfies the case classifies it. If no rule applies to the case, it takes the de- fault class (default-class). A simplified version of the algorithm for build- ing such a classifier is given in Fig. 3.16. The classifier is the RuleList. 98 3 Supervised Learning This algorithm can be easily implemented by making one pass through the training data for every rule. However, this is extremely inefficient for large data sets. An efficient algorithm that makes at most two passes over the data is given in [39]. Combine Multiple Rules: Like the first approach, this approach does not take any additional step to build a classifier. At the classification time, for each test instance, the system first finds the subset of rules that covers the instance. If all the rules in the subset have the same class, the class is as- signed to the test instance. If the rules have different classes, the system divides the rules into groups according to their classes, i.e., all rules of the same class are in the same group. The system then compares the aggre- gated effects of the rule groups and finds the strongest group. The class la- bel of the strongest group is assigned to the test instance. To measure the strength of each rule group, there again can be many possible techniques. For example, the CMAR system uses a weighted 2 measure [36]. 3.5.2 Class Association Rules as Features In the above two methods, rules are directly used for classification. In this method, rules are used as features to augment the original data or simply form a new data set, which is then fed to a classification algorithm, e.g., decision trees or the naïve Bayesian method. Such features were found to be particularly effective for text-based classification applications. To use CARs as features, only the conditional part of each CAR rule is needed, and it is often treated as a Boolean feature/attribute. If a data in- stance in the original data contains the conditional part, the value of the feature/attribute is set to 1, otherwise it is set to 0. Several applications of this method have been reported [2, 13, 27, 31]. The reason that such CAR- based features are helpful is that they capture multi-attribute or multi-item Algorithm CBA(S, D) 1 S = sort(S); // sorting is done according to the precedence ; 2 RuleList = ; // the rule list classifier 3 for each rule r  S in sequence do 4 if D   AND r classifies at least one example in D correctly then 5 delete from D all training examples covered by r; 6 add r at the end of RuleList 7 endif 8 endfor 9 add the majority class as the default class at the end of RuleList Fig. 3.16. A simple classifier building algorithm 3.5 Classification Based on Associations 99 correlations with class labels, which are useful for classification but are not considered by many classification algorithms (e.g., naïve Bayesian). 3.5.3 Classification Using Normal Association Rules Not only can class association rules be used for classification, but also normal association rules. For example, association rules are commonly used in e-commerce Web sites for product recommendations, which work as follows: When a customer purchases some products, the system will recommend him/her some other related products based on what he/she has already purchased (see Chap. 12). Recommendation is essentially a classification or prediction problem. It predicts what a customer is likely to buy. Association rules are naturally applicable to such applications. The classification process is as follows: 1. The system first uses previous purchase transactions (the same as mar- ket basket transactions) to mine association rules. In this case, there are no fixed classes. Any item can appear on the left-hand side or the right- hand side of a rule. For recommendation purposes, usually only one item appears on the right-hand side of a rule. 2. At the prediction (e.g., recommendation) time, given a transaction (e.g., a set of items already purchased by a customer), all the rules that cover the transaction are selected. The strongest rule is chosen and the item on the right-hand side of the rule (i.e., the consequent) is then the predicted item and is recommended to the user. If multiple rules are very strong, multiple items can be recommended. This method is basically the same as the “use the strongest rule” method described in Sect. 3.5.1. Again, the rule strength can be measured in vari- ous ways, e.g., confidence, 2 test, or a combination of both support and confidence. For example, in [38], the product of support and confidence is used as the rule strength. Clearly, the other two methods discussed in Sect. 3.5.1 can be applied as well. The key advantage of using association rules for recommendation is that they can predict any item since any item can be the class item on the right- hand side. Traditional classification algorithms only work with a single fixed class attribute, and are not easily applicable to recommendations. Finally, we note that multiple minimum supports (Sect. 2.4) can be of significant help. Otherwise, rare items will never be recommended, which causes the coverage problem (see Sect. 12.3.3). It is shown in [46] that us- ing multiple minimum supports can dramatically increase the coverage. 100 3 Supervised Learning 3.6 Naïve Bayesian Classification Supervised learning can be naturally studied from a probabilistic point of view. The task of classification can be regarded as estimating the class posterior probabilities given a test example d, i.e., Pr(C= cj | d). (17) We then see which class cj is more probable. The class with the highest probability is assigned to the example d. Formally, let A1, A2, …, A|A| be the set of attributes with discrete values in the data set D. Let C be the class attribute with |C| values, c1, c2, …, c|C|. Given a test example d with observed attribute values a1 through a|A|, where ai is a possible value of Ai (or a member of the domain of Ai), i.e., d = . The prediction is the class cj such that Pr(C=cj | A1=a1, , A|A|=a|A|) is maximal. cj is called a maximum a posteriori (MAP) hypothesis. By Bayes’ rule, the above quantity (17) can be expressed as . )Pr()|,...,Pr( )Pr()|,...,Pr( ),...,Pr( )Pr()|,...,Pr( ),...,|Pr( || 1 ||||11 ||||11 ||||11 ||||11 ||||11        C k kkAA jjAA AA jjAA AAj cCcCaAaA cCcCaAaA aAaA cCcCaAaA aAaAcC (18) Pr(C=cj) is the class prior probability of cj, which can be estimated from the training data. It is simply the fraction of the data in D with class cj. If we are only interested in making a classification, Pr(A1=a1, ..., A|A|=a|A|) is irrelevant for decision making because it is the same for every class. Thus, only Pr(A1=a1, ..., A|A|=a|A| | C=cj) needs to be computed, which can be written as Pr(A1=a1, ..., A|A|=a|A| | C=cj) = Pr(A1=a1 | A2=a2, ..., A|A|=a|A|, C=cj)Pr(A2=a2, ..., A|A|=a|A| | C=cj). (19) Recursively, the second term above (i.e., Pr(A2=a2, ..., A|A|=a|A||C=cj)) can be written in the same way (i.e., Pr(A2=a2|A3=a3 ..., A|A|=a|A|, C=cj) Pr(A3=a3, ..., A|A|=a|A||C=cj)), and so on. However, to further our derivation, we need to make an important assumption. Conditional independence assumption: We assume that all attributes are conditionally independent given the class C = cj. Formally, we assume, 3.6 Naïve Bayesian Classification 101 Pr(A1=a1 | A2=a2, ..., A|A|=a|A|, C=cj) = Pr(A1=a1 | C=cj) (20) and similarly for A2 through A|A|. We then obtain    || 1 ||||11 )|Pr()|,...,Pr( A i jiijAA cCaAcCaAaA (21) . )|Pr()Pr( )|Pr()Pr( ),...,|Pr( || 1 || 1 || 1 ||||11           C k A i kiik A i jiij AAj cCaAcC cCaAcC aAaAcC (22) Next, we need to estimate the prior probabilities Pr(C=cj) and the conditional probabilities Pr(Ai=ai | C=cj) from the training data, which are straightforward. set data in the examples ofnumber total class of examples ofnumber )Pr( j j ccC  (23) . class of examples ofnumber class and with examples ofnumber )|Pr( j jii jii c caAcCaA  (24) If we only need a decision on the most probable class for each test in- stance, we only need the numerator of Equation (22) since the denominator is the same for every class. Thus, given a test case, we compute the follow- ing to decide the most probable class for the test case:    || 1 )|Pr()Pr(maxarg A i jiij c cCaAcCc j (25) Example 17: Suppose that we have the training data set in Fig. 3.17, which has two attributes A and B, and the class C. We can compute all the probability values required to learn a naïve Bayesian classifier. A B C m b t m s t g q t h s t g q t g q f g s f h b f h q f m b f Fig. 3.17. An example of a training data set 102 3 Supervised Learning Pr(C = t) = 1/2, Pr(C= f) = 1/2 Pr(A=m | C=t) = 2/5 Pr(A=g | C=t) = 2/5 Pr(A=h | C=t) = 1/5 Pr(A=m | C=f) = 1/5 Pr(A=g | C=f) = 2/5 Pr(A=h | C=f) =2/5 Pr(B=b | C=t) = 1/5 Pr(B=s | C=t) = 2/5 Pr(B=q | C=t) = 2/5 Pr(B=b | C=f) = 2/5 Pr(B=s | C=f) = 1/5 Pr(B=q | C=f) = 2/5 Now we have a test example: A = m B = q C = ? We want to know its class. Equation (25) is applied. For C = t, we have .25 2 5 2 5 2 2 1)|Pr()Pr( 2 1   j jj tCaAtC For class C = f, we have .25 1 5 2 5 1 2 1)|Pr()Pr( 2 1   j jj fCaAfC Since C = t is more probable, t is the predicted class of the test case. ▀ It is easy to see that the probabilities (i.e., Pr(C=cj) and Pr(Ai=ai | C=cj)) required to build a naïve Bayesian classifier can be found in one scan of the data. Thus, the algorithm is linear in the number of training examples, which is one of the great strengths of the naïve Bayes, i.e., it is extremely efficient. In terms of classification accuracy, although the algorithm makes the strong assumption of conditional independence, several researchers have shown that its classification accuracies are surprisingly strong. See experimental comparisons of various techniques in [15, 29, 40]. To learn practical naïve Bayesian classifiers, we still need to address some additional issues: how to handle numeric attributes, zero counts, and missing values. Below, we deal with each of them in turn. Numeric Attributes: The above formulation of the naïve Bayesian learn- ing assumes that all attributes are categorical. However, most real-life data sets have numeric attributes. Therefore, in order to use the naïve Bayeisan algorithm, each numeric attribute needs to be discretized into intervals. This is the same as for class association rule mining. Existing discretiza- tion algorithms in [16, 19] can be used. Zero Counts: It is possible that a particular attribute value in the test set never occurs together with a class in the training set. This is problematic because it will result in a 0 probability, which wipes out all the other prob- abilities Pr(Ai=ai | C=cj) when they are multiplied according to Equation 3.7 Naïve Bayesian Text Classification 103 (25) or Equation (22). A principled solution to this problem is to incorpo- rate a small-sample correction into all probabilities. Let nij be the number of examples that have both Ai = ai and C = cj. Let nj be the total number of examples with C=cj in the training data set. The un- corrected estimate of Pr(Ai=ai | C=cj) is nij/nj, and the corrected estimate is ij ij jii mn ncCaA     )|Pr( (26) where mi is the number of values of attribute Ai (e.g., 2 for a Boolean at- tribute), and  is a multiplicative factor, which is commonly set to  = 1/n, where n is the total number of examples in the training set D [15, 29]. When  = 1, we get the well known Laplace’s law of succession [23]. The general form of correction (also called smoothing) in Equation (26) is called the Lidstone’s law of succession [37]. Applying the correction  = 1/n, the probabilities of Example 17 are revised. For example, Pr(A=m | C=t) = (2+1/10) / (5 + 3*1/10) = 2.1/5.3 = 0.396 Pr(B=b | C=t) = (1+1/10) / (5 + 3*1/10) = 1.1/5.3 = 0.208. Missing Values: Missing values are ignored, both in computing the prob- ability estimates in training and in classifying test instances. 3.7 Naïve Bayesian Text Classification Text classification or categorization is the problem of learning classifica- tion models from training documents labeled with pre-defined classes. That learned models are then used to classify future documents. For exam- ple, we have a set of news articles of three classes or topics, Sport, Politics, and Science. We want to learn a classifier that is able to classify future news articles into these classes. Due to the rapid growth of online documents in organizations and on the Web, automated document classification is an important problem. Al- though the techniques discussed in the previous sections can be applied to text classification, it has been shown that they are not as effective as the methods presented in this section and in the next two sections. In this sec- tion, we study a naïve Bayesian learning method that is specifically formu- lated for texts, which makes use of text specific characteristics. However, the ideas are similar to those in Sect. 3.6. Below, we first present a prob- abilistic framework for texts, and then study the naïve Bayesian equations for their classification. There are several slight variations of this model. This section is mainly based on the formulation given in [42]. 104 3 Supervised Learning 3.7.1 Probabilistic Framework The naïve Bayesian learning method for text classification is derived based on a probabilistic generative model. It assumes that each document is generated by a parametric distribution governed by a set of hidden pa- rameters. Training data is used to estimate these parameters. The parame- ters are then applied to classify each test document using Bayes rule by calculating the posterior probability that the distribution associated with a class (represented by the unobserved class variable) would have gener- ated the given document. Classification then becomes a simple matter of selecting the most probable class. The generative model is based on two assumptions: 1. The data (or the text documents) are generated by a mixture model. 2. There is a one-to-one correspondence between mixture components and document classes. A mixture model models the data with a number of statistical distribu- tions. Intuitively, each distribution corresponds to a data cluster and the pa- rameters of the distribution provide a description of the corresponding cluster. Each distribution in a mixture model is also called a mixture component (the distribution can be of any kind). Fig. 3.15 plots two probability density functions of a mixture of two Gaussian distributions that generate a 1-dimensional data set of two classes, one distribution per class, whose parameters (denoted by i) are the mean (i) and the standard deviation (i), i.e., i = (i, i). Fig. 3.18. Probability density functions of two distributions in a mixture model Let the number of mixture components (or distributions) in a mixture model be K, and the jth distribution has the parameters j. Let  be the set of parameters of all components,  = {1, 2, …, K, 1, 2, …, K}, where j is the mixture weight (or mixture probability) of the mixture compo- nent j and j is the set of parameters of component j. The mixture weights class 1 class 2 3.7 Naïve Bayesian Text Classification 105 are subject to the constraint .11   K j j The meaning of mixture weights (or probabilities) will be clear below. Let us see how the mixture model generates a collection of documents. Recall the classes C in our classification problem are c1, c2, …, c|C|. Since we assume that there is a one-to-one correspondence between mixture components and classes, each class corresponds to a mixture component. Thus |C| = K, and the jth mixture component can be represented by its cor- responding class cj and is parameterized by j. The mixture weights are class prior probabilities, i.e., j = Pr(cj|). The mixture model generates each document di by: 1. first selecting a mixture component (or class) according to class prior probabilities (i.e., mixture weights), j = Pr(cj|); 2. then having this selected mixture component (cj) generate a document di according to its parameters, with distribution Pr(di|cj; ) or more pre- cisely Pr(di|cj; j). The probability that a document di is generated by the mixture model can be written as the sum of total probability over all mixture components. Note that to simplify the notation, we use cj instead of C = cj as in the pre- vious section: ). ;|Pr()Θ|Pr()|Pr( || 1    C j jiji cdcd (27) Since each document is attached with its class label, we can now derive the naïve Bayesian model for text classification. Note that in the above prob- ability expressions, we include  to represent their dependency on  as we employ a generative model. In an actual implementation, we need not be concerned with , i.e., it can be ignored. 3.7.2 Naïve Bayesian Model A text document consists of a sequence of sentences, and each sentence consists of a sequence of words. However, due to the complexity of mod- eling words sequence and their relationships, several assumptions are made in the derivation of the Bayesian classifier. That is also why we call the final classification model, the naïve Bayesian classification model. Specifically, the naïve Bayesian classification treats each document as a “bag” of words. Apart from the mixture model assumptions described above, the generative model also makes the following words and document length based assumptions: 106 3 Supervised Learning 1. Words of a document are generated independently of their context, that is, independently of the other words in the same document given the class label. This is the familiar naïve Bayesian assumption used before. 2. The probability of a word is independent of its position in the document. For example, the probability of seeing the word “student” in the first po- sition of the document is the same as seeing it in any other position. 3. Document length is independent of the document class. With these assumptions, each document can be regarded as generated by a multinomial distribution. In other words, each document is drawn from a multinomial distribution of words with as many independent trials as the length of the document. The words are from a given vocabulary V = {w1, w2, …, w|V|}, |V| being the number of words in the vocabulary. To see why this is a multinomial distribution, we give a short introduction to the multi- nomial distribution. A multinomial trial is a process that can result in any of k outcomes, where k  2. Each outcome of a multinomial trial has a probability of oc- currence. The probabilities of the k outcomes are denoted by p1, p2, …, pk. For example, the rolling of a die is a multinomial trial, with six possible outcomes 1, 2, 3, 4, 5, 6. For a fair die, p1 = p2 = … = pk = 1/6. Now assume n independent trials are conducted, each with the k possi- ble outcomes and the k probabilities, p1, p2, …, pk. Let us number the out- comes 1, 2, 3, …, k. For each outcome, let Xt denote the number of trials that result in that outcome. Then, X1, X2, …, Xk are discrete random vari- ables. The collection of X1, X2, …, Xk is said to have the multinomial dis- tribution with parameters, n, p1, p2, …, pk. In our context, n corresponds to the length of a document, and the out- comes correspond to all the words in the vocabulary V (k = |V|). p1, p2, …, pk correspond to the probabilities of occurrence of the words in V in a doc- ument, which are Pr(wt|cj; ). Xt is a random variable representing the number of times that word wt appears in a document. We can thus directly apply the probability function of the multinomial distribution to find the probability of a document given its class (including the probability of doc- ument length, Pr(|di|), which is assumed to be independent of the class):    || 1 ! );|Pr(|!||)Pr(|);|Pr( V t ti N jt iiji N cwddcd ti , (28) where Nti is the number of times that word wt occurs in document di, || || 1 i V t ti dN   , and    || 1 1);|Pr( V t jt cw . (29) 3.7 Naïve Bayesian Text Classification 107 The parameters j of the generative component for each class cj are the probabilities of all words wt in V, written as Pr(wt|cj; ), and the probabili- ties of document lengths, which are the same for all classes (or mixture components) due to our assumption. Parameter Estimation: The parameters can be estimated from the train- ing data D = {D1, D2, …, D|C|}, where Dj is the subset of documents for class cj (recall |C| is the number of classes). The vocabulary V is the set of all distinctive words in D. Note that we do not need to estimate the prob- ability of each document length as it is not used in our final classifier. The estimate of  is written as ˆ . The parameters are estimated based on em- pirical counts. The estimated probability of word wt given class cj is simply the number of times that wt occurs in the training data Dj (of class cj) divided by the to- tal number of word occurrences in the training data for that class: . )|Pr( )|Pr( )ˆ;|Pr( || 1 || 1 || 1     V s D i ijsi D i ijti jt dcN dcN cw (30) In Equation (30), we do not use Dj explicitly. Instead, we include Pr(cj|di) to achieve the same effect because Pr(cj|di) = 1 for each document in Dj and Pr(cj|di) = 0 for documents of other classes. Again, Nti is the number of times that word wt occurs in document di. In order to handle 0 counts for infrequently occurring words that do not appear in the training set, but may appear in the test set, we need to smooth the probability to avoid probabilities of 0 or 1. This is the same problem as in Sect. 3.6. The standard way of doing this is to augment the count of each distinctive word with a small quantity  (0    1) or a fraction of a word in both the numerator and the denominator. Thus, any word will have at least a very small probability of occurrence. . )|Pr(|| )|Pr( )ˆ;|Pr( || 1 || 1 || 1        V s D i ijsi D i ijti jt dcNV dcN cw   (31) This is called the Lidstone smoothing (Lidstone’s law of succession). When  = 1, the smoothing is known as the Laplace smoothing. Many experiments have shown that  < 1 works better for text classification [1]. The best  value for a data set can be found through experiments using a validation set or through cross-validation. Finally, class prior probabilities, which are mixture weights j, can be easily estimated using the training data as well, 108 3 Supervised Learning .|| )|Pr( )ˆ|Pr( || 1 D dc c D i ij j   (32) Classification: Given the estimated parameters, at the classification time, we need to compute the probability of each class cj for the test document di. That is, we compute the probability that a particular mixture component cj generated the given document di. Using the Bayes rule and Equations (27), (28), (31), and (32), we have , )ˆ;|Pr()ˆ|Pr( )ˆ;|Pr()ˆ|Pr( )ˆ|Pr( )ˆ;|Pr()ˆ|Pr()ˆ;|Pr( || 1 || 1 , || 1 ,            C r d k rkdr d k jkdj i jij ij i i i i cwc cwc d cdcdc (33) where wdi,k is the word in position k of document di (which is the same as using wt and Nti). If the final classifier is to classify each document into a single class, the class with the highest posterior probability is selected: ).ˆ;|Pr(maxarg  ijCc dcj (34) 3.7.3 Discussion Most assumptions made by naïve Bayesian learning are violated in prac- tice. For example, words in a document are clearly not independent of each other. The mixture model assumption of one-to-one correspondence be- tween classes and mixture components may not be true either because a class may contain documents from multiple topics. Despite such viola- tions, researchers have shown that naïve Bayesian learning produces very accurate models. Naïve Bayesian learning is also very efficient. It scans the training data only once to estimate all the probabilities required for classification. It can be used as an incremental algorithm as well. The model can be updated easily as new data comes in because the probabilities can be conveniently revised. Naïve Bayesian learning is thus widely used for text classification. The naïve Bayesian formulation presented here is based on a mixture of multinomial distributions. There is also a formulation based on multi- variate Bernoulli distributions in which each word in the vocabulary is a binary feature, i.e., it either appears or does not appear in a document. 3.8 Support Vector Machines 109 Thus, it does not consider the number of times that a word occurs in a doc- ument. Experimental comparisons show that multinomial formulation con- sistently produces more accurate classifiers [42]. 3.8 Support Vector Machines Support vector machines (SVM) is another type of learning system [57], which has many desirable qualities that make it one of most popular algo- rithms. It not only has a solid theoretical foundation, but also performs classification more accurately than most other algorithms in many applica- tions, especially those applications involving very high dimensional data. For instance, it has been shown by several researchers that SVM is perhaps the most accurate algorithm for text classification. It is also widely used in Web page classification and bioinformatics applications. In general, SVM is a linear learning system that builds two-class clas- sifiers. Let the set of training examples D be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi = (xi1, xi2, …, xir) is a r-dimensional input vector in a real-valued space X   r, yi is its class label (output value) and yi  {1, -1}. 1 denotes the positive class and -1 denotes the negative class. Note that we use slightly different notations in this section. We use y instead of c to repre- sent a class because y is commonly used to represent a class in the SVM literature. Similarly, each data instance is called an input vector and de- noted by a bold face letter. In the following, we use bold face letters for all vectors. To build a classifier, SVM finds a linear function of the form f(x) = w  x + b (35) so that an input vector xi is assigned to the positive class if f(xi)  0, and to the negative class otherwise, i.e.,      0if1 0if1 b by i i i xw xw (36) Hence, f(x) is a real-valued function f: X   r . w = (w1, w2, …, wr)   r is called the weight vector. b   is called the bias. w  x is the dot product of w and x (or Euclidean inner product). Without using vector notation, Equation (35) can be written as: 110 3 Supervised Learning f(x1, x2, …, xr) = w1x1+w2x2 + … + wrxr + b, where xi is the variable representing the ith coordinate of the vector x. For convenience, we will use the vector notation from now on. In essence, SVM finds a hyperplane w  x + b = 0 (37) that separates positive and negative training examples. This hyperplane is called the decision boundary or decision surface. Geometrically, the hyperplane w  x + b = 0 divides the input space in- to two half spaces: one half for positive examples and the other half for negative examples. Recall that a hyperplane is commonly called a line in a 2-dimensional space and a plane in a 3-dimensional space. Fig. 3.19(A) shows an example in a 2-dimensional space. Positive in- stances (also called positive data points or simply positive points) are rep- resented with small filled rectangles, and negative examples are repre- sented with small empty circles. The thick line in the middle is the decision boundary hyperplane (a line in this case), which separates positive (above the line) and negative (below the line) data points. Equation (35), which is also called the decision rule of the SVM classifier, is used to make classification decisions on test instances. (A) (B) Fig. 3.19. (A) A linearly separable data set and (B) possible decision boundaries Fig. 3.19(A) raises two interesting questions: 1. There are an infinite number of lines that can separate the positive and negative data points as illustrated by Fig. 3.19(B). Which line should we choose? 2. A hyperplane classifier is only applicable if the positive and negative data can be linearly separated. How can we deal with nonlinear separa- tions or data sets that require nonlinear decision boundaries? w  x + b = 0 y = 1 y = -1 3.8 Support Vector Machines 111 The SVM framework provides good answers to both questions. Briefly, for question 1, SVM chooses the hyperplane that maximizes the margin (the gap) between positive and negative data points, which will be defined for- mally shortly. For question 2, SVM uses kernel functions. Before we dive into the details, we want to stress that SVM requires numeric data and only builds two-class classifiers. At the end of the section, we will discuss how these limitations may be addressed. 3.8.1 Linear SVM: Separable Case This sub-section studies the simplest case of linear SVM. It is assumed that the positive and negative data points are linearly separable. From linear algebra, we know that in w  x + b = 0, w defines a direc- tion perpendicular to the hyperplane (see Fig. 3.20). w is also called the normal vector (or simply normal) of the hyperplane. Without changing the normal vector w, varying b moves the hyperplane parallel to itself. Note also that w  x + b = 0 has an inherent degree of freedom. We can rescale the hyperplane to w  x + b = 0 for    + (positive real num- bers) without changing the function/hyperplane. Fig. 3.20. Separating hyperplanes and margin of SVM: Support vectors are circled Since SVM maximizes the margin between positive and negative data points, let us find the margin. Let d+ (respectively d) be the shortest dis- tance from the separating hyperplane (w  x + b = 0) to the closest posi- tive (negative) data point. The margin of the separating hyperplane is d++d. SVM looks for the separating hyperplane with the largest margin, which is also called the maximal margin hyperplane, as the final deci- sion boundary. The reason for choosing this hyperplane to be the decision w  x + b = 0 y = 1 y = -1 w |||| || w b H+: w  x + b = 1 H----: w  x + b = -1 x---- x+ d d+ margin 112 3 Supervised Learning boundary is because theoretical results from structural risk minimization in computational learning theory show that maximizing the margin mini- mizes the upper bound of classification errors. Let us consider a positive data point (x+, 1) and a negative data point (x----, -1) that are closest to the hyperplane + b = 0. We define two paral- lel hyperplanes, H+ and H----, that pass through x+ and x---- respectively. H+ and H---- are also parallel to + b = 0. We can rescale w and b to obtain H+: w  x+ + b = 1 (38) H----: w  x---- + b = -1 (39) such that w  xi + b  1 if yi = 1 w  xi + b  -1 if yi = -1, which indicate that no training data fall between hyperplanes H+ and H----. Now let us compute the distance between the two margin hyperplanes H+ and H----. Their distance is the margin (d+ + d). Recall from vector space in linear algebra that the (perpendicular) Euclidean distance from a point xi to a hyperplane w  x + b = 0 is: |||| || w xw bi  , (40) where ||w|| is the Euclidean norm of w, 22 2 2 1 ...|||| rwww  www . (41) To compute d+, instead of computing the distance from x+ to the separat- ing hyperplane w  x + b = 0, we pick up any point xs on w  x + b = 0 and compute the distance from xs to w  x+ + b = 1 by applying Equation (40) and noticing that w  xs + b = 0, |||| 1 |||| |1| ww xw s  bd . (42) Likewise, we can compute the distance from xs to w  x+ + b = -1 to ob- tain d = 1/||w||. Thus, the decision boundary w  x + b = 0 lies half way between H+ and H----. The margin is thus 3.8 Support Vector Machines 113 |||| 2 w  ddmargin (43) In fact, we can compute the margin in many ways. For example, it can be computed by finding the distances from the origin to the three hyper- planes, or by projecting the vector (x2---- x1+) to the normal vector w. Since SVM looks for the separating hyperplane that maximizes the mar- gin, this gives us an optimization problem. Since maximizing the margin is the same as minimizing ||w||2/2 = w  w/2. We have the following linear separable SVM formulation. Definition (Linear SVM: Separable Case): Given a set of linearly sepa- rable training examples, D = {(x1, y1), (x2, y2), …, (xn, yn)}, learning is to solve the following constrained minimization problem, niby ii ..., 2, 1, ,1)( :Subject to 2 :Minimize   xw ww (44) Note that the constraint niby ii ..., 2, 1, ,1)(  xw summarizes: w  xi + b  1 for yi = 1 w  xi + b  -1 for yi = -1. Solving the problem (44) will produce the solutions for w and b, which in turn give us the maximal margin hyperplane w  x + b = 0 with the margin 2/||w||. A full description of the solution method requires a significant amount of optimization theory, which is beyond the scope of this book. We will only use those relevant results from optimization without giving formal de- finitions, theorems, or proofs. Since the objective function is quadratic and convex and the constraints are linear in the parameters w and b, we can use the standard Lagrange multiplier method to solve it. Instead of optimizing only the objective function (which is called un- constrained optimization), we need to optimize the Lagrangian of the prob- lem, which considers the constraints at the same time. The need to con- sider constraints is obvious because they restrict the feasible solutions. Since our inequality constraints are expressed using “”, the Lagrangian is formed by the constraints multiplied by positive Lagrange multipliers and subtracted from the objective function, i.e., 114 3 Supervised Learning ]1)([2 1 1    byL i n i iiP xwww  , (45) where i  0 are the Lagrange multipliers. The optimization theory says that an optimal solution to (45) must sat- isfy certain conditions, called Kuhn–Tucker conditions, which play a central role in constrained optimization. Here, we give a brief introduction to these conditions. Let the general optimization problem be nibg f ii ..., 2, 1, ,)( :Subject to )( :Minimize x x (46) where f is the objective function and gi is a constraint function (which is different from yi in (44) as yi is not a function but a class label of 1 or -1). The Lagrangian of (46) is, )])([)( 1 i n i iiP bgfL    xx  (47) An optimal solution to the problem in (46) must satisfy the following necessary (but not sufficient) conditions: rjx L j P ..., ,2 ,1 ,0   (48) nibg ii ..., 2, 1, ,0)( x (49) nii ..., 2, 1, ,0  (50) nigb iiii ..., 2, 1, ,0))((  x (51) These conditions are called the Kuhn–Tucker conditions. Note that (49) is simply the original set of constraints in (46). The condition (51) is called the complementarity condition, which implies that at the solution point, If i > 0 then gi(x) = bi. If gi(x) > bi then i = 0. These mean that for active constraints, i > 0, whereas for inactive con- straints, i = 0. As we will see later, they give some very desirable proper- ties to SVM. Let us come back to our problem. For the minimization problem (44), the Kuhn–Tucker conditions are (52)–(56): 3.8 Support Vector Machines 115 rjxyww L n i ijiij j P ..., ,2 ,1 ,0 1      (52) 0 1     n i ii P yb L  (53) niby ii ..., 2, 1, ,01)(  xw (54) nii ..., 2, 1, ,0  (55) niby iii ..., 2, 1, ,0)1)((  xw (56) Inequality (54) is the original set of constraints. We also note that although there is a Lagrange multiplier i for each training data point, the comple- mentarity condition (56) shows that only those data points on the margin hyperplanes (i.e., H+ and H----) can have i > 0 since for them yi(w  xi + b) – 1 = 0. These data points are called support vectors, which give the name to the algorithm, support vector machines. All the other data points have i = 0. In general, Kuhn–Tucker conditions are necessary for an optimal solu- tion, but not sufficient. However, for our minimization problem with a convex objective function and a set of linear constraints, the Kuhn–Tucker conditions are both necessary and sufficient for an optimal solution. Solving the optimization problem is still a difficult task due to the ine- quality constraints. However, the Lagrangian treatment of the convex op- timization problem leads to an alternative dual formulation of the problem, which is easier to solve than the original problem, which is called the pri- mal problem (LP is called the primal Lagrangian). The concept of duality is widely used in the optimization literature. The aim is to provide an alternative formulation of the problem which is more convenient to solve computationally and/or has some theoretical signifi- cance. In the context of SVM, the dual problem is not only easy to solve computationally, but also crucial for using kernel functions to deal with nonlinear decision boundaries as we do not need to compute w explicitly (which will be clear later). Transforming from the primal to its corresponding dual can be done by setting to zero the partial derivatives of the Lagrangian (45) with respect to the primal variables (i.e., w and b), and substituting the resulting relations back into the Lagrangian. This is to simply substitute (52), which is 116 3 Supervised Learning rjxyw n i ijiij ..., ,2 ,1 , 1     (57) and (53), which is ,0 1   n i iiy  (58) into the original Lagrangian (45) to eliminate the primal variables, which gives us the dual objective function (denoted by LD), .2 1 1,1    ji n ji jiji n i iD yyL xx (59) LD contains only dual variables and must be maximized under the simpler constraints, (52) and (53), and i  0. Note that (52) is not needed as it has already been substituted into the objective function LD. Hence, the dual of the primal Equation (44) is Maximize: .2 1 1,1    ji n ji jiji n i iD yyL xx Subject to: . ..., ,2 ,1 ,0 0 1 ni y i n i ii      (60) This dual formulation is called the Wolfe dual. For our convex objec- tive function and linear constraints of the primal, it has the property that the i’s at the maximum of LD gives w and b occurring at the minimum of LP (the primal). Solving (60) requires numerical techniques and clever strategies beyond the scope of this book. After solving (60), we obtain the values for i, which are used to compute the weight vector w and the bias b using Equa- tions (52) and (56) respectively. Instead of depending on one support vec- tor (i > 0) to compute b, in practice all support vectors are used to com- pute b, and then take their average as the final value for b. This is because the values of i are computed numerically and can have numerical errors. Our final decision boundary (maximal margin hyperplane) is 0   byb svi iii xxxw  , (61) where sv is the set of indices of the support vectors in the training data. 3.8 Support Vector Machines 117 Testing: We apply (61) for classification. Given a test instance z, we clas- sify it using the following: .)(        svi iii bysignbsign zxzw  (62) If (62) returns 1, then the test instance z is classified as positive; otherwise, it is classified as negative. 3.8.2 Linear SVM: Non-separable Case The linear separable case is the ideal situation. In practice, however, the training data is almost always noisy, i.e., containing errors due to various reasons. For example, some examples may be labeled incorrectly. Fur- thermore, practical problems may have some degree of randomness. Even for two identical input vectors, their labels may be different. For SVM to be useful, it must allow noise in the training data. However, with noisy data the linear separable SVM will not find a solution because the constraints cannot be satisfied. For example, in Fig. 3.21, there is a negative point (circled) in the positive region, and a positive point in the negative region. Clearly, no solution can be found for the problem. Recall that the primal for the linear separable case was: . ..., 2, 1, ,1)( :Subject to 2 :Minimize niby ii   xw ww (63) To allow errors in data, we can relax the margin constraints by introduc- ing slack variables, i ( 0) as follows: w  xi + b  1  i for yi = 1 w  xi + b  1 + i for yi = -1. Thus we have the new constraints: Subject to: yi(w  xi + b)  1  i, i =1, 2, …, n, i  0, i =1, 2, …, n. The geometric interpretation is shown in Fig. 3.21, which has two error da- ta points xa and xb (circled) in wrong regions. 118 3 Supervised Learning Fig. 3.21. The non-separable case: xa and xb are error data points We also need to penalize the errors in the objective function. A natural way is to assign an extra cost for errors to change the objective function to kn i iC       12 :Minimize ww , (64) where C  0 is a user specified parameter. The resulting optimization prob- lem is still a convex programming problem. k = 1 is commonly used, which has the advantage that neither i nor its Lagrangian multipliers ap- pear in the dual formulation. We only discuss the k = 1 case below. The new optimization problem becomes: . ..., 2, 1, ,0 ..., 2, 1, ,1)( :Subject to 2 :Minimize 1 ni niby C i iii n i i         xw ww (65) This formulation is called the soft-margin SVM. The primal Lagrangian (denoted by LP) of this formulation is as follows    n i iiii n i ii n i iP byCL 111 ]1)([2 1  xwww , (66) where i, i  0 are the Lagrange multipliers. The Kuhn–Tucker condi- tions for optimality are the following: rjxyww L n i ijiij j P ..., ,2 ,1 ,0 1      (67) w  x + b = 0 |||| || w b w ||||w b xb ||||w a xa 3.8 Support Vector Machines 119 0 1     n i ii P yb L  (68) niCL ii i P ..., ,2 ,1 ,0    (69) niby iii ..., 2, 1, ,01)(  xw (70) nii ..., 2, 1, ,0  (71) nii ..., 2, 1, ,0  (72) nii ..., 2, 1, ,0  (73) niby iiii ..., 2, 1, ,0)1)((   xw (74) niii ..., 2, 1, ,0  (75) As the linear separable case, we then transform the primal to its dual by setting to zero the partial derivatives of the Lagrangian (66) with respect to the primal variables (i.e., w, b and i), and substituting the resulting rela- tions back into the Lagrangian. That is, we substitute Equations (67), (68) and (69) into the primal Lagrangian (66). From Equation (69), C  i  i = 0, we can deduce that i  C because i  0. Thus, the dual of (65) is Maximize:    ji n ji jiji n i iD yyL xxα 1,1 2 1)(  Subject to: . ..., ,2 ,1 ,0 0 1 niC y i n i ii      (76) Interestingly, i and its Lagrange multipliers i are not in the dual and the objective function is identical to that for the separable case. The only dif- ference is the constraint i  C (inferred from Cii = 0 and i  0). The dual problem (76) can also be solved numerically, and the resulting i values are then used to compute w and b. w is computed using Equation (67) and b is computed using the Kuhn–Tucker complementarity condi- tions (74) and (75). Since we do not have values for i, we need to get around it. From Equations (69), (74) and (75), we observe that if 0 < i < C then both i = 0 and .0)1)(  iii by xw Thus, we can use any training data point for which 0 < i < C and Equation (74) (with i = 0) to compute b: .1 1    j n i iii i yyb xx (77) 120 3 Supervised Learning Again, due to numerical errors, we can compute all possible b’s and then take their average as the final b value. Note that Equations (69), (74) and (75) in fact tell us more: i = 0  yi(w  xi + b)  1 and i = 0 0 < i < C  yi(w  xi + b) = 1 and i = 0 i = C  yi(w  xi + b)  1 and i  0 (78) Similar to support vectors for the separable case, (78) shows one of the most important properties of SVM: the solution is sparse in i. Most train- ing data points are outside the margin area and their i’s in the solution are 0. Only those data points that are on the margin (i.e., yi(w  xi + b) = 1, which are support vectors in the separable case), inside the margin (i.e., i = C and yi(w  xi + b) < 1), or errors are non-zero. Without this sparsity property, SVM would not be practical for large data sets. The final decision boundary is (we note that many i’s are 0) .0 1    byb n i iii xxxw  (79) The decision rule for classification (testing) is the same as the separable case, i.e., sign(w  x + b). We notice that for both Equations (79) and (77), w does not need to be explicitly computed. This is crucial for using kernel functions to handle nonlinear decision boundaries. Finally, we still have the problem of determining the parameter C. The value of C is usually chosen by trying a range of values on the training set to build multiple classifiers and then to test them on a validation set before selecting the one that gives the best classification result on the validation set. Cross-validation is commonly used as well. 3.8.3 Nonlinear SVM: Kernel Functions The SVM formulations discussed so far require that positive and negative examples can be linearly separated, i.e., the decision boundary must be a hyperplane. However, for many real-life data sets, the decision boundaries are nonlinear. To deal with nonlinearly separable data, the same formula- tion and solution techniques as for the linear case are still used. We only transform the input data from its original space into another space (usually a much higher dimensional space) so that a linear decision boundary can separate positive and negative examples in the transformed space, which is called the feature space. The original data space is called the input space. Thus, the basic idea is to map the data in the input space X to a feature space F via a nonlinear mapping , 3.8 Support Vector Machines 121 ).( : xx   6 FX  (80) After the mapping, the original training data set {(x1, y1), (x2, y2), …, (xn, yn)} becomes: {((x1), y1), ((x2), y2), …, ((xn), yn)}. (81) The same linear SVM solution method is then applied to F. Fig. 3.19 illus- trates the process. In the input space (figure on the left), the training exam- ples cannot be linearly separated. In the transformed feature space (figure on the right), they can be separated linearly. Fig. 3.22. Transformation from the input space to the feature space With the transformation, the optimization problem in (65) becomes . ..., 2, 1, ,0 ..., 2, 1, ,1))(( :Subject to 2 :Minimize 1 ni niby C i iii n i i         xw ww (82) Its corresponding dual is Maximize: .)()(2 1 1,1    ji n ji jiji n i iD yyL xx  Subject to: . ..., ,2 ,1 ,0 0 1 niC y i n i ii      (83) The final decision rule for classification (testing) is by n i iii  1 )()( xx  (84)  x Input space X x x x x o Feature space F o o o (x) (x) (x) (x) (x) (o) (o) (o) (o) 122 3 Supervised Learning Example 18: Suppose our input space is 2-dimensional, and we choose the following transformation (mapping): )2 , ,() ,( 21 2 2 2 121 xxxxxx 6 (85) The training example ((2, 3), -1) in the input space is transformed to the following training example in the feature space: ((4, 9, 8.5), -1). ▀ The potential problem with this approach of transforming the input data explicitly to a feature space and then applying the linear SVM is that it may suffer from the curse of dimensionality. The number of dimensions in the feature space can be huge with some useful transformations (see be- low) even with reasonable numbers of attributes in the input space. This makes it computationally infeasible to handle. Fortunately, explicit transformations can be avoided if we notice that in the dual representation both the construction of the optimal hyperplane (83) in F and the evaluation of the corresponding decision/classification function (84) only require the evaluation of dot products (x)  (z) and never the mapped vector (x) in its explicit form. This is a crucial point. Thus, if we have a way to compute the dot product (x)  (z) in the feature space F using the input vectors x and z directly, then we would not need to know the feature vector (x) or even the mapping function  itself. In SVM, this is done through the use of kernel functions, denoted by K, K(x, z) = (x)  (z), (86) which are exactly the functions for computing dot products in the trans- formed feature space using input vectors x and z. An example of a kernel function is the polynomial kernel, K(x, z) = x  zd. (87) Example 19: Let us compute this kernel with degree d = 2 in a 2- dimensional space. Let x = (x1, x2) and z = (z1, z2). ,)()( )2()2( 2 )( 21 2 2 2 121 2 2 2 1 2 2 2 22211 2 1 2 1 2 2211 2     zx zx  zz,z,zxx,x,x zxzxzxzx zxzx (88) 3.8 Support Vector Machines 123 where ),2()( 21 2 2 2 1 xx,x,x x  which shows that the kernel x  z2 is a dot product in the transformed feature space. The number of dimensions in the feature space is 3. Note that (x) is actually the mapping function used in Example 18. Incidentally, in general the number of dimensions in the feature space for the polynomial kernel function x  zd is        d dr 1 , which is a huge number even with a reasonable number (r) of attributes in the in- put space. Fortunately, by using the kernel function in (87), the huge num- ber of dimensions in the feature space does not matter. ▀ The derivation in (88) is only for illustration purposes. In fact, we do not need to find the mapping function. We can simply apply the kernel func- tion directly. That is, we replace all the dot products (x)  (z) in (83) and (84) with the kernel function K(x, z) (e.g., the polynomial kernel in (87)). This strategy of directly using a kernel function to replace dot prod- ucts in the feature space is called the kernel trick. We never need to ex- plicitly know what  is. However, the question is, how do we know whether a function is a ker- nel without performing the derivation such as that in (88)? That is, how do we know that a kernel function is indeed a dot product in some feature space? This question is answered by a theorem called the Mercer’s theo- rem, which we will not discuss here. See [12] for details. It is clear that the idea of kernel generalizes the dot product in the input space. The dot product is also a kernel with the feature map being the identity K(x, z) = x  z. (89) Commonly used kernels include Polynomial: dK)(),(  zxzx (90) Gaussian RBF: 2|||| 2 ),( zxzx  eK (91) where   , d  N, and  > 0. Summary SVM is a linear learning system that finds the maximal margin decision boundary to separate positive and negative examples. Learning is formu- lated as a quadratic optimization problem. Nonlinear decision boundaries are found via a transformation of the original data to a much higher dimen- sional feature space. However, this transformation is never explicitly done. 124 3 Supervised Learning Instead, kernel functions are used to compute dot products required in learning without the need to even know the transformation function. Due to the separation of the learning algorithm and kernel functions, kernels can be studied independently from the learning algorithm. One can design and experiment with different kernel functions without touching the underlying learning algorithm. SVM also has some limitations: 1. It works only in real-valued space. For a categorical attribute, we need to convert its categorical values to numeric values. One way to do this is to create an extra binary attribute for each categorical value, and set the attribute value to 1 if the categorical value appears, and 0 otherwise. 2. It allows only two classes, i.e., binary classification. For multiple class classification problems, several strategies can be applied, e.g., one- against-rest, and error-correcting output coding [14]. 3. The hyperplane produced by SVM is hard to understand by users. It is difficult to picture where the hyperplane is in a high-dimensional space. The matter is made worse by kernels. Thus, SVM is commonly used in applications that do not required human understanding. 3.9 K-Nearest Neighbor Learning All the previous learning methods learn some kinds of models from the training data, e.g., decision trees, sets of rules, posterior probabilities, and hyperplanes. These learning methods are often called eager learning me- thods as they learn models of the data before testing. In contrast, k-nearest neighbor (kNN) is a lazy learning method in the sense that no model is learned from the training data. Learning only occurs when a test example needs to be classified. The idea of kNN is extremely simple and yet quite effective in many applications, e.g., text classification. It works as follows: Again let D be the training data set. Nothing will be done on the training examples. When a test instance d is presented, the al- gorithm compares d with every training example in D to compute the simi- larity or distance between them. The k most similar (closest) examples in D are then selected. This set of examples is called the k nearest neighbors of d. d then takes the most frequent class among the k nearest neighbors. Note that k = 1 is usually not sufficient for determining the class of d due to noise and outliers in the data. A set of nearest neighbors is needed to ac- curately decide the class. The general kNN algorithm is given in Fig. 3.23. 3.9 K-Nearest Neighbor Learning 125 2 Choose the k examples in D that are nearest to d, denote the set by P ( D); 3 Assign d the class that is the most frequent class in P (or the majority class). Fig. 3.23. The k-nearest neighbor algorithm The key component of a kNN algorithm is the distance/similarity func- tion, which is chosen based on applications and the nature of the data. For relational data, the Euclidean distance is commonly used. For text docu- ments, cosine similarity is a popular choice. We will introduce these dis- tance functions and many others in the next chapter. The number of nearest neighbors k is usually determined by using a va- lidation set, or through cross validation on the training data. That is, a range of k values are tried, and the k value that gives the best accuracy on the validation set (or cross validation) is selected. Fig. 3.21 illustrates the importance of choosing the right k. Example 20: In Fig. 3.24, we have two classes of data, positive (filled squares) and negative (empty circles). If 1-nearest neighbor is used, the test data point  will be classified as negative, and if 2-nearest neighbors are used, the class cannot be decided. If 3-nearest neighbors are used, the class is positive as two positive examples are in the 3-nearest neighbors. Fig. 3.24. An illustration of k-nearest neighbor classification Despite its simplicity, researchers have showed that the classification accuracy of kNN can be quite strong and in many cases as accurate as those elaborated methods. For instance, it is showed in [62] that kNN per- forms equally well as SVM for some text classification tasks. kNN is also very flexible. It can work with any arbitrarily shaped decision boundaries. kNN is, however, slow at the classification time. Due to the fact that there is no model building, each test instance is compared with every train- ing example at the classification time, which can be quite time consuming especially when the training set D and the test set are large. Another disad- vantage is that kNN does not produce an understandable model. It is thus not applicable if an understandable model is required in the application. 1-nearst neighbor 2-nearst neighbor 3-nearst neighbor Algorithm kNN(D, d, k) 1 Compute the distance between d and every example in D; 126 3 Supervised Learning 3.10 Ensemble of Classifiers So far, we have studied many individual classifier building techniques. A natural question to ask is: can we build many classifiers and then combine them to produce a better classifier? Yes, in many cases. This section de- scribes two well known ensemble techniques, bagging and boosting. In both these methods, many classifiers are built and the final classification decision for each test instance is made based on some forms of voting of the committee of classifiers. 3.10.1 Bagging Given a training set D with n examples and a base learning algorithm, bag- ging (for Bootstrap Aggregating) works as follows [4]: Training: 1. Create k bootstrap samples S1, S2, and Sk. Each sample is produced by drawing n examples at random from D with replacement. Such a sample is called a bootstrap replicate of the original training set D. On aver- age, each sample Si contains 63.2% of the original examples in D, with some examples appearing multiple times. 2. Build a classifier based on each sample Si. This gives us k classifiers. All the classifiers are built using the same base learning algorithm. Testing: Classify each test (or new) instance by voting of the k classifiers (equal weights). The majority class is assigned as the class of the instance. Bagging can improve the accuracy significantly for unstable learning algorithms, i.e., a slight change in the training data resulting in a major change in the output classifier. Decision tree and rule induction methods are examples of unstable learning methods. k-nearest neighbor and naïve Bayesian methods are examples of stable techniques. For stable classifiers, Bagging may sometime degrade the accuracy. 3.10.2 Boosting Boosting is a family of ensemble techniques, which, like bagging, also manipulates the training examples and produces multiple classifiers to im- prove the classification accuracy [53]. Here we only describe the popular AdaBoost algorithm given in [20]. Unlike bagging, AdaBoost assigns a weight to each training example. 3.10 Ensemble of Classifiers 127 Training: AdaBoost produces a sequence of classifiers (also using the same base learner). Each classifier is dependent on the previous one, and focuses on the previous one’s errors. Training examples that are incor- rectly classified by the previous classifiers are given higher weights. Let the original training set D be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi is an input vector, yi is its class label and yi  Y (the set of class labels). With a weight attached to each example, we have, {(x1, y1, w1), (x2, y2, w2), …, (xn, yn, wn)}, and i wi = 1. The AdaBoost algorithm is given in Fig. 3.25. The algorithm builds a sequence of k classifiers (k is specified by the user) using a base learner, called BaseLeaner in line 3. Initially, the weight for each training example is 1/n (line 1). In each iteration, the training data set becomes Dt, which is the same as D but with different weights. Each it- eration builds a new classifier ft (line 3). The error of ft is calculated in line 4. If it is too large, delete the iteration and exit (lines 5–7). Lines 9–11 up- date and normalize the weights for building the next classifier. AdaBoost(D, Y, BaseLeaner, k) 1. Initialize D1(wi)  1/n for all i; // initialize the weights 2. for t = 1 to k do 3. ft  BaseLearner(Dt); // build a new classifier ft 4.    iitt yDfi itt wDe ))((: )( x ; // compute the error of ft 5. if et > ½ then // if the error is too large, 6. k  k – 1; // remove the iteration and 7. exit-loop // exit 8. else 9. t  et / (1 et); 10 Dt+1(wi)  Dt(wi)  ; otherwise1 ))(( if     iittt yDf x // update the weights 11. Dt+1(wi)      n i it it wD wD 1 1 1 )( )( // normalize the weights 12. endif 13. endfor 14.    yft tYy final t f )(: 1logmaxarg )( x x  // the final output classifier Fig. 3.25. The AdaBoost algorithm 128 3 Supervised Learning Testing: For each test case, the results of the series of classifiers are com- bined to determine the final class of the test case, which is shown in line 14 of Fig. 3.25 (a weighted voting). Boosting works better than bagging in most cases as shown in [48]. It also tends to improve performance more when the base learner is unstable. Bibliographic Notes Supervised learning has been studied extensively by the machine learning community. The book by Mitchell [45] covers most learning techniques and is easy to read. Duda et al.’s pattern classification book is also a great reference [17]. Most data mining books have one or two chapters on su- pervised learning, e.g., those by Han and Kamber [24], Hand et al. [25], Tan et al. [56], and Witten and Frank [59]. For decision tree induction, Quinlan’s book [49] has all the details and the code of his popular decision tree system C4.5. Other well-known sys- tems include CART by Breiman et al. [6] and CHAD by Kass [28]. Scal- ing up of decision tree algorithms was also studied in several papers. These algorithms can have the data on disk, and are thus able to run with huge data sets. See [22] for an algorithm and also additional references. Rule induction algorithms generate rules directly from the data. Well- known systems include AQ by Michalski et al. [44], CN2 by Clark and Niblett [9], FOIL by Quinlan [50], FOCL by Pazzani et al. [47], I-REP by Furnkranz and Widmer [21], and RIPPER by Cohen [10]. Using association rules to build classifiers was proposed by Liu et al. in [39], which also reported the CBA system. CBA selects a small subset of class association rules as the classifier. Other classifier building techniques include combining multiple rules by Li et al. [36], using rules as features by Meretakis and Wüthrich [43], Antonie and Zaiane [2], Deshpande and Karypis [13], and Lesh et al. [31], generating a subset of rules by Cong et al. [11], Wang et al. [58], Yin and Han [63], and Zaki and Aggarwal [64]. Additional systems include those by Li et al. [35], Yang et al. [61], etc. The naïve Bayesian classification model described in Sect. 3.6 is based on the papers by Domingos and Pazzani [15], Kohavi et al. [29] and Lang- ley et al [30]. The naïve Bayesian classification for text discussed in Sect. 3.7 is based on the multinomial formulation given by McCallum and Ni- gam [42]. This model was also used earlier by Lewis and Gale [33], and Li and Yamanishi [34]. Another formulation of naïve Bayesian classification is based on the multivariate Bernoulli model, which was used by Lewis [32], and Robertson and Sparck-Jones [52]. Bibliography 129 Support vector machines (SVM) was first introduced by Vapnik and his colleagues in 1992 [3]. Further details are given in his 1995 book [57]. Two other books on SVM and kernel methods are those by Cristianini and Shawe-Taylor [12] and Scholkopf and Smola [54]. The discussion on SVM in this chapter is heavily influenced by Cristianini and Shawe- Taylor’s book and the tutorial paper by Burges [8]. Two popular SVM sys- tems are SVMLight (available at http://svmlight.joachims.org/) and LIBSVM (available at http://www.csie.ntu.edu.tw/~cjlin/libsvm/). Existing classifier ensemble methods include bagging by Breiman [4], boosting by Schapire [53] and Freund and Schapire [20], random forest al- so by Breiman [5], stacking by Wolpert [60], random trees by Fan [18], and many others. Bibliography 1. Agrawal, R., R. Bayardo, and R. Srikant. Athena: Mining-based interactive management of text databases. Advances in Database Technology—EDBT 2000, 2000: p. 365-379. 2. Antonie, M. and O. Zaïane. Text document categorization by term association. In Proceedings of IEEE International Conference on Data Minig (ICDM-2002), 2002. 3. Boser, B., I. Guyon, and V. Vapnik. A training algorithm for optimal margin classifiers. In Proceedings of Fifth Annual Workshop on Computational Learning Theory, 1992. 4. Breiman, L. Bagging predictors. Machine learning, 1996, 24(2): p. 123-140. 5. Breiman, L. Random forests. Machine learning, 2001, 45(1): p. 5-32. 6. Breiman, L., J.H. Friedman, R. Olshen, and C.L. Stone. Classification and Regression Trees. 1984: Chapman and Hall. 7. Brunk, C. and M. Pazzani. An investigation of noise-tolerant relational concept learning algorithms. In Proceedings of International Workshop on Macine Learning, 1991. 8. Burges, C. A tutorial on support vector machines for pattern recognition. Data mining and knowledge discovery, 1998, 2(2): p. 121-167. 9. Clark, P. and T. Niblett. The CN2 induction algorithm. Machine learning, 1989, 3(4): p. 261-283. 10. Cohen, W. Fast effective rule induction. In Proceedings of International Conference on Machine Learning (ICML-1995), 1995. 11. Cong, G., A. Tung, X. Xu, F. Pan, and J. Yang. Farmer: Finding interesting rule groups in microarray datasets. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-2004), 2004. 12. Cristianini, N. and J. Shawe-Taylor. An introduction to support Vector Machines: and other kernel-based learning methods. 2000: Cambridge Univ Press. 130 3 Supervised Learning 13. Deshpande, M. and G. Karypis. Using conjunction of attribute values for classification. In Proceedings of ACM Intl. Conf. on Information and Knowledge Management (CIKM-2002), 2002. 14. Dietterich, T. and G. Bakiri. Solving multiclass learning problems via error- correcting output codes. Journal of Artificial Intelligence Research, 1995, 2. 15. Domingos, P. and M. Pazzani. On the optimality of the simple Bayesian classifier under zero-one loss. Machine learning, 1997, 29(2): p. 103-130. 16. Dougherty, J., R. Kohavi, and M. Sahami. Supervised and unsupervised discretization of continuous features. In Proceedings of International Conference on Machine Learning (ICML-1995), 1995. 17. Duda, R., P. Hart, and D. Stork. Pattern classification. 2001: John Wiley & Sons Inc. 18. Fan, W. On the optimality of probability estimation by random decision trees. In Proceedings of National Conf. on Artificial Intelligence (AAAI-2004), 2004. 19. Fayyad, U. and K. Irani. Multi-interval discretization of continuous-valued attributes for classification learning. In Proceedings of the Intl. Joint Conf. on Artificial Intelligence (IJCAI-1993), 1993. 20. Freund, Y. and R. Schapire. Experiments with a new boosting algorithm. In Proceedings of International Conference on Machine Learning (ICML-1996), 1996. 21. Fürnkranz, J. and G. Widmer. Incremental reduced error pruning. In Proceedings of International Conference on Machine Learning (ICML-1994), 1994. 22. Gehrke, J., R. Ramakrishnan, and V. Ganti. RainForest—a framework for fast decision tree construction of large datasets. Data mining and knowledge discovery, 2000, 4(2): p. 127-162. 23. Good, I. The estimation of probabilities: an essay on modern Bayesian methods. 1965: MIT Press. 24. Han, J. and M. Kamber. Data mining: concepts and techniques. 2006: Morgan Kaufmann Publishers. 25. Hand, D., H. Mannila, and P. Smyth. Principles of data mining. 2001: MIT Press. 26. Hyafil, L. and R. Rivest. Constructing optimal binary decision trees is NP- complete. Information Processing Letters, 1976, 5(1): p. 15-17. 27. Jindal, N. and B. Liu. Identifying comparative sentences in text documents. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2006), 2006. 28. Kass, G. An exploratory technique for investigating large quantities of categorical data. Applied statistics, 1980, 29(2): p. 119-127. 29. Kohavi, R., B. Becker, and D. Sommerfield. Improving simple bayes. In Proceedings of European Conference on Machine Learning (ECML-1997), 1997. 30. Langley, P., W. Iba, and K. Thompson. An analysis of Bayesian classifiers. In Proceedings of National Conf. on Artificial Intelligence (AAAI-1992), 1992. Bibliography 131 31. Lesh, N., M. Zaki, and M. Ogihara. Mining features for sequence classification. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 32. Lewis, D. An evaluation of phrasal and clustered representations on a text categorization task. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1992), 1992. 33. Lewis, D. and W. Gale. A sequential algorithm for training text classifiers. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1994), 1994. 34. Li, H. and K. Yamanishi. Document classification using a finite mixture model. In Proceedings of Annual Meeting of the Association for Computational Linguistics (ACL-1997), 1997. 35. Li, J., G. Dong, K. Ramamohanarao, and L. Wong. DeEPs: A new instance- based lazy discovery and classification system. Machine learning, 2004, 54(2): p. 99-124. 36. Li, W., J. Han, and J. Pei. CMAR: Accurate and efficient classification based on multiple class-association rules. In Proceedings of IEEE International Conference on Data Mining (ICDM-2001), 2001. 37. Lidstone, G. Note on the General Case of the Bayes-Laplace formula for Inductive or a Posteriori Probabilities. Transaction of the Faculty of Actuuaries, 1920, 8: p. 182-192. 38. Lin, W., S. Alvarez, and C. Ruiz. Efficient adaptive-support association rule mining for recommender systems. Data mining and knowledge discovery, 2002, 6(1): p. 83-105. 39. Liu, B., W. Hsu, and Y. Ma. Integrating classification and association rule mining. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1998), 1998. 40. Liu, B., Y. Ma, and C. Wong. Classification using association rules: weaknesses and enhancements. Data mining for scientific applications, 2001. 41. Liu, B., K. Zhao, J. Benkler, and W. Xiao. Rule interestingness analysis using OLAP operations. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2006), 2006. 42. McCallum, A. and K. Nigam. A comparison of event models for naive bayes text classification. In Proceedings of AAAI–98 Workshop on Learning for Text Categorization, 1998. 43. Meretakis, D. and B. Wuthrich. Extending na ve Bayes classifiers using long itemsets. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 44. Michalski, R., I. Mozetic, J. Hong, and N. Lavrac. The multi-purpose incremental learning system AQ15 and its testing application to three medical domains. In Proceedings of National Conf. on Artificial Intelligence (AAAI- 86), 1986. 45. Mitchell, T. Machine Learning. 1997: McGraw Hill. 46. Mobasher, B., H. Dai, T. Luo, and M. Nakagawa. Effective personalization based on association rule discovery from web usage data. In Proceedings of ACM Workshop on Web Information and Data Management, 2001. 132 3 Supervised Learning 47. Pazzani, M., C. Brunk, and G. Silverstein. A knowledge-intensive approach to learning relational concepts. In Proceedings of Intl. Workshop on Machine Learning (ML-1991), 1991. 48. Quinlan, J. Bagging, boosting, and C4. 5. In Proceedings of National Conf. on Artificial Intelligence (AAAI-1996), 1996. 49. Quinlan, J. C4. 5: programs for machine learning. 1993: Morgan Kaufmann Publishers. 50. Quinlan, J. Learning logical definitions from relations. Machine learning, 1990, 5(3): p. 239-266. 51. Rivest, R. Learning decision lists. Machine learning, 1987, 2(3): p. 229-246. 52. Robertson, S. and K. Jones. Relevance weighting of search terms. Journal of the American Society for Information Science, 1976, 27(3): p. 129-146. 53. Schapire, R. The strength of weak learnability. Machine learning, 1990, 5(2): p. 197-227. 54. Scholkopf, B. and A. Smola. Learning with kernels. 2002: MIT Press. 55. Shannon, E. A mathematical theory of communication. Bell System Technical Journal, 1948, 27: p. 379–423. 56. Tan, P., M. Steinbach, and V. Kumar. Introduction to data mining. 2006: Pearson Addison Wesley Boston. 57. Vapnik, V. The nature of statistical learning theory. 1995: Springer Verlag. 58. Wang, K., S. Zhou, and Y. He. Growing decision trees on support-less association rules. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2000), 2000: ACM. 59. Witten, I. and E. Frank. Data Mining: Practical machine learning tools and techniques. 2005: Morgan Kaufmann Publishers. 60. Wolpert, D. Stacked Generalization. Neural Networks, 1992, 5: p. 241–259. 61. Yang, Q., T. Li, and K. Wang. Building association-rule based sequential classifiers for web-document prediction. Data mining and knowledge discovery, 2004, 8(3): p. 253-273. 62. Yang, Y. and X. Liu. A re-examination of text categorization methods. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1999), 1999. 63. Yin, X. and J. Han. CPAR: Classification based on predictive association rules. In Proceedings of SIAM International Conference on Data Mining (SDM-2003), 2003. 64. Zaki, M. and C. Aggarwal. XRules: an effective structural classifier for XML data. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2003), 2003. 4 Unsupervised Learning Supervised learning discovers patterns in the data that relate data attributes to a class attribute. These patterns are then utilized to predict the values of the class attribute of future data instances. These classes indicate some real-world predictive or classification tasks such as determining whether a news article belongs to the category of sports or politics, or whether a pa- tient has a particular disease. However, in some other applications, the data have no class attributes. The user wants to explore the data to find some in- trinsic structures in them. Clustering is one technology for finding such structures. It organizes data instances into similarity groups, called clus- ters such that the data instances in the same cluster are similar to each oth- er and data instances in different clusters are very different from each oth- er. Clustering is often called unsupervised learning, because unlike supervised learning, class values denoting an a priori partition or grouping of the data are not given. Note that according to this definition, we can also say that association rule mining is an unsupervised learning task. However, due to historical reasons, clustering is closely associated and even syn- onymous with unsupervised learning while association rule mining is not. We follow this convention, and describe some main clustering techniques in this chapter. Clustering has been shown to be one of the most commonly used data analysis techniques. It also has a long history, and has been used in almost every field, e.g., medicine, psychology, botany, sociology, biology, arche- ology, marketing, insurance, library science, etc. In recent years, due to the rapid increase of online documents and the expansion of the Web, text document clustering too has become a very important task. In Chap. 12, we will also see that clustering is very useful in Web usage mining. 4.1 Basic Concepts Clustering is the process of organizing data instances into groups whose members are similar in some way. A cluster is therefore a collection of da- ta instances which are “similar” to each other and are “dissimilar” to data B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_4, © Springer-Verlag Berlin Heidelberg 2011 133 134 4 Unsupervised Learning instances in other clusters. In the clustering literature, a data instance is al- so called an object as the instance may represent an object in the real- world. It is also called a data point as it can be seen as a point in an r- dimension space, where r is the number of attributes in the data. Fig. 4.1 shows a 2-dimensional data set. We can clearly see three groups of data points. Each group is a cluster. The task of clustering is to find the three clusters hidden in the data. Although it is easy for a human to visu- ally detect clusters in a 2-dimensional or even 3-demensional space, it be- comes very hard, if not impossible, to detect clusters visually as the num- ber of dimensions increases. Additionally, in many applications, clusters are not as clear-cut or well separated as the three clusters in Fig. 4.1. Au- tomatic techniques are thus needed for clustering. Fig. 4.1. Three natural groups or clusters of data points After seeing the example in Fig. 4.1, you may ask the question: What is clustering for? To answer it, let us see some application examples from different domains. Example 1: A company wants to conduct a marketing campaign to pro- mote its products. The most effective strategy is to design a set of person- alized marketing materials for each individual customer according to his/her profile and financial situation. However, this is too expensive for a large number of customers. At the other extreme, the company designs only one set of marketing materials to be used for all customers. This one- size-fits-all approach, however, may not be effective. The most cost- effective approach is to segment the customers into a small number of groups according to their similarities and design some targeted marketing materials for each group. This segmentation task is commonly done using clustering algorithms, which partition customers into similarity groups. In marketing research, clustering is often called segmentation. ▀ Example 2: A company wants to produce and sell T-shirts. Similar to the case above, on one extreme, for each customer it can measure his/her size and have a T-shirt tailor-made for him/her. Obviously, this T-shirt is going to be expensive. On the other extreme, only one size of T-shirts is made. 4.1 Basic Concepts 135 Since this size may not fit most people, the company might not be able to sell as many T-shirts. Again, the most cost effective way is to group people based on their sizes and make a different generalized size of T-shirts for each group. This is why we see small, medium and large size T-shirts in shopping malls, and seldom see T-shirts with only a single size. The me- thod used to group people according to their sizes is clustering. The proc- ess is usually as follows: The T-shirt manufacturer first samples a large number of people and measure their sizes to produce a measurement data- base. It then clusters the data, which partitions the data into some similar- ity subsets, i.e., clusters. For each cluster, it computes the average of the sizes and then uses the average to mass-produce T-shirts for all people of similar size. ▀ Example 3: Everyday, news agencies around the world generate a large number of news articles. If a Web site wants to collect these news articles to provide an integrated news service, it has to organize the collected arti- cles according to some topic hierarchy. The question is: What should the topics be, and how should they be organized? One possibility is to employ a group of human editors to do the job. However, the manual organization is costly and very time consuming, which makes it unsuitable for news and other time sensitive information. Throwing all the news articles to the readers with no organization is clearly not an option. Although classifica- tion is able to classify news articles according to predefined topics, it is not applicable here because classification needs training data, which have to be manually labeled with topic classes. Since news topics change constantly and rapidly, the training data would need to change constantly as well, which is infeasible via manual labeling. Clustering is clearly a solution for this problem because it automatically groups a stream of news articles based on their content similarities. Hierarchical clustering algorithms can also organize documents hierarchically, i.e., each topic may contain sub-topics and so on. Topic hierarchies are particularly useful for texts. ▀ The above three examples indicate two types of clustering, partitional and hierarchical. Indeed, these are the two most important types of clus- tering approaches. We will study some specific algorithms of these two types of clustering. Our discussion and examples above also indicate that clustering needs a similarity function to measure how similar two data points (or objects) are, or alternatively a distance function to measure the distance between two data points. We will use distance functions in this chapter. The goal of clustering is thus to discover the intrinsic grouping of the input data through the use of a clustering algorithm and a distance function. 136 4 Unsupervised Learning 4.2 K-means Clustering The k-means algorithm is the best known partitional clustering algo- rithm. It is perhaps also the most widely used among all clustering algo- rithms due to its simplicity and efficiency. Given a set of data points and the required number of k clusters (k is specified by the user), this algorithm iteratively partitions the data into k clusters based on a distance function. 4.2.1 K-means Algorithm Let the set of data points (or instances) D be {x1, x2, …, xn}, where xi = (xi1, xi2, …, xir) is a vector in a real-valued space X   r, and r is the number of attributes in the data (or the number of dimensions of the data space). The k-means algorithm partitions the given data into k clus- ters. Each cluster has a cluster center, which is also called the cluster cen- troid. The centroid, usually used to represent the cluster, is simply the mean of all the data points in the cluster, which gives the name to the algo- rithm, i.e., since there are k clusters, thus k means. Fig. 4.2 gives the k- means clustering algorithm. At the beginning, the algorithm randomly selects k data points as the seed centroids. It then computes the distance between each seed centroid and every data point. Each data point is assigned to the centroid that is closest to it. A centroid and its data points therefore represent a cluster. Once all the data points in the data are assigned, the centroid for each clus- ter is re-computed using the data points in the current cluster. This process repeats until a stopping criterion is met. The stopping (or convergence) cri- terion can be any one of the following: Algorithm k-means(k, D) 1 choose k data points as the initial centroids (cluster centers) 2 repeat 3 for each data point x  D do 4 compute the distance from x to each centroid; 5 assign x to the closest centroid // a centroid represents a cluster 6 endfor 7 re-compute the centroid using the current cluster memberships 8 until the stopping criterion is met Fig. 4.2. The k-means algorithm 4.2 K-means Clustering 137 1. no (or minimum) re-assignments of data points to different clusters. 2. no (or minimum) change of centroids. 3. minimum decrease in the sum of squared error (SSE), ,),( 1 2   k jC j j distSSE x mx (1) where k is the number of required clusters, Cj is the jth cluster, mj is the centroid of cluster Cj (the mean vector of all the data points in Cj), and dist(x, mj) is the distance between data point x and centroid mj. The k-means algorithm can be used for any application data set where the mean can be defined and computed. In Euclidean space, the mean of a cluster is computed with: ,|| 1    ji C i j j C x xm (2) where |Cj| is the number of data points in cluster Cj. The distance from a data point xi to a cluster mean (centroid) mj is computed with .)(...)()( ||||),( 22 22 2 11 jrirjiji jiji mxmxmx dist   mxmx (3) Example 4: Fig. 4.3(A) shows a set of data points in a 2-dimensional space. We want to find 2 clusters from the data, i.e., k = 2. First, two data points (each marked with a cross) are randomly selected to be the initial centroids (or seeds) shown in Fig. 4.3(A). The algorithm then goes to the first iteration (the repeat-loop). Iteration 1: Each data point is assigned to its closest centroid to form 2 clusters. The resulting clusters are given in Fig. 4.3(B). Then the cen- troids are re-computed based on the data points in the current clusters (Fig. 4.3(C)). This leads to iteration 2. Iteration 2: Again, each data point is assigned to its closest new centroid to form two new clusters shown in Fig. 4.3(D). The centroids are then re- computed. The new centroids are shown in Fig. 4.3(E). Iteration 3: The same operations are performed as in the first two itera- tions. Since there is no re-assignment of data points to different clusters in this iteration, the algorithm ends. The final clusters are those given in Fig. 4.3(G). The set of data points in each cluster and its centroid are output to the user. 138 4 Unsupervised Learning Fig. 4.3. The working of the k-means algorithm through an example ▀ One problem with the k-means algorithm is that some clusters may be- come empty during the clustering process since no data point is assigned to them. Such clusters are called empty clusters. To deal with an empty clus- ter, we can choose a data point as the replacement centroid, e.g., a data point that is furthest from the centroid of a large cluster. If the sum of the squared error (SSE) is used as the stopping criterion, the cluster with the largest squared error may be used to find another centroid. + + + + + + (A). Random selection of k seeds (or centroids) + + + + + + Iteration 2: (D). Cluster assignment (E). Re-compute centroids + + Iteration 3: (F). Cluster assignment (G). Re-compute centroids Iteration 1: (B). Cluster assignment (C). Re-compute centroids 4.2 K-means Clustering 139 4.2.2 Disk Version of the K-means Algorithm The k-means algorithm may be implemented in such a way that it does not need to load the entire data set into the main memory, which is useful for large data sets. Notice that the centroids for the k clusters can be computed incrementally in each iteration because the summation in Equation (2) can be calculated separately first. During the clustering process, the number of data points in each cluster can be counted incrementally as well. This gives us a disk based implementation of the algorithm (Fig. 4.4), which produces exactly the same clusters as that in Fig. 4.2, but with the data on disk. In each for-loop, the algorithm simply scans the data once. The whole clustering process thus scans the data t times, where t is the number of iterations before convergence, which is usually not very large (< 50). In applications, it is quite common to set a limit on the number of iterations because later iterations typically result in only minor changes to the clusters. Thus, this algorithm may be used to cluster large data sets which cannot be loaded into the main memory. Although there are several special algorithms that scale-up clustering algorithms to large data sets, they all require sophisticated techniques. Algorithm disk-k-means(k, D) 1 Choose k data points as the initial centriods mj, j = 1, …, k; 2 repeat 3 initialize sj  0, j = 1, …, k; // 0 is a vector with all 0’s 4 initialize nj  0, j = 1, …, k; // nj is the number of points in cluster j 5 for each data point x  D do 6 );,(minarg },...2,1{ i ki distj mx   7 assign x to the cluster j; 8 sj  sj + x; 9 nj  nj + 1; 10 endfor 11 mj  sj/nj, j = 1, …, k; 12 until the stopping criterion is met Fig. 4.4. A simple disk version of the k-means algorithm Let us give some explanations of this algorithm. Line 1 does exactly the same thing as the algorithm in Fig. 4.2. Line 3 initializes vector sj which is used to incrementally compute the sum in Equation (2) (line 8). Line 4 ini- tializes nj which records the number of data points assigned to cluster j (line 9). Lines 6 and 7 perform exactly the same tasks as lines 4 and 5 in the original algorithm in Fig. 4.2. Line 11 re-computes the centroids, 140 4 Unsupervised Learning which are used in the next iteration. Any of the three stopping criteria may be used here. If the sum of squared error is applied, we can modify the al- gorithm slightly to compute the sum of square error incrementally. 4.2.3 Strengths and Weaknesses The main strengths of the k-means algorithm are its simplicity and effi- ciency. It is easy to understand and easy to implement. Its time complexity is O(tkn), where n is the number of data points, k is the number of clusters, and t is the number of iterations. Since both k and t are normally much smaller than n, the k-means algorithm is considered a linear algorithm in the number of data points. The weaknesses and ways to address them are as follows: 1. The algorithm is only applicable to data sets where the notion of the mean is defined. Thus, it is difficult to apply to categorical data sets. There is, however, a variation of the k-means algorithm called k-modes, which clusters categorical data. The algorithm uses the mode instead of the mean as the centroid. Assuming that the data instances are described by r categorical attributes, the mode of a cluster Cj is a tuple mj = (mj1, mj2, …, mjr) where mji is the most frequent value of the ith attribute of the data instances in cluster Cj. The similarity (or distance) between a data instance and a mode is the number of values that they match (or do not match). 2. The user needs to specify the number of clusters k in advance. In prac- tice, several k values are tried and the one that gives the most desirable result is selected. We will discuss the evaluation of clusters later. 3. The algorithm is sensitive to outliers. Outliers are data points that are very far away from other data points. Outliers could be errors in the data recording or some special data points with very different values. For ex- ample, in an employee data set, the salary of the Chief-Executive- Officer (CEO) of the company may be considered as an outlier because its value could be many times larger than everyone else. Since the k- means algorithm uses the mean as the centroid of each cluster, outliers may result in undesirable clusters as the following example shows. Example 5: In Fig. 4.5(A), due to an outlier data point, the two result- ing clusters do not reflect the natural groupings in the data. The ideal clusters are shown in Fig. 4.5(B). The outlier should be identified and reported to the user. ▀ There are several methods for dealing with outliers. One simple me- thod is to remove some data points in the clustering process that are 4.2 K-means Clustering 141 much further away from the centroids than other data points. To be safe, we may want to monitor these possible outliers over a few iterations and then decide whether to remove them. It is possible that a very small cluster of data points may be outliers. Usually, a threshold value is used to make the decision. Fig. 4.5. Clustering with and without the effect of outliers ▀ Another method is to perform random sampling. Since in sampling we only choose a small subset of the data points, the chance of selecting an outlier is very small. We can use the sample to do a pre-clustering and then assign the rest of the data points to these clusters, which may be done in any of the three ways below:  Assign each remaining data point to the centroid closest to it. This is the simplest method.  Use the clusters produced from the sample to perform supervised learning (classification). Each cluster is regarded as a class. The clus- tered sample is thus treated as the training data for learning. The re- sulting classifier is then applied to classify the remaining data points into appropriate classes or clusters.  Use the clusters produced from the sample as seeds to perform semi- supervised learning. Semi-supervised learning is a new learning model that learns from a small set of labeled examples (with classes) and a large set of unlabeled examples (without classes). In our case, the clustered sample data are used as the labeled set and the remain- ing data points are used as the unlabeled set. The results of the learn- ++ outlier ++ outlier (A): Undesirable clusters (B): Ideal clusters 142 4 Unsupervised Learning ing naturally cluster all the remaining data points. We will study this technique in the next chapter. 4. The algorithm is sensitive to initial seeds, which are the initially se- lected centroids. Different initial seeds may result in different clusters. Thus, if the sum of squared error is used as the stopping criterion, the algorithm only achieves local optimal. The global optimal is computa- tionally infeasible for large data sets. Example 6: Fig. 4.6 shows the clustering process of a 2-dimensional da- ta set. The goal is to find two clusters. The randomly selected initial seeds are marked with crosses in Fig. 4.6(A). Fig. 4.6(B) gives the clus- tering result of the first iteration. Fig. 4.6(C) gives the result of the sec- ond iteration. Since there is no re-assignment of data points, the algo- rithm stops. Fig. 4.6. Poor initial seeds (centroids) If the initial seeds are different, we may obtain entirely different clus- ters as Fig. 4.7 shows. Fig. 4.7 uses the same data as Fig. 4.6, but differ- ent initial seeds (Fig. 4.7(A)). After two iterations, the algorithm ends, and the final clusters are given in Fig. 4.7(C). These two clusters are more reasonable than the two clusters in Fig. 4.6(C), which indicates that the choice of the initial seeds in Fig. 4.6(A) is poor. To select good initial seeds, researchers have proposed several meth- ods. One simple method is to first compute the mean m (the centroid) of the entire data set (any random data point rather than the mean can be + + (A). Random selection of seeds (centroids) (B). Iteration 1 (C). Iteration 2 + + + + 4.2 K-means Clustering 143 used as well). Then the first seed data point x1 is selected to be the fur- thest from the mean m. The second data point x2 is selected to be the furthest from x1. Each subsequent data point xi is selected such that the sum of distances from xi to those already selected data points is the larg- est. However, if the data has outliers, the method will not work well. To deal with outliers, again, we can randomly select a small sample of the data and perform the same operation on the sample. As we discussed above, since the number of outliers is small, the chance that they show up in the sample is very small. Fig. 4.7. Good initial seeds (centroids) ▀ Another method is to sample the data and use the sample to perform hierarchical clustering, which we will discuss in Sect. 4.4. The centroids of the resulting k clusters are used as the initial seeds. Yet another approach is to manually select seeds. This may not be a difficult task for text clustering applications because it is easy for human users to read some documents and pick some good seeds. These seeds may help improve the clustering result significantly and also enable the system to produce clusters that meet the user’s needs. 5. The k-means algorithm is not suitable for discovering clusters that are not hyper-ellipsoids (or hyper-spheres). Example 7: Fig. 4.8(A) shows a 2-dimensional data set. There are two irregular shaped clusters. However, the two clusters are not hyper- (A). Random selection of k seeds (centroids) (B). Iteration 1 (C). Iteration 2 + + + + ++ 144 4 Unsupervised Learning ellipsoids, which means that the k-means algorithm will not be able to find them. Instead, it may find the two clusters shown in Fig. 4.8(B). The question is: are the two clusters in Fig. 4.8(B) necessarily bad? The answer is no. It depends on the application. It is not true that a clus- tering algorithm that is able to find arbitrarily shaped clusters is always better. We will discuss this issue in Sect. 4.3.2. Fig. 4.8. Natural (but irregular) clusters and k-means clusters ▀ Despite these weaknesses, k-means is still the most popular algorithm in practice due to its simplicity, efficiency and the fact that other clustering algorithms have their own lists of weaknesses. There is no clear evidence showing that any other clustering algorithm performs better than the k- means algorithm in general, although it may be more suitable for some specific types of data or applications than k-means. Note also that compar- ing different clustering algorithms is a very difficult task because unlike supervised learning, nobody knows what the correct clusters are, especially in high dimensional spaces. Although there are several cluster evaluation methods, they all have drawbacks. We will discuss the evaluation issue in Sect. 4.9. 4.3 Representation of Clusters Once a set of clusters is found, the next task is to find a way to represent the clusters. In some applications, outputting the set of data points that makes up the cluster to the user is sufficient. However, in other applica- tions that involve decision making, the resulting clusters need to be repre- sented in a compact and understandable way, which also facilitates the evaluation of the resulting clusters. (A): Two natural clusters (B): k-means clusters + + 4.3 Representation of Clusters 145 4.3.1 Common Ways of Representing Clusters There are three main ways to represent clusters: 1. Use the centroid of each cluster to represent the cluster. This is the most popular way. The centroid tells where the center of the cluster is. One may also compute the radius and standard deviation of the cluster to de- termine the spread in each dimension. The centroid representation alone works well if the clusters are of the hyper-spherical shape. If clusters are elongated or are of other shapes, centroids may not be suitable. 2. Use classification models to represent clusters. In this method, we treat each cluster as a class. That is, all the data points in a cluster are re- garded as having the same class label, e.g., the cluster ID. We then run a supervised learning algorithm on the data to find a classification model. For example, we may use the decision tree learning to distinguish the clusters. The resulting tree or set of rules provide an understandable rep- resentation of the clusters. Fig. 4.9 shows a partitioning produced by a decision tree algorithm. The original clustering gave three clusters. Data points in cluster 1 are represented by 1’s, data points in cluster 2 are represented by 2’s, and data points in cluster 3 are represented by 3’s. We can see that the three clusters are separated and each can be represented with a rule. x ≤ 2  cluster 1 x > 2, y > 1.5  cluster 2 x > 2, y ≤ 1.5  cluster 3 Fig. 4.9. Description of clusters using rules We make two remarks about this representation method:  The partitioning in Fig. 4.9 is an ideal case as each cluster is repre- sented by a single rectangle (or rule). However, in most applications, the situation may not be so ideal. A cluster may be split into a few 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 2 x y 1.5 2 2 146 4 Unsupervised Learning hyper-rectangles or rules. However, there is usually a dominant or large rule which covers most of the data points in the cluster.  One can use the set of rules to evaluate the clusters to see whether they conform to some existing domain knowledge or intuition. 3. Use frequent values in each cluster to represent it. This method is main- ly for clustering of categorical data (e.g., in the k-modes clustering). It is also the key method used in text clustering, where a small set of fre- quent words in each cluster is selected to represent the cluster. 4.3.2 Clusters of Arbitrary Shapes Hyper-elliptical and hyper-spherical clusters are usually easy to represent, using their centroids together with spreads (e.g., standard deviations), rules, or a combination of both. However, other arbitrary shaped clusters, like the natural clusters shown in Fig. 4.8(A), are hard to represent espe- cially in high dimensional spaces. A common criticism about an algorithm like k-means is that it is not able to find arbitrarily shaped clusters. However, this criticism may not be as bad as it sounds because whether one type of clustering is desirable or not depends on the application. Let us use the natural clusters in Fig. 4.8(A) to discuss this issue together with an artificial application. Example 8: Assume that the data shown in Fig. 4.8(A) is the measurement data of people’s physical sizes. We want to group people based on their sizes into only two groups in order to mass-produce T-shirts of only 2 sizes (say large and small). Even if the measurement data indicate two natural clusters as in Fig. 4.8(A), it is difficult to use the clusters because we need centroids of the clusters to design T-shirts. The clusters in Fig. 4.8(B) are in fact better because they provide us the centroids that are representative of the surrounding data points. If we use the centroids of the two natural clusters as shown in Fig. 4.10 to make T-shirts, it is clearly inappropriate because they are too near to each other in this case. In general, it does not make sense to define the concept of center or centroid for an irregularly shaped cluster. ▀ Note that clusters of arbitrary shapes can be found by neighborhood search algorithms such as some hierarchical clustering methods (see the next section), and density-based clustering methods [17]. Due to the diffi- culty of representing an arbitrarily shaped cluster, an algorithm that finds such clusters may only output a list of data points in each cluster, which are not as easy to use. These kinds of clusters are more useful in spatial and image processing applications, but less useful in others. 4.4 Hierarchical Clustering 147 Fig. 4.10. Two natural clusters and their centroids 4.4 Hierarchical Clustering Hierarchical clustering is another major clustering approach. It has a num- ber of desirable properties which make it popular. It clusters by producing a nested sequence of clusters like a tree (also called a dendrogram). Sin- gleton clusters (individual data points) are at the bottom of the tree and one root cluster is at the top, which covers all data points. Each internal cluster node contains child cluster nodes. Sibling clusters partition the data points covered by their common parent. Fig. 4.11 shows an example. Fig. 4.11. An illustration of hierarchical clustering At the bottom of the tree, there are 5 clusters (5 data points). At the next level, cluster 6 contains data points 1 and 2, and cluster 7 contains data points 4 and 5. As we move up the tree, we have fewer and fewer clusters. Since the whole clustering tree is stored, the user can choose to view clus- ters at any level of the tree. + + 9 8 6 1 32 7 4 5 148 4 Unsupervised Learning There are two main types of hierarchical clustering methods: Agglomerative (bottom up) clustering: It builds the dendrogram (tree) from the bottom level, and merges the most similar (or nearest) pair of clusters at each level to go one level up. The process continues until all the data points are merged into a single cluster (i.e., the root cluster). Divisive (top down) clustering: It starts with all data points in one cluster, the root. It then splits the root into a set of child clusters. Each child cluster is recursively divided further until only singleton clusters of in- dividual data points remain, i.e., each cluster with only a single point. Agglomerative methods are much more popular than divisive methods. We will focus on agglomerative hierarchical clustering. The general agglom- erative algorithm is given in Fig. 4.12. Algorithm Agglomerative(D) 1 Make each data point in the data set D a cluster, 2 Compute all pair-wise distances of x1, x2, …, xn  D; 2 repeat 3 find two clusters that are nearest to each other; 4 merge the two clusters form a new cluster c; 5 compute the distance from c to all other clusters; 12 until there is only one cluster left Fig. 4.12. The agglomerative hierarchical clustering algorithm Example 9: Fig. 4.13 illustrates the working of the algorithm. The data points are in a 2-dimensional space. Fig. 4.13(A) shows the sequence of nested clusters, and Fig. 4.13(B) gives the dendrogram. ▀ Fig. 4.13. The working of an agglomerative hierarchical clustering algorithm 4 3 1 p1 p3p2 p1 p2 p3 p4 p5 2 p4 p5 4 1 2 3 (A). Nested clusters (B) Dendrogram 4.4 Hierarchical Clustering 149 Unlike the k-means algorithm, which uses only the centroids in distance computation, hierarchical clustering may use anyone of several methods to determine the distance between two clusters. We introduce them next. 4.4.1 Single-Link Method In single-link (or single linkage) hierarchical clustering, the distance be- tween two clusters is the distance between two closest data points in the two clusters (one data point from each cluster). In other words, the single- link clustering merges the two clusters in each step whose two nearest data points (or members) have the smallest distance, i.e., the two clusters with the smallest minimum pair-wise distance. The single-link method is suit- able for finding non-elliptical shape clusters. However, it can be sensitive to noise in the data, which may cause the chain effect and produce strag- gly clusters. Fig. 4.14 illustrates this situation. The noisy data points (rep- resented with filled circles) in the middle connect two natural clusters and split one of them. Fig. 4.14. The chain effect of the single-link method With suitable data structures, single-link hierarchical clustering can be done in O(n2) time, where n is the number of data points. This is much slower than the k-means method, which performs clustering in linear time. 4.4.2 Complete-Link Method In complete-link (or complete linkage) clustering, the distance between two clusters is the maximum of all pair-wise distances between the data points in the two clusters. In other words, the complete-link clustering merges the two clusters in each step whose two furthest data points have the smallest distance, i.e., the two clusters with the smallest maximum pair-wise distance. Fig. 4.15 shows the clusters produced by complete-link clustering using the same data as in Fig. 4.14. 150 4 Unsupervised Learning Fig. 4.15. Clustering using the complete-link method Although the complete-link method does not have the problem of chain effects, it can be sensitive to outliers. Despite this limitation, it has been observed that the complete-link method usually produces better clusters than the single-link method. The worse case time complexity of the com- plete-link clustering is O(n2log n), where n is the number of data points. 4.4.3 Average-Link Method This is a compromise between the sensitivity of complete-link clustering to outliers and the tendency of single-link clustering to form long chains that do not correspond to the intuitive notion of clusters as compact, spherical objects. In this method, the distance between two clusters is the average distance of all pair-wise distances between the data points in two clusters. The time complexity of this method is also O(n2log n). Apart from the above three popular methods, there are several others. The following two methods are also commonly used: Centroid method: In this method, the distance between two clusters is the distance between their centroids. Ward's method: In this method, the distance between two clusters is de- fined as the increase in the sum of squared error (distances) from that of two clusters to that of one merged cluster. Thus, the clusters to be merged in the next step are the ones that will increase the sum the least. Recall that the sum of squared error (SSE) is one of the measures used in the k-means clustering (Equation (1)). 4.4.4. Strengths and Weaknesses Hierarchical clustering has several advantages compared to the k-means and other partitioning clustering methods. It is able to take any form of dis- tance or similarity function. Moreover, unlike the k-means algorithm which only gives k clusters at the end, the hierarchy of clusters from hier- 4.5 Distance Functions 151 archical clustering enables the user to explore clusters at any level of detail (or granularity). In many applications, this resulting hierarchy can be very useful in its own right. For example, in text document clustering, the clus- ter hierarchy may represent a topic hierarchy in the documents. Some studies have shown that agglomerative hierarchical clustering of- ten produces better clusters than the k-means method. It can also find clus- ters of arbitrary shapes, e.g., using the single-link method. Hierarchical clustering also has several weaknesses. As we discussed with the individual methods, the single-link method may suffer from the chain effect, and the complete-link method is sensitive to outliers. The main shortcomings of all hierarchical clustering methods are their compu- tation complexities and space requirements, which are at least quadratic. Compared to the k-means algorithm, this is very inefficient and not practi- cal for large data sets. One can use sampling to deal with the problems. A small sample is taken to do clustering and then the rest of the data points are assigned to each cluster either by distance comparison or by supervised learning (see Sect. 4.3.1). Some scale-up methods may also be applied to large data sets. The main idea of the scale-up methods is to find many small clusters first using an efficient algorithm, and then to use the cen- troids of these small clusters to represent the clusters to perform the final hierarchical clustering (see the BIRCH method in [54]). 4.5 Distance Functions Distance or similarity functions play a central role in all clustering algo- rithms. Numerous distance functions have been reported in the literature and used in applications. Different distance functions are also used for dif- ferent types of attributes (also called variables). 4.5.1 Numeric Attributes The most commonly used distance functions for numeric attributes are the Euclidean distance and Manhattan (city block) distance. Both distance measures are special cases of a more general distance function called the Minkowski distance. We use dist(xi, xj) to denote the distance between two data points of r dimensions. The Minkowski distance is: ,)||...|||(|),( 1 2211 hh jrir h ji h jiji xxxxxxdist xx (4) where h is a positive integer. 152 4 Unsupervised Learning If h = 2, it is the Euclidean distance, .)(...)()(),( 22 22 2 11 jrirjijiji xxxxxxdist xx (5) If h = 1, it is the Manhattan distance, .||...||||),( 2211 jrirjijiji xxxxxxdist xx (6) Other common distance functions include: Weighted Euclidean distance: A weight is associated with each attribute to express its importance in relation to other attributes. .)(...)()(),( 22 222 2 111 jrirrjijiji xxwxxwxxwdist xx (7) Squared Euclidean distance: the standard Euclidean distance is squared in order to place progressively greater weights on data points that are fur- ther apart. The distance is .)(...)()(),( 22 22 2 11 jrirjijiji xxxxxxdist xx (8) Chebychev distance: This distance measure is appropriate in cases where one wants to define two data points as “different” if they are different on any one of the attributes. The Chebychev distance is |).| ..., |,| |,max(|),( 2211 jrirjijiji xxxxxxdist xx (9) 4.5.2 Binary and Nominal Attributes The above distance measures are only appropriate for numeric attributes. For binary and nominal attributes (also called unordered categorical at- tributes), we need different functions. We discuss binary attributes first. A binary attribute has two states or values, usually represented by 1 and 0. The two states have no numerical ordering. For example, Gender has two values, male and female, which have no ordering relations but are just different. Existing distance functions for binary attributes are based on the proportion of value matches in two data points. A match means that, for a particular attribute, both data points have the same value. It is easy to use a confusion matrix to introduce these measures. Given the ith and jth data points, xi and xj, we can construct the confusion matrix in Fig. 4.16. To give the distance functions, we further divide binary attributes into symmetric and asymmetric attributes. For different types of attributes, different distance functions need to be used [31]: 4.5 Distance Functions 153 a: the number of attributes with the value of 1 for both data points. b: the number of attributes for which xif = 1 and xjf = 0, where xif (xjf) is the value of the fth attribute of the data point xi (xj). c: the number of attributes for which xif = 0 and xjf = 1. d: the number of attributes with the value of 0 for both data points. Fig. 4.16. Confusion matrix of two data points with only binary attributes Symmetric attributes: A binary attribute is symmetric if both of its states (0 and 1) have equal importance, and carry the same weight, e.g., male and female of the attribute Gender. The most commonly used distance function for symmetric attributes is the simple matching distance, which is the proportion of mismatches (Equation (10)) of their values. We assume that every attribute in the data set is a symmetric attribute. dcba cbdist ji  ),( xx (10) We can also weight some components in Equation (10) according to ap- plication needs. For example, we may want mismatches to carry twice the weight of matches, or vice versa: )(2 )(2),( cbda cbdist ji  xx (11) cbda cbdist ji   )(2),( xx (12) Example 10: Given the following two data points, where each attribute is a symmetric binary attribute, x1 1 1 1 0 1 0 0 x2 0 1 1 0 0 1 0 the distance computed based on the simple matching distance is Data point xj 1 0 1 a b a+b 0 c d c+d a+c b+d a+b+c+d Data point xi 154 4 Unsupervised Learning .429.07 3 2122 12),(  jidist xx (13) ▀ Asymmetric attributes: A binary attribute is asymmetric if one of the states is more important or valuable than the other. By convention, we use state 1 to represent the more important state, which is typically the rare or infrequent state. The most commonly used distance measure for asymmet- ric attributes is the Jaccard distance: .),( cba cbdist ji  xx (14) Similarly, we can vary the Jaccard distance by giving more weight to (b+c) or more weight to a to express different emphases. .)(2 )(2),( cba cbdist ji  xx (15) .2),( cba cbdist ji  xx (16) Note that there is also a Jaccard coefficient, which measures similarity (rather than distance) and is defined as a / (a+b+c). For general nominal attributes with more than two states or values, the commonly used distance measure is also based on the simple matching dis- tance. Given two data points xi and xj, let the number of attributes be r, and the number of values that match in xi and xj be q: .),( r qrdist ji xx (17) As that for binary attributes, we can give higher weights to different com- ponents in Equation (17) according to different application characteristics. 4.5.3 Text Documents Although a text document consists of a sequence of sentences and each sentence consists of a sequence of words, a document is usually considered as a “bag” of words in document clustering. The sequence and the position information of words are ignored. Thus a document can be represented as a vector just like a normal data point. However, we use similarity to com- pare two documents rather than distance. The most commonly used simi- 4.6 Data Standardization 155 larity function is the cosine similarity. We will study this similarity meas- ure in Sect. 6.2.2 when we discuss information retrieval and Web search. 4.6 Data Standardization One of the most important steps in data pre-processing for clustering is to standardize the data. For example, using the Euclidean distance, standardi- zation of attributes is highly recommended so that all attributes can have equal impact on the distance computation. This is to avoid obtaining clus- ters that are dominated by attributes with the largest amounts of variation. Example 11: In a 2-dimensional data set, the value range of one attribute is from 0 to 1, while the value range of the other attribute is from 0 to 1000. Consider the following pair of data points xi: (0.1, 20) and xj: (0.9, 720). The Euclidean distance between the two points is ,700.000457)20720()1.09.0(),( 22 jidist xx (18) which is almost completely dominated by (72020) = 700. To deal with the problem, we standardize the attributes, e.g., to force the attributes to have a common value range. If both attributes are forced to have a scale within the range 01, the values 20 and 720 become 0.02 and 0.72. The distance on the first dimension becomes 0.8 and the distance on the second dimension 0.7, which are more equitable. Then, dist(xi, xj) = 1.063. ▀ This example shows that standardizing attributes is important. In fact, different types of attributes require different treatments. We list these treatments below. Interval-scaled attributes: These are numeric/continuous attributes. Their values are real numbers following a linear scale. Examples of such attrib- utes are age, height, weight, cost, etc. The idea is that intervals keep the same importance through out the scale. For example, the difference in age between 10 and 20 is the same as that between 40 and 50. There are two main approaches to standardize interval scaled attributes, range and z-score. The range method divides each value by the range of valid values of the attribute so that the transformed value ranges between 0 and 1. Given the value xif of the fth attribute of the ith data point, the new value rg(xif) is, ,)min()max( )min()( ff fxxrg if if   (19) 156 4 Unsupervised Learning where min(f) and max(f) are the minimum value and maximum value of at- tribute f respectively. max(f)  min(f) is the value range of the valid values of attribute f. The z-score method transforms an attribute value based on the mean and the standard deviation of the attribute. That is, the z-score of the value in- dicates how far and in what direction the value deviates from the mean of the attribute, expressed in units of the standard deviation of the attribute. The standard deviation of attribute f, denoted by f, is computed with: ,1 )( 2 1     n x f n i if f  (20) where n is the number of data points in the data set, xif is the same as above, and f is the mean of attribute f, which is computed with: .1 1  n i iff xn (21) Given the value xif, its z-score (the new value after transformation) is z(xif), .)( f fif if xxz   (22) Ratio-Scaled Attributes: These are also numeric attributes taking real values. However, unlike interval-scaled attributes, their scales are not lin- ear. For example, the total amount of microorganisms that evolve in a time t is approximately given by AeBt, where A and B are some positive constants. This formula is referred to as exponential growth. If we have such attributes in a data set for clustering, we have one of the following two options: 1. Treat it as an interval-scaled attribute. This is often not recommended due to scale distortion. 2. Perform logarithmic transformation to each value, xif, i.e., ).log( ifx (23) After the transformation, the attribute can be treated as an interval- scaled attribute. Nominal (Unordered Categorical) Attributes: As we discussed in Sect. 4.5.2, the value of such an attribute can take anyone of a set of states (also 4.7 Handling of Mixed Attributes 157 called categories). The states have no logical or numerical ordering. For example, the attribute fruit may have the possible values, Apple, Orange, and Pear, which have no ordering. A binary attribute is a special case of a nominal attribute with only two states or values. Although nominal attributes are not standardized as numeric attributes, it is sometime useful to convert a nominal attribute to a set of binary at- tributes. Let the number of values of a nominal attribute be v. We can then create v binary attributes to represent them, i.e., one binary attribute for each value. If a data instance for the nominal attribute takes a particular value, the value of its corresponding binary attribute is set to 1, otherwise it is set to 0. The resulting binary attributes can be used as numeric attrib- utes. We will discuss this again in Sect. 4.7. Example 12: For the nominal attribute fruit, we create three binary attrib- utes called, Apple, Orange, and Pear in the new data. If a particular data instance in the original data has Apple as the value for fruit, then in the transformed data, we set the value of the attribute Apple to 1, and the val- ues of attributes Orange and Pear to 0. ▀ Ordinal (Ordered Categorical) Attributes: An ordinal attribute is like a nominal attribute, but its values have a numerical ordering. For example, the age attribute may have the values, Young, Middle-Age and Old. The common approach to distance computation is to treat ordinal attributes as interval-scaled attributes and use the same methods as for interval-scaled attributes to standardize the values of ordinal attributes. 4.7 Handling of Mixed Attributes So far, we have assumed that a data set contains only one type of attrib- utes. However, in practice, a data set may contain mixed attributes. That is, it may contain any subset of the six types of attributes, interval-scaled, symmetric binary, asymmetric binary, ratio-scaled, ordinal and nomi- nal attributes. Clustering a data set involving mixed attributes is a chal- lenging problem. One way to deal with such a data set is to choose a dominant attribute type and then convert the attributes of other types to this type. For exam- ple, if most attributes in a data set are interval-scaled, we can convert ordi- nal attributes and ratio-scaled attributes to interval-scaled attributes as dis- cussed above. It is also appropriate to treat symmetric binary attributes as interval-scaled attributes. However, it does not make much sense to con- vert a nominal attribute with more than two values or an asymmetric bi- nary attribute to an interval-scaled attribute, but it is still frequently done in 158 4 Unsupervised Learning practice by assigning some numbers to them according to some hidden or- dering. For instance, in the example of Apple, Orange, and Pear, one may order them according to their prices, and thus make the attribute fruit an ordinal attribute or even an interval-scaled attribute. In the previous sec- tion, we also saw that a nominal attribute can be converted to a set of (symmetric) binary attributes, which in turn can be regarded as interval- scaled attributes. Another method of handling mixed attributes is to compute the distance of each attribute of the two data points separately and then combine all the individual distances to produce an overall distance. We describe one such method, which is due to Gower [22] and is also described in [25, 31]. We describe the combination formula first (Equation (24)) and then present the methods to compute individual distances. .),( 1 1     r f f ij f ij r f f ij ji d dist   xx (24) This distance value is between 0 and 1. r is the number of attributes in the data set. The indicator f ij is 1 if both values xif and xjf for attribute f are non-missing, and it is set to 0 otherwise. It is also set to 0 if attribute f is asymmetric and the match is 0–0. Equation (24) cannot be computed if all f ij ’s are 0. In such a case, some default value may be used or one of the data points is removed. f ijd is the distance contributed by attribute f, and it is in the range 0–1. If f is a binary or nominal attribute,     otherwise xxifd jfiff ij 0 1 (25) If all the attributes are nominal, Equation (24) reduces to Equation (17). The same is true for symmetric binary attributes, in which we recover the simple matching distance (Equation (10)). When the attributes are all asymmetric, we obtain the Jaccard distance (Equation (14)). If attribute f is interval-scaled, we use f jfiff ij R xxd ||  (26) where Rf is the value range of attribute f, which is )min()max( ffRf  (27) 4.9 Cluster Evaluation 159 Ordinal attributes and ratio-scaled attributes are handled in the same way after conversion. If all the attributes are interval-scaled, Equation (24) becomes the Man- hattan distance assuming that all attribute values are standardized by divid- ing their values with the ranges of their corresponding attributes. 4.8 Which Clustering Algorithm to Use? Clustering research and application has a long history. Over the years, a vast collection of clustering algorithms has been designed. This chapter only introduced several of the main algorithms. Given an application data set, choosing the “best” clustering algorithm to cluster the data is a challenge. Every clustering algorithm has limitations and works well with only certain data distributions. However, it is very hard, if not impossible, to know what distribution the application data fol- lows. Worse still, the application data set may not fully follow any “ideal” structure or distribution required by the algorithms. Apart from choosing a suitable clustering algorithm from a large collection of available algo- rithms, deciding how to standardize the data, to choose a suitable distance function and to select other parameter values (e.g., k in the k-means algo- rithm) are complex as well. Due to these complexities, the common prac- tice is to run several algorithms using different distance functions and pa- rameter settings, and then to carefully analyze and compare the results. The interpretation of the results should be based on insight into the meaning of the original data together with knowledge of the algorithms used. That is, it is crucial that the user of a clustering algorithm fully un- derstands the algorithm and its limitations. He/she should also have the domain expertise to examine the clustering results. In many cases, generat- ing cluster descriptions using a supervised learning method (e.g., decision tree induction) can be particularly helpful to the analysis and comparison. 4.9 Cluster Evaluation After a set of clusters is found, we need to assess the goodness of the clus- ters. Unlike classification, where it is easy to measure accuracy using la- beled test data, for clustering nobody knows what the correct clusters are given a data set. Thus, the quality of a clustering is much harder to evalu- ate. We introduce a few commonly used evaluation methods below. 160 4 Unsupervised Learning User Inspection: A panel of experts is asked to inspect the resulting clus- ters and to score them. Since this process is subjective, we take the average of the scores from all the experts as the final score of the clustering. This manual inspection is obviously a labor intensive and time consuming task. It is subjective as well. However, in most applications, some level of man- ual inspection is necessary because no other existing evaluation methods are able to guarantee the quality of the final clusters. It should be noted that direct user inspection may be easy for certain types of data, but not for others. For example, user inspection is not hard for text documents because one can read them easily. However, for a relational table with only num- bers, staring at the data instances in each cluster makes no sense. The user can only meaningfully study the centroids of the clusters, or rules that cha- racterize the clusters generated by a decision tree algorithm or some other supervised learning methods (see Sect. 4.3.1). Ground Truth: In this method, classification data sets are used to evalu- ate clustering algorithms. Recall that a classification data set has several classes, and each data instance/point is labeled with one class. Using such a data set for cluster evaluation, we make the assumption that each class corresponds to a cluster. For example, if a data set has three classes, we as- sume that it has three clusters, and we request the clustering algorithm to also produce three clusters. After clustering, we compare the cluster mem- berships with the class memberships to determine how good the clustering is. A variety of measures can be used to assess the clustering quality, e.g., entropy, purity, precision, recall, and F-score. To facilitate evaluation, a confusion matrix can be constructed from the resulting clusters. From the matrix, various measurements can be com- puted. Let the set of classes in the data set D be C = (c1, c2, …, ck). The clustering method also produces k clusters, which partition D into k dis- joint subsets, D1, D2, …, Dk. Entropy: For each cluster, we can measure its entropy as follows: ),(Prlog)(Pr)( 1 2 ji k j jii ccDentropy    (28) where Pri(cj) is the proportion of class cj data points in cluster i or Di. The total entropy of the whole clustering (which considers all clusters) is .)(|| ||)( 1    k i i i total DentropyD DDentropy (29) Purity: This measures the extent that a cluster contains only one class of data. The purity of each cluster is computed with 4.9 Cluster Evaluation 161 )).((Prmax)( jiji cDpurity  (30) The total purity of the whole clustering (considering all clusters) is .)(|| ||)( 1    k i i i total DpurityD DDpurity (31) Precision, recall, and F-score can be computed as well for each cluster based on the class that is the most frequent in the cluster. Note that these measures are based on a single class (see Sect. 3.3.2). Example 13: Assume we have a text collection D of 900 documents from three topics (or three classes), Science, Sports, and Politics. Each class has 300 documents, and each document is labeled with one of the topics (classes). We use this collection to perform clustering to find three clus- ters. Class/topic labels are not used in clustering. After clustering, we want to measure the effectiveness of the clustering algorithm. First, a confusion matrix (Fig. 4.17) is constructed based on the cluster- ing results. From Fig. 4.17, we see that cluster 1 has 250 Science docu- ments, 20 Sports documents, and 10 Politics documents. The entries of the other rows have similar meanings. The last two columns list the entropy and purity values of each cluster and also the total entropy and purity of the whole clustering (last row). We observe that cluster 1, which contains mainly Science documents, is a much better (or purer) cluster than the oth- er two. This fact is also reflected by both their entropy and purity values. Cluster Science Sports Politics Entropy Purity 1 250 20 10 0.589 0.893 2 20 180 80 1.198 0.643 3 30 100 210 1.257 0.617 Total 300 300 300 1.031 0.711 Fig. 4.17. Confusion matrix with entropy and purity values Obviously, we can use the total entropy or the total purity to compare different clustering results from the same algorithm with different parame- ter settings or from different algorithms. Precision and recall may be computed similarly for each cluster. For ex- ample, the precision of Science documents in cluster 1 is 0.89. The recall of Science documents in cluster 1 is 0.83. The F-score for Science docu- ments is thus 0.86. ▀ 162 4 Unsupervised Learning A final remark about this evaluation method is that although an algo- rithm may perform well on some labeled data sets, there is no guarantee that it will perform well on the actual application data at hand, which has no class labels. However, the fact that it performs well on some labeled da- ta sets does give us some confidence on the quality of the algorithm. This evaluation method is said to be based on external data or information. There are also methods that evaluate clusters based on the internal in- formation in the clusters (without using external data with class labels). These methods measure intra-cluster cohesion (compactness) and inter- cluster separation (isolation). Cohesion measures how near the data points in a cluster are to the cluster centroid. Sum of squared error (SSE) is a com- monly used measure. Separation measures how far apart different cluster centroids are from one another. Any distance functions can be used for the purpose. We should note, however, that good values for these measurements do not always mean good clusters. In most applications, expert judgments are still the key. Clustering evaluation remains to be a very difficult problem. Indirect Evaluation: In some applications, clustering is not the primary task. Instead, it is used to help perform another more important task. Then, we can use the performance on the primary task to determine which clus- tering method is the best for the task. For instance, in a Web usage mining application, the primary task is to recommend books to online shoppers. If the shoppers can be clustered according to their profiles and their past pur- chasing history, we may be able to provide better recommendations. A few clustering methods can be tried, and their results are then evaluated based on how well they help with the recommendation task. Of course, here we assume that the recommendation results can be reliably evaluated. 4.10 Discovering Holes and Data Regions In this section, we wander a little to discuss something related but quite different from the preceding algorithms. We show that unsupervised learn- ing tasks may be performed by using supervised learning techniques [35]. In clustering, data points are grouped into clusters according to their dis- tances (or similarities). However, clusters only represent one aspect of the hidden knowledge in data. Another aspect that we have not studied is the holes. If we treat data instances as points in an r-dimensional space, a hole is simply a region in the space that contains no or few data points. The ex- istence of holes is due to the following two reasons: 1. insufficient data in certain areas, and/or 2. certain attribute-value combinations are not possible or seldom occur. 4.10 Discovering Holes and Data Regions 163 Although clusters are important, holes in the space can be quite useful too. For example, in a disease database we may find that certain symptoms and/or test values do not occur together, or when a certain medicine is used, some test values never go beyond certain ranges. Discovery of such information can be of great importance in medical domains because it could mean the discovery of a cure to a disease or some biological laws. The technique discussed in this section aims to divide the data space in- to two types of regions, data regions (also called dense regions) and empty regions (also called sparse regions). A data region is an area in the space that contains a concentration of data points and can be regarded as a cluster. An empty region is a hole. A supervised learning technique similar to decision tree induction is used to separate the two types of regions. The algorithm (called CLTree for CLuster Tree) works for numeric attributes, but can be extended to discrete or categorical attributes. Decision tree learning is a popular technique for classifying data of var- ious classes. For a decision tree algorithm to work, we need at least two classes of data. A clustering data set, however, has no class label for each data point. Thus, the technique is not directly applicable. However, the problem can be dealt with by a simple idea. We can regard each data instance/point in the data set as having a class label Y. We assume that the data space is uniformly distributed with an- other type of points, called non-existing points, which we will label N. With the N points added to the original data space, our problem of parti- tioning the data space into data regions and empty regions becomes a su- pervised classification problem. The decision tree algorithm can be adapted to solve the problem. Let us use an example to illustrate the idea. Example 14: Fig. 4.18(A) gives a 2-dimensional space with 24 data (Y) points. Two data regions (clusters) exist in the space. We then add some uniformly distributed N points (represented by “o”) to the data space (Fig. 4.18(B)). With the augmented data set, we can run a decision tree algo- rithm to obtain the partitioning of the space in Fig. 4.18(B). Data regions and empty regions are separated. Each region is a rectangle, which can be expressed as a rule. ▀ The reason that this technique works is that if there are clusters (or dense data regions) in the data space, the data points cannot be uniformly distributed in the entire space. By adding some uniformly distributed N points, we can isolate data regions because within each data region there are significantly more Y points than N points. The decision tree technique is well known for this partitioning task. 164 4 Unsupervised Learning Fig. 4.18. Separating data and empty regions using a decision tree An interesting question is: can the task be performed without physically adding the N points to the original data? The answer is yes. Physically add- ing N points increases the size of the data and thus the running time. A more important issue is that it is unlikely that we can have points truly uni- formly distributed in a high-dimensional space as we would need an expo- nential number of them. Fortunately, we do not need to physically add any N points. We can compute them when needed. The CLTree method is able to produce the partitioning in Fig. 4.18(C) with no N points added. The de- tails are quite involved. Interested readers can refer to [35]. This method has some interesting characteristics:  It provides descriptions or representations of the resulting data regions and empty regions in terms of hyper-rectangles, which can be expressed as rules as we have seen in Sect. 3.2 of Chap. 3 and in Sect. 4.3.1. Many applications require such descriptions, which can be easily interpreted by users.  It automatically detects outliers, which are data points in empty regions.  It may not use all attributes in the data just as in decision tree building for supervised learning. That is, it can automatically determine what at- tributes are important and what are not. This means that it can perform subspace clustering, i.e., finding clusters that exist in some subspaces (represented by some subsets of the attributes) of the original space. (A): The original data space (B). Partitioning with added (C). Partitioning without adding N points N points. Bibliographic Notes 165 This method also has limitations. The main limitation is that data re- gions of irregular shapes are hard to handle since decision tree learning on- ly generates hyper-rectangles (formed by axis-parallel hyper-planes), which are rules. Hence, an irregularly shaped data or empty region may be split into several hyper-rectangles. Post-processing is needed to join them if desired (see [35] for additional details). Bibliographic Notes Clustering or unsupervised learning has a long history and a very large body of work. This chapter described only some widely used core algo- rithms. Most other algorithms are variations or extensions of these meth- ods. For a comprehensive coverage of clustering, please refer to several books dedicated to clustering, e.g., those by Everitt [18], Hartigan [26], Jain and Dubes [30], and Kaufman and Rousseeuw [31]. Most data mining texts also have excellent coverage of clustering techniques, e.g., Han and Kamber [25] and Tan et al. [43], which have influenced the writing of this chapter. Below, we review some more recent developments on clustering and give some further readings. A density-based clustering algorithm based on local data densities was proposed by Ester et al. [17] and Xu et al. [48] for finding clusters of arbi- trary shapes. Hinneburg and Keim [29], Sheikholeslami et al. [40] and Wang et al. [46] proposed several grid-based clustering methods which first partition the space into small grids. A popular neural network cluster- ing algorithm is the Self-Organizing Map (SOM) by Kohonen [32]. Fuzzy clustering (e.g., fuzzy c-means) was studied by Bezdek [7] and Dunn [16]. Cheeseman et al. [9] and Moore [36] studied clustering using mixture models. The method assumes that clusters are a mixture of Gaussians and uses the EM algorithm [12] to learn a mixture density. We will see in Chap. 5 that EM based partially supervised learning algorithms are basi- cally clustering methods with some given initial seeds. Most clustering algorithms work on numeric data. Categorical data and/or transaction data clustering were investigated by Barbará et al. [5], Ganti et al. [20], Gibson et al. [21], Guha et al. [24], Wang et al. [45], etc. A related area in artificial intelligence is the conceptual clustering, which was studied by Fisher [19], and others. Many clustering algorithms, e.g., hierarchical clustering algorithms, have high time complexities and are thus not suitable for large data sets. Scaling up such algorithms becomes an important issue for large applica- tions. Several researchers have designed techniques to scale up clustering 166 4 Unsupervised Learning algorithms, e.g., Bradley et al. [8], Guha et al. [23], Ng and Han [38], and Zhang et al. [54]. In recent years, there were quite a few new developments in clustering. The first one is subspace clustering. Traditional clustering algorithms use the whole space to find clusters, but natural clusters may exist in only some sub-spaces. That is, some clusters may only use certain subsets of the attributes. This problem was investigated by Agrawal et al. [3], Aggarwal et al. [1], Aggarwal and Yu [2], Cheng et al. [10], Liu et al. [35], Zaki et al. [49], and many others. The second new research is semi-supervised clustering, which means that the user can provide some initial information to guide the clustering process. For example, the user can select some initial seeds [6] and/or spe- cify some constraints, e.g., must-link (two points must be in the same cluster) and cannot-link (two points cannot be in the same cluster) [44]. The third is the spectral clustering, which emerged from several fields, e.g., VLSI [4] and computer vision [39, 41, 47]. It clusters data points by computing eigenvectors of the similarity matrix. Recently, it was also stud- ied in machine learning and data mining [15, 37, 53]. Yet another new research is co-clustering, which simultaneously clus- ters both rows and columns. This approach was studied by Cheng and Church [11], Dhillon [13], Dhillon et al. [14], and Hartigan [27]. Regarding document and Web page clustering, most implementations are still based on k-means and hierarchical clustering methods or their var- iations but using text specific similarity or distance functions. Steinbach et al. [42], and Zhao and Karypis [55, 56] experimented with k-means and agglomerative hierarchical clustering methods and also proposed some improvements. Many researchers also worked on clustering of search en- gine results (or snippets) to organize search results into different topics, e.g., Hearst and Pedersen [28], Kummamuru et al. [33], Leouski and Croft [34], Zamir and Etzioni [50, 51], and Zeng et al. [52]. Bibliography 1. Agarwal, R., C. Aggarwal, and V. Prasad. A tree projection algorithm for generation of frequent item sets. Journal of Parallel and Distributed Computing, 2001, 61(3): p. 350-371. 2. Aggarwal, C. and P. Yu. Finding generalized projected clusters in high dimensional spaces. In Proceedings of ACM SIGMOD International Conference on Management of Data (SIGMOD-2000), 2000. 3. Agrawal, R., J. Gehrke, D. Gunopulos, and P. Raghavan. Automatic subspace clustering of high dimensional data for data mining applications. In Bibliography 167 Proceedings of ACM SIGMOD International Conference on Management of Data (SIGMOD-1998), 1998. 4. Alpert, C., A. Kahng, and S. Yao. Spectral partitioning: the more eigenvectors, the better. Discrete Applied Mathematics, 1999, 90(1-3): p. 3-26. 5. Barbará, D., Y. Li, and J. Couto. COOLCAT: an entropy-based algorithm for categorical clustering. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2002), 2002. 6. Basu, S., A. Banerjee, and R. Mooney. Semi-supervised clustering by seeding. In Proceedings of International Conference on Machine Learning (ICML-2002), 2002. 7. Bezdek, J.C. Cluster Validity with Fuzzy Sets. Journal of Cybernetics, 1974, 3: p. 58–72. 8. Bradley, P., U. Fayyad, and C. Reina. Scaling clustering algorithms to large databases. Knowledge Discovery and Data Mining, 1998: p. 9–15. 9. Cheeseman, P. and J. Stutz. Bayesian classification (AutoClass): Theory and results. Advances in Knowledge Discovery and Data Mining, 1996. 10. Cheng, C., A. Fu, and Y. Zhang. Entropy-based subspace clustering for mining numerical data. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 11. Cheng, Y. and G. Church. Biclustering of expression data. In Proceedings of ISMB-2000, 2000. 12. Dempster, A., N. Laird, and D. Rubin. Maximum likelihood from incomplete data via the EM algorithm. Journal of the Royal Statistical Society. Series B (Methodological), 1977, 39(1): p. 1-38. 13. Dhillon, I. Co-clustering documents and words using bipartite spectral graph partitioning. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2001), 2001. 14. Dhillon, I., S. Mallela, and D. Modha. Information-theoretic co-clustering. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2003), 2003. 15. Ding, C. and X. He. Linearized cluster assignment via spectral ordering. In Proceedings of International Conference on Machine Learning (ICML-2004), 2004. 16. Dunn, J. A fuzzy relative of the ISODATA process and its use in detecting compact well-separated clusters. Cybernetics and Systems, 1973, 3(3): p. 32-57. 17. Ester, M., H. Kriegel, J. Sander, and X. Xu. A density-based algorithm for discovering clusters in large spatial databases with noise. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1996), 1996. 18. Everitt, B. Cluster analysis. 1974: Heinemann, London. 19. Fisher, D. Knowledge acquisition via incremental conceptual clustering. Machine Learning, 1987, 2(2): p. 139-172. 20. Ganti, V., J. Gehrke, and R. Ramakrishnan. CACTUS—clustering categorical data using summaries. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-1999), 1999. 168 4 Unsupervised Learning 21. Gibson, D., J. Kleinberg, and P. Raghavan. Clustering categorical data: An approach based on dynamical systems. The VLDB Journal, 2000, 8(3-4): p. 236. 22. Gower, J. A general coefficient of similarity and some of its properties. Biometrics, 1971: p. 857-871. 23. Guha, S., R. Rastogi, and K. Shim. Cure: an efficient clustering algorithm for large databases. Information Systems, 2001, 26(1): p. 35-58. 24. Guha, S., R. Rastogi, and K. Shim. Rock: A robust clustering algorithm for categorical attributes* 1. Information Systems, 2000, 25(5): p. 345-366. 25. Han, J. and M. Kamber. Data mining: concepts and techniques. 2006: Morgan Kaufmann Publishers. 26. Hartigan, J. Clustering algorithms. 1975: John Wiley & Sons, Inc. 27. Hartigan, J. Direct clustering of a data matrix. Journal of the American Statistical Association, 1972: p. 123-129. 28. Hearst, M. and J. Pedersen. Reexamining the cluster hypothesis: scatter/gather on retrieval results. In Proceedings of ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR-1996), 1996. 29. Hinneburg, A. and D. Keim. Optimal grid-clustering: Towards breaking the curse of dimensionality in high-dimensional clustering. In Proceedings of International Conference on Very Large Data Bases (VLDB-1999), 1999. 30. Jain, A. and R. Dubes. Algorithms for clustering data. 1988: Prentice Hall. 31. Kaufman, L. and P. Rousseeuw. Finding groups in data: an introduction to cluster analysis. 2005: John Wiley & Sons, Inc. 32. Kohonen, T. Self-Organizing Maps. 1995: Springer. 33. Kummamuru, K., R. Lotlikar, S. Roy, K. Singal, and R. Krishnapuram. A hierarchical monothetic document clustering algorithm for summarization and browsing search results. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 34. Leouski, A. and W. Croft. An evaluation of techniques for clustering search results. In Technical Report IR–76. 1996, Department of Computer Science, University of Massachusetts. 35. Liu, B., Y. Xia, and P. Yu. Clustering through decision tree construction. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2000), 2000. 36. Moore, A. Very fast EM-based mixture model clustering using multiresolution kd-trees. In Proceedings of Neural Info. Processing Systems (NIPS-1998), 1999. 37. Ng, A., M. Jordan, and Y. Weiss. On spectral clustering: Analysis and an algorithm. In Proceedings of 14th Advances in Neural Information Processing Systems, 2001. 38. Ng, R. and J. Han. Efficient and effective clustering methods for spatial data mining. In Proceedings of International Conference on Very Large Data Bases (VLDB-1994), 1994. 39. Scott, G. and H. Longuet-Higgins. Feature grouping by relocalisation of eigenvectors of the proximity matrix. In Proceedings of British Machine Vision Conference, 1990. Bibliography 169 40. Sheikholeslami, G., S. Chatterjee, and A. Zhang. Wavecluster: A multi- resolution clustering approach for very large spatial databases. In Proceedings of International Conference on Very Large Data Bases (VLDB-1998), 1998. 41. Shi, J. and J. Malik. Normalized cuts and image segmentation. Pattern Analysis and Machine Intelligence, IEEE Transactions on, 2002, 22(8): p. 888-905. 42. Steinbach, M., G. Karypis, and V. Kumar. A comparison of document clustering techniques. In Proceedings of KDD Workshop on Text Mining, 2000. 43. Tan, P., M. Steinbach, and V. Kumar. Introduction to data mining. 2006: Pearson Addison Wesley Boston. 44. Wagstaff, K. and C. Cardie. Clustering with instance-level constraints. In Proceedings of International Conference on Machine Learning (ICML-2000), 2000. 45. Wang, K., Y. He, and J. Han. Mining frequent itemsets using support constraints. In Proceedings of International Conference on Very Large Data Bases (VLDB-2000), 2000. 46. Wang, W., J. Yang, and R. Muntz. STING: A statistical information grid approach to spatial data mining. In Proceedings of International Conference on Very Large Data Bases (VLDB-1997), 1997. 47. Weiss, Y. Segmentation using eigenvectors: a unifying view. In Proceedings of IEEE Intl. Conf. on Computer Vision, 1999. 48. Xu, X., M. Ester, H. Kriegel, and J. Sander. A nonparametric clustering algorithm for knowledge discovery in large spatial databases. In Proceedings of IEEE International Conference on Data Engingeering (ICDE-2003), 1998. 49. Zaki, M., M. Peters, I. Assent, and T. Seidl. Clicks: An effective algorithm for mining subspace clusters in categorical datasets. Data & Knowledge Engineering, 2007, 60(1): p. 51-70. 50. Zamir, O. and O. Etzioni. Grouper: a dynamic clustering interface to Web search results. Computer Networks, 1999, 31(11-16): p. 1361-1374. 51. Zamir, O. and O. Etzioni. Web document clustering: A feasibility demonstration. In Proceedings of International Conference on World Wide Web (WWW-1998), 1998. 52. Zeng, H., Q. He, Z. Chen, W. Ma, and J. Ma. Learning to cluster web search results. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2004), 2004. 53. Zha, H., X. He, C. Ding, M. Gu, and H. Simon. Spectral relaxation for k- means clustering. Advances in Neural Information Processing Systems, 2002, 2: p. 1057-1064. 54. Zhang, T., R. Ramakrishnan, and M. Livny. BIRCH: an efficient data clustering method for very large databases. ACM SIGMOD Record, 1996, 25(2): p. 103-114. 55. Zhao, Y. and G. Karypis. Empirical and theoretical comparisons of selected criterion functions for document clustering. Machine Learning, 2004, 55(3): p. 311-331. 56. Zhao, Y., G. Karypis, and U. Fayyad. Hierarchical clustering algorithms for document datasets. Data Mining and Knowledge Discovery, 2005, 10(2): p. 141-168. 5 Partially Supervised Learning In supervised learning, the learning algorithm uses labeled training exam- ples from every class to generate a classification function. One of the drawbacks of this classic paradigm is that a large number of labeled exam- ples are needed in order to learn accurately. Since labeling is often done manually, it can be very labor intensive and time consuming. In this chap- ter, we study two partially supervised learning problems. As their names suggest, these two learning problems do not need full supervision, and thus are able to reduce the labeling effort. The first is the problem of learning from la- beled and unlabeled examples, which is commonly known as semi- supervised learning. In this chapter, we also call it LU learning (L and U stand for “labeled” and “unlabeled” respectively). In this learning setting, there is a small set of labeled examples of every class, and a large set of unlabeled examples. The objective is to make use of the unlabeled exam- ples to improve learning. The second is the problem of learning from positive and unlabeled exam- ples. This problem assumes two-class classification. However, the training data only has a set of labeled positive examples and a set of unlabeled ex- amples, but no labeled negative examples. In this chapter, we also call this problem PU learning (P and U stand for “positive” and “unlabeled” re- spectively). The objective is to build an accurate classifier without labeling any negative examples. We study these two problems in the context of text classification and Web page classification in this chapter. However, the general ideas and the algorithms are also applicable to other kinds of clas- sification tasks. 5.1 Learning from Labeled and Unlabeled Examples As we described in Chap. 3, the common approach to learning a classifica- tion function is to label a set of examples with some pre-defined categories or classes, and then use a learning algorithm to produce a classifier. This classifier is applied to assign classes to future instances (or test data). In the context of text classification and Web page classification, the examples are text documents and Web pages. This approach to building a classifier With Wee Sun Lee B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_5, © Springer-Verlag Berlin Heidelberg 2011 171 172 5 Partially Supervised Learning is called supervised learning because the training documents/pages have been labeled with pre-defined classes. The main bottleneck of building such a classifier is that a large, often prohibitive, number of labeled training documents are needed to build ac- curate classifiers. In text classification, the labeling is typically done ma- nually by reading the documents, which is a time consuming task. How- ever, we cannot eliminate labeling completely because without it a machine learning algorithm will not know what the user is interested in. Although unsupervised learning or clustering may help to some extent, clustering does not guarantee to produce the categorization results required by the user. This raises an important question: Can the manual labeling ef- fort be reduced, and can other sources of information be used so that the number of labeled examples required for learning would not be too large? This section addresses the problem of learning from a small set of la- beled examples and a large set of unlabeled examples, i.e., LU learning. Thus, in this setting only a small set of examples needs to be labeled for each class. However, since a small set of labeled examples is not sufficient for building an accurate classifier, a large number of unlabeled examples are utilized to help. One key point to note is that although the number may be small, every class must have some labeled examples. In many applications, unlabeled examples are easy to come by. This is especially true for online documents. For example, if we want to build a classifier to classify news articles into different categories or classes, it is fairly easy to collect a huge number of unlabeled news articles from the Web. In fact, in many cases, the new data that need to be classified (which have no class labels) can be used as the unlabeled examples. The question is: why do the unlabeled data help? In the context of text classification, one reason is that the unlabeled data provide information on the joint probability distribution over words. For example, using only the labeled data we find that documents containing the word “homework” tend to belong to a particular class. If we use this fact to classify the unlabeled documents, we may find that “lecture” co-occurs with “homework” fre- quently in the unlabeled set. Then, “lecture” may also be an indicative word for the class. Such correlations provide a helpful source of informa- tion to increase classification accuracy, especially when the labeled data are scarce. Several researchers have shown that unlabeled data help learning. That is, under certain conditions using both labeled and unlabeled data in learn- ing is better than using a small set of labeled data alone. Their techniques can thus alleviate the labor-intensive labeling effort. We now study some of these learning techniques, and also discuss their limitations. 5.1 Learning from Labeled and Unlabeled Examples 173 5.1.1 EM Algorithm with Naïve Bayesian Classification One of the LU learning techniques uses the Expectation–Maximization (EM) algorithm [11]. EM is a popular iterative algorithm for maximum li- kelihood estimation in problems with missing data. The EM algorithm consists of two steps, the Expectation step (or E-step), and the Maximi- zation step (or M-step). The E-step basically fills in the missing data based on the current estimation of the parameters. The M-step, which max- imizes the likelihood, re-estimates the parameters. This leads to the next it- eration of the algorithm, and so on. EM converges to a local minimum when the model parameters stabilize. The ability of EM to work with missing data is exactly what is needed for learning from labeled and unlabeled examples. The documents in the labeled set (denoted by L) all have class labels (or values). The documents in the unlabeled set (denoted by U) can be regarded as having missing class labels. We can use EM to estimate them based on the current model, i.e., to assign probabilistic class labels to each document di in U, i.e., Pr(cj|di). After a number of iterations, all probabilities will converge. Note that the EM algorithm is not really a specific “algorithm”, but is a framework or strategy. It simply runs a base algorithm iteratively. We will use the naïve Bayesian (NB) algorithm discussed in Sect. 3.7 as the base algorithm, and run it iteratively. The parameters that EM estimates in this case are the probability of each word given a class and the class prior probabilities (see Equations (31) and ( Although it is quite involved to derive the EM algorithm with the NB classifier, it is fairly straightforward to implement and to apply the algo- rithm. That is, we use a NB classifier in each iteration of EM, Equation (33) in Chap. 3 for the E-step, and Equations (31) and (32) in Chap. 3 for the M-step. Specifically, we first build a NB classifier f using the labeled examples in L. We then use f to classify the unlabeled examples in U, more accurately to assign a probability to each class for every unlabeled exam- ple, i.e., Pr(cj|di), which takes the value in [0, 1] instead of {0, 1}. Some explanations are in order here. Let the set of classes be C = {c1, c2, …, c|C|}. Each iteration of EM will assign every example di in U a probability distribution on the classes that it may belong to. That is, it assigns di the class probabilities of Pr(c1|di), Pr(c2|di), …, Pr(c|C||di). This is different from the example in the labeled set L, where each document belongs to only a single class ck, i.e., Pr(ck|di) = 1 and Pr(cj|di) = 0 for j  k. Based on the assignments of Pr(cj|di) to each document in U, a new NB classifier can be constructed. This new classifier can use both the labeled set L and the unlabeled set U as the examples in U now have probabilistic 32) in Sect. 3.7 of Chap. 3). 174 5 Partially Supervised Learning labels, Pr(cj|di). This leads to the next iteration. The process continues until the classifier parameters (Pr(wt|cj) and Pr(cj)) no longer change (or have minimum changes). The EM algorithm with the NB classification was proposed for LU learning by Nigam et al. [34]. The algorithm is shown in Fig. 5.1. EM here can also be seen as a clustering method with some initial seeds (labeled da- ta) in each cluster. The class labels of the seeds indicate the class labels of the resulting clusters. The derivation of the EM algorithm in Fig. 5.1 is quite involved and is given as an appendix at the end of this chapter. Two assumptions are made in the derivation. They are in fact the two mixture model assumptions in Sect. 3.7 of Chap. 3 for deriving the naïve Bayesian classifier for text clas- sification, i.e., 1. the data is generated by a mixture model, and 2. there is a one-to-one correspondence between mixture components and classes. It has been shown that the EM algorithm in Fig. 5.1 works well if the two mixture model assumptions for a particular data set are true. Note that although naïve Bayesian classification makes additional assumptions as we discussed in Sect. 3.7 of Chap. 3, it performs surprisingly well despite the obvious violation of the assumptions. The two mixture model assumptions, however, can cause major problems when they do not hold. In many real- life situations, they may be violated. It is often the case that a class (or top- ic) contains a number of sub-classes (or sub-topics). For example, the class Sports may contain documents about different sub-classes of sports, e.g., Algorithm EM(L, U) 1 Learn an initial naïve Bayesian classifier f from only the labeled set L (us- ing Equations (31) and (32) in Chap. 3); 2 repeat // E-Step 3 for each example di in U do 4 Using the current classifier f to compute Pr(cj|di) (using Equation (33) in Chap. 3). 5 end // M-Step 6 learn a new naïve Bayesian classifier f from L  U by computing Pr(cj) and Pr(wt|cj) (using Equations (31) and (32) in Chap. 3). 7 until the classifier parameters stabilize Return the classifier f from the last iteration. Fig. 5.1. The EM algorithm with naïve Bayesian classification 5.1 Learning from Labeled and Unlabeled Examples 175 Baseball, Basketball, Tennis, and Softball. Worse still, a class cj may even contain documents from completely different topics, e.g., Science, Politics, and Sports. The first assumption above is usually not a problem. The sec- ond assumption is critical. If the condition holds, EM works very well and is particularly useful when the labeled set is very small, e.g., fewer than five labeled documents per class. In such cases, every iteration of EM is able to improve the classifier dramatically. However, if the second condi- tion does not hold, the classifier from each iteration can become worse and worse. That is, the unlabeled set hurts learning instead of helping it. Two methods are proposed to remedy the situation. Weighting the Unlabeled Data: In LU learning, the labeled set is small, but the unlabeled set is very large. So the EM’s parameter estimation is almost completely determined by the unlabeled set after the first iteration. This means that EM essentially performs unsupervised clustering. When the two mixture model assumptions are true, the natural clusters of the data are in correspondence with the class labels. The resulting clusters can be used as the classifier. However, when the assumptions are not true, the clustering can go very wrong, i.e., the clustering may not converge to mix- ture components corresponding to the given classes, and are therefore det- rimental to classification accuracy. In order to reduce the effect of the problem, we can weight down the unlabeled data during parameter estima- tion (EM iterations). Specifically, we change the computation of Pr(wt|cj) (Equation (31) in Chap. 3) to the following, where the counts of the unla- beled documents are decreased by a factor of , 0    1: , )|Pr()(|| )|Pr()( )|Pr( || 1 || 1 || 1        V s D i ijti D i ijti jt dcNiV dcNi cw   (1) where      . if1 if)(Ld Udi i i (2) When  = 1, each unlabeled document is weighted the same as a labeled document. When  = 0, the unlabeled data are not considered. The value of  can be chosen based on leave-one-out cross-validation accuracy on the labeled training data. The  value that gives the best result is used. Finding Mixture Components: Instead of weighting unlabeled data low, we can attack the problem head on, i.e., by finding the mixture compo- nents (sub-classes) of the class. For example, the original class Sports may consist of documents from Baseball, Tennis, and Basketball, which are 176 5 Partially Supervised Learning three mixture components (sub-classes or sub-topics) of Sports. Instead of using the original class, we try to find these components and treat each of them as a class. That is, if we can find the three mixture components, we can use them to replace the class Sports. There are several automatic ap- proaches for identifying mixture components. For example, a hierarchical clustering technique was proposed in [8] to find the mixture components, which showed good performances. A simple approach based on leave-one- out cross-validation on the labeled training set was also given in [34]. Manually identifying different components may not be a bad option for text documents because one only needs to read the documents in the la- beled set (or some sampled unlabeled documents), which is very small. 5.1.2 Co-Training Co-training is another approach to learning from labeled and unlabeled ex- amples. This approach assumes that the set of attributes (or features) in the data can be partitioned into two subsets. Each of them is sufficient for learning the target classification function. For example, in Web page clas- sification, one can build a classifier using either the text appearing on the page itself, or the anchor text attached to hyperlinks pointing to the page from other pages on the Web. This means that we can use the same train- ing data to build two classifiers using two subsets of features. Traditional learning algorithms do not consider this division of features, or feature redundancy. All the features are pooled together in learning. In some cases, feature selection algorithms are applied to remove redundant features. Co-training exploits this feature division to learn separate classi- fiers over each of the feature sets, and utilizes the fact that the two classifi- ers must agree on their labeling of the unlabeled data to do LU learning. Blum and Mitchell [3] formalize the co-training setting and provide a theoretical guarantee for accurate learning subject to certain assumptions. In the formalization, we have an example (data) space X = X1  X2, where X1 and X2 provide two different “views” of the example. That is, each ex- ample x (represented as a vector) is given as a pair (x1, x2). This simply means that the set of features (or attributes) is partitioned into two subsets. Each “view” or feature subset is sufficient for correct classification. Under some further assumptions, it was proven that co-training algorithms can learn from unlabeled data starting from only a weak classifier built using the small set of labeled training documents. The first assumption is that the example distribution is compatible with the target functions; that is, for most examples, the target classification functions over the feature sets predict the same label. In other words, if f denotes the combined classifier, f1 denotes the classifier learned from X1, f2 5.1 Learning from Labeled and Unlabeled Examples 177 denotes the classifier learned from X2 and c is the actual class label of ex- ample x, then f(x) = f1(x1) = f2(x2) = c for most examples. The second assumption is that the features in one set of an example are conditionally independent of the features in the other set, given the class of the example. In the case of Web page classification, this assumes that the words on a Web page are not related to the words on its incoming hy- perlinks, except through the class of the Web page. This is a somewhat un- realistic assumption in practice. The co-training algorithm explicitly uses the feature split to learn from labeled and unlabeled data. The algorithm is iterative. The main idea is that in each iteration, it learns a classifier from the labeled set L with each subset of the features, and then applies the classifier to classify (or label) the unlabeled examples in U. A number (ni) of most confidently classified examples in U from each class ci are added to L. This process ends when U becomes empty (or a fixed number of iterations is reached). In practice, we can set a different ni for a different class ci depending on class distribu- tions. For example, if a data set has one third of class 1 examples and two thirds of class 2 examples, we can set n1 = 1 and n2 = 2. The whole co-training algorithm is shown in Fig. 5.2. Lines 2 and 3 build two classifiers f1 and f2 from the two “views” of the data respectively. f1 and f2 are then applied to classify the unlabeled examples in U (lines 4 and 5). Some most confidently classified examples are removed from U and added to L. The algorithm then goes to the next iteration. Algorithm co-training(L, U) 1 repeat 2 Learn a classifier f1 using L based on only x1 portion of the examples x. 3 Learn a classifier f2 using L based on only x2 portion of the examples x. 4 Apply f1 to classify the examples in U, for each class ci, pick ni examples that f1 most confidently classifies as class ci, and add them to L. 5 Apply f2 to classify the examples in U, for each class ci, pick ni examples that f2 most confidently classifies as class ci, and add them to L. 6 until U becomes empty or a fixed number of iterations are reached Fig. 5.2. A co-training algorithm When the co-training algorithm ends, it returns two classifiers. At classi- fication time, for each test example the two classifiers are applied sepa- rately and their scores are combined to decide the class. For naïve Bayes- ian classifiers, we multiply the two probability scores, i.e., Pr(cj|x) = Pr(cj|x1)Pr(cj|x2) (3) The key idea of co-training is that classifier f1 adds examples to the la- beled set that are used for learning f2 based on the X2 view, and vice versa. 178 5 Partially Supervised Learning Due to the conditional independence assumption, the examples added by f1 can be considered as new and random examples for learning f2 based on the X2 view. Then the learning will progress. The situation is illustrated in Fig. 5.3. This example has classes, positive and negative, and assumes linear separation of the two classes. In the X1 view (Fig. 5.3(A)), the circled ex- amples are most confident positive and negative examples classified (or labeled) by f1 in the unlabeled set U. In the X2 view (Fig. 5.3(B)), these cir- cled examples appear randomly. With these random examples from U add- ed to L, a better f2 will be learned in the next iteration. Fig. 5.3. Two views of co-training. However, if the added examples to L are not random examples in the X2 space but very similar to the situation in Fig. 5.3(A), then these examples are not informative to learning. That is, if the two subsets of features are correlated given the class or the conditional independence assumption is violated, the added examples will not be random but isolated in a specific region similar to those in Fig. 5.3(A). Then they will not be as useful or in- formative to learning. Consequently, co-training will not be effective. In [33], it is shown that co-training produces more accurate classifiers than the EM algorithm presented in the previous section, even for data sets whose feature division does not completely satisfy the strict requirements of compatibility and conditional independence. 5.1.3 Self-Training Self-training, which is similar to both EM and co-training, is another me- thod for LU learning. It is an incremental algorithm that does not use the split of features. Initially, a classifier (e.g., naïve Bayesian classifier) is trained with the small labeled set considering all features. The classifier is then applied to classify the unlabeled set. Those most confidently classi- fied (or unlabeled) documents of each class, together with their predicted class labels, are added to the labeled set. The classifier is then re-trained + + + + + + + + + + + + + + + + + + +             + +          + + + + + + + + + + + + + +                + + + + ++        (A) X1 view: data in U labeled by h1 (B) X2 view: the same data 5.1 Learning from Labeled and Unlabeled Examples 179 and the procedure is repeated. This process iterates until all the unlabeled documents are given class labels. The basic idea of this method is that the classifier uses its own predictions to teach itself. 5.1.4 Transductive Support Vector Machines Support vector machines (SVM) is one of the most effective methods for text classification. One way to use unlabeled data in training SVM is by selecting the labels of the unlabeled data in such a way that the resulting margin of the classifier is maximized. Training for the purpose of labeling known (unlabeled) test instances is referred to as transduction, giving rise to the name transductive SVM [41]. An example of how transduction can change the decision boundary is shown in Fig. 5.4. In this example, the old decision boundary, constructed using only labeled data, would have a very small margin on the unlabeled data. By utilizing the unlabeled data in the training process, a classifier that has the largest margin on both the labeled and unlabeled data can be obtained. y = 1 y = - 1 x---- x+ Old decision boundary New decision boundary Fig. 5.4. The old decision boundary (before the addition of unlabeled data) and the new decision boundary created by transductive SVM. The unlabeled data are indi- cated by circles around them The main difficulty with applying transductive SVM is the computa- tional complexity. When all the labels are observed, training SVM is a convex optimization problem that can be solved efficiently. The problem of assigning labels to unlabeled examples in such a way that the resulting margin of the classifier is maximized can no longer be solved efficiently. To solve the problem, Joachims [20] used a sub-optimal iterative me- thod that starts by learning a classifier using only the labeled data. The me- thod then treats a subset of unlabeled instances that are most confidently labeled positive by the learned classifier as initial positive examples while 180 5 Partially Supervised Learning the rest of the unlabeled examples are treated as initial negative examples. The number of instances to label as positive can be specified by the user to change the precision–recall trade-off and is maintained throughout the it- erations. The method then tries to improve the soft margin cost function by iteratively changing the labels of some of the instances and retraining the SVM. The ratio of positive to negative instances is maintained by selecting one positively labeled instance p and one negatively labeled instance q to change in each iteration. It was shown in [20] that if the two instances are selected such that the slack variables p > 0, q > 0 and p + q > 2, the soft margin cost function will decrease at each iteration. Further improvements described in [20] include allowing the soft margin error of unlabeled ex- amples to be penalized differently from the soft margin error of the labeled examples and penalizing the soft margin error on the positive unlabeled examples differently from the soft margin error on the negative unlabeled examples. The penalty on the unlabeled examples is also iteratively in- creased from a small value to the desired value. This may improve the chances of finding a good local optimum as it may be easier to improve the cost function when the penalty is small. The method was applied success- fully to text classification problems. Like other methods of learning from labeled and unlabeled examples, transductive SVM can be sensitive to its assumptions. When the large margin assumption is correct on the dataset, it may improve performance but when the assumption is incorrect, it can decrease performance com- pared to supervised learning. As an example, the transductive SVM per- formed poorly using small labeled data sets when separating Project Web pages from other types of university Web pages in [20]. It was conjectured that, with a small number of labeled data, separating the Web pages ac- cording to some of the underlying topics of the Web pages may give a lar- ger margin than separating them according to whether the Web pages are Project pages or not. 5.1.5 Graph-Based Methods Graph-based LU learning methods can be viewed as extensions of nearest neighbor supervised learning algorithms that work with both labeled and unlabeled instances. The basic idea in these methods is to treat labeled and unlabeled instances as vertices in a graph where a similarity function is used to define the edge weights between instances. The graph, with similar instances connected by larger weights, is then used to help label the unla- beled instances in such a way that labels of vertices connected by edges with large weights tend to agree with each other. Methods used for con- structing the graphs include connecting each instance to its k-nearest 5.1 Learning from Labeled and Unlabeled Examples 181 neighbors, connecting each instance to other instances within a certain dis- tance  and using a fully connected graph with an exponentially decreasing similarity function such as the Gaussian function to assign the weights. The assumptions used in these methods are similar to those of the nearest neighbor classifier, that is, near neighbors should have the same labels and we have a good measure of similarity between instances. We discuss three types of graph-based LU learning methods below: mincut, Gaussian fields and spectral graph transducer. All three methods work on binary classification problems but, like the support vector machines, can be used with strategies such as one-against-rest for solving multiple class classifi- cation problems. Mincut: This method was proposed by Blum and Chalwa [2]. A weighted graph G = (V, E, W) is constructed first, where V consists of the labeled and unlabeled instances, E consists of edges between the instances and W is a function on the edges with W(i, j) = wij denoting the similarity of in- stances i and j. The vertices associated with labeled instances are then giv- en values from {0, 1} consistent with their binary labels. The idea in the mincut algorithm is to find an assignment of values vi from the set {0, 1} to the unlabeled instances in V such that the cost function   Eji jiij vvw),( || is minimized. The advantage of this formulation is that the problem can be solved in polynomial time even though it is a combinatorial optimization problem. One way to do this is to transform the problem into a max-flow problem (see [9] for a description of the max-flow problem). To do that, we convert the graph into a flow network by introducing a source vertex v+ and a sink vertex v, where the source vertex is connected by edges with infinite capacities to the positive labeled instances while the sink vertex is connected by edges with infinite capacities to the negative labeled in- stances. The other edge weights in the graph are also treated as edge ca- pacities in the flow network. A cut of the network is a partition of the ver- tices into two subsets V+ and V such that v+  V+ and v  V. A minimum cut is a partition that has the smallest sum of capacities in the edges con- necting V+ and V. Finding a minimum cut is equivalent to minimizing the function   Eji jiij vvw),( || since all the vertices are assigned values from {0, 1}. Max-flow algorithms can be used to efficiently find a mincut of the network in time O(|V|3). Gaussian Fields: Instead of minimizing   Eji jiij vvw),( || , Zhu et al. [46] proposed minimizing   Eji jiij vvw),( 2)( with the value of the vertices being selected from [0, 1] instead of {0, 1}. The advantage of using this formulation 182 5 Partially Supervised Learning is that it allows the solution to be obtained using linear algebra. Let W be the weight matrix corresponding to the graph,     UUUL LULL WW WWW, (4) where WLL , WLU , WUL and WUU are sub-matrices with the subscript L de- noting labeled instances and the subscript U denoting the unlabeled in- stances. Let D be a diagonal matrix where  j ijii wD is the sum of the en- tries in row (or column) i. We also form a vector v, consisting of values assigned to the labeled and unlabeled instances. The labeled instances are assigned fixed values in {0, 1} consistent with their labels while the values vi assigned to the unlabeled instances are chosen to minimize   Eji jiij vvw),( 2.)( The solution can be written as ,)( 1 LULUUUUU vv WWD  (5) where vU is the part of the vector v that contains values assigned to the un- labeled instances, vL is the part of the vector that contains values assigned to labeled instances and DUU is the sub-matrix of D consisting of sum of entries of rows in W associated with unlabeled instances. The optimization problem   Eji jiij vvw),( 2)( can also be written in ma- trix form as vTv where  = D  W is known as the combinatorial Lapla- cian of the graph. The matrix  is known to be positive semidefinite, so it can be viewed as an inverse covariance matrix of a multivariate Gaussian random variable, giving rise to the name Gaussian field. Spectral Graph Transducer: One potential problem with the mincut formulation is that the mincut cost function tends to prefer unbalanced cuts where the number of instances in either the positive or negative class vast- ly outnumbers the number of instances in the other class. Unbalanced cuts tend to have a lower cost in the mincut formulation because the number of edges between V+ and V is maximized when the sizes of V+ and V are equal and is small when either one of them is small. For example, if we have n vertices and V+ contains a single element, then there are potentially n1 edges between V+ and V . In contrast, if V+ and V are the same size, then there are potentially n2/4 edges between the two sets of vertices. Let cut(V+, V) be the sum of the edge weights connecting V+ and V. To mitigate the effect of preferring unbalanced cut, Joachims [21] proposed to minimize a cost function of normalized cut |||| ),(   VV VVcut , where the cut value 5.1 Learning from Labeled and Unlabeled Examples 183 is normalized by the number of edges between the two sets. Minimizing this cost function is computationally difficult, so Joachims [21] proposed minimizing a relaxed version of the problem. Let  be the combinatorial Laplacian of the graph. It can be shown that minimizing the normalized cut (with no labeled data) using  and  num- ber of instances ( and  are specified by the user) in the two partitions is equivalent to minimizing vTv for vi  {+, }, where    and    . (6) Instead of using vi  {+, }, Joachims [21] proposed to allow vi to take real values under the constraint vT1=0 and vTv=n, where 1 is the all one vector. To make sure that the labeled instances are properly classified, a term (v)TC(v) is added to the cost function, where C is a diagonal ma- trix with non-zero entries only for labeled instances and  is the target vec- tor for approximation by v. The components of  that correspond to posi- tive and negative instances are set to + and  respectively, while the components of  that correspond to unlabeled instances do not affect the cost function because their corresponding diagonal entries of C are set to zero. The values of the non-zero entries of C can be set by the user to give different misclassification costs to each instance. This gives the combined optimization problem of nandts c TT TT v   vv1v vvvv 0.. )()(min  C (7) where c gives a trade-off between the cost for the labeled and unlabeled parts. The solution of Equation (7) is obtained using spectral methods. The Gaussian field method and spectral graph transduction have been applied to the natural language processing problem of word sense disam- biguation in [35, 37]. Word sense disambiguation is the problem of assign- ing appropriate meanings to words (which may have multiple meanings) according to the context that they appear in. Although some improvements are observed, the success of these methods is still limited. 5.1.6 Discussion We discuss two issues: (1) whether the unlabeled set U is always helpful and (2) the evaluation of LU learning. Does the Unlabeled Set Always Help? The answer is no. As we have seen, all approaches make strong assumptions. For example, EM makes 184 5 Partially Supervised Learning two mixture model assumptions, and co-training makes the feature split as- sumption. When the assumptions are true for an application data set, unla- beled data can help learning (even dramatically). When the assumptions are not true, the unlabeled data may harm learning. Automatically detect- ing bad match of the problem structure with the model assumptions in ad- vance is, however, very hard and remains an open problem. A related issue is that researchers have not shown that when the labeled set is sufficiently large, the unlabeled data still help. Manual labeling more documents may not be as difficult as it seems in some applications, espe- cially when the number of classes is small. In most cases, to label a docu- ment one does not even need to read the entire document (if it is long). Typically, the first few sentences can already tell its class. Compounded with the problem of inability to decide whether the unlabeled data indeed help classification, practical applications of LU learning are still limited. Evaluation: The evaluation of LU learning is commonly done in the same way as traditional classification. However, there is a problem with the availability of sufficient test data. In practice, users always want to have a reasonable guarantee on the predictive accuracy of a classification system before they use the system. This means that test data sets have to be used to evaluate the system. Existing algorithms for LU learning assume that there is a large set of labeled test data for this purpose. However, this con- tradicts the LU learning problem statement, which says that the labeled set is very small. If we can ask the user to label more data, then we do not need LU learning because some examples of the test set can be used in training. Evaluation without more labeled data is also an open problem. One may look at this problem in another way. We first use the classifier generated by LU learning to classify the unlabeled set or a new test set and then sample some classified documents to be checked manually in order to estimate the classification accuracy. If classification is sufficiently accu- rate, the results of the classifier will be used. Otherwise, improvements need to be made. In this case, additional labeled data obtained during man- ual inspection can be added to the original labeled set. You see we end up doing more labeling! Hopefully, we do not have to do too much labeling. 5.2 Learning from Positive and Unlabeled Examples In some applications, the problem is to identify a particular class P of doc- uments from a set of mixed documents, which contains documents of class P and also other kinds of documents. We call the class of documents that one is interested in the positive class documents, or simply positive docu- 5.2 Learning from Positive and Unlabeled Examples 185 ments. We call the rest of the documents the negative class documents or simply negative documents. This problem can be seen as a classification problem with two classes, positive and negative. However, there are no labeled negative documents for training. The problem is stated more formally as follows, Problem Statement: Given a set P of positive documents that we are in- terested in, and a set U of unlabeled documents (the mixed set), which contains both positive documents and negative documents, we want to build a classifier using P and U that can identify positive documents in U or in a separate test set  in other words, we want to accurately classify positive and negative documents in U or in the test (or future) data set. This problem is called PU learning. Note that the set U can be used in both training and testing because U is unlabeled. The key feature of this problem is that there is no labeled negative doc- ument for learning. Traditional supervised learning algorithms are thus not directly applicable because they all require both labeled positive and la- beled negative documents to build a classifier. This is also the case for LU learning, although the labeled set for each class may be very small. 5.2.1 Applications of PU Learning The PU learning problem occurs frequently in Web and text retrieval ap- plications, because most of the time the user is only interested in Web pag- es or text documents of a particular topic. For example, one may be inter- ested in only travel-related pages (positive pages). Then all the other types of pages are negative pages. Let us use a concrete example to show the ac- tual setting of a PU learning application. Example 1: We want to build a repository of data mining research papers. We can start with an initial set of papers from a data mining conference or journal, which are positive examples. We then want to find data mining papers from online journals and conference series in the fields of databases and artificial intelligence. Journals and conferences in these fields all con- tain some data mining papers. They also contain many other types of pa- pers. The problem is how to extract data mining papers from such confer- ences and journals, or in other words, how to classify the papers from these sources into data mining papers and non-data mining papers without label- ing any negative papers in any source. ▀ In practical applications, positive documents are usually available be- cause if one has worked on a particular task for some time one should have accumulated many related documents. Even if no positive document is 186 5 Partially Supervised Learning available initially, collecting some from the Web or any other source is relatively easy. One can then use this set to find the same class of docu- ments from other sources without manually labeling any negative docu- ments. PU learning is particularly useful in the following situations: 1. Learning with multiple unlabeled sets: In some applications, one needs to find positive documents from a large number of document col- lections. For example, we want to identify Web pages that sell printers. We can easily obtain a set of positive pages from an online merchant, e.g., amazon.com. Then we want to find printer pages from other mer- chants. We can crawl each site one by one and extract printer pages from each site using PU learning. We do not need to manually label negative pages (non-printer pages) from any site. Although it may not be hard to label some negative pages from a sin- gle site, it is difficult to label for every site. Note that in general the classifier built based on the negative pages from one site s1 may not be used to classify pages from another site s2 because the negative pages in s2 can be very different from the negative pages in s1. The reason is that although both sites sell printers, the other products that they sell can be quite different. Thus using the classifier built for s1 to classify pages in s2 may violate the fundamental assumption of machine learning: the dis- tribution of training examples is identical to the distribution of test ex- amples. As a consequence, we may obtain poor accuracy results. 2. Learning with unreliable negative examples: This situation often oc- curs in experimental sciences. For example, in biology, biologists per- form experiments to determine some biological functions. They are of- ten quite confident about positive cases that they have discovered. However, they may not be confident about negative cases because labo- ratory results can be affected by all kinds of conditions. The negative cases are thus unreliable. It is perhaps more appropriate to treat such negative cases as unlabeled examples than negative examples. PU learning is also useful for modeling and solving the following prob- lems, which have been dealt with traditionally using other techniques: Set expansion: Given a set S of seeds or examples of a particular class, and a set D of candidate instances, we wish to determine which of the can- didates in D belong to S. In other words, we “expand” the set S based on the given seeds. This is clearly a classification problem which requires ar- riving at a binary decision for each candidate in D (belonging to S or not). However, in practice, the problem is often solved as a ranking problem, i.e., ranking the instances in D based on their likelihoods of belonging to S. It is shown in [28] that the set expansion problem can be modeled by PU learning exactly, with S and D as P and U respectively. The paper uses a 5.2 Learning from Positive and Unlabeled Examples 187 PU learning method called S-EM [30] to solve an entity set expansion problem in text mining, i.e., to expand a set of given named entities (seeds) based on a text corpus. The classic methods for solving this problem in text mining and natural language processing were based on distributional similarity [23, 36]. The approach works by comparing the similarity of the surrounding word distributions of each candidate with those of the seeds, and then ranking the candidates using their similarity scores. However, it is shown in [28] that S-EM outperforms distributional similarity significantly for the problem. In machine learning, there is also a technique called Bayesian Sets [16] which was specifically designed for solving the set ex- pansion problem. However, it does not perform as well as S-EM. The rea- son given in the paper is as follows: Distributional similarity does not use any information in the candidate set (or the unlabeled set U) to separate positive and negative instances. It ranks the candidates solely through similarity comparisons with the given seeds (or positive cases). Bayesian Sets is better because it considers U. Its learning method produces a weight vector for features based on their occurrence differences in the positive set P and the unlabeled set U. This weight vector is then used in computing the final scores for ranking. S-EM also considers these differences and in addition, it uses automatically identified reliable negative instances to help distinguish negative and positive cases, which both Bayesian Sets and dis- tributional similarity do not do. This balanced approach by S-EM to sepa- rate the positive and negative cases is the reason for its higher accuracy. Covariate shift or sample selection bias: Most machine learning methods assume that the training and the test data have identical distributions. However, this assumption may not hold in practice, i.e., the training and the test distributions can be different. The problem is called covariate shift or sample selection bias [18, 19, 40, 44]. In general, this problem is not solvable because the two distributions can be arbitrarily far apart from each other. Various assumptions were made to solve some special cases. A special case of the problem is studied in [27], where the positive train- ing and test samples have identical distributions, but the negative training and test samples may have different distributions. This scenario occurs in many binary text classification problems. It is shown that PU learning pro- vides a good solution, which does not need the negative training data. 5.2.2 Theoretical Foundation Before discussing the theoretical foundation of PU learning, let us first de- velop some intuition on why PU learning is possible and why unlabeled data are helpful. Fig. 5.5 shows the idea. 188 5 Partially Supervised Learning Fig. 5.5. Unlabeled data are helpful In Fig. 5.5(A), we see only positive documents (data points) represented with +’s. We assume that a linear classifier is sufficient for the classifica- tion task. In this case, it is hard to know where to draw the line to separate positive and negative examples because we do not know where the nega- tive examples might be. There are infinite possibilities. However, if the un- labeled data (represented by small circles) are added to the space (Fig. 5.5(B)), it is very clear where the separation line should be. Let us now discuss a theoretical result of PU learning. Let (xi, yi) be random variables drawn independently from probability distribution D(x,y) where y  {1, 1} is the conditional random variable that we wish to estimate given x. xi represents a document, and yi is its class, which can be 1 (positive) or –1 (negative). Let Dx|y=1 be the conditional distribution from which the positive examples are independently drawn and let Dx be the marginal distribution from which unlabeled examples are independently drawn. Our objective is to learn a classification function f that can separate positive and negative documents. Since learning is to produce a classifier that has the minimum probability of error, Pr(f(x)y), let us rewrite it into a more useful form, Pr(f(x)y) = Pr(f(x)=1 and y=1) + Pr(f(x)= 1 and y=1). (8) The first term can be rewritten as Pr(f(x)=1 and y=1) = Pr(f(x)=1) – Pr(f(x)=1 and y=1) = Pr(f(x)=1) – (Pr(y=1) – Pr(f(x)= 1 and y=1)). (9) Substituting (9) into Equation (8), we obtain Pr(f(x)y) = Pr(f(x)=1) – Pr(y=1) + 2Pr(f(x)= 1|y=1)Pr(y=1). (10) Since Pr(y = 1) is constant (although it is unknown), we can minimize the probability of error by minimizing (A) With only positive data (B) With both positive and unlabeled data + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5.2 Learning from Positive and Unlabeled Examples 189 Pr(f(x)=1) + 2Pr(f(x)= 1|y =1)Pr(y=1). (11) If we can hold Pr(f(x)= 1|y=1) small, then learning is approximately the same as minimizing Pr(f(x)=1). Holding Pr(f(x)= 1|y=1) small while minimizing Pr(f(x)=1) is approximately the same as minimizing Pru(f(x)=1) (on the unlabeled set U) while holding PrP(f(x)=1) ≥ r (on the positive set P), where r is the recall, i.e., Pr(f(x)=1|y=1). Note that (PrP(f(x)=1) ≥ r) is the same as (PrP(f(x)= 1) ≤ 1–r). Two theorems given by Liu et al. [30] state these formally and show that in both the noiseless case (P has no error) and the noisy case (P contains errors, i.e., some negative documents) reasonably good learning results can be achieved if  the problem is posed as a constrained optimization problem where the algorithm tries to minimize the number of unlabeled examples labeled positive subject to the constraint that the fraction of errors on the posi- tive examples is no more than 1 r. Example 2: Fig. 5.6 illustrates the constrained optimization problem. As- sume that positive and negative documents can be linearly separated. Posi- tive documents are represented with +’s, and unlabeled documents with small circles. Assume also that the positive set has no error and we want the recall r on the positive set to be 100%. Each line in the figure is a pos- sible linear classifier. Every document on the left of each line will be la- beled (classified) by the line as positive, and every document on the right will be labeled as negative. Lines 1 and 2 are clearly not solutions because the constraint “the fraction of errors on the positive examples must be no more than 1 r (= 0)” is violated, although the number of unlabeled exam- ples labeled (classified) as positive is minimized by line 1. Lines 4, 5, and 6 are poor solutions too because the number of unlabeled examples labeled as positive is not minimized by any of them. Line 3 is the optimal solution. Under the constraint that no positive example is labeled negative, line 3 minimizes the number of unlabeled examples labeled as positive. ▀ Fig. 5.6. An illustration of the constrained optimization problem + + + + + + + + + + o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o 1 2 3 4 5 6 190 5 Partially Supervised Learning Based on the constrained optimization idea, two kinds of approaches have been proposed to build PU classifiers: the two-step approach and the direct approach. In the actual learning algorithms, the user may not need to specify a desired recall level r on the positive set because some of these algorithms have their evaluation methods that can automatically determine whether a good solution has been found. 5.2.3 Building Classifiers: Two-Step Approach As its name suggests the two-step approach works in two steps: 1. Identifying a set of reliable negative documents (denoted by RN) from the unlabeled set U. 2. Building a classifier using P, RN and U  RN. This step may apply an existing learning algorithm once or iteratively depending on the quality and the size of the RN set. Fig. 5.7. An illustration of the two-step approach This two-step approach is illustrated in Fig. 5.7. Here, we assume that step 2 uses an iterative algorithm. In step 1, a set of reliable negative doc- uments (RN) is found from the unlabeled set U, which divides U into two subsets, RN and Q (= U  RN). Q is called the likely positive set. In step 2, the algorithm iteratively improves the results by adding more documents to RN until a convergence criterion is met. We can see that the process is try- ing to minimize the number of unlabeled examples labeled positive since U P …… Reliable negative (RN) Q=URN P Step 1 Step 2 positive negative PP 5.2 Learning from Positive and Unlabeled Examples 191 Q becomes smaller and smaller while RN becomes larger and larger. In other words, it tries to iteratively increase the number of unlabeled exam- ples that are labeled negative while maintaining the positive examples in P correctly classified. We present several techniques for each step below. Techniques for Step 1 We introduce four methods to extract reliable negative documents from the unlabeled set U. Spy Technique: This technique works by sending some “spy” documents from the positive set P to the unlabeled set U. Fig. 5.8 gives the algorithm of the technique, which is used in the S-EM system [30]. The algorithm has three sub-steps: 1. It randomly samples a set S of positive documents from P and put them in U (lines 2 and 3). The default sampling ratio of s% is 15% in S-EM. The documents in S act as “spy” documents from the positive set to the unlabeled set U. Since the spies behave similarly to the unknown posi- tive documents in U, they allow the algorithm to infer the behavior of the unknown positive documents in U. 2. It runs the naïve Bayesian (NB) algorithm using the set P  S as positive and the set U  S as negative (lines 3–7). The NB classifier is then ap- plied to classify each document d in U  S (or Us), i.e., to assign it a probabilistic class label Pr(1|d), where 1 represents the positive class. 3. It uses the probabilistic labels of the spies to decide which documents are most likely to be negative. A threshold t is employed to make the Algorithm Spy(P, U) 1. RN  ; 2. S  Sample(P, s%); 3. Us  U  S; 4. Ps  P – S; 5. Assign each document in Ps the class label 1; 6. Assign each document in Us the class label 1; 7. NB(Us, Ps); // This produces a NB classifier. 8. Classify each document in Us using the NB classifier; 9. Determine a probability threshold t using S; 10. for each document d  Us do 11. if its probability Pr(1|d) < t then 12. RN  RN  {d}; 13. endif 14. endfor Fig. 5.8. The spy technique for step 1. 192 5 Partially Supervised Learning decision. Those documents in U with lower probabilities (Pr(1|d)) than t are the most likely negative documents, denoted by RN (lines 10–14). We now discuss how to determine t using spies (line 9). Let the set of spies be S = {s1, s2, …, sk}, and the probabilistic labels assigned to each si be Pr(1|si). Intuitively, we can use the minimum probability in S as the threshold value t, i.e., t = min{Pr(1|s1), Pr(1|s2), …, Pr(1|sk)}, which means that we want to retrieve all spy documents. In a noiseless case, using the minimum probability is acceptable. However, most real-life document collections have outliers and noise. Using the minimum prob- ability is unreliable. The reason is that the posterior probability Pr(1|si) of an outlier document si in S could be 0 or smaller than most (or even all) actual negative documents. However, we do not know the noise lev- el of the data. To be safe, the S-EM system uses a large noise level l = 15% as the default. The final classification result is not very sensitive to l as long it is not too small. To determine t, we first sort the documents in S according to their Pr(1|si) values. We then use the selected noise level l to decide t: we select t such that l percent of documents in S have probability less than t. Hence, t is not a fixed value. The actual parame- ter is in fact l. Note that the reliable negative set RN can also be found through multiple iterations. That is, we run the spy algorithm multiple times. Each time a new random set of spies S is selected from P and a different set of reliable negative documents is obtained, denoted by RNi. The final set of reliable negative documents is the intersection of all RNi. This may be a better technique because we do not need to worry that one set of random spies S may not be chosen well, especially when the set P is not large. Cosine-Rocchio (CR) Technique: This method (Fig. 5.9) is used in [27]. It consists of two sub-steps: Sub-step 1 (lines 1–9, Fig. 5.9): This sub-step extracts a set of potential negatives PN from U by computing similarities of the unlabeled docu- ments in U with the positive documents in P. Those documents in U that are very dissimilar to the documents in P are likely to be negative (lines 7–9). To make the decisions, a similarity measure and a similarity thre- shold are needed. The similarity measure is the well-known cosine simi- larity (see Sect. 6.2.2). To compute the similarity, each document in P and U is first converted to a vector d using the TF-IDF scheme (see Sect. 6.2.2). Note that we use a lower case bold letter to represent a vector here. The positive documents in P are used to compute the threshold value. First, a positive representative vector (vP) is constructed by sum- ming up the documents in P (line 3). The similarity of each document d in P with vP is calculated using the cosine measure, cos(vP, d), in line 4. 5.2 Learning from Positive and Unlabeled Examples 193 Line 5 sorts the documents in P according to their cos(vP, d) values, which helps to determine the similarity threshold. The threshold is used to filter out as many as possible hidden positive documents from U so that a very pure negative set PN can be obtained. Since the hidden posi- tives in U should have the same behaviors as the positives in P in terms of their similarities to vP, ideally we should set the minimum similarity value of all documents d  P and vP as the threshold value ω. However, as in the spy technique, we need to consider possible noise in P. It would therefore be prudent to ignore a small percentage l of documents in P that are most dissimilar to vP and assume them to be noise or outliers. The default noise level of l = 5% is used in [27]. In line 6, l is used to decide the similarity threshold ω. Then, for each document d in U, if its cosine similarity cos(vP, d) is less then ω, it is regarded as a potential negative and stored in PN (lines 8–9). PN, however, is still not sufficient for accurate learning. Using PN, sub-step 2 produces the final RN. Sub-step 2 (line 10–14, Fig. 5.9): To extract the final reliable negatives, the algorithm employs the Rocchio classification method to build a classifier f using P and PN. Those documents in U that are classified as negatives by f are regarded as the final reliable negatives and stored in set RN. Fol- lowing the Rocchio formula in Sect. 6.3, the classifier f actually consists of a positive and a negative prototype vectors cP and cPN (lines 11 and Algorithm CR(P, U) 1. PN = ; RN = ; 2. Represent each document d  P and U as a vector using the TF-IDF scheme; 3.    P P P d d d ||v |||| 1 ; 4. Compute cos(vp, d) for each d  P; 5. Sort all the documents dP according to cos(vp, d) in a decreasing order; 6. ω = cos(vp, d) where d is ranked in the position of (1- l)*|P|; 7. for each d  U do 8. if cos(vp, d) < ω then 9. PN = PN ∪ {d} 10.    PNP P PNP dd d d ||d d ||c ||||||||  ; 11.    PPN PN PPN dd d d ||d d ||c ||||||||  ; 12. for each d  U do 13. if cos(cPN, d) > cos(cP, d) then 14. RN = RN ∪ {d} Fig. 5.9. The CR technique for step 1 194 5 Partially Supervised Learning 12). α and β are parameters for adjusting the relative impact of the ex- amples in P and PN. As suggested in [4], α = 16 and β = 4 are used in [27]. The classification is done in lines 12–14. Details about Rocchio classification can be found in Sect. 6.3. 1DNF Technique: The 1DNF method (Fig. 5.10) is used in [43]. It first builds a positive feature set PF containing words that occur in the positive set P more frequently than in the unlabeled set U (lines 1–7). Line 1 col- lects all the words in U  P to obtain a vocabulary V. Lines 8–13 try to identify reliable negative documents from U. A document in U that does not contain any feature in PF is regarded as a reliable negative document. NB Technique: This method is employed in [29]. It simply uses a naïve Bayesian classifier to identify a set of reliable negative documents RN from the unlabeled set U. The algorithm is given in Fig. 5.11. This method may also be run multiple times. Each time we randomly remove a few documents from P to obtain a different set of reliable nega- tive documents, denoted by RNi. The final set of reliable negative docu- ments RN is the intersection of all RNi. Rocchio technique: This method is employed in [26]. The algorithm is the same as that in Fig. 5.11 except that NB is replaced with Rocchio. The Rocchio classification method is described in Sect. 6.3. Techniques for Step 2 There are two approaches for this step. 1. Run a learning algorithm (e.g., NB or SVM) using P and RN. The set of documents in URN is discarded. This method works well if the reliable negative set RN is sufficiently large and contains mostly negative docu- ments. The spy technique, NB and Rocchio in step 1 are often able to produce a sufficiently large set of negative documents. The 1DNF tech- nique may only identify a very small set of negative documents. Then running a learning algorithm will not be able to build a good classifier. 2. Run a learning algorithm iteratively till it converges or some stopping criterion is met. This method is used when the set RN is small. We will not discuss the first approach as it is straightforward. SVM usually does very well. Below, we introduce two techniques for the second ap- proach, which are based on EM and SVM respectively. EM Algorithm with Naïve Bayesian Classification: The EM algorithm can be used naturally here [30]. As in LU learning, the Expectation step basically fills in the missing data. In our case, it produces and revises the 5.2 Learning from Positive and Unlabeled Examples 195 probabilistic labels of the documents in URN (see below). The parameters are estimated in the Maximization step after the missing data are filled. This leads to the next iteration of the algorithm. EM converges when its parameters stabilize. Using NB in each iteration, EM employs the same equations as those used in building a NB classifier (Equation (33) for the Expectation step, and Equations (31) and (32) for the Maximization step). The class probability given to each document in URN takes the value in [0, 1] instead of {0, 1}. The algorithm is given in Fig. 5.12. The EM algorithm here makes the same mixture model assumptions as in LU learning. Thus, it has the same problem of model mismatch. See the discussions in Sect. 5.1.1. Iterative SVM: In this method, SVM is run iteratively using P, RN and Q (= URN). The algorithm, called I-SVM, is given in Fig. 5.13. The basic idea is as follows: In each iteration, a new SVM classifier f is constructed from P and RN (line 4). Here RN is regarded as the set of negative exam- ples (line 2). The classifier f is then applied to classify the documents in Q (line 5). The set W of documents in Q that are classified as negative (line 6) is removed from Q (line 8) and added to RN (line 9). The iteration stops Algorithm 1DNF(P, U) 1. Assume the word feature set be V = {w1,…, wn}, wi U  P; 2. Let positive feature set PF  ; 3. for each wi  V do // freq(wi, P): number of times 4. if (freq(wi, P) / |P| > freq(wi, U) / |U|) then // that wi appears in P 5. PF  PF  {wi}; 6. endif 7. endfor; 8. RN  U; 9. for each document d  U do 10. if wj freq(wj, d) > 0 and wj  PF then 11. RN  RN – {d} 12. endif 13. endfor Fig. 5.10. The 1DNF technique for step 1 1. Assign each document in P the class label 1; 2. Assign each document in U the class label 1; 3. Build a NB classifier using P and U; 4. Use the classifier to classify U. Those documents in U that are classified as negative form the reliable negative set RN. Fig. 5.11. The NB method for Step 1 196 5 Partially Supervised Learning when no document in Q is classified as negative, i.e., W =  (line 7). The final classifier is the result. This method is used in [26] [42, 43]. Finally, we note again that if the first step is able to identify a large number of reliable negative documents from U, running SVM once in step 2 is sufficient. Iterative approaches may not be necessary, which are also less efficient. The Spy, NB and Rocchio methods for step 1 are often able to identify a large number of reliable negative documents. See [29] for an evaluation of various methods based on two benchmark text collections. Classifier Selection The iterative methods discussed above produce a new classifier at each it- eration. However, the classifier at the convergence may not be the best Algorithm EM(P, U, RN) 1. Each document in P is assigned the class label 1; 2. Each document in RN is assigned the class label 1; 3. Learn an initial NB classifier f from P and RN (using Equations (31) and (32) in Chap. 3); 4 repeat // E-Step 5 for each example di in URN do 6 Using the current classifier f to compute Pr(cj|di) using Equation (33) in Chap. 3. 7 end // M-Step 8 learn a new NB classifier f from P, RN and URN by computing Pr(cj) and Pr(wt|cj) (using Equations (31) and (32) in Chap. 3). 9 until the classifier parameters stabilize 10. Return the classifier f from the last iteration. Fig. 5.12. EM algorithm with the NB classifier Algorithm I-SVM(P, RN, Q) 1. Every document in P is assigned the class label 1; 2. Every document in RN is assigned the class label –1; 3. loop 4. Use P and RN to train a SVM classifier f; 5. Classify Q using f; 6. Let W be the set of documents in Q that is classified as negative; 7. if W =  then exit-loop // convergence 8. else Q  Q – W; 9. RN  RN  W; 10. endif; Fig. 5.13. Running SVM iteratively 5.2 Learning from Positive and Unlabeled Examples 197 classifier. In general, each iteration of the algorithm gives a classifier that may potentially be a better classifier than the classifier produced at con- vergence. This is true for both EM and SVM. The main problem with EM is that classes and topics may not have one- to-one correspondence. This is the same problem as in LU learning. SVM may also produce poor classifiers at the convergence because SVM is sen- sitive to noise. If the RN set is not chosen well or in an iteration some posi- tive documents are classified as negative, then the subsequent iterations may produce very poor results. In such cases, it is often better to stop at an earlier iteration. One simple method is to apply the theory directly. That is, each classifier is applied to classify a positive validation set, Pv. If many documents from Pv (e.g., > 5%) are classified as negative, the algorithm should stop (that is a recall of 95%). If the set P is small, the method can also be applied to P directly. A principled method is given in the next sub- section, i.e., Equation (14). 5.2.4 Building Classifiers: Biased-SVM We now present a direct approach, called biased-SVM. This approach modi- fies the SVM formulation slightly so that it is suitable for PU learning. Let the set of training examples be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi is an input vector and yi is its class label, yi  {1, 1}. Assume that the first k1 examples are positive examples P (labeled 1), while the rest are unlabeled examples U, which are treated as negative and labeled 1. Thus, the negative set has errors, i.e., containing positive documents. We consider two cases. 1. Noiseless case: There is no error in the positive examples but only in unlabeled examples. The theoretical result in Sect. 5.2.2 states that if the sample size is large enough, minimizing the number of unlabeled exam- ples classified as positive while constraining the positive examples to be correctly classified will give a good classifier. Following the theory, in this noiseless case, we have this following SVM formulation nkki nkkib kib C i ii i n ki i ..., 1, , ,0 ..., 1, , ,1)(1 1,...,2,1 ,1 :Subject to 2 :Minimize          xw xw ww (12) In this formulation, we do not allow any error in the positive set P, which is the first constraint, but allow errors for the negative set (the original unlabeled set), which is the second constraint. Clearly, the for- 198 5 Partially Supervised Learning mulation follows the theory exactly due to the second term in the objec- tive function. The subscript in the second term starts from k, which is the index of the first unlabeled example. To distinguish this formulation from the classic SVM, we call it the biased-SVM [29]. 2. Noisy case: In practice, the positive set may also contain some errors. Thus, if we allow noise (or error) in positive examples, we have the fol- lowing soft margin version of the biased-SVM which uses two parame- ters C+ and C to weight positive errors and negative errors differently. ni niby CC i iii n ki i k i i ..., ,2 ,1 ,0 ,...,2,1 ,1)( :Subject to 2 :Minimize 1 1             xw ww (13) We can vary C+ and C to achieve our objective. Intuitively, we give a bigger value for C+ and a smaller value for C because the unlabeled set, which is assumed to be negative, contains positive data. We now focus on Equation (13) as it is more realistic in practice. We need to choose values for C+ and C. The common practice is to try a range of values for both C+ and C and use a separate validation set to verify the performance of the resulting classifier. The C+ and C values that give the best classification results on the validation set are selected as the final pa- rameter values for them. Cross-validation is another possible technique for the purpose. Since the need to learn from positive and unlabeled examples often arises in retrieval situations (retrieving positive documents from the unlabeled set), we employ the commonly used F-score as the performance measure, F = 2pr/(p+r), where p is the precision and r is the recall. Unfortunately it is not clear how to estimate the F-score without labeled negative examples. In [24], Lee and Liu proposed an alternative perform- ance measure to compare different classifiers. It behaves similarly to the F- score but can be estimated directly from the validation set without the need of labeled negative examples. The measure is ,)1)(Pr( 2 xf r (14) where f is the classifier and Pr(f(x)=1) is the probability that a document is classified as positive. It is not easy to see why Equation (14) behaves simi- larly to the F-score, but we can show that r2/Pr(f(x)=1) = pr/Pr(y=1), where Pr(y=1) is the probability of positive documents. pr/Pr(y=1) behaves simi- larly to the F-score in the sense that it is large when both p and r are large and is small when either p or r is small. 5.2 Learning from Positive and Unlabeled Examples 199 We first write recall (r) and precision (p) in terms of probability: r = Pr(f(x)=1| y=1), (15) p = Pr(y=1| f(x)=1). (16) According to probability theory, we have Pr(f(x)=1|y=1)Pr(y=1) = Pr(y=1| f(x)=1)Pr(f(x)=1), (17) which can be written as )1Pr()1)(Pr(  y p f r x . (18) Multiplying both sides by r, we obtain the result: )1Pr()1)(Pr( 2  y pr f r x . (19) The quantity r2/Pr(f(x)=1) can be estimated based on the validation set, which contains both positive and unlabeled documents. r can be estimated using the positive examples in the validation set and Pr(f(x) = 1) can be es- timated from the whole validation set. This criterion in fact reflects the theory in Sect. 5.2.2 very well. The quantity is large when r is large and Pr(f(x) = 1) is small, which means that the number of unlabeled examples labeled as positive should be small. In [29], it is shown that biased-SVM works better than two-step techniques. 5.2.5 Building Classifiers: Probability Estimation We now present another direct approach, which is proposed in [14] and is based on a probabilistic formulation. We use similar notations as in Sect. 5.2.4. Let x be an example and y  {1, 1} be a binary class label. Let s = 1 if the example x is labeled, and let s = 0 if x is unlabeled. Only positive examples are labeled, so y = 1 is certain when s = 1 (i.e., every labeled ex- ample must be positive), but when s = 0, then either y = 1 or y = -1 may be true (i.e., an unlabelled example can be positive or negative). The fact that only positive examples are labeled can be stated formally as follows, Pr(s = 1|x, y = 1) = 0. (20) In words, the probability that x appears in the labeled set is zero if y = 1. Our goal is to learn a classification function f(x) such that f(x) = Pr(y = 1|x) as closely as possible. To achieve this goal, an assumption called se- lected completely at random is made which states that the labeled posi- 200 5 Partially Supervised Learning tive examples are chosen randomly from all positive examples. What this means is that if y = 1, the probability that a positive example is labeled is the same constant regardless of x. Stated formally, the assumption is that Pr(s = 1|x, y = 1) = Pr(s = 1|y = 1). (21) Here, c = Pr(s = 1|y = 1) is the constant probability that a positive exam- ple is labeled. For learning, a training sample, which consists of two subsets, the “la- beled” (s = 1) set P and the “unlabeled” (s = 0) set U, is randomly drawn from a distribution Pr(x, y, s) that satisfies Equations (20) and (21). If these two sets are given to a standard learning algorithm, the algorithm will yield a function g(x) such that g(x) = Pr(s = 1|x) approximately. The main result of [14] is the following lemma which shows how to obtain f(x) from g(x). Lemma 1: Suppose the “selected completely at random” assumption holds. Then, c gf )()( xx  , (22) where c = Pr(s = 1|y = 1). Proof: We consider g(x), which is Pr(s = 1|x). Due to the assumption Pr(s = 1|y =1, x) = Pr(s = 1|y = 1), we have g(x) = Pr(s = 1|x) = Pr(y = 1  s = 1|x) = Pr(y = 1|x)Pr(s = 1|y = 1, x) = Pr(y = 1|x)Pr(s = 1|y = 1) = f(x)Pr(s = 1|y = 1) The result follows by dividing each side by Pr(s = 1|y = 1), which is c. ▀ The value of the constant c = Pr(s = 1|y = 1) can be estimated using a trained classifier g and a validation set of examples. Let V be such a vali- dation set that is drawn from the overall distribution Pr(x, y, s) in the same manner as the training set. Let VP be the subset of examples in V that is la- beled (and hence positive). The estimator of c (= Pr(s = 1|y = 1)) is the av- erage value of g(x) for all x in P. Formally, the estimator is    PVxP xgVc )(|| 1ˆ . (23) This is a reasonable estimator of c because theoretically g(x) = c for all x  VP, 5.2 Learning from Positive and Unlabeled Examples 201 g(x) = Pr(s = 1|x) = Pr(s = 1|x, y = 1)Pr(y = 1|x) + Pr(s = 1|x, y = 1)Pr(y = 1|x) = Pr(s = 1|x, y = 1) × 1 + 0 × 0 since x  VP = Pr(s = 1|y = 1). With this estimator, Equation (22) can be used to build a PU classifier f. In [14], the classifier g was built using SVM, and the scaling method in [38] was used to get probability estimates from the SVM output. It is also worth noting that an interesting consequence of Lemma 1 is that f is an increasing function of g. This means that if the classifier f is on- ly used to rank examples x according to the chance that they belong to class y = 1, then the classifier g can be used directly instead of f. 5.2.6 Discussion Does PU Learning Always Work? Theoretical results show that it should if the positive set and the unlabeled set are sufficiently large [30]. This has been confirmed by many experimental studies. Interested readers can find the detailed results in [29, 30], which we summarize below: 1. PU learning can achieve about the same classification results as fully supervised learning when the positive set and the unlabeled set are suf- ficiently large. This implies that labeled negative examples do not pro- vide much information for learning. When the positive set is very small, PU learning is poorer than fully supervised learning. 2. For the two-step approaches, using SVM for the second step performs better than EM. SVM needs to be run only once if step 1 can extract a large number of reliable negative documents. Both Spy and Rocchio are able to do that. Thus, the iterative method in step 2 is not necessary. The generative model of naïve Bayes with EM in the second step can perform very well if the mixture model assumption holds [30]. How- ever, if the mixture model assumption does not hold, the classification results can be very poor [29]. Note that SVM is called a discriminative model (or classifier) because it does not make any model assumptions. It simply finds a surface to separate positive and negative examples. 3. Biased-SVM performs slightly better than the 2-step approaches. How- ever, it is slow in training because SVM needs to be run a large number of times in order to select the best values for C+ and C. Evaluation: Unlike LU learning, here we do not even have labeled nega- tive examples, which makes the evaluation difficult. Although Equation (14) and other heuristics allow a system to choose a “better” classifier among a set of classifiers, it is unable to give the actual accuracy, precision 202 5 Partially Supervised Learning or recall of each classifier. Evaluation is an open problem. The results re- ported in the literature assume that a set of labeled positive and negative test examples is available, which, of source, is unrealistic because the PU learning model states that no labeled negative example is available. In some cases, the evaluation can be done with some confidence. For example, if the user needs to extract positive documents from many unla- beled sets (document sources) as in the example of identifying printer pag- es from multiple Web sites, a PU learning algorithm can be applied to one site and then the user manually checks the classification result to see whether it is satisfactory. If the result is satisfactory, the algorithm can be applied to the rest of the sites without further manual evaluation. Appendix: Derivation of EM for Naïve Bayesian Classification EM is a method for performing a classical statistical estimation technique called maximum likelihood estimation. In maximum likelihood estima- tion, the aim is to find the model parameter ˆ that maximizes the likeli- hood function Pr(Do; ) for observed data Do. In other words, maximum likelihood estimation aims to select the model that is most likely to have generated the observed data. In many cases, such as in the naïve Bayesian classification model, the maximum likelihood estimator is easy to find and has a closed form solution when all components of the data D are ob- served. However, the problem becomes difficult when the data D actually consists of an observed component Do and an unobserved component Du. In such cases, iterative methods that converge only to a local maximum, such as the EM method, are usually used. Maximizing the log likelihood function logPr(Do; ) produces the same solution as maximizing the likelihood function and is easier to handle mathe- matically. In the presence of unobserved data Du, the log likelihood function becomes ).;,Pr(log);Pr(log   uoDo DDD u Instead of maximizing the log likelihood   uD uo DD);,Pr(log directly, at each iteration T, the EM al- gorithm finds the value  that maximizes the expected complete log likelihood ),;,Pr(log);|( 1   uoD T ou DDDDE u (24) where T1 is the parameter that was produced in iteration T1. In many cases, such as in the naïve Bayesian model, the expected log likelihood is easy to maximize and has a closed form solution. It can be shown (see [11]) that the log likelihood increases monotonically with each iteration of the EM algorithm. Appendix: Derivation of EM for Naïve Bayesian Classification 203 We now derive the EM update for the naïve Bayesian model. We first consider the complete log likelihood, that is, the log likelihood when all variables are observed. The conditional probability of a document given its class is (see Sect. 3.7.2 in Chap. 3)    || 1 ! );|Pr(|!||)Pr(|);|Pr( V t N ti tijt iiji N cwddcd (25) Each document and its class label are assumed to have been sampled independently. Let c(i) be the class label of document i. The likelihood function can hence be written as .);Pr(! );|Pr(|!||)Pr(|);Pr();|Pr( || 1 || 1 || 1 )( )( )()(   D i D i V t N i ti tiit iiiii cN cwddccd (26) Taking logs, we have the complete log likelihood function ,);Pr(log);|Pr(log || 1 || 1 || 1 )()(    D i D i V t ti iit ccwN (27) where  is a constant containing the terms unaffected by . To facilitate the process of taking expectation when some of the class labels are not ob- served, we introduce indicator variables, hik, that take the value 1 when document i takes the label k and the value 0 otherwise. The complete log likelihood can be written in the following equivalent form .);Pr(log);|Pr(log || 1 || 1 || 1 || 1 || 1    D i k C k ikk D i V t ti C k ik chcwNh t (28) When some of the labels are not observed, we take the conditional expec- tation for the unobserved variables hik with respect to T1 to get the ex- pected complete log likelihood ,);Pr(log);|Pr( );|Pr(log);|Pr( || 1 || 1 1 || 1 || 1 || 1 1         D i k C k T ik k D i V t ti C k T ik cdc cwNdc t (29) where, for the observed labels c(i), we use the convention that Pr(ck|di;T1) takes the value one for ck = c(i) and zero otherwise. We maximize the ex- pected complete log likelihood subject to the coefficients summing to one using the Lagrange multiplier method. The Lagrangian is 204 5 Partially Supervised Learning .);|Pr(1);Pr(1 );Pr(log);|Pr( );|Pr(log);|Pr( || 1 || 1 || 1 || 1 || 1 || 1 1 || 1 || 1 || 1 1                           V t C k V t kttk C k k D i k C k T ik k D i V t ti C k T ik cwc cdc cwNdc t (30) Differentiating the Lagrangian with respect to , we get .1);Pr(|| 1   C k kc Differentiating with respect to Pr(ck; ), we get .||,...,1);Pr();|Pr( || 1 1 Ckforcdc k D i T ik     (31) Summing the left and right-hand side over k and using ,1);Pr(|| 1   C k kc we get .||);|Pr( || 1 || 1 1    D i C k T ik Ddc Substituting back, we obtain the up- date equation .|| );|Pr( );Pr( || 1 1 D dc c D i T T ij j     (32) Working similarly, we can get the update equation for Pr(wt|cj; T), . );|Pr( );|Pr( );|Pr( || 1 || 1 1 || 1 1          V s D i T ijsi D i T ijtiT jt dcN dcN cw (33) To handle the 0 count problem (see Sect. 3.7.2 in Chap. 3), we can use Lidstone smoothing (Equation (31) in Chap. 3). Bibliographic Notes Learning with labeled and unlabeled examples (LU learning) using naïve Bayes and EM was proposed by Nigam et al. [34]. They also noted the problem of having mixtures of subclasses in the classes and proposed to identify and use such subclasses as a possible solution. A hierarchical clus- tering technique was also proposed by Cong et al. [8] for handling the mix- ture of subclasses problem. Castelli and Cover [5] presented a theoretical study of LU learning using mixture models. Bibliographic Notes 205 Co-training was introduced by Blum and Mitchell [3]. Follow-on works include those by Collins and Singer [7], Goldman and Zhou [17], etc. Gen- eralization error bounds within the Probably Approximately Correct (PAC) framework was given in [10] by Dasgupta et al. Nigam and Ghani [33] ex- amined the importance of feature division in co-training and compared it to the EM algorithm and self-training. Transduction was proposed by Vapnik [41] as learning when the test in- stances are known. Joachims described a heuristic algorithm and built a system for performing transduction using SVM [20]. The transductive SVM given in [20] can also be used for induction, i.e. classifying future unseen instances. In contrast, most graph-based methods are more suited for transduction. The graph-based mincut algorithm was introduced by Blum and Chalwa [2]. The graph-based Gaussian field method was pro- posed by Zhu et al. [46] while the spectral graph transducer was proposed by Joachims [21]. The edited book by Chapelle et al. [6] gives a compre- hensive coverage of various LU learning algorithms. On learning from positive and unlabeled examples (or PU learning), Denis [13] reported a theoretical study of PAC learning in this setting un- der the statistical query model [22], which assumes that the proportion of positive instances in the unlabeled set is known. Letouzey et al. [25] pre- sented a learning algorithm based on a modified decision tree method in this model. Liu et al. [30] gives another theoretical study. It was concluded that learning can be achieved if the problem is posed as a constrained op- timization problem (see Sect. 5.2.2). Most existing algorithms for solving the problem are based on this constrained optimization model. Over the years, several practical algorithms were proposed. The first class of algorithms deals with the problem in two steps. These algorithms include S-EM [30], PEBL [42, 43], and Roc-SVM [26], which have been studied in this chapter. The second class of algorithm follows the theoreti- cal result directly. Lee and Liu [24] described a weighted logistic regres- sion technique. Liu et al. [29] described a biased-SVM technique. A com- prehensive comparison of various techniques was also reported in [29]. It was shown that biased-SVM performed better than other techniques. Re- cently, Elkan and Noto proposed a new probabilistic model [14], which has also been applied to a real-life bioinformatics problem [31] with prom- ising results. Some other works on PU learning include those of Barbara et al. [1], Deng et al. [12], Fung, et al. [15], Zhang and Lee [45], etc. A closely related work to PU learning is one-class SVM, which uses on- ly positive examples to build a classifier. This method was proposed by Scholkopf et al. [39]. Manevitz and Yousef [32] studied text classification using one-class SVM. Li and Liu [26] showed that its accuracy results were poorer than PU learning for text classification. 206 5 Partially Supervised Learning Bibliography 1. Barbará, D., C. Domeniconi, and N. Kang. Classifying documents without labels. In Proceedings of SIAM International Conference on Data Mining (SDM-2004), 2004. 2. Blum, A. and S. Chawla. Learning from Labeled and Unlabeled Data Using Graph Mincuts. In Proceedings of International Conference on Machine Learning (ICML-2001), 2001. 3. Blum, A. and T. Mitchell. Combining labeled and unlabeled data with co- training. In Proceedings of Conference on Computational Learning Theory, 1998. 4. Buckley, C., G. Salton, and J. Allan. The effect of adding relevance information in a relevance feedback environment. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1994), 1994. 5. Castelli, V. and T. Cover. Classification rules in the unknown mixture parameter case: relative value of labeled and unlabeled samples. In Proceedings of IEEE International Symp. Information Theory, 1994. 6. Chapelle, O., B. Schölkopf, and A. Zien. Semi-supervised learning. Vol. 2. 2006: MIT Press. 7. Collins, M. and Y. Singer. Unsupervised models for named entity classification. In Proceedings of Conference on Empirical Methods in Natural Language Processing (EMNLP-1999), 1999. 8. Cong, G., W. Lee, H. Wu, and B. Liu. Semi-supervised text classification using partitioned EM. In Proceedings of Conference of Database Systems for Advanced Applications (DASFAA 2004), 2004. 9. Cormen, T., C. Leiserson, R. Rivest, and C. Stein. Introduction to Algorithms. 2001: MIT Press. 10. Dasgupta, S., M. Littman, and D. McAllester. PAC generalization bounds for co-training. In Proceedings of Advances in Neural Information Processing Systems (NIPS-2001), 2001. 11. Dempster, A., N. Laird, and D. Rubin. Maximum likelihood from incomplete data via the EM algorithm. Journal of the Royal Statistical Society. Series B (Methodological), 1977, 39(1): p. 1-38. 12. Deng, L., X. Chai, Q. Tan, W. Ng, and D. Lee. Spying out real user preferences for metasearch engine personalization. In Proceedings of Workshop on WebKDD, 2004. 13. Denis, F. PAC learning from positive statistical queries. In Proceedings of Intl. Conf. on Algorithmic Learning Theory (ALT-1998), 1998. 14. Elkan, C. and K. Noto. Learning classifiers from only positive and unlabeled data. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2008), 2008. 15. Fung, G., J. Yu, H. Lu, and P. Yu. Text classification without labeled negative documents. In Proceedings of IEEE International Conference on Data Engingeering (ICDE-2005), 2005. 16. Ghahramani, Z. and K. Heller. Bayesian sets. Advances in Neural Information Processing Systems, 2006, 18: p. 435. Bibliography 207 17. Goldman, S. and Y. Zhou. Enhanced Supervised Learning with Unlabeled Data. In Proceedings of International Conference on Machine Learning (ICML-2000), 2000. 18. Heckman, J. Sample selection bias as a specification error. Econometrica: Journal of the econometric society, 1979: p. 153-161. 19. Huang, J., A. Smola, A. Gretton, K. Borgwardt, and B. Scholkopf. Correcting sample selection bias by unlabeled data. Advances in Neural Information Processing Systems, 2007, 19: p. 601. 20. Joachims, T. Transductive inference for text classification using support vector machines. In Proceedings of International Conference on Machine Learning (ICML-1999), 1999. 21. Joachims, T. Transductive learning via spectral graph partitioning. In Proceedings of International Conference on Machine Learning (ICML-2003), 2003. 22. Kearns, M. Efficient noise-tolerant learning from statistical queries. Journal of the ACM (JACM), 1998, 45(6): p. 983-1006. 23. Lee, L. Measures of distributional similarity. In Proceedings of Annual Meeting of the Association for Computational Linguistics (ACL-1999), 1999. 24. Lee, W. and B. Liu. Learning with positive and unlabeled examples using weighted logistic regression. In Proceedings of International Conference on Machine Learning (ICML-2003), 2003. 25. Letouzey, F., F. Denis, and R. Gilleron. Learning from positive and unlabeled examples. In Proceedings of Intl. Conf. on Algorithmic Learning Theory (ALT-200), 2000. 26. Li, X. and B. Liu. Learning to classify texts using positive and unlabeled data. In Proceedings of International Joint Conference on Artificial Intelligence (IJCAI-2003), 2003. 27. Li, X., B. Liu, and S. Ng. Negative Training Data can be Harmful to Text Classification. In Proceedings of Conference on Empirical Methods in Natural Language Processing (EMNLP-2010), 2010. 28. Li, X., L. Zhang, B. Liu, and S. Ng. Distributional similarity vs. PU learning for entity set expansion. In Proceedings of Annual Meeting of the Association for Computational Linguistics (ACL-2010), 2010. 29. Liu, B., Y. Dai, X. Li, W. Lee, and P. Yu. Building text classifiers using positive and unlabeled examples. In Proceedings of IEEE International Conference on Data Mining (ICDM-2003), 2003. 30. Liu, B., W. Lee, P. Yu, and X. Li. Partially supervised classification of text documents. In Proceedings of International Conference on Machine Learning (ICML-2002), 2002. 31. Luigi, C., E. Charles, and C. Michele. Learning gene regulatory networks from only positive and unlabeled data. BMC Bioinformatics, 2010, 11. 32. Manevitz, L. and M. Yousef. One-class svms for document classification. The Journal of Machine Learning Research, 2002, 2. 33. Nigam, K. and R. Ghani. Analyzing the effectiveness and applicability of co- training. In Proceedings of ACM International Conference on Information and Knowledge Management (CIKM-2000), 2000. 208 5 Partially Supervised Learning 34. Nigam, K., A. McCallum, S. Thrun, and T. Mitchell. Text classification from labeled and unlabeled documents using EM. Machine Learning, 2000, 39(2): p. 103-134. 35. Niu, Z., D. Ji, and C. Tan. Word sense disambiguation using label propagation based semi-supervised learning. In Proceedings of Meeting of the Association for Computational Linguistics (ACL-2005), 2005. 36. Pantel, P., E. Crestan, A. Borkovsky, A. Popescu, and V. Vyas. Web-scale distributional similarity and entity set expansion. In Proceedings of Conference on Empirical Methods in Natural Language Processing (EMNLP- 2009), 2009. 37. Pham, T., H. Ng, and W. Lee. Word sense disambiguation with semi- supervised learning. In Proceedings of National Conference on Artificial Intelligence (AAAI-2005), 2005. 38. Platt, J.C. Probabilities for SV machines. In Advances in Large Margin Classifiers, A. J. Smola, P. Bartlett, B. Schölkopf, and D. Schuurmans, Editors. 1999, MIT Press. p. 61–73. 39. Schölkopf, B., J. Platt, J. Shawe-Taylor, A. Smola, and R. Williamson. Estimating the support of a high-dimensional distribution. Neural computation, 2001, 13(7): p. 1443-1471. 40. Shimodaira, H. Improving predictive inference under covariate shift by weighting the log-likelihood function. Journal of Statistical Planning and Inference, 2000, 90(2): p. 227-244. 41. Vapnik, V. and V. Vapnik. Statistical learning theory. Vol. 2. 1998: Wiley New York. 42. Yu, H. General MC: Estimating boundary of positive class from small positive data. In Proceedings of IEEE International Conference on Data Mining (ICDM-2003), 2003: IEEE. 43. Yu, H., J. Han, and K. Chang. PEBL: positive example based learning for Web page classification using SVM. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD- 2002), 2002. 44. Zadrozny, B. Learning and evaluating classifiers under sample selection bias. In Proceedings of International Conference on Machine Learning (ICML- 2004), 2004. 45. Zhang, D. and W. Lee. A simple probabilistic approach to learning from positive and unlabeled examples. In Proceedings of 5th Annual UK Workshop on Computational Intelligence, 2005. 46. Zhu, X., Z. Ghahramani, and J. Lafferty. Semi-supervised learning using gaussian fields and harmonic functions. In Proceedings of International Conference on Machine Learning (ICML-2003), 2003. Part II Web Mining 6 Information Retrieval and Web Search Web search needs no introduction. Due to its convenience and the richness of information on the Web, searching the Web is increasingly becoming the dominant information seeking method. People make fewer and fewer trips to libraries, but more and more searches on the Web. In fact, without effective search engines and rich Web contents, writing this book would have been much harder. Web search has its root in information retrieval (or IR for short), a field of study that helps the user find needed information from a large collection of text documents. Traditional IR assumes that the basic information unit is a document, and a large collection of documents is available to form the text database. On the Web, the documents are Web pages. Retrieving information simply means finding a set of documents that is relevant to the user query. A ranking of the set of documents is usually also performed according to their relevance scores to the query. The most commonly used query format is a list of keywords, which are also called terms. IR is different from data retrieval in databases using SQL queries because the data in databases are highly structured and stored in relational tables, while information in text is unstructured. There is no structured query language like SQL for text retrieval. It is safe to say that Web search is the single most important application of IR. To a great extent, Web search also helped IR. Indeed, the tremendous success of search engines has pushed IR to the center stage. Search is, however, not simply a straightforward application of traditional IR models. It uses some IR results, but it also has its unique techniques and presents many new problems for IR research. First of all, efficiency is a paramount issue for Web search, but is only secondary in traditional IR systems mainly due to the fact that document collections in most IR systems are not very large. However, the number of pages on the Web is huge. For example, Google indexed more than 8 billion pages when this book was written. Web users also demand very fast responses. No matter how effective an algorithm is, if the retrieval cannot be done efficiently, few people will use it. B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_6, © Springer-Verlag Berlin Heidelberg 2011 211 212 6 Information Retrieval and Web Search Web pages are also quite different from conventional text documents used in traditional IR systems. First, Web pages have hyperlinks and anchor texts, which do not exist in traditional documents (except citations in research publications). Hyperlinks are extremely important for search and play a central role in search ranking algorithms as we will see in the next chapter. Anchor texts associated with hyperlinks too are crucial because a piece of anchor text is often a more accurate description of the page that its hyperlink points to. Second, Web pages are semi-structured. A Web page is not simply a few paragraphs of text like in a traditional document. A Web page has different fields, e.g., title, metadata, body, etc. The information contained in certain fields (e.g., the title field) is more important than in others. Furthermore, the content in a page is typically organized and presented in several structured blocks (of rectangular shapes). Some blocks are important and some are not (e.g., advertisements, privacy policy, copyright notices, etc). Effectively detecting the main content block(s) of a Web page is useful to Web search because terms appearing in such blocks are more important. Finally, spamming is a major issue on the Web, but not a concern for traditional IR. This is so because the rank position of a page returned by a search engine is extremely important. If a page is relevant to a query but is ranked very low (e.g., below top 30), then the user is unlikely to look at the page. If the page sells a product, then this is bad for the business. In order to improve the ranking of some target pages, “illegitimate” means, called spamming, are often used to boost their rank positions. Detecting and fighting Web spam is a critical issue as it can push low quality (even irrelevant) pages to the top of the search rank, which harms the quality of the search results and the user’s search experience. In this chapter, we first study some information retrieval models and methods that are closely related to Web search. We then dive into some Web search specific issues. 6.1 Basic Concepts of Information Retrieval Information retrieval (IR) is the study of helping users to find information that matches their information needs. Technically, IR studies the acquisition, organization, storage, retrieval, and distribution of information. Historically, IR is about document retrieval, emphasizing document as the basic unit. Fig. 6.1 gives a general architecture of an IR system. In Fig. 6.1, the user with information need issues a query (user query) to the retrieval system through the query operations module. The retrieval module uses the document index to retrieve those documents that 6.1 Basic Concepts of Information Retrieval 213 contain some query terms (such documents are likely to be relevant to the query), compute relevance scores for them, and then rank the retrieved documents according to the scores. The ranked documents are then presented to the user. The document collection is also called the text database, which is indexed by the indexer for efficient retrieval. Fig. 6.1. A general IR system architecture A user query represents the user’s information needs, which is in one of the following forms: 1. Keyword queries: The user expresses his/her information needs with a list of (at least one) keywords (or terms) aiming to find documents that contain some (at least one) or all the query terms. The terms in the list are assumed to be connected with a “soft” version of the logical AND. For example, if one is interested in finding information about Web mining, one may issue the query ‘Web mining’ to an IR or search engine system. ‘Web mining’ is retreated as ‘Web AND mining’. The retrieval system then finds those likely relevant documents and ranks them suitably to present to the user. Note that a retrieved document does not have to contain all the terms in the query. In some IR systems, the ordering of the words is also significant and will affect the retrieval results. 2. Boolean queries: The user can use Boolean operators, AND, OR, and NOT to construct complex queries. Thus, such queries consist of terms and Boolean operators. For example, ‘data OR Web’ is a Boolean query, which requests documents that contain the word ‘data’ or ‘Web. A page is returned for a Boolean query if the query is logically true in the page (i.e., exact match). Although one can write complex Boolean queries using the three operators, users seldom write such queries. User query Document collection The user Query operations Retrieval system Ranked documents Executable query user feedback indexer Document index 214 6 Information Retrieval and Web Search Search engines usually support a restricted version of Boolean queries. 3. Phrase queries: Such a query consists of a sequence of words that makes up a phrase. Each returned document must contain at least one instance of the phrase. In a search engine, a phrase query is normally enclosed with double quotes. For example, one can issue the following phrase query (including the double quotes), “Web mining techniques and applications” to find documents that contain the exact phrase. 4. Proximity queries: The proximity query is a relaxed version of the phrase query and can be a combination of terms and phrases. Proximity queries seek the query terms within close proximity to each other. The closeness is used as a factor in ranking the returned documents or pages. For example, a document that contains all query terms close together is considered more relevant than a page in which the query terms are far apart. Some systems allow the user to specify the maximum allowed distance between the query terms. Most search engines consider both term proximity and term ordering in retrieval. 5. Full document queries: When the query is a full document, the user wants to find other documents that are similar to the query document. Some search engines (e.g., Google) allow the user to issue such a query by providing the URL of a query page. Additionally, in the returned results of a search engine, each snippet may have a link called “more like this” or “similar pages.” When the user clicks on the link, a set of pages similar to the page in the snippet is returned. 6. Natural language questions: This is the most complex case, and also the ideal case. The user expresses his/her information need as a natural language question. The system then finds the answer. However, such queries are still hard to handle due to the difficulty of natural language understanding. Nevertheless, this is an active research area, called question answering. Some search systems are starting to provide question answering services on some specific types of questions, e.g., definition questions, which ask for definitions of technical terms. Definition questions are usually easier to answer because there are strong linguistic patterns indicating definition sentences, e.g., “defined as”, “refers to”, etc. Definitions can usually be extracted offline [34, 39]. The query operations module can range from very simple to very complex. In the simplest case, it does nothing but just pass the query to the retrieval engine after some simple pre-processing, e.g., removal of stopwords (words that occur very frequently in text but have little meaning, e.g., “the”, “a”, “in”, etc). We will discuss text pre-processing in Sect. 6.5. In more complex cases, it needs to transform natural language queries into executable queries. It may also accept user feedback and use it 6.2 Information Retrieval Models 215 to expand and refine the original queries. This is usually called relevance feedback, which will be discussed in Sect. 6.3. The indexer is the module that indexes the original raw documents in some data structures to enable efficient retrieval. The result is the document index. In Sect. 6.6, we study a particular type of indexing scheme, called the inverted index, which is used in search engines and most IR systems. An inverted index is easy to build and very efficient to search. The retrieval system computes a relevance score for each indexed document to the query. According to their relevance scores, the documents are ranked and presented to the user. Note that it usually does not compare the user query with every document in the collection, which is too inefficient. Instead, only a small subset of the documents that contains at least one query term is first found from the index and relevance scores with the user query are then computed only for this subset of documents. 6.2 Information Retrieval Models An IR model governs how a document and a query are represented and how the relevance of a document to a user query is defined. There are four main IR models: Boolean model, vector space model, language model and probabilistic model. The most commonly used models in IR systems and on the Web are the first three models, which we study in this section. Although these three models represent documents and queries differently, they use the same framework. They all treat each document or query as a “bag” of words or terms. Term sequence and position in a sentence or a document are ignored. That is, a document is described by a set of distinctive terms. A term is simply a word whose semantics helps remember the document’s main themes. We should note that the term here may not be a natural language word in a dictionary. Each term is associated with a weight. Given a collection of documents D, let V = {t1, t2, ..., t|V|} be the set of distinctive terms in the collection, where ti is a term. The set V is usually called the vocabulary of the collection, and |V| is its size, i.e., the number of terms in V. A weight wij > 0 is associated with each term ti of a document dj  D. For a term that does not appear in document dj, wij = 0. Each document dj is thus represented with a term vector, dj = (w1j, w2j, ..., w|V|j), where each weight wij corresponds to the term ti  V, and quantifies the level of importance of ti in document dj. The sequence of the components (or terms) in the vector is not significant. Note that following the convention of this book, a bold lower case letter is used to represent a vector. 216 6 Information Retrieval and Web Search With this vector representation, a collection of documents is simply represented as a relational table (or a matrix). Each term is an attribute, and each weight is an attribute value. In different retrieval models, wij is computed differently. 6.2.1 Boolean Model The Boolean model is one of the earliest and simplest information retrieval models. It uses the notion of exact matching to match documents to the user query. Both the query and the retrieval are based on Boolean algebra. Document Representation: In the Boolean model, documents and queries are represented as sets of terms. That is, each term is only considered present or absent in a document. Using the vector representation of the document above, the weight wij ( {0, 1}) of term ti in document dj is 1 if ti appears in document dj, and 0 otherwise, i.e.,    .otherwise0 in appears if1 ji ij tw d (1) Boolean Queries: As we mentioned in Sect. 6.1, query terms are combined logically using the Boolean operators AND, OR, and NOT, which have their usual semantics in logic. Thus, a Boolean query has a precise semantics. For instance, the query, ((x AND y) AND (NOT z)) says that a retrieved document must contain both the terms x and y but not z. As another example, the query expression (x OR y) means that at least one of these terms must be in each retrieved document. Here, we assume that x, y and z are terms. In general, they can be Boolean expressions themselves. Document Retrieval: Given a Boolean query, the system retrieves every document that makes the query logically true. Thus, the retrieval is based on the binary decision criterion, i.e., a document is either relevant or irrelevant. Intuitively, this is called exact match. There is no notion of partial match or ranking of the retrieved documents. This is one of the major disadvantages of the Boolean model, which often leads to poor retrieval results. It is quite clear that the frequency of terms and their proximity contribute significantly to the relevance of a document. Due to this problem, the Boolean model is seldom used alone in practice. Most search engines support some limited forms of Boolean retrieval using explicit inclusion and exclusion operators. For example, the following query can be issued to Google, ‘mining –data +“equipment price”’, where + (inclusion) and – (exclusion) are similar to Boolean operators AND and NOT respectively. The operator OR may be supported as well. 6.2 Information Retrieval Models 217 6.2.2 Vector Space Model This model is perhaps the best known and most widely used IR model. Document Representation A document in the vector space model is represented as a weight vector, in which each component weight is computed based on some variation of TF or TF-IDF scheme. The weight wij of term ti in document dj is no longer in {0, 1} as in the Boolean model, but can be any number. Term Frequency (TF) Scheme: In this method, the weight of a term ti in document dj is the number of times that ti appears in document dj, denoted by fij. Normalization may also be applied (see Equation (2)). The shortcoming of the TF scheme is that it does not consider the situation where a term appears in many documents of the collection. Such a term may not be discriminative. TF-IDF Scheme: This is the most well known weighting scheme, where TF still stands for the term frequency and IDF the inverse document frequency. There are several variations of this scheme. Here we only give the most basic one. Let N be the total number of documents in the system or the collection and dfi be the number of documents in which term ti appears at least once. Let fij be the raw frequency count of term ti in document dj. Then, the normalized term frequency (denoted by tfij) of ti in dj is given by ,},...,,max{ ||21 jVjj ij ij fff ftf  (2) where the maximum is computed over all terms that appear in document dj. If term ti does not appear in dj then tfij = 0. Recall that |V| is the vocabulary size of the collection. The inverse document frequency (denoted by idfi) of term ti is given by: .log i i df Nidf  (3) The intuition here is that if a term appears in a large number of documents in the collection, it is probably not important or not discriminative. The final TF-IDF term weight is given by: .iijij idftfw  (4) 218 6 Information Retrieval and Web Search Queries A query q is represented in exactly the same way as a document in the document collection. The term weight wiq of each term ti in q can also be computed in the same way as in a normal document, or slightly differently. For example, Salton and Buckley [52] suggested the following: .log},...,,max{ 5.05.0 ||21 iqVqq iq qi df N fff fw        (5) Document Retrieval and Relevance Ranking It is often difficult to make a binary decision on whether a document is relevant to a given query. Unlike the Boolean model, the vector space model does not make such a decision. Instead, the documents are ranked according to their degrees of relevance to the query. One way to compute the degree of relevance is to calculate the similarity of the query q to each document dj in the document collection D. There are many similarity measures. The most well known one is the cosine similarity, which is the cosine of the angle between the query vector q and the document vector dj, .||||||||),( || 1 2|| 1 2 || 1        V i iq V i ij V i iqij j j j ww wwcosine qd qdqd (6) Cosine similarity is also widely used in text/document clustering. The dot product of the two vectors is another similarity measure, .),(  qdqd jjsim (7) Ranking of the documents is done using their similarity values. The top ranked documents are regarded as more relevant to the query. Another way to assess the degree of relevance is to directly compute a relevance score for each document to the query. The Okapi method and its variations are popular techniques in this setting. The Okapi retrieval formula given here is based on that in [51, 55]. It has been shown that Okapi variations are more effective than cosine for short query retrieval. Since it is easier to present the formula directly using the “bag” of words notation of documents than vectors, document dj will be denoted by dj and query q will be denoted by q. Additional notations are as follows: ti is a term fij is the raw frequency count of term ti in document dj fiq is the raw frequency count of term ti in query q N is the total number of documents in the collection 6.2 Information Retrieval Models 219 dfi is the number of documents that contain the term ti dlj is the document length (in bytes) of dj avdl is the average document length of the collection The Okapi relevance score of a document dj for a query q is: ,)1( )1( )1( 5.0 5.0ln),( 2 2 1 1 , iq iq ij j ij dqt i i j fk fk favdl dlbbk fk df dfNqdokapi ji        (8) where k1 (between 1.0-2.0), b (usually 0.75) and k2 (between 1-1000) are parameters. Yet another score function is the pivoted normalization weighting score function, denoted by pnw [55]: i iq dqt j ij j df Nf avdl dlss fqdpnw ji 1ln )1( ))ln(1ln(1),( ,      , (9) where s is a parameter (usually set to 0.2). Note that these are empirical functions based on intuitions and experimental evaluations. There are many variations of these functions used in practice. 6.2.3 Statistical Language Model Statistical language models (or simply language models) are based on probability and have foundations in statistical theory. The basic idea of this approach to retrieval is simple. It first estimates a language model for each document, and then ranks documents by the likelihood of the query given the language model. Similar ideas have previously been used in natural language processing and speech recognition. The formulation and discussion in this section is based on those in [68, 69]. Information retrieval using language models was first proposed by Ponte and Croft [46]. Let the query q be a sequence of terms, q = q1q2…qm and the document collection D be a set of documents, D = {d1, d2, …, dN}. In the language modeling approach, we consider the probability of a query q as being “generated” by a probabilistic model based on a document dj, i.e., Pr(q|dj). To rank documents in retrieval, we are interested in estimating the posterior probability Pr(dj|q). Using the Bayes rule, we have )Pr( )Pr()|Pr()|Pr( q ddqqd jj j  (10) For ranking, Pr(q) is not needed as it is the same for every document. Pr(dj) is usually considered uniform and thus will not affect ranking. We only need to compute Pr(q|dj). 220 6 Information Retrieval and Web Search The language model used in most existing work is based on unigram, i.e., only individual terms (words) are considered. That is, the model assumes that each term (word) is generated independently, which is essentially a multinomial distribution over words. The general case is the n-gram model, where the nth term is conditioned on the previous n-1 terms. Based on the multinomial distribution and the unigram model, we have    || 11 21 ,)|Pr()|Pr()|...Pr( V i f ji m i jijm iqdtdqdqqqq (11) where fiq is the number of times that term ti occurs in q, and   || 1 1)|Pr(V i ji dt . The retrieval problem is reduced to estimating Pr(ti|dj), which can be the relative frequency, .||)|Pr( j ij ji d fdt  (12) Recall that fij is the number of times that term ti occurs in document dj. |dj| denotes the total number of words in dj. However, one problem with this estimation is that a term that does not appear in dj has the probability of 0, which underestimates the probability of the unseen term in the document. This situation is similar to text classification using the naïve Bayesian model (see Sect. 3.7). A non-zero probability is typically assigned to each unseen term in the document, which is called smoothing. Smoothing adjusts the estimates of probabilities to produce more accurate probabilities. The name smoothing comes from the fact that these techniques tend to make distributions more uniform, by adjusting low probabilities such as zero probabilities upward, and high probabilities downward. Not only do smoothing methods aim to prevent zero probabilities, but they also attempt to improve the accuracy of the model as a whole. Traditional additive smoothing is .||||)|(Pr j ij jiadd dV fdt     (13) When  = 1, it is the Laplace smoothing and when 0 <  < 1, it is the Lidstone smoothing. Many other more sophisticated smoothing methods can be found in [16, 69]. 6.3 Relevance Feedback To improve the retrieval effectiveness, researchers have proposed many techniques. Relevance feedback is one of the effective ones. It is a process 6.3 Relevance Feedback 221 where the user identifies some relevant and irrelevant documents in the initial list of retrieved documents, and the system then creates an expanded query by extracting some additional terms from the sample relevant and irrelevant documents for a second round of retrieval. The system may also produce a classification model using the user-identified relevant and irrelevant documents to classify the documents in the document collection into relevant and irrelevant documents. The relevance feedback process may be repeated until the user is satisfied with the retrieved result. The Rocchio Method This is one of the early and effective relevance feedback algorithms. It is based on the first approach above. That is, it uses the user-identified relevant and irrelevant documents to expand the original query. The new (or expanded) query is then used to perform retrieval again. Let the original query vector be q, the set of relevant documents selected by the user be Dr, and the set of irrelevant documents be Dir. The expanded query qe is computed as follows,    irirrr D ir irD r r e DD dd d||d||qq , (14) where ,  and  are parameters. Equation (14) simply augments the original query vector q with additional terms from relevant documents. The original query q is still needed because it directly reflects the user’s information need. Relevant documents are considered more important than irrelevant documents. The subtraction is used to reduce the influence of those terms that are not discriminative (i.e., they appear in both relevant and irrelevant documents), and those terms that appear in irrelevant documents only. The three parameters are set empirically. Note that a slight variation of the algorithm is one without the normalization of |Dr| and |Dir|. Both these methods are simple and efficient to compute, and usually produce good results. Machine Learning Methods Since we have a set of relevant and irrelevant documents, we can construct a classification model from them. Then the relevance feedback problem becomes a learning problem. Any supervised learning method may be used, e.g., naïve Bayesian classification and SVM. Similarity comparison with the original query is no longer needed. In fact, a variation of the Rocchio method above, called the Rocchio classification method, can be used for this purpose too. Building a 222 6 Information Retrieval and Web Search Rocchio classifier is done by constructing a prototype vector ci for each class i, which is either relevant or irrelevant in this case (negative elements or components of the vector ci are usually set to 0):    ii DDiDi i DDD dd d d ||d d ||c ||||||||  , (15) where Di is the set of documents of class i, and  and  are parameters. Using the TF-IDF term weighting scheme,  = 16 and  = 4 usually work quite well. In classification, cosine similarity is applied. That is, each test document dt is compared with every prototype ci based on cosine similarity. dt is assigned to the class with the highest similarity value (Fig. 6.2). Algorithm 1 for each class i do 2 construct its prototype vector ci using Equation (15) 3 endfor 4 for each test document dt do 5 the class of dt is ),(maxarg iti cosine cd 6 endfor Fig. 6.2. Training and testing of a Rocchio classifier Apart from the above classic methods, the following learning techniques are also applicable: Learning from Labeled and Unlabeled Examples (LU Learning): Since the number of user-selected relevant and irrelevant documents may be small, it can be difficult to build an accurate classifier. However, unlabeled examples, i.e., those documents that are not selected by the user, can be utilized to improve learning to produce a more accurate classifier. This fits the LU learning model exactly (see Sect. 5.1). The user-selected relevant and irrelevant documents form the small labeled training set. Learning from Positive and Unlabeled Examples (PU Learning): The two learning models mentioned above assume that the user can confidently identify both relevant and irrelevant documents. However, in some cases, the user only selects (or clicks) documents that he/she feels relevant based on the title or summary information (e.g., snippets in Web search), which are most likely to be true relevant documents, but does not indicate irrelevant documents. Those documents that are not selected by the user may not be treated as irrelevant because he/she has not seen them. Thus, they can only be regarded as unlabeled documents. This is called implicit feedback. In order to learn in this case, we can use PU learning, i.e., learning from positive and unlabeled examples (see Sect. 5.2). We regard the user-selected documents as positive examples, and unselected documents 6.4 Evaluation Measures 223 as unlabeled examples. Researchers have experimented with this approach in the Web search context and obtained good results [19]. Using Ranking SVM and Language Models: In the implicit feedback setting, a technique called ranking SVM is proposed in [31] to rank the unselected documents based on the selected documents. A language model based approach is also proposed in [54]. Pseudo-Relevance Feedback Pseudo-relevance feedback is another technique used to improve retrieval effectiveness. Its basic idea is to extract some terms (usually frequent terms) from the top-ranked documents and add them to the original query to form a new query for a second round of retrieval. Again, the process can be repeated until the user is satisfied with the final results. The main difference between this method and the relevance feedback method is that in this method, the user is not involved in the process. The approach simply assumes that the top-ranked documents are likely to be relevant. Through query expansion, some relevant documents missed in the initial round can be retrieved to improve the overall performance. Clearly, the effectiveness of this method relies on the quality of the selected expansion terms. 6.4 Evaluation Measures Precision and recall measures have been described in Chap. 3 on supervised learning, where each document is classified to a specific class. In IR and Web search, usually no decision is made on whether a document is relevant or irrelevant to a query. Instead, a ranking of the documents is produced for the user. This section studies how to evaluate such rankings. Again, let the collection of documents in the database be D, and the total number of documents in D be N. Given a user query q, the retrieval algorithm first computes relevance scores for all documents in D and then produce a ranking Rq of the documents based on the relevance scores, i.e., ,,...,, : 21  q N qq qR ddd (16) where d1 q  D is the most relevant document to query q and dq N  D is the most irrelevant document to query q. Let Dq ( D) be the set of actual relevant documents of query q in D. We can compute the precision and recall values at each di q in the ranking. Recall at rank position i or document di q (denoted by r(i)) is the fraction of relevant documents from d1 q to di q in Rq. Let the number of relevant documents from d1 q to di q in Rq be si (≤ |Dq|) (|Dq| is the size of Dq). Then, 224 6 Information Retrieval and Web Search .||)( q i D sir  (17) Precision at rank position i or document di q (denoted by p(i)) is the fraction of documents from d1 q to di q in Rq that are relevant: i sip i)( (18) Example 1: We have a document collection D with 20 documents. Given a query q, we know that eight documents are relevant to q. A retrieval algorithm produces the ranking (of all documents in D) shown in Fig. 6.3. Rank i +/ p(i) r(i) 1 + 1/1 = 100% 1/8 = 13% 2 + 2/2 = 100% 2/8 = 25% 3 + 3/3 = 100% 3/8 = 38% 4  3/4 = 75% 3/8 = 38% 5 + 4/5 = 80% 4/8 = 50% 6  4/6 = 67% 4/8 = 50% 7 + 5/7 = 71% 5/8 = 63% 8  5/8 = 63% 5/8 = 63% 9 + 6/9 = 67% 6/8 = 75% 10 + 7/10 = 70% 7/8 = 88% 11  7/11 = 63% 7/8 = 88% 12  7/12 = 58% 7/8 = 88% 13 + 8/13 = 62% 8/8 = 100% 14  8/14 = 57% 8/8 = 100% 15  8/15 = 53% 8/8 = 100% 16  8/16 = 50% 8/8 = 100% 17  8/17 = 53% 8/8 = 100% 18  8/18 = 44% 8/8 = 100% 19  8/19 = 42% 8/8 = 100% 20  8/20 = 40% 8/8 = 100% Fig. 6.3. Precision and recall values at each rank position In column 1 of Fig. 6.3, 1 represents the highest rank and 20 represents the lowest rank. “+” and “” in column 2 indicate a relevant document and an irrelevant document respectively. The precision (p(i)) and recall (r(i)) values at each position i are given in columns 3 and 4. ▀ Average Precision: Sometimes we want a single precision to compare different retrieval algorithms on a query q. An average precision (pavg) can be computed based on the precision at each relevant document in the ranking, 6.4 Evaluation Measures 225 || )( q Dd avg D ip p q q i  . (19) For the ranking in Fig. 6.3 of Example 1, the average precision is 81%: %.818 %62%70%67%71%80%100%100%100 avgp (20) Precision–Recall Curve: Based on the precision and recall values at each rank position, we can draw a precision–recall curve where the x-axis is the recall and the y-axis is the precision. Instead of using the precision and recall at each rank position, the curve is commonly plotted using 11 standard recall levels, 0%, 10%, 20%, …, 100%. Since we may not obtain exactly these recall levels in the ranking, interpolation is needed to obtain the precisions at these recall levels, which is done as follows: Let ri be a recall level, i  {0, 1, 2, …, 10}, and p(ri) be the precision at the recall level ri. p(ri) is computed with )(max)( 10 rprp rrri i  . (21) That is, to interpolate precision at a particular recall level ri, we take the maximum precision of all recalls between level ri and level r10. Example 2: Following Example 1, we obtain the interpolated precisions at all 11 recall levels in the table of Fig. 6.4. The precision-recall curve is shown on the right. i p(ri) ri 0 100% 0% 1 100% 10% 2 100% 20% 3 100% 30% 4 80% 40% 5 80% 50% 6 71% 60% 7 70% 70% 8 70% 80% 9 62% 90% 10 62% 100% Fig. 6.4. The precision-recall curve ▀ Comparing Different Algorithms: Frequently, we need to compare the retrieval results of different algorithms. We can draw their precision-recall curves together in the same figure for comparison. Fig. 6.5 shows the 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% Recall Precision 226 6 Information Retrieval and Web Search curves of two algorithms on the same query and the same document collection. We observe that the precisions of one algorithm are better than those of the other at low recall levels, but are worse at high recall levels. 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% Recall Precision Fig. 6.5. Comparison of two retrieval algorithms based on their precision-recall curves Evaluation Using Multiple Queries: In most retrieval evaluations, we are interested in the performance of an algorithm on a large number of queries. The overall precision (denoted by )( irp ) at each recall level ri is computed as the average of individual precisions at that recall level, i.e., ),(|| 1)( || 1 i Q j ji rpQrp    (22) where Q is the set of all queries and pj(ri) is the precision of query j at the recall level ri. Using the average precision at each recall level, we can also draw a precision-recall curve. Although in theory precision and recall do not depend on each other, in practice a high recall is almost always achieved at the expense of precision, and a high precision is achieved at the expense of recall. Thus, precision and recall has a trade-off. Depending on the application, one may want a high precision or a high recall. One problem with precision and recall measures is that, in many applications, it can be very hard to determine the set of relevant documents Dq for each query q. For example, on the Web, Dq is almost impossible to determine because there are simply too many pages to manually inspect. Without Dq, the recall value cannot be computed. In fact, recall does not make much sense for Web search because the user seldom looks at pages ranked below 30. However, precision is critical, and it can be estimated for top ranked documents. Manual inspection of only the top 30 pages is reasonable. The following precision computation is commonly used. 6.5 Text and Web Page Pre-Processing 227 Rank Precision: We compute the precision values at some selected rank positions. For a Web search engine, we usually compute precisions for the top 5, 10, 15, 20, 25 and 30 returned pages (as the user seldom looks at more than 30 pages). We assume that the number of relevant pages is more than 30. Following Example 1, we have p(5) = 80%, p(10) = 70%, p(15) = 53%, and p(20) = 40%. We should note that precision is not the only measure for evaluating search ranking. Reputation or quality of important as we will see later in this chapter and also in Chap. 7. F-score: Another often used evaluation measure is the F-score, which we have used in Chap. 3. Here we can compute the F-score at each rank position i. Recall that F-score is the harmonic mean of precision and recall: .)()( )()(2 )( 1 )( 1 2)( irip irip ipir iF    (23) Finally, the precision and recall breakeven point is also a commonly used measure, which we have discussed in Sect. 3.3.2 in Chap. 3. 6.5 Text and Web Page Pre-Processing Before the documents in a collection are used for retrieval, some pre- processing tasks are usually performed. For traditional text documents (no HTML tags), the tasks are stopword removal, stemming, and handling of digits, hyphens, punctuations, and cases of letters. For Web pages, additional tasks such as HTML tag removal and identification of main content blocks also require careful considerations. We discuss them in this section. 6.5.1 Stopword Removal Stopwords are frequently occurring and insignificant words in a language that help construct sentences but do not represent any content of the documents. Articles, prepositions and conjunctions and some pronouns are natural candidates. Common stopwords in English include: a, about, an, are, as, at, be, by, for, from, how, in, is, of, on, or, that, the, these, this, to, was, what, when, where, who, will, with Such words should be removed before documents are indexed and stored. Stopwords in the query are also removed before retrieval is performed. the top ranked pages is also very 228 6 Information Retrieval and Web Search 6.5.2 Stemming In many languages, a word has various syntactical forms depending on the contexts that it is used. For example, in English, nouns have plural forms, verbs have gerund forms (by adding “ing”), and verbs used in the past tense are different from the present tense. These are considered as syntactic variations of the same root form. Such variations cause low recall for a retrieval system because a relevant document may contain a variation of a query word but not the exact word itself. This problem can be partially dealt with by stemming. Stemming refers to the process of reducing words to their stems or roots. A stem is the portion of a word that is left after removing its prefixes and suffixes. In English, most variants of a word are generated by the introduction of suffixes (rather than prefixes). Thus, stemming in English usually means suffix removal, or stripping. For example, “computer”, “computing”, and “compute” are reduced to “comput”. “walks”, “walking” and “walker” are reduced to “walk”. Stemming enables different variations of the word to be considered in retrieval, which improves the recall. There are several stemming algorithms, also known as stemmers. In English, the most popular stemmer is perhaps the Martin Porter's stemming algorithm [47], which uses a set of rules for stemming. Over the years, many researchers evaluated the advantages and disadvantages of using stemming. Clearly, stemming increases the recall and reduces the size of the indexing structure. However, it can hurt precision because many irrelevant documents may be considered relevant. For example, both “cop” and “cope” are reduced to the stem “cop”. However, if one is looking for documents about police, a document that contains only “cope” is unlikely to be relevant. Although many experiments have been conducted by researchers, there is still no conclusive evidence one way or the other. In practice, one should experiment with the document collection at hand to see whether stemming helps. 6.5.3 Other Pre-Processing Tasks for Text Digits: Numbers and terms that contain digits are removed in traditional IR systems except some specific types, e.g., dates, times, and other pre- specified types expressed with regular expressions. However, in search engines, they are usually indexed. Hyphens: Breaking hyphens are usually applied to deal with inconsistency of usage. For example, some people use “state-of-the-art”, but others use “state of the art”. If the hyphens in the first case are removed, we eliminate the 6.5 Text and Web Page Pre-Processing 229 inconsistency problem. However, some words may have a hyphen as an integral part of the word, e.g., “Y-21”. Thus, in general, the system can follow a general rule (e.g., removing all hyphens) and also have some exceptions. Note that there are two types of removal, i.e., (1) each hyphen is replaced with a space and (2) each hyphen is simply removed without leaving a space so that “state-of-the-art” may be replaced with “state of the art” or “stateoftheart”. In some systems both forms are indexed as it is hard to determine which is correct, e.g., if “pre-processing” is converted to “pre processing”, then some relevant pages will not be found if the query term is “preprocessing”. Punctuation Marks: Punctuation can be dealt with similarly as hyphens. Case of Letters: All the letters are usually converted to either the upper or lower case. 6.5.4 Web Page Pre-Processing We have indicated at the beginning of the section that Web pages are different from traditional text documents. Thus, additional pre-processing is needed. We describe some important ones below. 1. Identifying different text fields: In HTML, there are different text fields, e.g., title, metadata, and body. Identifying them allows the retrieval system to treat terms in different fields differently. For example, in search engines terms that appear in the title field of a page are regarded as more important than terms that appear in other fields and are assigned higher weights because the title is usually a concise description of the page. In the body text, those emphasized terms (e.g., under header tags

,

, …, bold tag , etc.) are also given higher weights. 2. Identifying anchor text: Anchor text associated with a hyperlink is treated specially in search engines because the anchor text often represents a more accurate description of the information contained in the page pointed to by its link. In the case that the hyperlink points to an external page (not in the same site), it is especially valuable because it is a summary description of the page given by other people rather than the author/owner of the page, and is thus more trustworthy. 3. Removing HTML tags: The removal of HTML tags can be dealt with similarly to punctuation. One issue needs careful consideration, which affects proximity queries and phrase queries. HTML is inherently a visual presentation language. In a typical commercial page, information is presented in many rectangular blocks (see Fig. 6.6). Simply removing HTML tags may cause problems by joining text that should not be joined. For example, in Fig. 6.6, “cite this article” at the bottom of the 230 6 Information Retrieval and Web Search left column will join “Main Page” on the right, but they should not be joined. They will cause problems for phrase queries and proximity queries. This problem had not been dealt with satisfactorily by search engines at the time when this book was written. 4. Identifying main content blocks: A typical Web page, especially a commercial page, contains a large amount of information that is not part of the main content of the page. For example, it may contain banner ads, navigation bars, copyright notices, etc., which can lead to poor results for search and mining. In Fig. 6.6, the main content block of the page is the block containing “Today’s featured article.” It is not desirable to index anchor texts of the navigation links as a part of the content of this page. Several researchers have studied the problem of identifying main content blocks. They showed that search and data mining results can be improved significantly if only the main content blocks are used. We briefly discuss two techniques for finding such blocks in Web pages. Partitioning based on visual cues: This method uses visual information to help find main content blocks in a page. Visual or rendering information of each HTML element in a page can be obtained from the Web Fig. 6.6. An example of a Web page from Wikipedia 6.5 Text and Web Page Pre-Processing 231 browser. For example, Internet Explorer provides an API that can output the X and Y coordinates of each element. A machine learning model can then be built based on the location and appearance features for identifying main content blocks of pages. Of course, a large number of training examples need to be manually labeled (see [12, 56] for details). Tree matching: This method is based on the observation that in most commercial Web sites pages are generated by using some fixed templates. The method thus aims to find such hidden templates. Since HTML has a nested structure, it is thus easy to build a tag tree for each page. Tree matching of multiple pages from the same site can be performed to find such templates. In Chap. 9, we will describe a tree matching algorithm for this purpose. Once a template is found, we can identify which blocks are likely to be the main content blocks based on the following observation: the text in main content blocks are usually quite different across different pages of the same template, but the non- main content blocks are often quite similar in different pages. To determine the text similarity of corresponding blocks (which are sub- trees), the shingle method described in the next section can be used. 6.5.5 Duplicate Detection Duplicate documents or pages are not a problem in traditional IR. However, in the context of the Web, it is a significant issue. There are different types of duplication of pages and contents on the Web. Copying a page is usually called duplication or replication, and copying an entire site is called mirroring. Duplicate pages and mirror sites are often used to improve efficiency of browsing and file downloading worldwide due to limited bandwidth across different geographic regions and poor or unpredictable network performances. Of course, some duplicate pages are the results of plagiarism. Detecting such pages and sites can reduce the index size and improve search results. Several methods can be used to find duplicate information. The simplest method is to hash the whole document, e.g., using the MD5 algorithm, or computing an aggregated number (e.g., checksum). However, these methods are only useful for detecting exact duplicates. On the Web, one seldom finds exact duplicates. For example, even different mirror sites may have different URLs, different Web masters, different contact information, different advertisements to suit local needs, etc. One efficient duplicate detection technique is based on n-grams (also called shingles). An n-gram is simply a consecutive sequence of words of a fixed window size n. For example, the sentence, “John went to school 232 6 Information Retrieval and Web Search with his brother,” can be represented with five 3-gram phrases “John went to”, “went to school”, “to school with”, “school with his”, and “with his brother”. Note that 1-gram is simply the individual words. Let Sn(d) be the set of distinctive n-grams (or shingles) contained in document d. Each n-gram may be coded with a number or a MD5 hash (which is usually a 32-digit hexadecimal number). Given the n-gram representations of the two documents d1 and d2, Sn(d1) and Sn(d2), the Jaccard coefficient can be used to compute the similarity of the two documents, |)()(| |)()(|),( 21 21 21 dSdS dSdSddsim nn nn   . (24) A threshold is used to determine whether d1 and d2 are likely to be duplicates of each other. For a particular application, the window size n and the similarity threshold are chosen through experiments. 6.6 Inverted Index and Its Compression The basic method of Web search and traditional IR is to find documents that contain the terms in the user query. Given a user query, one option is to scan the document database sequentially to find the documents that contain the query terms. However, this method is obviously impractical for a large collection, such as the Web. Another option is to build some data structures (called indices) from the document collection to speed up retrieval or search. There are many index schemes for text [5]. The inverted index, which has been shown superior to most other indexing schemes, is a popular one. It is perhaps the most important index method used in search engines. This indexing scheme not only allows efficient retrieval of documents that contain query terms, but also very fast to build. 6.6.1 Inverted Index In its simplest form, the inverted index of a document collection is basically a data structure that attaches each distinctive term with a list of all documents that contains the term. Thus, in retrieval, it takes constant time to find the documents that contains a query term. Finding documents containing multiple query terms is also easy as we will see later. Given a set of documents, D = {d1, d2, …, dN}, each document has a unique identifier (ID). An inverted index consists of two parts: a vocabulary V, containing all the distinct terms in the document set, and for each distinct term ti an inverted list of postings. Each posting stores the 6.6 Inverted Index and Its Compression 233 ID (denoted by idj) of the document dj that contains term ti and other pieces of information about term ti in document dj. Depending on the need of the retrieval or ranking algorithm, different pieces of information may be included. For example, to support phrase and proximity search, a posting for a term ti usually consists of the following, where idj is the ID of document dj that contains the term ti, fij is the frequency count of ti in dj, and ok are the offsets (or positions) of term ti in dj. Postings of a term are sorted in increasing order based on the idj’s and so are the offsets in each posting (see Example 3). This facilitates compression of the inverted index as we will see in Sect. 6.6.4. Example 3: We have three documents of id1, id2, and id3: id1: Web mining is useful. 1 2 3 4 id2: Usage mining applications. 1 2 3 id3: Web structure mining studies the Web hyperlink structure. 1 2 3 4 5 6 7 8 The numbers below each document are the offset position of each word. The vocabulary is the set: {Web, mining, useful, applications, usage, structure, studies, hyperlink} Stopwords “is” and “the” have been removed, but no stemming is applied. Fig. 6.7 shows two inverted indices. Applications: id2 Applications: Hyperlink: id3 Hyperlink: Mining: id1, id2, id3 Mining: , , Structure: id3 Structure: Studies: id3 Studies: Usage: id2 Usage: Useful: id1 Useful: Web: id1, id3 Web: , (A) (B) Fig. 6.7. Two inverted indices: a simple version and a more complex version Fig. 6.7(A) is a simple version, where each term is attached with only an inverted list of IDs of the documents that contain the term. Each inverted list in Fig. 6.7(B) is more complex as it contains additional information, i.e., the frequency count of the term and its positions in each document. Note that we use idi as the document IDs to distinguish them from offsets. 234 6 Information Retrieval and Web Search In an actual implementation, they may also be positive integers. Note also that a posting can contain other types of information depending on the need of the retrieval or search algorithm (see Sect. 6.8). ▀ 6.6.2 Search Using an Inverted Index Queries are evaluated by first fetching the inverted lists of the query terms, and then processing them to find the documents that contain all (or some) terms. Specifically, given the query terms, searching for relevant documents in the inverted index consists of three main steps: Step 1 (vocabulary search): This step finds each query term in the vocabulary, which gives the inverted list of each term. To speed up the search, the vocabulary usually resides in the main memory. Various indexing methods, e.g., hashing, tries or B-tree, can be used to speed up the search. Lexicographical ordering may also be employed due to its space efficiency. Then the binary search method can be applied. The complexity is O(log|V|), where |V| is the vocabulary size. If the query contains only a single term, this step gives all the relevant documents and the algorithm then goes to step 3. If the query contains multiple terms, the algorithm proceeds to step 2. Step 2 (results merging): After the inverted list of each term is found, merging of the lists is performed to find their intersection, i.e., the set of documents containing all query terms. Merging simply traverses all the lists in synchronization to check whether each document contains all query terms. One main heuristic is to use the shortest list as the base to merge with the other longer lists. For each posting in the shortest list, a binary search may be applied to find it in each longer list. Note that partial match (i.e., documents containing only some of the query terms) can be achieved as well in a similar way, which is more useful in practice. Usually, the whole inverted index cannot fit in memory, so part of it is cached in memory for efficiency. Determining which part to cache involves analysis of query logs to find frequent query terms. The inverted lists of these frequent query terms can be cached in memory. Step 3 (Rank score computation): This step computes a rank (or relevance) score for each document based on a relevance function (e.g., okapi or cosine), which may also consider the phrase and term proximity information. The score is then used in the final ranking. Example 4: Using the inverted index built in Fig. 6.7(B), we want to search for “web mining” (the query). In step 1, two inverted lists are found: Mining: , , Web: , 6.6 Inverted Index and Its Compression 235 In step 2, the algorithm traverses the two lists and finds documents containing both words (documents id1 and id3). The word positions are also retrieved. In step 3, we compute the rank scores. Considering the proximity and the sequence of words, we give id1 a higher rank (or relevance) score than id3 as “web” and “mining” are next to each other in id1 and in the same sequence as that in the query. Different search engines may use different algorithms to combine these factors. ▀ 6.6.3 Index Construction The construction of an inverted index is quite simple and can be done efficiently using a trie data structure among many others. The time complexity of the index construction is O(T), where T is the number of all terms (including duplicates) in the document collection (after pre-processing). For each document, the algorithm scans it sequentially and for each term, it finds the term in the trie. If it is found, the document ID and other information (e.g., the offset of the term) are added to the inverted list of the term. If the term is not found, a new leaf is created to represent the term. Example 5: Let us build an inverted index for the three documents in Example 3, which are reproduced below for easy reference. Fig. 6.8 shows the vocabulary trie and the inverted lists for all terms. id1: Web mining is useful. 1 2 3 4 id2: Usage mining applications. 1 2 3 id3: Web structure mining studies the Web hyperlink structure ▀ 1 2 3 4 5 6 7 8 To build the index efficiently, the trie is usually stored in memory. However, in the context of the Web, the whole index will not fit in the main memory. The following technique can be applied. We follow the above algorithm to build the index until the memory is full. The partial index I1 obtained so far is written on the disk. Then, we process the subsequent documents and build the partial index I2 in memory, and so on. After all documents have been processed, we have k partial indices, I1, I2, …, Ik, on disk. We then merge the partial indices in a hierarchical manner. That is, we first perform pair-wise merges of I1 and I2, I3 and I4, and so on. This gives us larger indices I1-2, I3-4 and so on. After the first level merging is complete, we proceed to the second level merging, i.e., we merge I1-2 and I3-4, I5-6 and I7-8 and so on. This process continues until all the partial indices are merged into a single index. Each merge is fairly straightforward because the vocabulary in each partial index is 236 6 Information Retrieval and Web Search sorted by the trie construction. The complexity of each merge is thus linear in the number of terms in both partial indices. Since each level needs a linear process of the whole index, the complete merging process takes O(klog k) time. To reduce the disk space requirement, whenever a new partial index is generated, we can merge it with a previously merged index. That is, when we have I1 and I2, we can merge them immediately to produce I1-2, and when I3 is produced, it is merged with I1-2 to produce I1-2-3 and so on. Fig. 6.8. The vocabulary trie and the inverted lists Instead of using a trie, an alternative method is to use an in-memory hash table (or other data structures) for terms. The algorithm is quite straightforward and will not be discussed further. On the Web, an important issue is that pages are constantly added, modified or deleted. It may be quite inefficient to modify the main index because a single page change can require updates to a large number of records of the index. One simple solution is to construct two additional indices, one for added pages and one for deleted pages. Modification can be regarded as a deletion and then an addition. Given a user query, it is searched in the main index and also in the two auxiliary indices. Let the pages returned from the search in the main index be D0, the pages returned from the search in the index of added pages be D+ and the pages returned from the search in the index of deleted pages be D–. Then, the final results returned to the user is (D0  D+) – D. When the two auxiliary indices become too large, they can be merged into the main index. 6.6.4 Index Compression An inverted index can be very large. In order to speed up the search, it should reside in memory as much as possible to avoid disk I/O. Because of this, reducing the index size becomes an important issue. A natural solution to this is index compression, which aims to represent the same structure: useful: hyperlink: mining: , , usage: web: , applications: studies: ‘a’ ‘s’ ‘m’ ‘h’ ‘u’ ‘u’ ‘r’ ‘t’ ‘s’ ‘e’ ‘w’ ‘a’ 6.6 Inverted Index and Its Compression 237 information with fewer bits or bytes. Using compression, the size of an inverted index can be reduced dramatically. In the lossless compression, the original index can also be reconstructed exactly using the compressed version. Lossless compression methods are the focus of this section. The inverted index is quite amiable to compression. Since the main space used by an inverted index is for the storage of document IDs and offsets of each term, we thus want to reduce this space requirement. Since all the information is represented with positive integers, we only discuss integer compression techniques in this section. Without compression, on most architectures an integer has a fixed-size representation of four bytes (32 bits). However, few integers need 4 bytes to represent, so a more compact representation (compression) is clearly possible. There are generally two classes of compression schemes for inverted lists: the variable-bit scheme and the variable-byte scheme. In the variable-bit (also called bitwise) scheme, an integer is represented with an integral number of bits. Well known bitwise methods include unary coding, Elias gamma coding and delta coding [20], and Golomb coding [24]. In the variable-byte scheme, an integer is stored in an integral number of bytes, where each byte has 8 bits. A simple bytewise scheme is the variable-byte coding [58]. These coding schemes basically map integers onto self-delimiting binary codewords (bits), i.e., the start bit and the end bit of each integer can be detected with no additional delimiters or markers. An interesting feature of the inverted index makes compression even more effective. Since document IDs in each inverted list are sorted in increasing order, we can store the difference between any two adjacent document IDs, idi and idi+1, where idi+1> idi, instead of the actual IDs. This difference is called the gap between idi and idi+1. The gap is a smaller number than idi+1 and thus requires fewer bits. In search, if the algorithm linearly traverses each inverted list, document IDs can be recovered easily. Since offsets in each posting are also sorted, they can be stored similarly. For example, the sorted document IDs are: 4, 10, 300, and 305. They can be represented with gaps, 4, 6, 290 and 5. Given the gap list 4, 6, 290 and 5, it is easy to recover the original document IDs, 4, 10, 300, and 305. We note that for frequent terms (which appear in a large number of documents) the gaps are small and can be encoded with short codes (fewer bits). For infrequent or rare terms, the gaps can be large, but they do not use up much space due to the fact that only a small number of documents contain them. Storing gaps can significantly reduce the index size. We now discuss each of the coding schemes in detail. Each scheme includes a method for coding (or compression) and a method for decoding (decompression). 238 6 Information Retrieval and Web Search Unary Coding Unary coding is simple. It represents a number x with x1 bits of zeros followed by a bit of one. For example, 5 is represented as 00001. The one bit is simply the delimitor. Decoding is also straightforward. This scheme is effective for very small numbers, but wasteful for large numbers. It is thus seldom used alone in practice. Table 6.1 shows example codes of different coding schemes for 10 decimal integers. Column 2 shows the unary code for each integer. Table 6.1: Example codes for integers of different coding schemes: Spacing in the Elias, Golomb, and variable-byte codes separates the prefix of the code from the suffix. Elias Elias Golomb Golomb Variable Decimal Unary Gamma Delta (b = 3) (b = 10) byte 1 1 1 1 1 10 1 001 0000001 0 2 01 0 10 0 100 1 11 1 010 0000010 0 3 001 0 11 0 101 01 0 1 011 0000011 0 4 0001 00 100 0 1100 01 10 1 100 0000100 0 5 00001 00 101 0 1101 01 11 1 101 0000101 0 6 000001 00 110 0 1110 001 0 1 1100 0000110 0 7 0000001 00 111 0 1111 001 10 1 1101 0000111 0 8 00000001 000 1000 00 100000 001 11 1 1110 0001000 0 9 000000001 000 1001 00 100001 0001 0 1 1111 0001001 0 10 0000000001 000 1010 00 100010 0001 10 01 000 0001010 0 Elias Gamma Coding Coding: In the Elias gamma coding, a positive integer x is represented by: 1+log2x in unary (i.e., log2x 0-bits followed by a 1-bit), followed by the binary representation of x without its most significant bit. Note that 1+log2x is simply the number of bits of x in binary. The coding can also be described with the following two steps: 1. Write x in binary. 2. Subtract 1 from the number of bits written in step 1 and prepend that many zeros. Example 6: The number 9 is represented by 0001001, since 1+log29 = 4, or 0001 in unary, and 9 is 001 in binary with the most significant bit removed. Alternatively, we first write 9 in binary, which is 1001 with 4 bits, and then prepend three zeros. In this way, 1 is represented by 1 (in one bit), and 2 is represented by 010. Additional examples are shown in column 3 of Table 6.1. ▀ Decoding: We decode an Elias gamma-coded integer in two steps: 1. Read and count zeroes from the stream until we reach the first one. Call this count of zeroes K. 6.6 Inverted Index and Its Compression 239 2. Consider the one that was reached to be the first digit of the integer, with a value of 2K, read the remaining K bits of the integer. Example 7: To decompress 0001001, we first read all zero bits from the beginning until we see a bit of 1. We have K = 3 zero bits. We then include the 1 bit with the following 3 bits, which give us 1001 (binary for 9). ▀ Gamma coding is efficient for small integers but is not suited to large integers for which the parameterized Golomb code or the Elias delta code is more suitable. Elias Delta Coding Elias delta codes are somewhat longer than gamma codes for small integers, but for larger integers such as document numbers in an index of Web pages, the situation is reversed. Coding: In the Elias delta coding, a positive integer x is stored with the gamma code representation of 1+log2x, followed by the binary representation of x less the most significant bit. Example 8: Let us code the number 9. Since 1+log2x = 4, we have its gamma code 00100 for 4. Since 9’s binary representation less the most significant bit is 001, we have the delta code of 00100001 for 9. Additional examples are shown in column 4 of Table 6.1. ▀ Decoding: To decode an Elias delta-coded integer x, we first decode the gamma-code part 1+log2x as the magnitude M (the number of bits of x in binary), and then retrieve the binary representation of x less the most significant bit. Specifically, we use the following steps: 1. Read and count zeroes from the stream until you reach the first one. Call this count of zeroes L. 2. Considering the one that was reached to be the first bit of an integer, with a value of 2L, read the remaining L digits of the integer. This is the integer M. 3. Put a one in the first place of our final output, representing the value 2M. Read and append the following M-1 bits. Example 9: We want to decode 00100001. We can see that L = 2 after step 1, and after step 2, we have read and consumed 5 bits. We also obtain M = 4 (100 in binary). Finally, we prepend 1 to the M-1 bits (which is 001) to give 1001, which is 9 in binary. ▀ While Elias codes yield acceptable compression and fast decoding, a better performance in both aspects is possible with the Golomb coding. 240 6 Information Retrieval and Web Search Golomb Coding The Golomb coding is a form of parameterized coding in which integers to be coded are stored as values relative to a constant b. Several variations of the original Golomb scheme exist, which save some bits in coding compared to the original scheme. We describe one version here. Coding: A positive integer x is represented in two parts: 1. The first part is a unary representation of q+1, where q is the quotient (x/b), and 2. The second part is a special binary representation of the remainder r = xqb. Note that there are b possible remainders. For example, if b = 3, the possible remainders will be 0, 1, and 2. The binary representation of a remainder requires log2b or log2b bits. Clearly, it is not possible to write every remainder in log2b bits in binary. To save space, we want to write the first few remainders using log2b bits and the rest using log2b bits. We must do so such that the decoder knows when log2b bits are used and when log2b bits are used. Let i = log2b. We code the first d remainders using i bits, d = 2i+1 – b. (25) It is worth noting that these d remainders are all less than d. The rest of the remainders are coded with log2b bits and are all greater than or equal to d. They are coded using a special binary code (also called a fixed prefix code) with log2b (or i+1) bits. Example 10: For b = 3, to code x = 9, we have the quotient q = 9/3 = 3. For remainder, we have i = log2 3 = 1 and d = 1. Note that for b = 3, there are three remainders, i.e., 0, 1, and 2, which are coded as 0, 10, and 11 respectively. The remainder for 9 is r = 9  3  3 = 0. The final code for 9 is 00010. Additional examples for b = 3 are shown in column 5 of Table 6.1. For b = 10, to code x = 9, we have the quotient q = 9/10 = 0. For remainder, we have i = log2 10 = 3 and d = 6. Note that for b = 10, there are 10 remainders, i.e., 0, 1, 2, …, 10, which are coded as 000, 001, 010, 011, 100, 101, 1100, 1101, 1110, 1111 respectively. The remainder of 9 is r = 9  0  5 = 9. The final code for 9 is 11111. Additional examples for b = 10 are shown in column 6 of Table 6.1. ▀ We can see that the first d remainders are standard binary codes, but the rest are not. They are generated using a tree instead. Fig. 6.9 shows an example based on b = 5. The leaves are the five remainders. The first three remainders (0, 1, 2) are in the standard binary code, and the rest (3 and 4) have an additional bit. It is important to note that the first 2 bits (i = 2) of 6.6 Inverted Index and Its Compression 241 the remainder 3 (the first remainder coded in i+1 bits) is 11, which is 3 (i.e., d) in binary. This information is crucial for decoding because it enables the algorithm to know when i+1 bits are used. We also notice that d is completely determined by b, which helps decoding. Fig. 6.9. The coding tree for b = 5 If b is a power of 2 (called Golomb–Rice coding), i.e., b = 2k for integer k  0, every remainder is coded with the same number of bits because log2b = log2b. This is also easy to see from Equation (25), i.e., d = 2k. Decoding: To decode a Golomb-coded integer x, we use the following steps: 1. Decode unary-coded quotient q (the relevant bits are comsumed). 2. Compute i = log2 b and d = 2i+1 – b. 3. Retrieve the next i bits and assign it to r. 4. If r  d then retrieve one more bit and append it to r at the end; r = r – d. 5. Return x = qb + r. Some explanation is in order for step 4. As we discussed above, if r  d we need i+1 bits to code the remainder. The first line of step 4 retrieves the additional bit and appends it to r. The second line obtains the true value of the remainder r. Example 11: We want to decode 11111 for b = 10. We see that q = 0 because there is no zero at the beginning. The first bit is consumed. We know that i = log2 10 = 3 and d = 6. We then retrieve the next three bits, 111, which is 7 in decimal, and assign it to r (= 111). Since 7 > 6 (which is d), we retrieve one more bit, which is 1, and r is now 1111 (15 in decimal). The new r = r – d = 15 – 6 = 9. Finally, x = qb + r = 0 + 9 = 9. ▀ Now we discuss the selection of b for each term. For gap compression, Witten et al. [59] reported that a suitable b is ,69.0       tn Nb (26) 1 0 1 0 1 0 1 0 1 0 3 2 4 242 6 Information Retrieval and Web Search where N is the total number of documents and nt is the number of documents that contain term t. Variable-Byte Coding Coding: In this method, seven bits in each byte are used to code an integer, with the least significant bit set to 0 in the last byte, or to 1 if further bytes follow. In this way, small integers are represented efficiently. For example, 135 is represented in two bytes, since it lies in the range 27 and 214, as 00000011 00001110. Additional examples are shown in column 6 of Table 6.1. Decoding: Decoding is performed in two steps: 1. Read all bytes until a byte with the zero last bit is seen. 2. Remove the least significant bit from each byte read so far and concatenate the remaining bits. For example, 00000011 00001110 is decoded to 00000010000111, which is 135. Finally, experimental results in [58] show that non-parameterized Elias coding is generally not as space-efficient or as fast as parameterized Golomb coding for retrieval. Gamma coding does not work well. Variable- byte integers are often faster than variable-bit integers, despite having higher storage costs, because fewer CPU operations are required to decode variable-byte integers and they are byte-aligned on disk. A suitable compression technique can allow retrieval to be up to twice as fast than without compression, while the space requirement averages 20% – 25% of the cost of storing uncompressed integers. 6.7 Latent Semantic Indexing The retrieval models discussed so far are based on keyword or term matching, i.e., matching terms in the user query with those in the documents. However, many concepts or objects can be described in multiple ways (using different words) due to the context and people’s language habits. If a user query uses different words from the words used in a document, the document will not be retrieved although it may be relevant because the document uses some symonyms of the words in the user query. This causes low recall. For example, “picture”, “image” and “photo” are synonyms in the context of digital cameras. If the user query only has the word “picture”, relevant documents that contain “image” or “photo” but not “picture” will not be retrieved. Latent semantic indexing (LSI), proposed by Deerwester et al. [18], aims to deal with this problem through the identification of statistical 6.7 Latent Semantic Indexing 243 associations of terms. It is assumed that there is some underlying latent semantic structure in the data that is partially obscured by the randomness of word choice. It then uses a statistical technique, called singular value decomposition (SVD) [25], to estimate this latent structure, and to remove the “noise”. The results of this decomposition are descriptions of terms and documents based on the latent semantic structure derived from SVD. This structure is also called the hidden “concept” space, which associates syntactically different but semantically similar terms and documents. These transformed terms and documents in the “concept” space are then used in retrieval, not the original terms or documents. Furthermore, the query is also transformed into the “concept” space before retrieval. Let D be the text collection, the number of distinctive words in D be m and the number of documents in D be n. LSI starts with an mn term- document matrix A. Each row of A represents a term and each column represents a document. The matrix may be computed in various ways, e.g., using term frequency or TF-IDF values. We use term frequency as an example in this section. Thus, each entry or cell of the matrix A, denoted by Aij, is the number of times that term i occurs in document j. 6.7.1 Singular Value Decomposition What SVD does is to factor matrix A (a mn matrix) into the product of three matrices, i.e., ,TVUΣA  (27) where U is a mr matrix and its columns, called left singular vectors, are eigenvectors associated with the r non-zero eigenvalues of AAT. Furthermore, the columns of U are unit orthogonal vectors, i.e., UTU = I (identity matrix). V is an nr matrix and its columns, called right singular vectors, are eigenvectors associated with the r non-zero eigenvalues of ATA. The columns of V are also unit orthogonal vectors, i.e., VTV = I.  is a rr diagonal matrix,  = diag(1, 2, …, r), i > 0. 1, 2, …, and r, called singular values, are the non-negative square roots of the r (non-zero) eigenvalues of AAT. They are arranged in decreasing order, i.e., 1  2  …  r > 0. We note that initially U is in fact an mm matrix and V an nn matrix and  an mn diagonal matrix.  ’s diagonal consists of nonnegative eigenvalues of AAT or ATA. However, due to zero eigenvalues,  has zero-valued rows and columns. Matrix multiplication tells us that those 244 6 Information Retrieval and Web Search zero-valued rows and columns from  can be dropped. Then, the last mr columns in U and the last nr columns in V can also be dropped. m is the number of row (terms) in A, representing the number of terms. n is the number of columns in A, representing the number of documents. r is the rank of A, r  min(m, n). The singular value decomposition of A always exists and is unique up to 1. allowable permutations of columns of U and V and elements of  leaving it still diagonal; that is, columns i and j of  may be interchanged iff row i and j of  are interchanged, and columns i and j of U and V are interchanged. 2. sign (+/) flip in U and V. An important feature of SVD is that we can delete some insignificant dimensions in the transformed (or “concept”) space to optimally (in the least square sense) approximate matrix A. The significance of the dimensions is indicated by the magnitudes of the singular values in , which are already sorted. In the context of information retrieval, the insignificant dimensions may represent “noise” in the data, and should be removed. Let us use only the k largest singular values in  and set the remaining small ones to zero. The approximated matrix of A is denoted by Ak. We can also reduce the size of the matrices , U and V by deleting the last rk rows and columns from , the last rk columns in U and the last rk columns in V. We then obtain ,T kkkk VΣUA  (28) which means that we use the k-largest singular triplets to approximate the original (and somewhat “noisy”) term-document matrix A. The new space is called the k-concept space. Fig. 6.10 shows the original matrices and the reduced matrices schematically. Fig. 6.10. The schematic representation of A and Ak = Term vectors k k k k Terms Documents VT Document vectors mn rnmr  rr U A/Ak Uk Vk T k 6.7 Latent Semantic Indexing 245 It is critical that the LSI method does not re-construct the original term- document matrix A perfectly. The truncated SVD captures most of the important underlying structures in the association of terms and documents, yet at the same time removes the noise or variability in word usage that plagues keyword matching retrieval methods. Intuitive Idea of LSI: The intuition of LSI is that SVD rotates the axes of m-dimensional space of A such that the first axis runs along the largest variation (variance) among the documents, the second axis runs along the second largest variation (variance) and so on. Fig. 6.11 shows an example. The original x-y space is mapped to the x-y space generated by SVD. We can see that x and y are clearly correlated. In our retrieval context, each data point represents a document and each axis (x or y) in the original space represents a term. Hence, the two terms are correlated or co-occur frequently. In the SVD, the direction of x in which the data has the largest variation is represented by the first column vector of U, and the direction of y is represented by the second column vector of U. VT represents the documents in the transformed “concept” space. The singular values in  are simply scaling factors. We observe that y direction is insignificant, and may represent some “noise”, so we can remove it. Then, every data point (document) is projected to x. We have an outlier document di that contains term x, but not term y. However, if it is projected to x, it becomes closer to other points. Let us see what happens if we have a query q represented with a star in Fig. 6.11, which contains only a single term “y”. Using the traditional exact term matching, di is not relevant because “y” does not appear in di. However, in the new space after projection, they are quite close or similar. Fig. 6.11. Intuition of the LSI. 6.7.2 Query and Retrieval Given a user query q (represented by a column vector as those in A), it is first converted into a document in the k-concept space, denoted by qk. This transformation is necessary because SVD has transformed the original x x' y’ y di q 246 6 Information Retrieval and Web Search documents into the k-concept space and stored them in Vk. The idea is that q is treated as a new document in the original space represented as a column in A, and then mapped to qk (a row vector) as an additional document (or column) in Vk T. From Equation (28), it is easy to see that .T kkk qq ΣU (29) Since the columns in U are unit orthogonal vectors, Uk TUk = I. Thus, .T kk T k qq ΣU  (30) As the inverse of a diagonal matrix is still a diagonal matrix, and each entry on the diagonal is 1/i (1  i  k), if it is multiplied on both sides of Equation (30), we obtain, .1 T k T k - k qq UΣ (31) Finally, we get the following (notice that the transpose of a diagonal matrix is itself), .1- kk T k ΣUqq  (32) For retrieval, we simply compare qk with each document (row) in Vk using a similarity measure, e.g., the cosine similarity. Recall that each row of Vk (or each column of Vk T ) corresponds to a document (column) in A. This method has been used traditionally. Alternatively, since kVk T (not Vk T) represents the documents in the transformed k-concept space, we can compare the similarity of the query document in the transformed space, which is kqk T, and each transformed document in kVk T for retrieval. The difference between the two methods is obvious. This latter method considers scaling effects of the singular values in k, but the former does not. However, it is not clear which method performs better as I know of no reported study on this alternative method. 6.7.3 An Example Example 12: We will use the example in [18] to illustrate the process. The document collection has the following nine documents. The first five documents are related to human computer interaction, and the last four documents are related to graphs. To reduce the size of the problem, only the underlined terms are used in our computation. 6.7 Latent Semantic Indexing 247 c1: Human machine interface for Lab ABC computer applications c2: A survey of user opinion of computer system response time c3: The EPS user interface management system c4: System and human system engineering testing of EPS c5: Relation of user-perceived response time to error measurement m1: The generation of random, binary, unordered trees m2: The intersection graph of paths in trees m3: Graph minors IV: Widths of trees and well-quasi-ordering m4: Graph minors: A survey The term-document matrix A is given below, which is a 912 matrix. c1 c2 c3 c4 c5 m1 m2 m3 m4 1 0 0 1 0 0 0 0 0 human 1 0 1 0 0 0 0 0 0 interface 1 1 0 0 0 0 0 0 0 computer 0 1 1 0 1 0 0 0 0 user 0 1 1 2 0 0 0 0 0 system A = 0 1 0 0 1 0 0 0 0 response 0 1 0 0 1 0 0 0 0 time 0 0 1 1 0 0 0 0 0 EPS 0 1 0 0 0 0 0 0 1 survey 0 0 0 0 0 1 1 1 0 trees 0 0 0 0 0 0 1 1 1 graph 0 0 0 0 0 0 0 1 1 minors After performing SVD, we obtain three matrices, U,  and VT, which are given below. Singular values on the diagonal of  are in decreasing order. 0.22 -0.11 0.29 -0.41 -0.11 -0.34 0.52 -0.06 -0.41 0.20 -0.07 0.14 -0.55 0.28 0.50 -0.07 -0.01 -0.11 0.24 0.04 -0.16 -0.59 -0.11 -0.25 -0.30 0.06 0.49 0.40 0.06 -0.34 0.10 0.33 0.38 0.00 0.00 0.01 0.64 -0.17 0.36 0.33 -0.16 -0.21 -0.17 0.03 0.27 U = 0.27 0.11 -0.43 0.07 0.08 -0.17 0.28 -0.02 -0.05 0.27 0.11 -0.43 0.07 0.08 -0.17 0.28 -0.02 -0.05 0.30 -0.14 0.33 0.19 0.11 0.27 0.03 -0.02 -0.17 0.21 0.27 -0.18 -0.03 -0.54 0.08 -0.47 -0.04 -0.58 0.01 0.49 0.23 0.03 0.59 -0.39 -0.29 0.25 -0.23 0.04 0.62 0.22 0.00 -0.07 0.11 0.16 -0.68 0.23 0.03 0.45 0.14 -0.01 -0.30 0.28 0.34 0.68 0.18 3.34 0 0 0 0 0 0 0 0 0 2.54 0 0 0 0 0 0 0 0 0 2.35 0 0 0 0 0 0 0 0 0 1.64 0 0 0 0 0  = 0 0 0 0 1.50 0 0 0 0 0 0 0 0 0 1.31 0 0 0 0 0 0 0 0 0 0.85 0 0 0 0 0 0 0 0 0 0.56 0 0 0 0 0 0 0 0 0 0.36 248 6 Information Retrieval and Web Search 0.20 -0.06 0.11 -0.95 0.05 -0.08 0.18 -0.01 -0.06 0.61 0.17 -0.50 -0.03 -0.21 -0.26 -0.43 0.05 0.24 0.46 -0.13 0.21 0.04 0.38 0.72 -0.24 0.01 0.02 0.54 -0.23 0.57 0.27 -0.21 -0.37 0.26 -0.02 -0.08 V = 0.28 0.11 -0.51 0.15 0.33 0.03 0.67 -0.06 -0.26 0.00 0.19 0.10 0.02 0.39 -0.30 -0.34 0.45 -0.62 0.01 0.44 0.19 0.02 0.35 -0.21 -0.15 -0.76 0.02 0.02 0.62 0.25 0.01 0.15 0.00 0.25 0.45 0.52 0.08 0.53 0.08 -0.03 -0.60 0.36 0.04 -0.07 -0.45 Now let us choose only two largest singular values from , i.e., k = 2. Thus, the concept space has only two dimensions. The other two matrices are also truncated accordingly. We obtain the 3 matrix Uk, k and Vk T: Uk k Vk T 0.22 -0.11 3.34 0 0.20 0.61 0.46 0.54 0.28 0.00 0.02 0.02 0.08 0.20 -0.07 0 2.54 -0.06 0.17 -0.13 -0.23 0.11 0.19 0.44 0.62 0.53 0.24 0.04 0.40 0.06 0.64 -0.17 Ak = 0.27 0.11 0.27 0.11 0.30 -0.14 0.21 0.27 0.01 0.49 0.04 0.62 0.03 0.45 Now we issue a search query q, “user interface”, to find relevant documents. The transformed query document qk of query q in the k-concept space is computed below using Equation (32), which is (0.179 -0.004). 0 T 0.22 -0.11 1 0.20 -0.07 0 0.24 0.04 1 0.40 0.06 0 0.64 -0.17 0 0.27 0.11 3.34 0 -1 0 0.27 0.11 0 2.54 0 0.30 -0.14 0 0.21 0.27 0 0.01 0.49 0 0.04 0.62 0 0.03 0.45 qk is then compared with every document vector in Vk using the cosine similarity. The similarity values are as follows: c1: 0.964 c2: 0.957 c3: 0.968 c4: 0.928 c5: 0.922 = (0.179 0.004) qk = 6.8 Web Search 249 m1: 0.022 m2: 0.023 m3: 0.010 m4: 0.127 We obtain the final ranking of (c3, c1, c2, c4, c5, m4, m2, m3, m1). ▀ 6.7.4 Discussion LSI has been shown to perform better than traditional keywords based methods. The main drawback is the time complexity of the SVD, which is O(m2n). It is thus difficult to use for a large document collection such as the Web. Another drawback is that the concept space is not interpretable as its description consists of all numbers with little semantic meaning. Determining the optimal number of dimensions k of the concept space is also a major difficulty. There is no general consensus for an optimal number of dimensions. The original paper [18] of LSI suggests 50–350 dimensions. In practice, the value of k needs to be determined based on the specific document collection via trial and error, which is a very time consuming process due to the high time complexity of the SVD. To close this section, one can imagine that association rules may be able to approximate the results of LSI and avoid its shortcomings. Association rules represent term correlations or co-occurrences. Association rule mining has two advantages. First, its mining algorithm is very efficient. Since we may only need rules with 2-3 terms, which are sufficient for practical purposes, the mining algorithm only needs to scan the document collection 2-3 times. Second, rules are easy to understand. However, little research has been done in this direction so far. 6.8 Web Search We now put it all together and describe the working of a search engine. Since it is difficult to know the internal details of a commercial search engine, most contents in this section are based on research papers, especially the early Google paper [10]. Due to the efficiency problem, latent semantic indexing is probably not used in Web search yet. Current search algorithms are still mainly based on the vector space model and term matching. A search engine starts with the crawling of pages on the Web. The crawled pages are then parsed, indexed, and stored. At the query time, the index is used for efficient retrieval. We will not discuss crawling here. Its details can be found in Chap. 8. The subsequent operations of a search engine are described below: 250 6 Information Retrieval and Web Search Parsing: A parser is used to parse the input HTML page, which produces a stream of tokens or terms to be indexed. The parser can be constructed using a lexical analyzer generator such as YACC and Flex (which is from the GNU project). Some pre-processing tasks described in Sect. 6.5 may also be performed before or after parsing. Indexing: This step produces an inverted index, which can be done using any of the methods described in Sect. 6.6. For retrieval efficiency, a search engine may build multiple inverted indices. For example, since the titles and anchor texts are often very accurate descriptions of the pages, a small inverted index may be constructed based on the terms appeared in them alone. Note that here the anchor text is for indexing the page that its link points to, not the page containing it. A full index is then built based on all the text in each page, including anchor texts (a piece of anchor text is indexed both for the page that contains it, and for the page that its link points to). In searching, the algorithm may search in the small index first and then the full index. If a sufficient number of relevant pages are found in the small index, the system may not search in the full index. Searching and Ranking: Given a user query, searching involves the following steps: 1. pre-processing the query terms using some of the methods described in Sect. 6.5, e.g., stopword removal and stemming; 2. finding pages that contain all (or most of) the query terms in the inverted index; 3. ranking the pages and returning them to the user. The ranking algorithm is the heart of a search engine. However, little is known about the algorithms used in commercial search engines. We give a general description based on the algorithm in the early Google system. As we discussed earlier, traditional IR uses cosine similarity values or any other related measures to rank documents. These measures only consider the content of each document. For the Web, such content based methods are not sufficient. The problem is that on the Web there are too many relevant documents for almost any query. For example, using “web mining” as the query, the search engine Google estimated that there were 46,500,000 relevant pages. Clearly, there is no way that any user will look at this huge number of pages. Therefore, the issue is how to rank the pages and present the user the “best” pages at the top. An important ranking factor on the Web is the quality of the pages, which was hardly studied in traditional IR because most documents used in IR evaluations are from reliable sources. However, on the Web, anyone can publish almost anything, so there is no quality control. Although a 6.8 Web Search 251 page may be 100% relevant, it may not be a quality page due to several reasons. For example, the author may not be an expert of the query topic, the information given in the page may be unreliable or biased, etc. However, the Web does have an important mechanism, the hyperlinks (links), that can be used to assess the quality of each page to some extent. A link from page x to page y is an implicit conveyance of authority of page x to page y. That is, the author of page x believes that page y contains quality or authoritative information. One can also regard the fact that page x points to page y as a vote of page x for page y. This democratic nature of the Web can be exploited to assess the quality of each page. In general, the more votes a page receives, the more likely it is a quality page. The actual algorithms are more involved than simply counting the number of votes or links pointing to a page (called in-links). We will describe the algorithms in the next chapter. PageRank is the most well known such algorithm (see Sect. 7.3). It makes use of the link structure of Web pages to compute a quality or reputation score for each page. Thus, a Web page can be evaluated based on both its content factors and its reputation. Content-based evaluation depends on two kinds of information: Occurrence Type: There are several types of occurrences of query terms in a page: Title: a query term occurs in the title field of the page. Anchor text: a query term occurs in the anchor text of a page pointing to the current page being evaluated. URL: a query term occurs in the URL of the page. Many URL addresses contain some descriptions of the page. For example, a page on Web mining may have the URL http://www.domain.edu/Web-mining.html. Body: a query term occurs in the body field of the page. In this case, the prominence of each term is considered. Prominence means whether the term is emphasized in the text with a large font, or bold and/or italic tags. Different prominence levels can be used in a system. Note that anchor texts in the page can be treated as plain texts for the evaluation of the page. Count: The number of occurrences of a term of each type. For example, a query term may appear in the title field of the page 2 times. Then, the title count for the term is 2. Position: This is the position of each term in each type of occurrence. The information is used in proximity evaluation involving multiple query terms. Query terms that are near to each other are better than those that are far apart. Furthermore, query terms appearing in the page in the same sequence as they are in the query are also better. 252 6 Information Retrieval and Web Search For the computation of the content based score (also called the IR score), each occurrence type is given an associated weight. All type weights form a fixed vector. Each raw term count is converted to a count weight, and all count weights also form a vector. The quality or reputation of a page is usually computed based on the link structure of Web pages, which we will study in Chap. 7. Here, we assume that a reputation score has been computed for each page. Let us now look at two kinds of queries, single word queries and multi-word queries. A single word query is the simplest case with only a single term. After obtaining the pages containing the term from the inverted index, we compute the dot product of the type weight vector and the count weight vector of each page, which gives us the IR score of the page. The IR score of each page is then combined with its reputation score to produce the final score of the page. For a multi-word query, the situation is similar, but more complex since there is now the issue of considering term proximity and ordering. Let us simplify the problem by ignoring the term ordering in the page. Clearly, terms that occur close to each other in a page should be weighted higher than those that occur far apart. Thus multiple occurrences of terms need to be matched so that nearby terms can be identified. For every matched set, a proximity value is calculated, which is based on how far apart the terms are in the page. Counts are also computed for every type and proximity. Each type and proximity pair has a type-proximity-weight. The counts are converted into count-weights. The dot product of the count-weights and the type-proximity-weights gives an IR score to the page. Term ordering can be considered similarly and included in the IR score, which is then combined with the page reputation score to produce the final rank score. 6.9 Meta-Search and Combining Multiple Rankings In the last section, we described how an individual search engine works. We now discuss how several search engines can be used together to produce a meta-search engine, which is a search system that does not have its own database of Web pages. Instead, it answers the user query by combining the results of some other search engines which normally have their databases of Web pages. Fig. 6.12 shows a meta-search architecture. After receiving a query from the user through the search interface, the meta-search engine submits the query to the underlying search engines (called its component search engines). The returned results from all these search engines are then combined (fused or merged) and sent to the user. 6.9 Meta-Search and Combining Multiple Rankings 253 A meta-search engine has some intuitive appeals. First of all, it increases the search coverage of the Web. The Web is a huge information source, and each individual search engine may only cover a small portion of it. If we use only one search engine, we will never see those relevant pages that are not covered by the search engine. Fig. 6.12. A meta-search architecture Meta-search may also improve the search effectiveness. Each component search engine has its ranking algorithm to rank relevant pages, which is often biased, i.e., it works well for certain types of pages or queries but not for others. By combining the results from multiple search engines, their biases can be reduced and thus the search precision can be improved. The key operation in meta-search is to combine the ranked results from the component search engines to produce a single ranking. The first task is to identify whether two pages from different search engines are the same, which facilitates combination and duplicate removal. Without downloading the full pages (which is too time consuming), this process is not simple due to aliases, symbolic links, redirections, etc. Typically, several heuristics are used for the purpose, e.g., comparing domain names of URLs, titles of the pages, etc. The second task is to combine the ranked results from individual search engines to produce a single ranking, i.e., to fuse individual rankings. There are two main classes of meta-search combination (or fusion) algorithms: ones that use similarity scores returned by each component system and ones that do not. Some search engines return a similarity score (with the query) for each returned page, which can be used to produce a better combined ranking. We discuss these two classes of algorithms below. It is worth noting that the first class of algorithms can also be used to combine scores from different similarity functions in a single IR system or in a single search engine. Indeed, the algorithms below were originally proposed for this purpose. It is likely that search engines already use some Search interface Metasearch engine Search engine 1 Search engine 2 Search engine n  254 6 Information Retrieval and Web Search such techniques (or their variations) within their ranking mechanisms because a ranking algorithm needs to consider multiple factors. 6.9.1 Combination Using Similarity Scores Let the set of candidate documents to be ranked be D = {d1, d2, …, dN}. There are k underlying systems (component search engines or ranking techniques). The ranking from system or technique i gives document dj the similarity score, sij. Some popular and simple combination methods are defined by Fox and Shaw in [22]. CombMIN: The combined similarity score for each document dj is the minimum of the similarities from all underlying search engine systems: CombMIN(dj) = min(s1j, s2j, …, skj). (33) CombMAX: The combined similarity score for each document dj is the maximum of the similarities from all underlying search engine systems: CombMAX(dj) = max(s1j, s2j, …, skj). (34) CombSUM: The combined similarity score for each document dj is the sum of the similarities from all underlying search engine systems. .)(CombSUM 1  k i ijj sd (35) CombANZ: It is defined as ,)(CombSUM)(CombANZ j j j r dd  (36) where rj is the number of non-zero similarities, or the number of systems that retrieved dj. CombMNZ: It is defined as jjj rdd  )(CombSUM)(CombMNZ (37) where rj is the number of non-zero similarities, or the number of systems that retrieved dj. It is a common practice to normalize the similarity scores from each ranking using the maximum score before combination. Researchers have shown that, in general, CombSUM and CombMNZ perform better. CombMNZ outperforms CombSUM slightly in most cases. 6.9 Meta-Search and Combining Multiple Rankings 255 6.9.2 Combination Using Rank Positions We now discuss some popular rank combination methods that use only rank positions of each search engine. In fact, there is a field of study called the social choice theory [33] that studies voting algorithms as techniques to make group or social decisions (choices). The algorithms discussed below are based on voting in elections. In 1770 Jean-Charles de Borda proposed “election by order of merit”. Each voter announces a (linear) preference order on the candidates. For each voter, the top candidate receives n points (if there are n candidates in the election), the second candidate receives n1 points, and so on. The points from all voters are summed up to give the final points for each candidate. If there are candidates left unranked by a voter, the remaining points are divided evenly among the unranked candidates. The candidate with the most points wins. This method is called the Borda ranking. An alternative method was proposed by Marquis de Condorcet in 1785. The Condorcet ranking algorithm is a majoritarian method where the winner of the election is the candidate(s) that beats each of the other candidates in a pair-wise comparison. If a candidate is not ranked by a voter, the candidate loses to all other ranked candidates. All unranked candidates tie with one another. Yet another simple method, called the reciprocal ranking, sums one over the rank of each candidate across all voters. For each voter, the top candidate has the score of 1, the second ranked candidate has the score of 1/2, and the third ranked candidate has the score of 1/3 and so on. If a candidate is not ranked by a voter, it is skipped in the computation for this voter. The candidates are then ranked according to their final total scores. This rank strategy gives much higher weight than Borda ranking to candidates that are near the top of a list. Example 13: We use an example in the context of meta-search to illustrate the working of these methods. Consider a meta-search system with five underlying search engine systems, which have ranked four candidate documents or pages, a, b, c, and d as follows: system 1: a, b, c, d system 2: b, a, d, c system 3: c, b, a, d system 4: c, b, d system 5: c, b Let us denote the score of each candidate x by Score(x). Borda Ranking: The score for each page is as follows: 256 6 Information Retrieval and Web Search Score(a) = 4 + 3 + 2 + 1 + 1.5 = 11.5 Score(b) = 3 + 4 + 3 + 3 + 3 = 16 Score(c) = 2 + 1 + 4 + 4 + 4 = 15 Score(d) = 1 + 2 + 1 + 2 + 1.5 = 7.5 Thus the final ranking is: b, c, a, d. Condorcet Ranking: We first build an nn matrix for all pair-wise comparisons, where n is the number of pages. Each non-diagonal entry (i, j) of the matrix shows the number of wins, loses, and ties of page i over page j, respectively. For our example, the matrix is as follows: a b c d a - 1:4:0 2:3:0 3:1:1 b 4:1:0 - 2:3:0 5:0:0 c 3:2:0 3:2:0 - 4:1:0 d 1:3:1 0:5:0 1:4:0 - Fig. 6.13. The pair-wise comparison matrix for the four candidate pages After the matrix is constructed, pair-wise winners are determined, which produces a win, lose and tie table. Each pair in Fig. 6.13 is compared, and the winner receives one point in its “win” column and the loser receives one point in its “lose” column. For a pair-wise tie, both receive one point in the “tie” column. For example, for page a, it only beats d because a is ranked ahead of d three times out of 5 ranks (Fig. 6.13). The win, lose and tie table for Fig. 6.13 is given in Fig. 6.14 below. win lose tie a 1 2 0 b 2 1 0 c 3 0 0 d 0 3 0 Fig. 6.14. The win, lose and tie table for the comparison matrix in Fig. 6.13 To rank the pages, we use their win and lose values. If the number of wins that a page i has is higher than another page j, then i wins over j. If their win property is equal, we consider their lose scores, and the page which has a lower lose score wins. If both their win and lose scores are the same, then the pages are tied. The final ranks of the tied pages are randomly assigned. Clearly c is the Condorcet winner in our example. The final ranking is: c, b, a, d. 6.10 Web Spamming 257 Reciprocal Ranking: Score(a) = 1 + 1/2 + 1/3 = 1.83 Score(b) = 1/2 + 1 + 1/2 + 1/2 + 1/2 = 3 Score(c) = 1/3 + 1/4 + 1 + 1 + 1 = 3.55 Score(d) = 1/4 + 1/3 + 1/4 + 1/3= 1.17 The final ranking is: c, b, a, d. ▀ 6.10 Web Spamming Web search has become very important in the information age. Increased exposure of pages on the Web can result in significant financial gains and/or fames for organizations and individuals. The rank positions of Web pages in search are perhaps the single most important indicator of such exposures of pages. If a user searches for information that is relevant to your pages but your pages are ranked low by search engines, then the user may not see the pages because one seldom clicks a large number of returned pages. This is not acceptable for businesses, organizations, and even individuals. Thus, it has become very important to understand search engine ranking algorithms and to present the information in one’s pages in such a way that the pages will be ranked high when terms related to the contents of the pages are searched. Unfortunately, this also results in spamming, which refers to human activities that deliberately mislead search engines to rank some pages higher than they deserve. There is a gray area between spamming and legitimate page optimization. It is difficult to define precisely what are justifiable and unjustifiable actions aimed at boosting the importance and consequently the rank positions of one’s pages. Assume that, given a user query, each page on the Web can be assigned an information value. All the pages are then ranked according to their information values. Spamming refers to actions that do not increase the information value of a page, but dramatically increase its rank position by misleading search algorithms to rank it high. Due to the fact that search engine algorithms do not understand the content of each page, they use syntactic or surface features to assess the information value of the page. Spammers exploit this weakness to boost the ranks of their pages. Spamming is annoying for users because it makes it harder to find truly useful information and leads to frustrating search experiences. Spamming is also bad for search engines because spam pages consume crawling bandwidth, pollute the Web, and distort search ranking. 258 6 Information Retrieval and Web Search There are in fact many companies that are in the business of helping others improve their page ranking. These companies are called Search Engine Optimization (SEO) companies, and their businesses are thriving. Some SEO activities are ethical and some, which generate spam, are not. As we mentioned earlier, search algorithms consider both content based factors and reputation based factors in scoring each page. In this section, we briefly describe some spam methods that exploit these factors. The section is mainly based on [28] by Gyongyi and Garcia-Molina. 6.10.1 Content Spamming Most search engines use variations of TF-IDF based measures to assess the relevance of a page to a user query. Content-based spamming methods basically tailor the contents of the text fields in HTML pages to make spam pages more relevant to some queries. Since TF-IDF is computed based on terms, content spamming is also called term spamming. Term spamming can be placed in any text field: Title: Since search engines usually give higher weights to terms in the title of a page due to the importance of the title to a page, it is thus common to spam the title. Meta-Tags: The HTML meta-tags in the page header enable the owner to include some meta information of the page, e.g., author, abstract, keywords, content language, etc. However, meta-tags are very heavily spammed. Search engines now give terms within these tags very low weights or completely ignore their contents. Body: Clearly spam terms can be placed within the page body to boost the page ranking. Anchor Text: As we discussed in Sect. 6.8, the anchor text of a hyperlink is considered very important by search engines. It is indexed for the page containing it and also for the page that it points to, so anchor text spam affects the ranking of both pages. URL: Some search engines break down the URL of a page into terms and consider them in ranking. Thus, spammers can include spam terms in the URL. For example, a URL may be http://www.xxx.com/cheap-MP3- player-case-battery.html There are two main term spam techniques, which simply create synthetic contents containing spam terms. 1. Repeating some important terms: This method increases the TF scores of the repeated terms in a document and thus increases the relevance of the document to these terms. Since plain repetition can be 6.10 Web Spamming 259 easily detected by search engines, the spam terms can be weaven into some sentences, which may be copied from some other sources. That is, the spam terms are randomly placed in these sentences. For example, if a spammer wants to repeat the word “mining”, it may add it randomly in an unrelated (or related) sentence, e.g., “the picture mining quality of this camera mining is amazing,” instead of repeating it many times consecutively (next to each other), which is easy to detect. 2. Dumping of many unrelated terms: This method is used to make the page relevant to a large number of queries. In order to create the spam content quickly, the spammer may simply copy sentences from related pages on the Web and glue them together. Advertisers may also take advantage of some frequently searched terms on the Web and put them in the target pages so that when users search for the frequently search terms, the target pages become relevant. For example, to advertise cruise liners or cruise holiday packages, spammers put “Tom Cruise” in their advertising pages as “Tom Cruise” is a popular film actor in USA and is searched very frequently. 6.10.2 Link Spamming Since hyperlinks play an important role in determining the reputation score of a page, spammers also spam on hyperlinks. Out-Link Spamming: It is quite easy to add out-links in one’s pages pointing to some authoritative pages to boost the hub cores of one’s pages. A page is a hub page if it points to many authoritative (or quality) pages. The concepts of authority and hub will be formally studied in the next chapter (Sect. 7.4). To create massive out-links, spammers may use a technique called directory cloning. There are many directories, e.g., Yahoo!, DMOZ Open Directory, on the Web which contain a large number of links to other Web pages that are organized according to some pre-specified topic hierarchies. Spammers simply replicate a large portion of a directory in the spam page to create a massive out-link structure quickly. In-Link Spamming: In-link spamming is harder to achieve because it is not easy to add hyperlinks on the Web pages of others. Spammers typically use one or more of the following techniques. 1. Creating a honey pot: If a page wants to have a high reputation/quality score, it needs quality pages pointing to it (see Sect. 7.3 in the next chapter). This method basically tries to create some important pages that contain links to target spam pages. For example, the spammer can create a set of pages that contains some very useful information, e.g., glossary 260 6 Information Retrieval and Web Search of Web mining terms, or Java FAQ and help pages. The honey pots attract people pointing to them because they contain useful information, and consequently have high reputation scores (high quality pages). Such honey pots contain (hidden) links to target spam pages that the spammers want to promote. This strategy can significantly boost the spam pages. 2. Adding links to Web directories: Many Web directories allow the user to submit URLs. Spammers can submit the URLs of spam pages at multiple directory sites. Since directory pages often have high quality (or authority) and hub scores, they can boost reputation scores of spam pages significantly. 3. Posting links to the user-generated content (reviews, forum discussions, blogs, etc): There are numerous sites on the Web that allow the user to freely post messages, which are called the user-generated content. Spammers can add links pointing to their pages to the seemly innocent messages that they post. 4. Participating in link exchange: In this case, many spammers form a group and set up a link exchange scheme so that their sites point to each other in order to promote the pages of all the sites. 5. Creating own spam farm: In this case, the spammer needs to control a large number of sites. Then, any link structure can be created to boost the ranking of target spam pages. 6.10.3 Hiding Techniques In most situations, spammer wants to conceal or to hide the spamming sentences, terms and links so that the Web users do not see them. They can use a number of techniques. Content Hiding: Spam items are made invisible. One simple method is to make the spam terms the same color as the background color. For example, one may use the following for hiding, spam items … To hide a hyperlink, one can also use a very small and blank image, e.g., A spammer can also use scripts to hide some of the visual elements on the page, for instance, by setting the visible HTML style attribute to false. 6.10 Web Spamming 261 Cloaking: Spam Web servers return a HTML document to the user and a different document to a Web crawler. In this way, the spammer can present the Web user with the intended content and send a spam page to the search engine for indexing. Spam Web servers can identify Web crawlers in one of the two ways: 1. It maintains a list of IP addresses of search engines and identifies search engine crawlers by matching IP addresses. 2. It identifies Web browsers based on the user–agent field in the HTTP request message. For instance, the user–agent name of the following HTTP request message is the one used by the Microsoft Internet Explorer 6 browser: GET /pub/WWW/TheProject.html HTTP/1.1 Host: www.w3.org User–Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) User–agent names are not standard, so it is up to the requesting application what to include in the corresponding message field. However, search engine crawlers usually identify themselves by names distinct from normal Web browsers in order to allow well-intended and legitimate optimization. For example, some sites serve search engines a version of their pages that is free of navigation links and advertisements. Redirection: Spammers can also hide a spammed page by automatically redirecting the browser to another URL as soon as the page is loaded. Thus, the spammed page is given to the search engine for indexing (which the user will never see), and the target page is presented to the Web user through redirection. One way to achieve redirection is to use the “refresh” meta-tag, and set the refresh time to zero. Another way is to use scripts. 6.10.4 Combating Spam Some spamming activities, like redirection using refresh meta-tag, are easy to detect. However, redirections by using scripts are hard to identify because search engine crawlers do not execute scripts. To prevent cloaking, a search engine crawler may identify itself as a regular Web browser. Using the terms of anchor texts of links that point to a page to index the page is able to fight content spam to some extent because anchor texts from other pages are more trustworthy. This method was originally proposed to index pages that were not fetched by search engine crawlers [40]. It is now a general technique used by search engines as we have seen in Sect. 6.8, i.e., search engines give terms in such anchor texts higher weights. In fact, the terms near a piece of anchor text also offer good editorial judgment about the target page. 262 6 Information Retrieval and Web Search The PageRank algorithm [10] is able to combat content spam to a certain degree as it is based on links that point to the target pages, and the pages that point to the target pages need to be reputable or with high PageRank scores as well (see Chap. 7). However, it does not deal with the in-link based spamming methods discussed above. Instead of combating each individual type of spam, a method (called TrustRank) is proposed in [29] to combat all kinds of spamming methods at the same time. It takes advantage of the approximate isolation of reputable and non-spam pages, i.e., reputable Web pages seldom pointing to spam pages, and spam pages often link to many reputable pages (in an attempt to improve their hub scores). Link analysis methods are used to separate reputable pages and any form of spam without dealing with each spam technique individually. Combating spam can also be seen as a classification problem, i.e., predicting whether a page is a spam page or not. One can use any supervised learning algorithm to train a spam classifier. The key issue is to design features used in learning. The following are some example features used in [44] to detect content spam. 1. Number of words in the page: A spam page tends to contain more words than a non-spam page so as to cover a large number of popular words. 2. Average word length: The mean word length for English prose is about 5 letters. Average word length of synthetic content is often different. 3. Number of words in the page title: Since search engines usually give extra weights to terms appearing in page titles, spammers often put many keywords in the titles of the spam pages. 4. Fraction of visible content: Spam pages often hide spam terms by making them invisible to the user. Other features used include the amount of anchor text, compressibility, fraction of page drawn from globally popular words, independent n-gram likelihoods, conditional n-gram likelihoods, etc. Details can be found in [44]. Its spam detection classifier gave very good results. Testing on 2364 spam pages and 14806 non-spam pages (17170 pages in total), the classifier was able to correctly identify 2,037 (86.2%) of the 2364 spam pages, while misidentifying only 526 spam and non-spam pages. Another interesting technique for fighting spam is to partition each Web page into different blocks using techniques discussed in Sect. 6.5. Each block is given an importance level automatically. To combat link spam, links in less important blocks are given lower transition probabilities to be used in the PageRank computation. The original PageRank algorithm assigns every link in a page an equal transition probability (see Sect. 7.3). The non-uniform probability assignment results in lower PageRank scores Bibliographic Notes 263 for pages pointed to by links in less important blocks. This method is effective because in the link exchange scheme and the honey pot scheme, the spam links are usually placed in unimportant blocks of the page, e.g., at the bottom of the page. The technique may also be used to fight term spam in a similar way, i.e., giving terms in less important blocks much lower weights in rank score computation. This method is proposed in [11]. However, sophisticated spam is still hard to detect. Combating spam is an on-going process. Once search engines are able to detect certain types of spam, spammers invent more sophisticated spamming methods. Bibliographic Notes Information retrieval (IR) is a major research field. This chapter only gives a brief introduction to some commonly used models and techniques. There are several text books that have a comprehensive coverage of the field, e.g., those by Baeza-Yates and Ribeiro-Neto [5], Grossman and Frieder [26], Salton and McGill [53], van Rijsbergen (http://www.dcs.gla.ac.uk /Keith/Preface.html), Witten et al. [59], and Yu and Meng [67]. A similar chapter in the book by Chakrabarti [14] also discusses many Web specific issues and has influenced the writing of this chapter. Below, we discuss some further readings related to Web search and mining. On index compression, Elias coding was introduced by Elias [20] and Golomb coding was introduced by Golomb [24]. Their applications to index compression was studied by several researchers, e.g., Witten et al. [59], Bell et al. [8], Moffat et al. [42], and Williams and Zobel [58]. Wikipedia is a great source of information on this topic as well. Latent semantic index (LSI) was introduced by Deerwester et al. [18], which uses the singular value decomposition technique (SVD) [25]. Additional information about LSI and/or SVD can be found in [9, 35, 67]. Telcordia Technologies, where LSI was developed, maintains a LSI page at http://lsi.research.telcordia.com/ with more references. On Web page pre-processing, the focus has been on identifying the main content blocks of each page because a typical Web page contains a large amount of noise, which can adversely affect the search or mining accuracy. Several researchers have attempted the task, e.g., Bar-Yossef et al. [7], Li et al. [37], Lin and Ho [38], Yi et al. [65], Debnath et al. [17], Gibson, et al. [23], Ramaswamy et al. [49], Song et al. [56], Yin and Lee [66], etc. Although search is probably the biggest application on the Web, little is known about the actual implementation of a search engine except some principal ideas. Sect. 6.8 is largely based on the Google paper by Brin and Page [10], and bits and pieces in various other sources. Over the years, a 264 6 Information Retrieval and Web Search large number of researchers have studied various aspects of Web search, e.g., [6, 13, 15, 30, 32, 36, 48, 50, 57, 63, 64, 70]. For metasearch, the combination methods in Sect. 6.9.1 were proposed by Fox and Shaw [22]. Aslam and Montague [3], Montague and Aslam [43], and Nuray and Can [45] provide good descriptions of Borda ranking and Condorcet ranking. In addition to ranking, Meng et al. [41] discussed many other metasearch issues. On Web spam, Gyongyi and Garcia-Molina gave an excellent taxonomy of different types of spam [28], and the TrustRank algorithm is also due to them [29]. An improvement to TrustRank was proposed by Wu et al. [62]. General link spam detection was studied by Adali et al. [1], Amitay et al. [2], Baeza-Yates et al. [4], Gyongyi and Garcia-Molina [27], Wu and Davison [61], etc. Content spam detection was studied by Fetterly et al. [21], and Ntoulas et al. [44]. A cloaking detection algorithm is reported in [60]. Bibliography 1. Adali, S., T. Liu, and M. Magdon-Ismail. Optimal Link Bombs are Uncoordinated. In Proceedings of 1st International Workshop on Adversarial Information Retrieval on the Web, 2005. 2. Amitay, E., D. Carmel, A. Darlow, R. Lempel, and A. Soffer. The connectivity sonar: detecting site functionality by structural patterns. In Proceedings of ACM Conference on Hypertext and Hypermedia, 2003. 3. Aslam, J. and M. Montague. Models for metasearch. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR- 2001), 2001. 4. Baeza-Yates, R., C. Castillo, V. López, and C. Telefónica. PageRank increase under different collusion topologies. In Proceedings of Intl. Workshop on Adversarial Information Retrieval on the Web, 2005. 5. Baeza-Yates, R. and B. Ribeiro-Neto. Modern information retrieval. 1999: Addison-Wesley. 6. Bar-Yossef, Z. and M. Gurevich. Random sampling from a search engine's index. Journal of the ACM (JACM), 2008, 55(5): p. 1-74. 7. Bar-Yossef, Z. and S. Rajagopalan. Template detection via data mining and its applications. In Proceedings of International Conference on World Wide Web (WWW-2002), 2002. 8. Bell, T., A. Moffat, C. Nevill-Manning, I. Witten, and J. Zobel. Data compression in full-text retrieval systems. Journal of the American Society for Information Science, 1993, 44(9): p. 508-531. 9. Berry, M., S. Dumais, and G. O'Brien. Using linear algebra for intelligent information retrieval. SIAM review, 1995, 37(4): p. 573-595. 10. Brin, S. and P. Lawrence. The anatomy of a large-scale hypertextual web search engine. Computer Networks, 1998, 30(1-7): p. 107-117. Bibliography 265 11. Cai, D., S. Yu, J. Wen, and W. Ma. Block-based web search. In Proceedings of ACM SIGIR Research and Development in Information Retrieval (SIGIR- 2004), 2004. 12. Cai, D., S. Yu, J. Wen, and W. Ma. Extracting content structure for web pages based on visual representation. In In Processings of APWeb-2003, 2003. 13. Cao, Y., J. Xu, T. Liu, H. Li, Y. Huang, and H. Hon. Adapting ranking SVM to document retrieval. In Proceedings of ACM SIGIR Research and Development in Information Retrieval (SIGIR-2006), 2006. 14. Chakrabarti, S. Mining the Web: discovering knowledge from hypertext data. 2003: Morgan Kaufmann Publishers. 15. Chakrabarti, S., K. Puniyani, and S. Das. Optimizing scoring functions and indexes for proximity search in type-annotated corpora. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 16. Chen, S. and J. Goodman. An empirical study of smoothing techniques for language modeling, 1996: Association for Computational Linguistics. 17. Debnath, S., P. Mitra, and C. Giles. Automatic extraction of informative blocks from webpages. In Proceedings of ACM Symposium on Applied Computing, 2005. 18. Deerwester, S., S. Dumais, G. Furnas, T. Landauer, and R. Harshman. Indexing by latent semantic analysis. Journal of the American Society for Information Science, 1990, 41(6): p. 391-407. 19. Deng, L., X. Chai, Q. Tan, W. Ng, and D. Lee. Spying out real user preferences for metasearch engine personalization. In Proceedings of Workshop on WebKDD, 2004. 20. Elias, P. Universal codeword sets and representations of the integers. Information Theory, IEEE Transactions on, 1975, 21(2): p. 194-203. 21. Fetterly, D., M. Manasse, and M. Najork. Detecting phrase-level duplication on the world wide web. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2005), 2005. 22. Fox, E. and J. Shaw. Combination of multiple searches. NIST Special Publications, 1994: p. 243-243. 23. Gibson, D., K. Punera, and A. Tomkins. The volume and evolution of web page templates. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 24. Golomb, S. Run-length encoding. IEEE Transactions on Information Theory, 1966, 12(3): p. 399–401. 25. Golub, G. and C. Van Loan. Matrix computations. 1996: Johns Hopkins Univ Press. 26. Grossman, D.A. and O. Frieder. Information Retrieval: Algorithms and Heuristics. 2004: Springer. 27. Gyöngyi, Z. and H. Garcia-Molina. Link spam alliances. In Proceedings of International Conference on Very Large Data Bases (VLDB-2005), 2005: VLDB Endowment. 28. Gyöngyi, Z. and H. Garcia-Molina. Web spam taxonomy. In Technical Report, Stanford University, 2004. 266 6 Information Retrieval and Web Search 29. Gyöngyi, Z., H. Garcia-Molina, and J. Pedersen. Combating web spam with TrustRank. In Proceedings of International Conference on Very Large Data Bases (VLDB-2004), 2004. 30. Ho Kwok, S. and C. Yang. Searching the peer-to-peer networks: The community and their queries. Journal of the American Society for Information Science and Technology, 2004, 55(9): p. 783-793. 31. Joachims, T. Optimizing search engines using clickthrough data. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2002), 2002. 32. Jones, R., B. Rey, O. Madani, and W. Greiner. Generating query substitutions. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 33. Kelly, J. Social choice theory: An introduction. 1988: Springer-Verlag. 34. Klavans, J. and S. Muresan. DEFINDER: Rule-based methods for the extraction of medical terminology and their associated definitions from on- line text. In Proceedings of Conference of American Medical Informatics Association, 2000. 35. Korn, F., H. Jagadish, and C. Faloutsos. Efficiently supporting ad hoc queries in large datasets of time sequences. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-1997), 1997. 36. Kraft, R., C. Chang, F. Maghoul, and R. Kumar. Searching with context. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 37. Li, X., T. Phang, M. Hu, and B. Liu. Using micro information units for internet search. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2002), 2002. 38. Lin, S. and J. Ho. Discovering informative content blocks from Web documents. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2002), 2002. 39. Liu, B., C. Chin, and H. Ng. Mining topic-specific concepts and definitions on the web. In Proceedings of International Conference on World Wide Web (WWW-2003), 2003. 40. McBryan, O. GENVL and WWWW: Tools for Taming the Web. In Proceedings of International Conference on World Wide Web (WWW-1994), 1994. 41. Meng, W., C. Yu, and K. Liu. Building efficient and effective metasearch engines. ACM Computing Surveys (CSUR), 2002, 34(1): p. 48-89. 42. Moffat, A., R. Neal, and I. Witten. Arithmetic coding revisited. ACM Transactions on Information Systems (TOIS), 1998, 16(3): p. 256-294. 43. Montague, M. and J. Aslam. Condorcet fusion for improved retrieval. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2002), 2002. 44. Ntoulas, A., M. Najork, M. Manasse, and D. Fetterly. Detecting spam web pages through content analysis. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. Bibliography 267 45. Nuray, R. and F. Can. Automatic ranking of information retrieval systems using data fusion. Information Processing & Management, 2006, 42(3): p. 595-614. 46. Ponte, J. and W. Croft. A language modeling approach to information retrieval. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1998), 1998. 47. Porter, M. An algorithm for suffix stripping. Program: electronic library and information systems, 2006, 40(3): p. 211-218. 48. Qiu, F. and J. Cho. Automatic identification of user interest for personalized search. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 49. Ramaswamy, L., A. Iyengar, L. Liu, and F. Douglis. Automatic detection of fragments in dynamically generated web pages. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 50. Richardson, M., A. Prakash, and E. Brill. Beyond PageRank: machine learning for static ranking. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 51. Robertson, S., S. Walker, and M. Beaulieu. Okapi at TREC-7: automatic ad hoc, filtering, VLC and interactive track. NIST Special Publications, 1999: p. 253-264. 52. Salton, G. and C. Buckley. Term-weighting approaches in automatic text retrieval. Information Processing & Management, 1988, 24(5): p. 513-523. 53. Salton, G. and M. McGill. An Introduction to Modern Information Retrieval. 1983: McGraw-Hill. 54. Shen, X., B. Tan, and C. Zhai. Context-sensitive information retrieval using implicit feedback. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2005), 2005. 55. Singhal, A. Modern information retrieval: A brief overview. IEEE Data Engineering Bulletin, 2001, 24(4): p. 35-43. 56. Song, R., H. Liu, J. Wen, and W. Ma. Learning block importance models for web pages. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 57. Sun, J., X. Wang, D. Shen, H. Zeng, and Z. Chen. CWS: a comparative web search system. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 58. Williams, H. and J. Zobel. Compressing integers for fast file access. The Computer Journal, 1999, 42(3): p. 193. 59. Witten, I., A. Moffat, and T. Bell. Managing gigabytes: compressing and indexing documents and images. 1999: Morgan Kaufmann Publishers. 60. Wu, B. and B. Davison. Cloaking and redirection: A preliminary study. Adversarial Information Retrieval on the Web, 2005. 61. Wu, B. and B. Davison. Identifying link farm spam pages. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 62. Wu, B., V. Goel, and B. Davison. Topical TrustRank: Using topicality to combat web spam. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 268 6 Information Retrieval and Web Search 63. Yang, B. and G. Jeh. Retroactive answering of search queries. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 64. Yang, C. and K. Chan. Retrieving multimedia web objects based on pagerank algorithm. In WWW’05 Poster, 2005. 65. Yi, L., B. Liu, and X. Li. Eliminating noisy information in web pages for data mining. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2003), 2003. 66. Yin, X. and W. Lee. Using link analysis to improve layout on mobile devices. In Proceedings of International Conference on World Wide Web (WWW- 2004), 2004. 67. Yu, C. and W. Meng. Principles of database query processing for advanced applications. 1998: Morgan Kaufmann Publishers. 68. Zhai, C. Statistical Language Model for Information Retrieval. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2001), 2001. 69. Zhai, C. and J. Lafferty. A study of smoothing methods for language models applied to information retrieval. ACM Transactions on Information Systems (TOIS), 2004, 22(2): p. 179-214. 70. Zhao, Q., S. Hoi, T. Liu, S. Bhowmick, M. Lyu, and W. Ma. Time-dependent semantic similarity measure of queries using historical click-through data. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 7 Social Network Analysis Early search engines retrieved relevant pages for the user based primarily on the content similarity of the user query and the indexed pages of the search engines. The retrieval and ranking algorithms were simply direct implementation of those from information retrieval. Starting from 1996, it became clear that content similarity alone was no longer sufficient for search due to two reasons. First, the number of Web pages grew rapidly during the middle to late 1990s. Given any query, the number of relevant pages can be huge. For example, given the search query “classification technique”, the Google search engine estimates that there are about 10 million relevant pages. This abundance of information causes a major problem for ranking, i.e., how to choose only 10–30 pages and rank them suitably to present to the user. Second, content similarity methods are easily spammed. A page owner can repeat some important words and add many remotely related words in his/her pages to boost the rankings of the pages and/or to make the pages relevant to a large number of possible queries. Starting from around 1996, researchers in academia and search engine companies began to work on the problem. They resort to hyperlinks. Unlike text documents used in traditional information retrieval, which are often considered independent of one another (i.e., with no explicit relationships or links among them except in citation analysis), Web pages are connected through hyperlinks, which carry important information. Some hyperlinks are used to organize a large amount of information at the same Web site, and thus only point to pages in the same site. Other hyperlinks point to pages in other Web sites. Such out-going hyperlinks often indicate an implicit conveyance of authority to the pages being pointed to. Therefore, those pages that are pointed to by many other pages are likely to contain authoritative or quality information. Such linkages should obviously be used in page evaluation and ranking in search engines. During the period of 1997-1998, two most influential hyperlink based search algorithms PageRank [9, 52] and HITS [37] were designed. PageRank is the algorithm that powers the successful search engine Google. Both PageRank and HITS were originated from social network analysis [60]. They both exploit the hyperlink structure of the Web to rank pages according to their levels of “prestige” or “authority”. We will study these B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_7, © Springer-Verlag Berlin Heidelberg 2011 269 270 7 Social Network Analysis algorithms in this chapter. We should also note that hyperlink-based page evaluation and ranking is not the only method used by search engines. As we discussed in Chap. 6, contents and many other factors are also considered in producing the final ranking presented to the user. Apart from search ranking, hyperlinks are also useful for finding Web communities. A Web community is a cluster of densely linked pages representing a group of people with a common interest. Beyond explicit hyperlinks on the Web, explicit or implicit links in other contexts are useful too, e.g., for discovering communities of named entities (e.g., people and organizations) in free text documents and for analyzing social phenomena in emails and friendship networks on social networking sites. 7.1 Social Network Analysis Social network is the study of social entities (people in an organization, called actors), and their interactions and relationships. The interactions and relationships can be represented with a network or graph, where each vertex (or node) represents an actor and each link represents a relationship. From the network we can study the properties of its structure, and the role, position and prestige of each social actor. We can also find various kinds of sub-graphs, e.g., communities formed by groups of actors. Social network analysis is useful for the Web because the Web is essentially a virtual society, and thus a virtual social network, where each page can be regarded as a social actor and each hyperlink as a relationship. Many of the results from social networks can be adapted and extended for use in the Web context. The ideas from social network analysis are indeed instrumental to the success of Web search engines. In this section, we introduce two types of social network analysis, centrality and prestige, which are closely related to hyperlink analysis and search on the Web. Both centrality and prestige are measures of degree of prominence of an actor in a social network. We introduce them below. For a more complete treatment of the topics, please refer to the authoritative text by Wasserman and Faust [60]. 7.1.1 Centrality Important or prominent actors are those that are linked or involved with other actors extensively. In the context of an organization, a person with extensive contacts (links) or communications with many other people in the organization is considered more important than a person with relatively 7.1 Social Network Analysis 271 fewer contacts. The links can also be called ties. A central actor is one involved in many ties. Fig. 7.1 shows a simple example using an undirected graph. Each node in the social network is an actor and each link indicates that the actors on the two ends of the link communicate with each other. Intuitively, we see that the actor i is the most central actor because he/she can communicate with most other actors. Fig. 7.1. An example of a social network There are different types of links or involvements between actors. Thus, several types of centrality are defined on undirected and directed graphs. We discuss three popular types below. Degree Centrality Central actors are the most active actors that have most links or ties with other actors. Let the total number of actors in the network be n. Undirected Graph: In an undirected graph, the degree centrality of an actor i (denoted by CD(i)) is simply the node degree (the number of edges) of the actor node, denoted by d(i), normalized with the maximum degree, n1. .1 )()(  n idiCD (1) The value of this measure ranges between 0 and 1 as n1 is the maximum value of d(i). Directed Graph: In this case, we need to distinguish in-links of actor i (links pointing to i), and out-links (links pointing out from i). The degree centrality is defined based on only the out-degree (the number of out-links or edges), do(i). .1 )()('  n idiC o D (2) i 272 7 Social Network Analysis Closeness Centrality This view of centrality is based on the closeness or distance. The basic idea is that an actor xi is central if it can easily interact with all other actors. That is, its distance to all other actors is short. Thus, we can use the shortest distance to compute this measure. Let the shortest distance from actor i to actor j be d(i, j) (measured as the number of links in a shortest path). Undirected Graph: The closeness centrality CC(i) of actor i is defined as . ),( 1)( 1   n j C jid niC (3) The value of this measure also ranges between 0 and 1 as n1 is the minimum value of the denominator, which is the sum of the shortest distances from i to all other actors. Note that this equation is only meaningful for a connected graph. Directed Graph: The same equation can be used for a directed graph. The distance computation needs to consider directions of links or edges. Betweenness Centrality If two non-adjacent actors j and k want to interact and actor i is on the path between j and k, then i may have some control over their interactions. Betweenness measures this control of i over other pairs of actors. Thus, if i is on the paths of many such interactions, then i is an important actor. Undirected Graph: Let pjk be the number of shortest paths between actors j and k. The betweenness of an actor i is defined as the number of shortest paths that pass i (denoted by pjk(i), j  i and k  i) normalized by the total number of shortest paths of all pairs of actors not including i: .)()(    kj jk jk B p ipiC (4) Note that there may be multiple shortest paths between actor j and actor k. Some pass i and some do not. We assume that all paths are equally likely to be used. CB(i) has a minimum of 0, attained when i falls on no shortest path. Its maximum is (n1)(n2)/2, which is the number of pairs of actors not including i. In the network of Fig. 7.2, actor 1 is the most central actor. It lies on all 15 shortest paths linking the other 6 actors. CB(1) has the maximum value of 15, and CB(2) = CB(3) = CB(4) = CB(5) = CB(6) = CB(7) = 0. 7.1 Social Network Analysis 273 Fig. 7.2. An example of a network illustrating the betweenness centrality If we are to ensure that the value range is between 0 and 1, we can normalize it with (n1)(n2)/2, which is the maximum value of CB(i). The standardized betweenness of actor i is defined as )2)(1( )(2 )('    nn p ip iC kj jk jk B. (5) Unlike the closeness measure, the betweenness can be computed even if the graph is not connected. Directed Graph: The same equation can be used but must be multiplied by 2 because there are now (n1)(n2) pairs considering a path from j to k is different from a path from k to j. Likewise, pjk must consider paths from both directions. 7.1.2 Prestige Prestige is a more refined measure of prominence of an actor than centrality as we will see below. We need to distinguish between ties sent (out-links) and ties received (in-links). A prestigious actor is defined as one who is object of extensive ties as a recipient. In other words, to compute the prestige of an actor, we only look at the ties (links) directed or pointed to the actor (in-links). Hence, the prestige cannot be computed unless the relation is directional or the graph is directed. The main difference between the concepts of centrality and prestige is that centrality focuses on out-links while prestige focuses on in-links. We define three prestige measures. The third prestige measure (i.e., rank prestige) forms the basis of most Web page link analysis algorithms, including PageRank and HITS. 6 2 7 3 4 1 5 274 7 Social Network Analysis Degree Prestige Based on the definition of the prestige, it is clear that an actor is prestigious if it receives many in-links or nominations. Thus, the simplest measure of prestige of an actor i (denoted by PD(i)) is its in-degree, ,1 )()(  n idiPI D (6) where dI(i) is the in-degree of i (the number of in-links of i) and n is the total number of actors in the network. As in the degree centrality, dividing by n – 1 standardizes the prestige value to the range from 0 and 1. The maximum prestige value is 1 when every other actor links to or chooses actor i. Proximity Prestige The degree index of prestige of an actor i only considers the actors that are adjacent to i. The proximity prestige generalizes it by considering both the actors directly and indirectly linked to actor i. That is, we consider every actor j that can reach i, i.e., there is a directed path from j to i. Let Ii be the set of actors that can reach actor i, which is also called the influence domain of actor i. The proximity is defined as closeness or distance of other actors to i. Let d(j, i) denote the shortest path distance from actor j to actor i. Each link has the unit distance. To compute the proximity prestige, we use the average distance, which is ,|| ),( i Ij I ijd i   (7) where |Ii| is the size of the set Ii. If we look at the ratio or proportion of actors who can reach i to the average distance that these actors are from i, we obtain the proximity prestige, which has the value range of [0, 1]: , ||),( )1(||)( i Ij i PIijd nIiP i    (8) where |Ii|/(n1) is the proportion of actors that can reach actor i. In one extreme, every actor can reach actor i, which gives |Ii|/(n1) = 1. The denominator is 1 if every actor is adjacent to i. Then, PP(i) = 1. On the other extreme, no actor can reach actor i. Then |Ii| = 0, and PP(i) = 0. 7.2 Co-Citation and Bibliographic Coupling 275 Rank Prestige The above two prestige measures are based on in-degrees and distances. However, an important factor that has not been considered is the prominence of individual actors who do the “voting” or “choosing.” In the real world, a person i chosen by an important person is more prestigious than chosen by a less important person. For example, a company CEO voting for a person is much more important than a worker voting for the person. If one’s circle of influence is full of prestigious actors, then one’s own prestige is also high. Thus one’s prestige is affected by the ranks or statuses of the involved actors. Based on this intuition, the rank prestige PR(i) is defined as a linear combination of links that point to i: ),(...)2()1()( 21 nPAPAPAiPRniRiRiR  (9) where Aji = 1 if j points to i, and 0 otherwise. This equation says that an actor’s rank prestige is a function of the ranks of the actors who vote or choose the actor, which makes perfect sense. Since we have n equations for n actors, we can write them in the matrix notation. We use P to represent the vector that contains all the rank prestige values, i.e., P = (PR(1), PR(2), …, PR(n))T (T means matrix transpose). P is represented as a column vector. We use matrix A (where Aij = 1 if i points to j, and 0 otherwise) to represent the adjacency matrix of the network or graph. As a notational convention, we use bold italic letters to represent matrices. We then have PAPT . (10) This equation is precisely the characteristic equation used for finding the eigensystem of the matrix AT. P is an eigenvector of AT. This equation and the idea behind it turn out to be very useful in Web search. Indeed, the most well known ranking algorithms for Web search, PageRank and HITS, are directly related to this equation. Sect. 7.3 and 7.4 will focus on these two algorithms and describe how to solve the equation to obtain the prestige value of each actor (or each page on the Web). 7.2 Co-Citation and Bibliographic Coupling Another area of research concerned with links is the citation analysis of scholarly publications. A scholarly publication usually cites related prior work to acknowledge the origins of some ideas in the publication and to compare the new proposal with existing work. Citation analysis is an area 276 7 Social Network Analysis of bibliometric research, which studies citations to establish the relationships between authors and their work. When a publication (also called a paper) cites another publication, a relationship is established between the publications. Citation analysis uses these relationships (links) to perform various types of analysis. A citation can represent many types of links, such as links between authors, publications, journals and conferences, and fields, or even between countries. We will discuss two specific types of citation analysis, co-citation and bibliographic coupling. The HITS algorithm of Sect. 7.4 is related to these two types of analysis. 7.2.1 Co-Citation Co-citation is used to measure the similarity of two papers (or publications). If papers i and j are both cited by paper k, then they may be said to be related in some sense to each other, even though they do not directly cite each other. Fig. 7.3 shows that papers i and j are co-cited by paper k. If papers i and j are cited together by many papers, it means that i and j have a strong relationship or similarity. The more papers they are cited by, the stronger their relationship is. Fig. 7.3. Paper i and paper j are co-cited by paper k Let L be the citation matrix. Each cell of the matrix is defined as follows: Lij = 1 if paper i cites paper j, and 0 otherwise. Co-citation (denoted by Cij) is a similarity measure defined as the number of papers that co-cite i and j, and is computed with , 1    n k kjkiij LLC (11) where n is the total number of papers. Cii is naturally the number of papers that cite i. A square matrix C can be formed with Cij, and it is called the co- citation matrix. Co-citation is symmetric, Cij = Cji, and is commonly used as a similarity measure of two papers in clustering to group papers of similar topics together. i k j 7.3 PageRank 277 7.2.2 Bibliographic Coupling Bibliographic coupling operates on a similar principle, but in a way it is the mirror image of co-citation. Bibliographic coupling links papers that cite the same articles so that if papers i and j both cite paper k, they may be said to be related, even though they do not directly cite each other. The more papers they both cite, the stronger their similarity is. Fig. 7.4 shows both papers i and j citing (referencing) paper k. Fig. 7.4. Both paper i and paper j cite paper k We use Bij to represent the number of papers that are cited by both papers i and j: . 1    n k jkikij LLB (12) Bii is naturally the number of references (in the reference list) of paper i. A square matrix B can be formed with Bij, and it is called the bibliographic coupling matrix. Bibliographic coupling is also symmetric and is regarded as a similarity measure of two papers in clustering. We will see later that two important types of pages on the Web, hubs and authorities, found by the HITS algorithm are directly related to co- citation and bibliographic coupling matrices. 7.3 PageRank The year 1998 was an important year for Web link analysis and Web search. Both the PageRank and the HITS algorithms were reported in that year. HITS was presented by Jon Kleinberg in January, 1998 at the Ninth Annual ACM-SIAM Symposium on Discrete Algorithms. PageRank was presented by Sergey Brin and Larry Page at the Seventh International World Wide Web Conference (WWW7) in April, 1998. Based on the algorithm, they built the search engine Google. The main ideas of PageRank and HITS are really quite similar. However, it is their dissimilarity that made a i k j 278 7 Social Network Analysis huge difference as we will see later. Since that year, PageRank has emerged as the dominant link analysis model for Web search, partly due to its query-independent evaluation of Web pages and its ability to combat spamming, and partly due to Google’s business success. In this section, we focus on PageRank. In the next section, we discuss HITS. A detailed study of these algorithms can also be found in [40]. PageRank relies on the democratic nature of the Web by using its vast link structure as an indicator of an individual page's quality. In essence, PageRank interprets a hyperlink from page x to page y as a vote, by page x, for page y. However, PageRank looks at more than just the sheer number of votes or links that a page receives. It also analyzes the page that casts the vote. Votes casted by pages that are themselves “important” weigh more heavily and help to make other pages more “important.” This is exactly the idea of rank prestige in social networks (see Sect. 7.1.2). 7.3.1 PageRank Algorithm PageRank is a static ranking of Web pages in the sense that a PageRank value is computed for each page off-line and it does not depend on search queries. Since PageRank is based on the measure of prestige in social networks, the PageRank value of each page can be regarded as its prestige. We now derive the PageRank formula. Let us first state some main concepts again in the Web context. In-links of page i: These are the hyperlinks that point to page i from other pages. Usually, hyperlinks from the same site are not considered. Out-links of page i: These are the hyperlinks that point out to other pages from page i. Usually, links to pages of the same site are not considered. From the perspective of prestige, we use the following to derive the PageRank algorithm. 1. A hyperlink from a page pointing to another page is an implicit conveyance of authority to the target page. Thus, the more in-links that a page i receives, the more prestige the page i has. 2. Pages that point to page i also have their own prestige scores. A page with a higher prestige score pointing to i is more important than a page with a lower prestige score pointing to i. In other words, a page is important if it is pointed to by other important pages. According to rank prestige in social networks, the importance of page i (i’s PageRank score) is determined by summing up the PageRank scores of all pages that point to i. Since a page may point to many other pages, its 7.3 PageRank 279 prestige score should be shared among all the pages that it points to. Notice the difference from rank prestige, where the prestige score is not shared. To formulate the above ideas, we treat the Web as a directed graph G = (V, E), where V is the set of vertices or nodes, i.e., the set of all pages, and E is the set of directed edges in the graph, i.e., hyperlinks. Let the total number of pages on the Web be n (i.e., n = |V|). The PageRank score of the page i (denoted by P(i)) is defined by: ,)()( ),(    Eij jO jPiP (13) where Oj is the number of out-links of page j. Mathematically, we have a system of n linear equations (13) with n unknowns. We can use a matrix to represent all the equations. Let P be a n-dimensional column vector of PageRank values, i.e., P = (P(1), P(2), …, P(n))T. Let A be the adjacency matrix of our graph with     otherwise0 ),(if1 EjiOA iij (14) We can write the system of n equations with (similar to Equation 10) PAPT . (15) This is the characteristic equation of the eigensystem, where the solution to P is an eigenvector with the corresponding eigenvalue of 1. Since this is a circular definition, an iterative algorithm is used to solve it. It turns out that if some conditions are satisfied (which will be described shortly), 1 is the largest eigenvalue and the PageRank vector P is the principal eigenvector. A well known mathematical technique called power iteration can be used to find P. However, the problem is that Equation (15) does not quite suffice because the Web graph does not meet the conditions. To introduce these conditions and the enhanced equation, let us derive the same Equation (15) based on the Markov chain [32]. In the Markov chain model, each Web page or node in the Web graph is regarded as a state. A hyperlink is a transition, which leads from one state to another state with a probability. Thus, this framework models Web surfing as a stochastic process. It models a Web surfer randomly surfing 280 7 Social Network Analysis the Web as a state transition in the Markov chain. Recall that we used Oi to denote the number of out-links of a node i. Each transition probability is 1/Oi if we assume the Web surfer will click the hyperlinks in the page i uniformly at random, the “back” button on the browser is not used and the surfer does not type in an URL. Let A be the state transition probability matrix, a square matrix of the following format,                    nnnn n n AAA AAA AAA ... ... ... ... ... ... . 21 22221 11211 A Aij represents the transition probability that the surfer in state i (page i) will move to state j (page j). Aij is defined exactly as in Equation (14). Given an initial probability distribution vector that a surfer is at each state (or page) p0 = (p0(1), p0(2), …, p0(n))T (a column vector) and an nn transition probability matrix A, we have    n i ip 1 0 1)( (16)    n j ijA 1 1. (17) Equation (17) is not quite true for some Web pages because they have no out-links. If the matrix A satisfies Equation (17), we say that A is the stochastic matrix of a Markov chain. Let us assume A is a stochastic matrix for the time being and deal with it not being that later. In a Markov chain, a question of common interest is: Given the initial probability distribution p0 at the beginning, what is the probability that m steps/transitions later that the Markov chain will be at each state j? We can determine the probability that the system (or the random surfer) is in state j after 1 step (1 state transition) by using the following reasoning: ,)()1()( 1 01    n i ij ipAjp (18) where Aij(1) is the probability of going from i to j in 1 transition, and Aij(1) = Aij. We can write it with a matrix: 7.3 PageRank 281 0pAp T1 . (19) In general, the probability distribution after k steps/transitions is: 1-kk pAp T . (20) Equation (20) looks very similar to Equation (15). We are getting there. By the Ergodic Theorem of Markov chains [32], a finite Markov chain defined by the stochastic transition matrix A has a unique stationary probability distribution if A is irreducible and aperiodic. These mathematical terms will be defined as we go along. The stationary probability distribution means that after a series of transitions pk will converge to a steady-state probability vector  regardless of the choice of the initial probability vector p0, i.e., πp   kk lim . (21) When we reach the steady-state, we have pk = pk+1 =, and thus  =AT.  is the principal eigenvector of AT with eigenvalue of 1. In PageRank,  is used as the PageRank vector P. Thus, we again obtain Equation (15), which is re-produced here as Equation (22): PAP T . (22) Using the stationary probability distribution  as the PageRank vector is reasonable and quite intuitive because it reflects the long-run probabilities that a random surfer will visit the pages. A page has a high prestige if the probability of visiting it is high. Now let us come back to the real Web context and see whether the above conditions are satisfied, i.e., whether A is a stochastic matrix and whether it is irreducible and aperiodic. In fact, none of them is satisfied. Hence, we need to extend the ideal-case Equation (22) to produce the “actual PageRank model”. Let us look at each condition below. First of all, A is not a stochastic (transition) matrix. A stochastic matrix is the transition matrix for a finite Markov chain whose entries in each row are non-negative real numbers and sum to 1 (i.e., Equation 17). This requires that every Web page must have at least one out-link. This is not true on the Web because many pages have no out-links, which are reflected in transition matrix A by some rows of complete 0’s. Such pages are called the dangling pages (nodes). Example 1: Fig. 7.5 shows an example of a hyperlink graph. 282 7 Social Network Analysis Fig. 7.5. An example of a hyperlink graph If we assume that the Web surfer will click the hyperlinks in a page uniformly at random, we have the following transition probability matrix:                    02121000 000000 313103100 000010 00021021 00021210 A. (23) For example A12 = A13 = 1/2 because node 1 has two out-links. We can see that A is not a stochstic matrix because the fifth row is all 0’s, i.e., page 5 is a dangling page. ▀ We can fix this problem in several ways in order to convert A to a stochastic transition matrix. We describe only two ways here: 1. Remove those pages with no out-links from the system during the PageRank computation as these pages do not affect the ranking of any other page directly. Out-links from other pages pointing to these pages are also removed. After PageRanks are computed, these pages and hyperlinks pointing to them can be added in. Their PageRanks are easy to calculate based on Equation (22). Note that the transition probabilities of those pages with removed links will be slightly affected but not significantly. This method is suggested in [9]. 2. Add a complete set of outgoing links from each such page i to all the pages on the Web. Thus the transition probability of going from i to every page is 1/n assuming uniform probability distribution. That is, we replace each row containing all 0’s with e/n, where e is n-dimensional vector of all 1’s. If we use the second method to make A a stochastic matrix by adding a link from page 5 to every page, we obtain 3 4 6 1 2 5 7.3 PageRank 283                    02121000 616161616161 313103100 000010 00021021 00021210 A . (24) Below, we assume that either one of the above is done to make A a stochastic matrix. Second, A is not irreducible. Irreducible means that the Web graph G is strongly connected. Definition (strongly connected): A directed graph G = (V, E) is strongly connected if and only if, for each pair of nodes u, v ∈ V, there is a path from u to v. A general Web graph represented by A is not irreducible because for some pair of nodes u and v, there is no path from u to v. For example, in Fig. 7.5, there is no directed path from node 3 to node 4. The adjustment in Equation (24) is not enough to ensure irreducibility. That is, in A , there is still no directed path from node 3 to node 4. This problem and the next problem can be dealt with using a single strategy (to be described shortly). Finally, A is not aperiodic. A state i in a Markov chain being periodic means that there exists a directed cycle that the chain has to traverse. Definition (aperiodic): A state i is periodic with period k > 1 if k is the smallest number such that all paths leading from state i back to state i have a length that is a multiple of k. If a state is not periodic (i.e., k = 1), it is aperiodic. A Markov chain is aperiodic if all states are aperiodic. Example 2: Fig. 7.6 shows a periodic Markov chain with k = 3. The transition matrix is given on the left. Each state in this chain has a period of 3. For example, if we start from state 1, to come back to state 1 the only path is 1-2-3-1 for some number of times, say h. Thus any return to state 1 will take 3h transitions. In the Web, there could be many such cases. ▀ Fig. 7.6. A periodic Markov chain with k = 3. 1            001 100 010 A 1 1 2 1 3 284 7 Social Network Analysis It is easy to deal with the above two problems with a single strategy.  We add a link from each page to every page and give each link a small transition probability controlled by a parameter d. The augmented transition matrix becomes irreducible because it is clearly strongly connected. It is also aperiodic because the situation in Fig. 7.6 no longer exists as we now have paths of all possible lengths from state i back to state i. That is, the random surfer does not have to traverse a fixed cycle for any state. After this augmentation, we obtain an improved PageRank model. In this model, at a page, the random surfer has two options: 1. With probability d, he randomly chooses an out-link to follow. 2. With probability 1d, he jumps to a random page without a link. Equation (25) gives the improved model, PAEP       Tdnd)1( (25) where E is eeT (e is a column vector of all 1’s) and thus E is a nn square matrix of all 1’s. 1/n is the probability of jumping to a particular page. n is the total number of nodes in the Web graph. Note that Equation (25) assumes that A has already been made a stochastic matrix. Example 3: If we follow our example in Fig. 7.5 and Equation (24) (we use A for A here), the augmented transition matrix is                    061610619061061061 157610619061061061 15761061061061061 061610619061157157 061610611211061157 06161061061157061 )1( Tdnd AE (26) ▀ (1d)E/n + dAT is a stochastic matrix (but transposed). It is also irreducible and aperiodic as we discussed above. Here we use d = 0.9. If we scale Equation (25) so that eTP = n, we obtain PAeP Tdd  )1( . (27) Before scaling, we have eTP = 1 (i.e., P(1) + P(2) + … + P(n) = 1 if we recall that P is the stationary probability vector  of the Markov chain). The scaling is equivalent to multiplying n on both sides of Equation (25). 7.3 PageRank 285 This gives us the PageRank formula for each page i as follows:    n j ji jPAddiP 1 ),()1()( (28) which is equivalent to the formula given in the PageRank papers [9, 52]:    Eij jO jPddiP ),( )()1()( . (29) The parameter d is called the damping factor which can be set to between 0 and 1. d = 0.85 is used in [9, 52]. The computation of PageRank values of the Web pages can be done using the well known power iteration method [31], which produces the principal eigenvector with the eigenvalue of 1. The algorithm is simple, and is given in Fig. 7.7. One can start with any initial assignments of PageRank values. The iteration ends when the PageRank values do not change much or converge. In Fig. 7.7, the iteration ends after the 1-norm of the residual vector is less than a pre-specified threshold . Note that the 1-norm for a vector is simply the sum of all the components. PageRank-Iterate(G) P0  e/n k  1 repeat ;)1( 1-k T k dd PAeP  k  k + 1; until ||Pk – Pk-1||1 <  return Pk Fig. 7.7. The power iteration method for PageRank Since we are only interested in the ranking of the pages, the actual convergence may not be necessary. Thus, fewer iterations are needed. In [9], it is reported that on a database of 322 million links the algorithm converges to an acceptable tolerance in roughly 52 iterations. 7.3.2 Strengths and Weaknesses of PageRank The main advantage of PageRank is its ability to fight spam. A page is important if the pages pointing to it are important. Since it is not easy for Web page owner to add in-links into his/her page from other important pages, it is thus not easy to influence PageRank. Nevertheless, there are 286 7 Social Network Analysis reported ways to influence PageRank. Recognizing and fighting spam is an important issue in Web search. Another major advantage of PageRank is that it is a global measure and is query independent. That is, the PageRank values of all the pages on the Web are computed and saved off-line rather than at the query time. At the query time, only a lookup is needed to find the value to be integrated with other strategies to rank the pages. It is thus very efficient at the query time. Both these two advantages contributed greatly to Google’s success. The main criticism is also the query-independence nature of PageRank. It could not distinguish between pages that are authoritative in general and pages that are authoritative on the query topic. Google may have other ways to deal with the problem, which we do not know due to the proprietary nature of the Google’s search algorithm. Another criticism is that PageRank does not consider time, which we discuss in the next sub-section. Finally, we note again that the link-based ranking is not the only strategy used in a search engine. Many other information retrieval methods, heuristics, and empirical parameters are also employed. However, their details are not published. We should also note that PageRank is not the only link-based static and global ranking algorithm. All major search engines, such as Bing and Yahoo!, have their own algorithms. Researchers also proposed other ranking methods that are not based on links, e.g., BrowseRank [45], which is based on a browsing graph built from the user search log. 7.3.3 Timed PageRank and Recency Search The Web is a dynamic environment. It changes constantly. Quality pages in the past may not be quality pages now or in the future. Apart from pages that contain well-established facts and classics which do not change significantly over time, most contents on the Web change constantly. New pages or contents are added, and ideally, outdated contents and pages are deleted. However, in practice, many outdated pages and links are not deleted. This causes problems for Web search because such outdated pages may still be ranked high. Furthermore, the Web also has a huge number of time sensitive content pages, e.g., news articles, current events pages, and research publications. Thus, search has a temporal dimension. However, PageRank favors pages that have many in-links. To some extent, we can say that it favors older pages because they have existed on the Web for a long time and thus have accumulated many in-links. Then the problem is that new pages, which are of high quality and also give the up-to-date information, will not be assigned high scores and consequently will not be ranked high because they have fewer or no in-links. It is thus difficult for users to find the latest information on the Web based on PageRank. 7.3 PageRank 287 Researchers have addressed this problem (called recency search). Here, we only describe a time-sensitive ranking algorithm called TS-Rank [43], which is an extension to PageRank. The idea of TS-Rank is simple. Instead of using a constant damping factor d as a parameter in PageRank, TS-Rank uses a function of time f(ti) (0 ≤ f(ti) ≤ 1) to achieve the time sensitive purpose, where ti is the difference between the current time and the time when page i was created or last updated. f(ti) returns a probability that the random surfer will follow an actual link on the page. 1  f(ti) returns the probability that the surfer will jump to a random page. Note that the same trick as in PageRank of adding artificial links to pages is still used. Thus, at a particular page i, the surfer can take one of the two actions: 1. With probability f(ti), he randomly chooses an out-going link to follow. 2. With probability 1  f(ti), he jumps to a random page without a link. The intuition here is that if the page was created or last updated a long time ago, then the pages that it points to are even older and are probably out of date. Then the 1  f(ti) value for the page should be large, which means that the surfer will have a high probability of jumping to a random page. If a page is new, then its 1  f(ti) value should be small, which means that the surfer will have a high probability to follow an out-link of the page and a small probability of jumping to a random page. Note that in this formulation, the age of the page being pointed to is irrelevant. With this augmentation, the non-matrix form of Equation (15) becomes: ),()()(1)( 1 jPAtfn tfiPT n j jij j T          (30) where PT(i) is the TS-Rank value of page i, n is the total number of pages, and 1/n is the probability of going to a random page j. In the matrix form, Equation (30) is:   ,T TTPHFP  (31) where F and H are both n  n square matrices defined by ,)(1 n tfF i ij  (32) . otherwise0 ),( if)(     EjiO tf H i i ij (33) It is easy to show that (F + H) is a stochastic transition matrix of the augmented Markov chain. (F + H) is also irreducible and aperiodic 288 7 Social Network Analysis because of the random jump links. Like PageRank, the Markov chain defined by (F + H) thus also has a unique stationary probability distribution regardless of the choice of the initial probability of the random surfer being at each state/page. The stationary probability distribution is the final TS-Rank (column) vector PT. To solve Equation (31), we still use the power iteration method. Note that if f(ti) is a constant between 0 and 1 for every page, TS-Rank becomes PageRank. As for how to define f(ti), it is application dependent. For different applications, different functions may be needed depending on how the time affects the domains. For an application of publication research in [44], the following exponential decay function was used: .5.0)( 3it itf  (34) For a complete new page in a Web site, which has few or no in-links, we can use the average TS-Rank value of the past pages of the site, which represents the reputation of the site. 7.4 HITS HITS stands for Hypertext Induced Topic Search [37]. Unlike PageRank which is a static ranking algorithm, HITS is search query dependent. When the user issues a search query, HITS first expands the list of relevant pages returned by a search engine and then produces two rankings of the expanded set of pages, authority ranking and hub ranking. An authority is a page with many in-links. The idea is that the page may have authoritative content on some topic and thus many people trust it and thus link to it. A hub is a page with many out-links. The page serves as an organizer of the information on a particular topic and points to many good authority pages on the topic. When a user comes to this hub page, he/she will find many useful links which take him/her to good content pages on the topic. Fig. 7.8 shows an authority page and a hub page. The key idea of HITS is that a good hub points to many good authorities and a good authority is pointed to by many good hubs. Thus, authorities and hubs have a mutual reinforcement relationship. Fig. 7.9 shows a set of densely linked authorities and hubs (a bipartite sub-graph). Below, we first present the HITS algorithm, and also make a connection between HITS and co-citation and bibliographic coupling in bibliometric research. We then discuss the strengths and weaknesses of HITS, and describe some possible ways to deal with its weaknesses. 7.4 HITS 289 Fig. 7.8. An authority page and a hub page Fig. 7.9. A densely linked set of authorities and hubs 7.4.1 HITS Algorithm Before describing the HITS algorithm, let us first describe how HITS collects pages to be ranked. Given a broad search query, q, HITS collects a set of pages as follows: 1. It sends the query q to a search engine system. It then collects t (t = 200 is used in the HITS paper) highest ranked pages, which assume to be highly relevant to the search query. This set is called the root set W. 2. It then grows W by including any page pointed to by a page in W and any page that points to a page in W. This gives a larger set called S. However, this set can be very large. The algorithm restricts its size by allowing each page in W to bring at most k pages (k = 50 is used in the HITS paper) pointing to it into S. The set S is called the base set. HITS then works on the pages in S, and assigns every page in S an authority score and a hub score. Let the number of pages to be studied be n. We again use G = (V, E) to denote the (directed) link graph of S. V is the set of pages (or nodes) and E is the set of directed edges (or links). We use L to denote the adjacency matrix of the graph. An authority A hub Authorities Hubs 290 7 Social Network Analysis     otherwise0 ),(if1 EjiLij (35) Let the authority score of the page i be a(i), and the hub score of page i be h(i). The mutual reinforcing relationship of the two scores is represented as follows:    Eij jhia ),( )()( (36)    Eji jaih ),( )()( (37) Writing them in the matrix form, we use a to denote the column vector with all the authority scores, a = (a(1), a(2), …, a(n))T, and use h to denote the column vector with all the hub scores, h = (h(1), h(2), …, h(n))T, a = LTh (38) h = La (39) The computation of authority scores and hub scores is basically the same as the computation of the PageRank scores using the power iteration method. If we use ak and hk to denote authority and hub scores at the kth iteration, the iterative processes for generating the final solutions are ak = LTLak1 (40) hk = LLThk1 (41) starting with a0 = h0 = (1, 1, …, 1). (42) Note that Equation (40) (or Equation 41) does not use the hub (or authority) vector due to substitutions of Equation (38) and Equation (39). After each iteration, the values are also normalized (to keep them small) so that    n i ia 1 1)( (43)    n i ih 1 1)( (44) 7.4 HITS 291 The power iteration algorithm for HITS is given in Fig. 7.10. The iteration ends after the 1-norms of the residual vectors are less than some thresholds a and h. Hence, the algorithm finds the principal eigenvectors at “equilibrium” as in PageRank. The pages with large authority and hub scores are better authorities and hubs respectively. HITS will select a few top ranked pages as authorities and hubs, and return them to the user. Although HITS will always converge, there is a problem with uniqueness of limiting (converged) authority and hub vectors. It is shown that for certain types of graphs, different initializations to the power method produce different final authority and hub vectors. Some results can be inconsistent or wrong. Farahat et al. [23] gave several examples. The heart of the problem is that there are repeated dominant (principal) eigenvalues (several eigenvalues are the same and are dominant eigenvalues), which are caused by the problem that LTL (respectively LLT) is reducible [39, 40]. The first PageRank solution (Equation (22)) has the same problem. However, the PageRank inventors found a way to get around the problem. A modification similar to PageRank may be applied to HITS. 7.4.2 Finding Other Eigenvectors The HITS algorithm given in Fig. 7.10 finds the principal eigenvectors, which in a sense represent the most densely connected authorities and hubs in the graph G defined by a query. However, in some cases, we may also be interested in finding several densely linked collections of hubs and authorities among the same base set of pages. Each of such collections HITS-Iterate(G) a0  h0  (1, 1, …, 1); k  1 Repeat ;1 k T k LaLa ;1 k T k hLLh ak  ak /||ak||1; // normalization hk  hk /||hk||1; // normalization k  k + 1; until ||ak – ak-1||1 < a and ||hk – hk-1||1 < h; return ak and hk Fig. 7.10. The HITS algorithm based on power iteration 292 7 Social Network Analysis could potentially be relevant to the query topic, but they could be well- separated from one another in the graph G for a variety of reasons. For example, 1. The query string may be ambiguous with several very different meanings, e.g., “jaguar”, which could be a cat or a car. 2. The query string may represent a topic that may arise as a term in the multiple communities, e.g. “classification”. 3. The query string may refer to a highly polarized issue, involving groups that are not likely to link to one another, e.g. “abortion”. In each of these examples, the relevant pages can be naturally grouped into several clusters, also called communities. In general, the top ranked authorities and hubs represent the major cluster (or community). The smaller clusters (or communities), which are also represented by bipartite sub-graphs as that in Fig. 7.9, can be found by computing non-principal eigenvectors. Non-principal eigenvectors are calculated in a similar way to power iteration using methods such as orthogonal iteration and QR iteration. We will not discuss the details of these methods. Interested readers can refer to the book by Golub and Van Loan [31]. 7.4.3 Relationships with Co-Citation and Bibliographic Coupling Authority pages and hub pages have their matches in the bibliometric citation context. An authority page is like an influential research paper (publication) which is cited by many subsequent papers. A hub page is like a survey paper which cites many other papers (including those influential papers). It is no surprise that there is a connection between authority and hub, and co-citation and bibliographic coupling. Recall that co-citation of pages i and j, denoted by Cij, is computed as ij T n k kjkiij LLC)( 1 LL   . (45) This shows that the authority matrix (LTL) of HITS is in fact the co- citation matrix C in the Web context. Likewise, recall that bibliographic coupling of two pages i and j, denoted by Bij, is computed as ,)( 1 ij T n k jkikij LLB LL   (46) 7.4 HITS 293 which shows that the hub matrix (LLT) of HITS is the bibliographic coupling matrix B in the Web context. 7.4.4 Strengths and Weaknesses of HITS The main strength of HITS is its ability to rank pages according to the query topic, which may be able to provide more relevant authority and hub pages. The ranking may also be combined with information retrieval based rankings. However, HITS has several disadvantages.  First of all, it does not have the anti-spam capability of PageRank. It is quite easy to influence HITS by adding out-links from one’s own page to point to many good authorities. This boosts the hub score of the page. Because hub and authority scores are interdependent, it in turn also increases the authority score of the page.  Another problem of HITS is topic drift. In expanding the root set, it can easily collect many pages (including authority pages and hub pages) which have nothing to do the search topic because out-links of a page may not point to pages that are relevant to the topic and in-links to pages in the root set may be irrelevant as well because people put hyperlinks for all kinds of reasons, including spamming.  The query time evaluation is also a major drawback. Getting the root set, expanding it and then performing eigenvector computation are all time consuming operations. Over the years, many researchers tried to deal with these problems. We briefly discuss some of them below. It was reported by several researchers in [7, 41, 49] that small changes to the Web graph topology can significantly change the final authority and hub vectors. Minor perturbations have little effect on PageRank, which is more stable than HITS. This is essentially due to the random jump step of PageRank. Ng et al. [49] proposed a method by introducing the same random jump step to HITS (by jumping to the base set uniformly at random with probability d), and showed that it could improve the stability of HITS significantly. Lempel and Moran [41] proposed SALSA, a stochastic algorithm for link structure analysis. SALSA combines some features of both PageRank and HITS to improve the authority and hub computation. It casts the problem as two Markov chains, an authority Markov chain and a hub Markov chain. SALSA is less susceptible to spam since the coupling between hub and authority scores is much less strict. Bharat and Henzinger [7] proposed a simple method to fight two site nepotistic links. That means that a set of pages on one host points to a 294 7 Social Network Analysis single page on a second host. This drives up the hub scores of the pages on the first host and the authority score of the page on the second host. A similar thing can be done for hubs. These links may be authored by the same person and thus are regarded as “nepotistic” links to drive up the ranking of the target pages. [7] suggests weighting the links to deal with this problem. That is, if there are k edges from documents on a first host to a single document on a second host we give each edge an authority weight of 1/k. If there are l edges from a single page on a first host to a set of pages on a second host, we give each edge a hub weight of 1/l. These weights are used in the authority and hub computation. There are much more sophisticated spam techniques now involving more than two sites. Regarding the topic drifting of HITS, existing fixes are mainly based on content similarity comparison during the expansion of the root set. In [12], if an expanded page is too different from the pages in the root set in terms of content similarity (based on cosine similarity), it is discarded. The remaining links are also weighted according to similarity. [12] proposes a method that uses the similarity between the anchor text of a link and the search topic to weight the link (instead of giving each link 1 as in HITS). [11] goes further to segment the page based on the DOM (Document Object Model) tree structure to identify the blocks or subtrees that are more related to the query topic instead of regarding the whole page as relevant to the search query. This is a good way to deal with multi-topic pages, which are abundant on the Web. A recent work on this is block- based link analysis [10], which segments each Web page into different blocks. Each block is given a different importance value according to its location in the page and other information. The importance value is then used to weight the links in the HITS (and also PageRank) computation. This will reduce the impact of unimportant links, which usually cause topic drifting and may even be a link spam. 7.5 Community Discovery Intuitively, a community is simply a group of entities (e.g., people or organizations) that shares a common interest or is involved in an activity or event. In Sect. 7.4.2, we showed that the HITS algorithm can be used to find communities. The communities are represented by dense bipartite sub-graphs. We now describe several other community finding algorithms. Apart from the Web, communities also exist in emails and text documents. This section describes two community finding algorithms for the Web, one community finding algorithm for emails, and one community finding algorithm for text documents. 7.5 Community Discovery 295 There are many reasons for discovering communities. For example, in the context of the Web, Kumar et al. [38] listed three reasons: 1. Communities provide valuable and possibly the most reliable, timely, and up-to-date information resources for a user interested in them. 2. They represent the sociology of the Web: studying them gives insights into the evolution of the Web. 3. They enable target advertising at a very precise level. 7.5.1 Problem Definition Definition (community): Given a finite set of entities S = {s1, s2, …, sn} of the same type, a community is a pair C = (T, G), where T is the community theme and G  S is the set of all entities in S that shares the theme T. If si  G, si is said to be a member of the community C. Some remarks about this definition are in order:  A theme defines a community. That is, given a theme T, the set of members of the community is uniquely determined. Thus, two communities are equal if they have the same theme.  A theme can be defined arbitrarily. For example, it can be an event (e.g., a sport event or a scandal) or a concept (e.g., Web mining).  An entity si in S can be in any number of communities. That is, communities may overlap, or multiple communities may share members.  The entities in S are of the same type. For example, this definition does not allow people and organizations to be in the same community.  By no means does this definition cover every aspect of communities in the real world. For example, it does not consider the temporal dimension of communities. Usually a community exists within a specific period of time. Similarly, an entity may belong to a community during some time periods.  This is a conceptual definition. In practice, different community mining algorithms have their own operational definitions which usually depend on how communities manifest themselves in the given data (which we will discuss shortly). Furthermore, the algorithms may not be able to discover all the members of a community or its precise theme. Communities may also have hierarchical structures. Definition (sub-community, super-community, and sub-theme): A community (T, G) may have a set of sub-communities {(T1, G1), …, 296 7 Social Network Analysis (Tm, Gm)}, where Ti is a sub-theme of T and Gi  G. (T, G) is also called a super-community of (Ti, Gi). In the same way, each sub-community (Ti, Gi) can be further decomposed, which gives us a community hierarchy. Community Manifestation in Data: Given a data set, which can be a set of Web pages, a collection of emails, or a set of text documents, we want to find communities of entities in the data. However, the data itself usually does not explicitly give us the themes or the entities (community members) associated with the themes. The system needs to discover the hidden community structures. Thus, the first issue that we need to know is how communities manifest themselves. From such manifested evidences, the system can discover possible communities. Different types of data may have different forms of manifestation. We give three examples. Web Pages: 1. Hyperlinks: A group of content creators sharing a common interest is usually inter-connected through hyperlinks. That is, members in a community are more likely to be connected among themselves than outside the community. 2. Content words: Web pages of a community usually contain words that are related to the community theme. Emails: 1. Email exchange between entities: Members of a community are more likely to communicate with one another. 2. Content words: Email contents of a community also contain words related to the theme of the community. Text documents: 1. Co-occurrence of entities: Members of a community are more likely to appear together in the same sentence and/or the same document. 2. Content words: Words in sentences indicate the community theme. Clearly, the key form of manifestation of a community is that its members are linked in some way. The associated text often contains words that are indicative of the community theme. Objective of Community Discovery: Given a data set containing entities, we want to discover hidden communities of the entities. For each community, we want to find the theme and its members. The theme is usually represented with a set of keywords. 7.5 Community Discovery 297 7.5.2 Bipartite Core Communities HITS finds dense bipartite graph communities based on broad topic queries. The question is whether it is possible to find all such communities efficiently from the crawl of the whole Web without using eigenvector computation which is relatively inefficient. Kumar et al. [38] presented a technique for finding bipartite cores, which are defined as follows. Recall that the node set of a bipartite graph can be partitioned into two subsets, which we denote as set F and set C. A bipartite core is a complete bipartite sub-graph with at least i nodes in F and at least j nodes in C. A complete bipartite graph on node sets F and C contains all possible edges between the vertices of F and the vertices of C. Note that edges within F or within C are allowed here to suit the Web context, which deviate from the traditional definition of a complete bipartite graph. Intuitively, the core is a small (i, j)-sized complete bipartite sub-graph of the community, which contains some core members of the community but not all. The cores that we seek are directed, i.e., there is a set of i pages all of which link to a set of j pages, while no assumption is made of links out of the latter set of j pages. Intuitively, the former is the set of pages created by members of the community, pointing to what they believe are the most valuable pages for that community. For this reason we will refer to the i pages that contain the links as fans, and the j pages that are referenced as centers (as in community centers). Fans are like specialized hubs, and centers are like authorities. Fig. 7.11 shows an example of a bipartite core. Fig. 7.11. A (4, 3) bipartite core In Fig. 7.11, each fan page links to every center page. Since there are four fans and three centers, this is called a (4, 3) bipartite core. Such a core almost certainly represents a Web community, but a community may have multiple bipartite cores. Given a large number of pages crawled from the Web, which is represented as a graph, the procedure for finding bipartite cores consists of two major steps: pruning and core generation. 4 Fans 3 Centers 298 7 Social Network Analysis Step 1: Pruning We describe two types of pruning to remove those unqualified pages to be fans or centers. There are also other pruning methods given in [38]. 1. Pruning by in-degree: we can delete all pages that are very highly referenced (linked) on the Web, such as homepages of Web portals (e.g., Yahoo!, AOL, etc). These pages are referenced for a variety of reasons, having little to do with any single emerging community, and they can be safely deleted. That is, we delete pages with the number of in-links great than k, which is determined empirically (k = 50 in [38]). 2. Iterative pruning of fans and centers: If we are interested in finding (i, j) cores, clearly any potential fan with an out-degree smaller than j can be pruned and the associated edges deleted from the graph. Similarly, any potential center with an in-degree smaller than i can be pruned and the corresponding edges deleted from the graph. This process can be done iteratively: when a fan gets pruned, some of the centers that it points to may have their in-degrees fall below the threshold i and qualify for pruning as a result. Similarly, when a center gets pruned, a fan that points to it could have its out-degree fall below its threshold of j and qualify for pruning. Step 2: Generating all (i, j) Cores After pruning, the remaining pages are used to discover cores. The method works as follows: Fixing j, we start with all (1, j) cores. This is simply the set of all vertices with out-degree at least j. We then construct all (2, j) cores by checking every fan which also points to any center in a (1, j) core. All (3, j) cores can be found in the same fashion by checking every fan which points to any center in a (2, j) core, and so on. The idea is similar to the Apriori algorithm for association rule mining (see Chap. 2) as every proper subset of the fans in any (i, j) core forms a core of smaller size. Based on the algorithm, Kumar et al. found a large number of topic coherent cores from a crawl of the Web [38]. We note that this algorithm only finds the core pages of the communities, not all members (pages). It also does not find the themes of the communities or their hierarchical organizations. 7.5.3 Maximum Flow Communities Bipartite cores are usually very small and do not represent full communities. In this section, we define and find maximum flow communities based on the work of Flake et al. [24, 25]. The algorithm requires the user to give a 7.5 Community Discovery 299 set of seed pages, which are examples of the community that the user wishes to find. Given a Web link graph G = (V, E), a maximum flow community is defined as a collection C  V of Web pages such that each member page u  C has more hyperlinks (in either direction) within the community C than outside of the community V-C. Identifying such a community is intractable in the general case because it can be mapped into a family of NP-complete graph partition problems. Thus, we need to approximate and recast it into a framework with less stringent conditions based on the network flow model from operations research, specifically the maximum flow model. The maximum flow model can be stated as follows: We are given a graph G = (V, E), where each edge (u, v) is thought of as having a positive capacity c(u, v) that limits the quantity of a product that may be shipped through the edge. In such a situation, it is often desirable to have the maximum amount of flow from a starting point s (called the source) and a terminal point t (called the sink). Intuitively, the maximum flow of the graph is determined by the bottleneck edges. For example, given the graph in Fig. 7.12 with the source s and the sink t, if every edge has the unit capacity, the bottleneck edges are W-X and Y-Z. Fig. 7.12. A simple flow network. The Max Flow-Min Cut theorem of Ford and Fulkerson [26] proves that the maximum flow of a network is identical to the minimum cut that separates s and t. Many polynomial time algorithms exist for solving the s- t maximum flow problem. If Fig. 7.12 is a Web link graph, it is natural to cut the edges W-X and Y-Z to produce two Web communities. The basic idea of the approach in [26] is as follows: It starts with a set S of seed pages, which are example pages of the community that the user wishes to find. The system then crawls the Web to find more pages using the seed pages. A maximum flow algorithm is then applied to separate the community C involving the seed pages and the other pages. These steps may need to be repeated in order to find the desired community. Fig. 7.13 gives the algorithm. The algorithm Find-Community is the control program. It takes a set S of seed Web pages as input, and crawls to a fixed depth including in-links W X Y Z s t 300 7 Social Network Analysis as well as out-links (with in-links found by querying a search engine). It then applies the procedure Max-Flow-Community to the induced graph G from the crawl. After a community C is found, it ranks the pages in the community by the number of edges that each has inside of the community. Some highest ranked non-seed pages are added to the seed set. This is to create a big seed set for the next iteration in order to crawl more pages. The algorithm then iterates the procedure. Note that the first iteration may only identify a very small community. However, when new seeds are added, increasingly larger communities are identified. Heuristics are used to decide when to stop. The procedure Max-Flow-Community finds the actual community from G. Since a Web graph has no source and sink, it first augments the web graph by adding an artificial source, s, with infinite capacity edges routed to all seed vertices in S; making each pre-existing edge bidirectional and assigning each edge a constant capacity k. It then adds an artificial sink t Algorithm Find-Community (S) while number of iteration is less than desired do build G = (V, E) by doing a fixed depth crawl starting from S; k = |S|; C = Max-Flow-Community(G, S, k); rank all v  C by the number of edges in C; add the highest ranked non-seed vertices to S end-while return all v  V still connected to the source s Procedure Max-Flow-Community(G, S, k) create artificial vertices, s and t and add to V; // V is the vertex set of G. for all v  S do add (s, v) to E with c(s, v) =  // E is the edge set of G. endfor for all (u, v)  E, u  s do c(u, v) = k; if (v, u)  E then add (v, u) to E with c(v, u) = k endif endfor for all v  V, v  S  {s, t} do add (v, t) to E with c(v, t) = 1 endfor Max-Flow(G, s, t); return all v  V still connected to s. Fig. 7.13. The algorithm for mining maximum flow communities 7.5 Community Discovery 301 and routes all vertices except the source, the sink, and the seed vertices to t with unit capacity. After augmenting the web graph, a residual flow graph is produced by a maximum flow procedure (Max-Flow()). All vertices accessible from s through non-zero positive edges form the desired result. The value k is heuristically chosen to be the size of the set S to ensure that after the artificial source and sink are added to the original graph, the same cuts will be produced as the original graph (see the proof in [24]). Fig. 7.14 shows the community finding process. Finally, we note that this algorithm does not find the theme of the community or the community hierarchy (i.e., sub-communities and so on). Fig. 7.14. Schematic representation of the community finding process 7.5.4 Email Communities Based on Betweenness Email has become the predominant means of communication in the information age. It has been established as an indicator of collaboration and knowledge (or information) exchange. Email exchanges provide plenty of data on personal communication for the discovery of shared interests and relationships between people, which were hard to discover previously. It is fairly straightforward to construct a graph based on email data. People are the vertices and the edges are added between people who corresponded through email. Usually, the edge between two people is added if a minimum number of messages passed between them. The minimum number is controlled by a threshold, which can be tuned. To analyze an email graph or network, one can make use of all the centrality measures and prestige measures discussed in Sect. 7.1. We now focus on community finding only. We are interested in people communities, which are subsets of vertices that are related. One way to identify communities is by partitioning the S Community Artificial source Cut set Artificial sink Outside of the community 302 7 Social Network Analysis graph into discrete clusters such that there are few edges lying between the clusters. This definition is similar to that of the maximum flow community. Betweenness in social networks is a natural measure for identifying those edges in between clusters or communities [59]. The idea is that inter- community links, which are few, have high betweenness values, while the intra-community edges have low betweenness values. However, the betweenness discussed in Sect. 7.1 is evaluated on each person in the network. Here, we need to evaluate the betweenness of each edge. The idea is basically the same and Equation (4) can be used here without normalization because we only find communities in a single graph. The betweenness of an edge is simply the number of shortest paths that pass it. If the graph is not connected, we identify communities from each connected component. Given a connected graph, the method works iteratively in two steps (Fig. 7.15): repeat Compute the betweenness of each edge in the remaining graph; Remove the edge with the highest betweenness until the graph is suitably partitioned. Fig. 7.15. Community finding using the betweenness measure. Since the removal of an edge can strongly affect the betweenness of many other edges, we need to repeatedly re-compute the betweenness of all edges. The idea of the method is very similar to the minimum-cut method discussed in Sect. 7.5.3. The stopping criteria can be designed according to applications. In general, we consider that the smallest community is a triangle. The algorithm should stop producing more unconnected components if there is no way to generate triangle communities. A component of five or fewer vertices cannot consist of two viable communities. The smallest such component is six, which has two triangles connected by one edge, see Fig. 7.16. If any discovered community does not have a triangle, it may not be considered as a community. Clearly, other stopping criteria can be used. Fig. 7.16. The smallest possible graph of two viable communities. 7.5 Community Discovery 303 7.5.5 Overlapping Communities of Named Entities Most community discovery algorithms are based on graph partitioning, which means that an entity can belong to only a single community. However, in real life, a person can be in multiple communities (see the definition in Sect. 7.5.1). For example, he/she can be in the community of his/her family, the community of his/her colleagues and the community of his/her friends. A heuristic technique is presented in [42] for finding overlapping communities of entities in text documents. In the Web or email context, there are explicit links connecting entities and forming communities. In free text documents, no explicit links exist. Then the question is: what constitutes a link between two entities in text documents? As we indicated earlier, one simple technique is to regard two entities as being linked if they co-occur in the same sentence. This method is reasonable because if two people are mentioned in a sentence there is usually a relationship between them. The objective is to find entity communities from a text corpus, which could be a set of given documents or the returned pages from a search engine using a given entity as the search query. An entity here refers to the name of a person or an organization. The algorithm in [42] consists of four steps: 1. Building a link graph: The algorithm first parses each document. For each sentence, it identifies named entities contained in the sentence. If a sentence has more than one named entities, these entities are pair-wise linked. The keywords in the sentence are attached to the linked pairs to form their textual contents. All the other sentences are discarded. 2. Finding all triangles: The algorithm then finds all triangles, which are the basic building blocks of communities. A triangle consists of three entities bound together. The reason for using triangles is that it has been observed by researchers that a community expands predominantly by triangles sharing a common edge. 3. Finding community cores: It next finds community cores. A community core is a group of tightly bound triangles, which are relaxed complete sub-graphs (or cliques). Intuitively, a core consists of a set of tightly connected members of a community. 4. Clustering around community cores: For those triangles and also entity pairs that are not in any core, they are assigned to cores according to their textual content similarities with the discovered cores. It is clear that in this algorithm a single entity can appear in multiple communities because an entity can appear in multiple triangles. To finish off, the algorithm also ranks the entities in each community according to 304 7 Social Network Analysis degree centrality. Keywords associated with the edges of each community are also ranked. The top keywords are assumed to represent the theme of the community. The technique has been applied to find communities of political figures and celebrities from Web documents with promising results. Bibliographic Notes Social network analysis has a relative long history. A large number of interesting problems were studied in the past 60 years. The book by Wasserman and Faust [60] is an authoritative text of the field. There are also many other books, e.g., “Social Network Analysis: A Handbook” by John Scott, “Networks: An Introduction” by Mark Newman, “Models and Methods in Social Network Analysis” by Peter J. Carrington, "Social Network Analysis" by David Knoke and Song Yang, "Models and Methods in Social Network Analysis" edited by Peter J. Carrington, John Scott and Stanley Wasserman, and "Link Analysis: An Information Science Approach" by Mike Thelwall. Co-citation [55] and bibliographic coupling [36] are from bibliometrics. The book edited by Borgman [8] is a good source of information on both the research and applications of bibliometrics. The use of social network analysis in the Web context (also called link analysis) started with the PageRank algorithm proposed by Brin and Page [9] and Page et al. [52], and the HITS algorithm proposed by Kleinberg [37]. PageRank is also the algorithm that powers the Google search engine. Due to several weaknesses of HITS, many researchers have tried to improve it. Various enhancements were reported by Lempel and Moran [41], Bharat and Henzinger [7], Chakrabarti et al. [13], Cai et al. [10], etc. The book by Langville and Meyer [40] contains in-depth analyses of PageRank, HITS and many enhancements to HITS. Other works related to Web link analysis include those in [14, 35, 46] on improving the PageRank computation, in [22] on searching workspace Web, in [15, 27, 28, 51] on the evolution of the Web and the search engine influence on the Web, in [18, 19, 50, 56] on other link based models, in [5, 47, 48, 54] on Web graph and its characteristics, in [4, 6, 33] on sampling of Web pages, and in [3, 16, 20, 43, 53, 62, 63] on the temporal dimension of Web search. In [45], a new user-browsing based link analysis and ranking method was proposed. This method employs search logs and the continuous time Markov model for ranking, unlike PageRank which uses the discrete time Markov model. On community discovery, HITS can find some communities by computing non-principal eigenvectors [29, 37]. Kumar et al. [38] proposed Bibliography 305 the algorithm for finding bipartite cores. Flake et al. [25] introduced the maximum flow community mining. Ino et al. [34] presented a more strict definition of communities. Tyler et al. [59] gave the method for finding email communities based on betweenness. The algorithm for finding overlapping communities of named entities from texts was given by Li et al. [42]. More recent developments on communities and social networks on the Web can be found in [1, 2, 17, 21, 30, 57, 58, 61, 64]. Bibliography 1. Aleman-Meza, B., M. Nagarajan, C. Ramakrishnan, L. Ding, P. Kolari, A. Sheth, I. Arpinar, A. Joshi, and T. Finin. Semantic analytics on social networks: experiences in addressing the problem of conflict of interest detection. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 2. Andersen, R. and K. Lang. Communities from seed sets. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 3. Baeza-Yates, R., F. Saint-Jean, and C. Castillo. Web structure, dynamics and page quality. In Proceedings of String Processing and Information Retrieval, 2002: Springer. 4. Bar-Yossef, Z. and M. Gurevich. Random sampling from a search engine's index. Journal of the ACM (JACM), 2008, 55(5): p. 1-74. 5. Barabasi, L. and R. Albert. Emergence of Scaling in Random Walk. Science, 1999, 286(5439): p. 509-512. 6. Bharat, K. and A. Broder. A technique for measuring the relative size and overlap of public web search engines. Computer Networks, 1998, 30(1-7): p. 379-388. 7. Bharat, K. and M. Henzinger. Improved algorithms for topic distillation in a hyperlinked environment. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1998), 1998. 8. Borgman, C. and J. Furner. Scholarly communication and bibliometrics. Annual Review of Information Science and Technology, 2002, 36: p. 3-72. 9. Brin, S. and P. Lawrence. The anatomy of a large-scale hypertextual web search engine. Computer Networks, 1998, 30(1-7): p. 107-117. 10. Cai, D., S. Yu, J. Wen, and W. Ma. Block-based web search. In Proceedings of ACM SIGIR Research and Development in Information Retrieval (SIGIR- 2004), 2004. 11. Chakrabarti, S. Integrating the document object model with hyperlinks for enhanced topic distillation and information extraction. In Proceedings of 10th international conference on World Wide Web. 2001, ACM: Hong Kong, Hong Kong. p. 211-220. 12. Chakrabarti, S., B. Dom, S. Kumar, P. Raghavan, S. Rajagopalan, A. Tomkins, D. Gibson, and J. Kleinberg. Mining the Web's link structure. Computer, 2002, 32(8): p. 60-67. 306 7 Social Network Analysis 13. Chakrabarti, S., K. Puniyani, and S. Das. Optimizing scoring functions and indexes for proximity search in type-annotated corpora. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 14. Chen, Y., Q. Gan, and T. Suel. Local methods for estimating pagerank values. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2004), 2004. 15. Cho, J. and S. Roy. Impact of search engines on page popularity. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 16. Diaz, F. Integration of news content into web results. In Proceedings of ACM International Conference on Web Search and Data Mining (WSDM-2009), 2009. 17. Diesner, J. and K. Carley. Exploration of communication networks from the Enron email corpus. In Proceedings of Workshop on Link Analysis, Counterterrorism and Security at SDM’05, 2005. 18. Diligenti, M., M. Gori, and M. Maggini. Web page scoring systems for horizontal and vertical search. In Proceedings of International Conference on World Wide Web (WWW-2002), 2002. 19. Ding, C., X. He, P. Husbands, H. Zha, and H. Simon. PageRank, HITS and a unified framework for link analysis. In Proceedings of SIAM International Conference on Data Mining (SDM-2002), 2002. 20. Dong, A., Y. Chang, Z. Zheng, G. Mishne, J. Bai, R. Zhang, K. Buchner, C. Liao, and F. Diaz. Towards recency ranking in web search. In Proceedings of ACM International Conference on Web Search and Data Mining (WSDM- 2010), 2010. 21. Eckmann, J. and E. Moses. Curvature of co-links uncovers hidden thematic layers in the world wide web. Proceedings of the National Academy of Sciences of the United States of America, 2002, 99(9): p. 5825. 22. Fagin, R., R. Kumar, K. McCurley, J. Novak, D. Sivakumar, J. Tomlin, and D. Williamson. Searching the workplace web. In Proceedings of International Conference on World Wide Web (WWW-2003), 2003. 23. Farahat, A., T. LoFaro, J. Miller, G. Rae, and L. Ward. Authority rankings from HITS, PageRank, and SALSA: Existence, uniqueness, and effect of initialization. SIAM Journal on Scientific Computing, 2006, 27(4): p. 1181- 1201. 24. Flake, G., S. Lawrence, and C. Giles. Efficient identification of web communities. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2000), 2000. 25. Flake, G., S. Lawrence, C. Giles, and F. Coetzee. Self-organization of the web and identification of communities. IEEE Computer, 2002, 35(3): p. 66-71. 26. Ford, L. and D. Fulkerson. Maximal flow through a network. Canadian Journal of Mathematics, 1956, 8(3): p. 399-404. 27. Fortunato, S., A. Flammini, and F. Menczer. Scale-free network growth by ranking. Physical review letters, 2006, 96(21): p. 218701. Bibliography 307 28. Fortunato, S., A. Flammini, F. Menczer, and A. Vespignani. Topical interests and the mitigation of search engine bias. Proceedings of the National Academy of Sciences, 2006, 103(34): p. 12684. 29. Gibson, D., J. Kleinberg, and P. Raghavan. Inferring web communities from link topology. In Proceedings of ACM Conference on Hypertext and Hypermedia, 1998. 30. Girvan, M. and M. Newman. Community structure in social and biological networks. Proceedings of the National Academy of Sciences of the United States of America, 2002, 99(12): p. 7821. 31. Golub, G. and C. Van Loan. Matrix computations. 1996: Johns Hopkins Univ Press. 32. Grimmet, G. and D. Stirzaker. Probability and Random Process. 1989: Oxford University Press. 33. Henzinger, M., A. Heydon, M. Mitzenmacher, and M. Najork. Measuring index quality using random walks on the Web. Computer Networks, 1999, 31(11-16): p. 1291-1303. 34. Ino, H., M. Kudo, and A. Nakamura. Partitioning of Web graphs by community topology. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 35. Kamvar, S., T. Haveliwala, C. Manning, and G. Golub. Extrapolation methods for accelerating PageRank computations. In Proceedings of International Conference on World Wide Web (WWW-2003), 2003. 36. Kessler, M. Bibliographic coupling between scientific papers. American documentation, 1963, 14(1): p. 10-25. 37. Kleinberg, J. Authoritative sources in a hyperlinked environment. Journal of the ACM (JACM), 1999, 46(5): p. 604-632. 38. Kumar, R., P. Raghavan, S. Rajagopalan, and A. Tomkins. Trawling the Web for emerging cyber-communities. Computer Networks, 1999, 31(11-16): p. 1481-1493. 39. Langville, A. and C. Meyer. Deeper inside pagerank. Internet Mathematics, 2004, 1(3): p. 335-380. 40. Langville, A. and C. Meyer. Google's PageRank and beyond: the science of search engine rankings. 2006: Princeton University Press. 41. Lempel, R. and S. Moran. The stochastic approach for link-structure analysis (SALSA) and the TKC effect. Computer Networks, 2000, 33(1-6): p. 387- 401. 42. Li, X., B. Liu, and P. Yu. Discovering overlapping communities of named entities. Knowledge Discovery in Databases: PKDD 2006, 2006: p. 593-600. 43. Li, X., B. Liu, and P. Yu. Time Sensitive Ranking with Application to Publication Search. In Proceedings of IEEE International Conference on Data Mining (ICDM-2008), 2008. 44. Li, X., B. Liu, and P. Yu. Time sensitive ranking with application to publication search. In Link Mining: Models, Algorithms, and Applications, P. Yu, J. Han, and C. Faloutsos, Editors. 2010, Springer. p. 187-209. 45. Liu, Y., B. Gao, T. Liu, Y. Zhang, Z. Ma, S. He, and H. Li. BrowseRank: letting web users vote for page importance. In Proceedings of ACM SIGIR 308 7 Social Network Analysis Conf. on Research and Development in Information Retrieval (SIGIR-2008), 2008. 46. McSherry, F. A uniform approach to accelerated PageRank computation. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 47. Menczer, F. Evolution of document networks. Proceedings of the National Academy of Sciences of the United States of America, 2004, 101(Suppl 1): p. 5261. 48. Menczer, F. Growing and navigating the small world web by local content. Proceedings of the National Academy of Sciences of the United States of America, 2002, 99(22): p. 14014. 49. Ng, A., A. Zheng, and M. Jordan. Stable algorithms for link analysis. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2001), 2001. 50. Nie, Z., Y. Zhang, J. Wen, and W. Ma. Object-level ranking: bringing order to web objects. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 51. Ntoulas, A., J. Cho, and C. Olston. What's new on the web?: the evolution of the web from a search engine perspective. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 52. Page, L., S. Brin, R. Motwani, and T. Winograd. The pagerank citation ranking: Bringing order to the web. In Technical Report 1999–0120. 1998, Computer Science Department, Stanford University. 53. Pandey, S., S. Roy, C. Olston, J. Cho, and S. Chakrabarti. Shuffling a stacked deck: The case for partially randomized ranking of search engine results. In Proceedings of International Conference on Very Large Data Bases (VLDB- 2005), 2005. 54. Pennock, D., G. Flake, S. Lawrence, E. Glover, and C. Giles. Winners don't take all: Characterizing the competition for links on the web. Proceedings of the National Academy of Sciences of the United States of America, 2002, 99(8): p. 5207. 55. Small, H. Co citation in the scientific literature: A new measure of the relationship between two documents. Journal of the American Society for Information Science, 1973, 24(4): p. 265-269. 56. Tomlin, J. A new paradigm for ranking pages on the world wide web. In Proceedings of International Conference on World Wide Web (WWW-2003), 2003. 57. Toyoda, M. and M. Kitsuregawa. Creating a Web community chart for navigating related communities. In Proceedings of ACM Conf. on Hypertext and Hypermedia, 2001. 58. Toyoda, M. and M. Kitsuregawa. Extracting evolution of web communities from a series of web archives. In Proceedings of ACM Conf. on Hypertext and Hypermedia, 2003. 59. Tyler, J.R., D.M. Wilkinson, and B.A. Huberman. Email as Spectroscopy: Automated Discovery of Community Structure within Organizations. Communities and Technologies, 2003. Bibliography 309 60. Wasserman, S. and K. Faust. Social Network Analysis. 1994: Cambridge University Press. 61. Wu, X., L. Zhang, and Y. Yu. Exploring social annotations for the semantic web. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 62. Yu, P.S., X. Li, and B. Liu. Adding the Temporal Dimension to Search – A Case Study in Publication Search. In Proceedings of International Conference on Web Intelligence, 2005. 63. Zhang, R., Y. Chang, Z. Zheng, D. Metzler, and J. Nie. Search result re- ranking by feedback control adjustment for time-sensitive query. In Proceedings of Human Language Technologies: The 2009 Annual Conference of the North American Chapter of the Association for Computational Linguistics, 2009. 64. Zhou, D., E. Manavoglu, J. Li, C. Giles, and H. Zha. Probabilistic models for discovering e-communities. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 8 Web Crawling Web crawlers, also known as spiders or robots, are programs that auto- matically download Web pages. Since information on the Web is scattered among billions of pages served by millions of servers around the globe, users who browse the Web can follow hyperlinks to access information, virtually moving from one page to the next. A crawler can visit many sites to collect information that can be analyzed and mined in a central location, either online (as it is downloaded) or off-line (after it is stored). Were the Web a static collection of pages, we would have little long term use for crawling. Once all the pages are fetched and saved in a repository, we are done. However, the Web is a dynamic entity evolving at rapid rates. Hence there is a continuous need for crawlers to help applications stay current as pages and links are added, deleted, moved or modified. There are many applications for Web crawlers. One is business intelligence, whereby organizations collect information about their competitors and potential collaborators. Another use is to monitor Web sites and pages of interest, so that a user or community can be notified when new information appears in certain places. There are also malicious applications of crawlers, for example, that harvest email addresses to be used by spammers or collect personal information to be used in phishing and other identity theft attacks. The most widespread use of crawlers is, however, in support of search engines. In fact, crawlers are the main consumers of Internet bandwidth. They collect pages for search engines to build their indexes. Well known search engines such as Google, Yahoo! and MSN run very efficient universal crawlers designed to gather all pages irrespective of their content. Other crawlers, sometimes called preferential crawlers, are more targeted. They attempt to download only pages of certain types or topics. This chapter introduces the main concepts, algorithms and data structures behind Web crawlers. After discussing the implementation issues that all crawlers have to address, we describe different types of crawlers: universal, focused, and topical. We also discuss some of the ethical issues around crawlers. Finally, we peek at possible future uses of crawlers in support of alternative models where crawling and searching activities are distributed among a large community of users connected by a dynamic and adaptive peer network. By Filippo Menczer B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_8, © Springer-Verlag Berlin Heidelberg 2011 311 312 8 Web Crawling 8.1 A Basic Crawler Algorithm In its simplest form, a crawler starts from a set of seed pages (URLs) and then uses the links within them to fetch other pages. The links in these pages are, in turn, extracted and the corresponding pages are visited. The process repeats until a sufficient number of pages are visited or some other objective is achieved. This simple description hides many delicate issues related to network connections, spider traps, URL canonicalization, page parsing, and crawling ethics. In fact, Google founders Sergey Brin and Lawrence Page, in their seminal paper, identified the Web crawler as the most sophisticated yet fragile component of a search engine [6]. Figure 8.1 shows the flow of a basic sequential crawler. Such a crawler fetches one page at a time, making inefficient use of its resources. Later in the chapter we discuss how efficiency can be improved by the use of multiple processes, threads, and asynchronous access to resources. The crawler maintains a list of unvisited URLs called the frontier. The list is initialized with seed URLs which may be provided by the user or another program. In each iteration of its main loop, the crawler picks the next URL from the frontier, fetches the page corresponding to the URL through HTTP, parses the retrieved page to extract its URLs, adds newly discovered URLs to the frontier, and stores the page (or other extracted information, possibly index terms) in a local disk repository. The crawling process may be terminated when a certain number of pages have been crawled. The crawler may also be forced to stop if the frontier becomes empty, although this rarely happens in practice due to the high average number of links (on the order of ten out-links per page across the Web). A crawler is, in essence, a graph search algorithm. The Web can be seen as a large graph with pages as its nodes and hyperlinks as its edges. A crawler starts from a few of the nodes (seeds) and then follows the edges to reach other nodes. The process of fetching a page and extracting the links within it is analogous to expanding a node in graph search. The frontier is the main data structure, which contains the URLs of unvisited pages. Typical crawlers attempt to store the frontier in the main memory for efficiency. Based on the declining price of memory and the spread of 64-bit processors, quite a large frontier size is feasible. Yet the crawler designer must decide which URLs have low priority and thus get discarded when the frontier is filled up. Note that given some maximum size, the frontier will fill up quickly due to the high fan-out of pages. Even more importantly, the crawler algorithm must specify the order in which new URLs are extracted from the frontier to be visited. These mechanisms determine the graph search algorithm implemented by the crawler. 8.1 A Basic Crawler Algorithm 313 Fig. 8.1. Flow chart of a basic sequential crawler. The main data operations are shown on the left, with dashed arrows. 8.1.1 Breadth-First Crawlers The frontier may be implemented as a first-in-first-out (FIFO) queue, corresponding to a breadth-first crawler. The URL to crawl next comes from the head of the queue and new URLs are added to the tail of the queue. Once the frontier reaches its maximum size, the breadth-first crawler can add to the queue only one unvisited URL from each new page crawled. 314 8 Web Crawling The breadth-first strategy does not imply that pages are visited in “random” order. To understand why, we have to consider the highly skewed, long-tailed distribution of indegree in the Web graph. Some pages have a number of links pointing to them that are orders of magnitude larger than the mean. Indeed, the mean indegree is not statistically significant when the indegree k is distributed according to a power law Pr(k)  k with exponent  < 3 [49]. For the Web graph, this is the case, with   2.1 [7]. This means that the fluctuations of indegree are unbounded, i.e., the standard deviation is bounded only by the finite size of the graph. Intuitively, popular pages have so many incoming links that they act like attractors for breadth-first crawlers. It is therefore not surprising that the order in which pages are visited by a breadth-first crawler is highly correlated with their PageRank or indegree values. An important implication of this phenomenon is an intrinsic bias of search engines to index well connected pages. Another reason that breadth-first crawlers are not “random” is that they are greatly affected by the choice of seed pages. Topical locality measures indicate that pages in the link neighborhood of a seed page are much more likely to be related to the seed pages than randomly selected pages. These and other types of bias are important to universal crawlers (Sect. 8.3). As mentioned earlier, only unvisited URLs are to be added to the frontier. This requires some data structure to be maintained with visited URLs. The crawl history is a time-stamped list of URLs fetched by the crawler tracking its path through the Web. A URL is entered into the history only after the corresponding page is fetched. This history may be used for post-crawl analysis and evaluation. For example, we want to see if the most relevant or important resources are found early in the crawl process. While history may be stored on disk, it is also maintained as an in-memory data structure for fast look-up, to check whether a page has been crawled or not. This check is required to avoid revisiting pages or wasting space in the limited-size frontier. Typically a hash table is appropriate to obtain quick URL insertion and look-up times (O(1)). The look-up process assumes that one can identify two URLs effectively pointing to the same page. This introduces the need for canonical URLs (see Sect. 8.2). Another important detail is the need to prevent duplicate URLs from being added to the frontier. A separate hash table can be maintained to store the frontier URLs for fast look-up to check whether a URL is already in it. 8.1.2 Preferential Crawlers A different crawling strategy is obtained if the frontier is implemented as a priority queue rather than a FIFO queue. Typically, preferential crawlers 8.2 Implementation Issues 315 assign each unvisited link a priority based on an estimate of the value of the linked page. The estimate can be based on topological properties (e.g., the indegree of the target page), content properties (e.g., the similarity between a user query and the source page), or any other combination of measurable features. For example, the goal of a topical crawler is to follow edges that are expected to lead to portions of the Web graph that are relevant to a user-selected topic. The choice of seeds is even more important in this case than for breadth-first crawlers. We will discuss various preferential crawling algorithms in Sects. 8.4 and 8.5. For now let us simply assume that some function exists to assign a priority value or score to each unvisited URL. If pages are visited in the order specified by the priority values in the frontier, then we have a best-first crawler. The priority queue may be a dynamic array that is always kept sorted by URL scores. At each step, the best URL is picked from the head of the queue. Once the corresponding page is fetched, the URLs extracted from it must, in turn, be scored. They are then added to the frontier in such a manner that the sorting order of the priority queue is maintained. As for breadth-first, best-first crawlers also need to avoid duplicate URLs in the frontier. Keeping a separate hash table for look-up is an efficient way to achieve this. The time complexity of inserting a URL into the priority queue is O(logF), where F is the frontier size (looking up the hash requires constant time). To dequeue a URL, it must first be removed from the priority queue (O(logF)) and then from the hash table (again O(1)). Thus the parallel use of the two data structures yields a logarithmic total cost per URL. Once the frontier’s maximum size is reached, only the best URLs are kept; the frontier must be pruned after each new set of links is added. 8.2 Implementation Issues 8.2.1 Fetching To fetch pages, a crawler acts as a Web client; it sends an HTTP request to the server hosting the page and reads the response. The client needs to timeout connections to prevent spending unnecessary time waiting for responses from slow servers or reading huge pages. In fact, it is typical to restrict downloads to only the first 10-100 KB of data for each page. The client parses the response headers for status codes and redirections. Redirect loops are to be detected and broken by storing URLs from a redirection chain in a hash table and halting if the same URL is encountered twice. One may also parse and store the last-modified header to determine the age of the document, although this information is known 316 8 Web Crawling to be unreliable. Error-checking and exception handling is important during the page fetching process since the same code must deal with potentially millions of remote servers. In addition, it may be beneficial to collect statistics on timeouts and status codes to identify problems or automatically adjust timeout values. Programming languages such as Java, Python and Perl provide simple programmatic interfaces for fetching pages from the Web. However, one must be careful in using high-level interfaces where it may be harder to detect lower-level problems. For example, a robust crawler in Perl should use the Socket module to send HTTP requests rather than the higher-level LWP library (the World-Wide Web library for Perl). The latter does not allow fine control of connection timeouts. 8.2.2 Parsing Once (or while) a page is downloaded, the crawler parses its content, i.e., the HTTP payload, and extracts information both to support the crawler’s master application (e.g., indexing the page if the crawler supports a search engine) and to allow the crawler to keep running (extracting links to be added to the frontier). Parsing may imply simple URL extraction from hyperlinks, or more involved analysis of the HTML code. The Document Object Model (DOM) establishes the structure of an HTML page as a tag tree, as illustrated in Fig. 8.2. HTML parsers build the tree in a depth-first manner, as the HTML source code of a page is scanned linearly. Unlike program code, which must compile correctly or else will fail with a syntax error, correctness of HTML code tends to be laxly enforced by browsers. Even when HTML standards call for strict interpretation, de facto standards imposed by browser implementations are very forgiving. This, together with the huge population of non-expert authors generating Web pages, imposes significant complexity on a crawler's HTML parser. Many pages are published with missing required tags, tags improperly nested, missing close tags, misspelled or missing attribute names and values, missing quotes around attribute values, unescaped special characters, and so on. As an example, the double quotes character in HTML is reserved for tag syntax and thus is forbidden in text. The special HTML entity " is to be used in its place. However, only a small number of authors are aware of this, and a large fraction of Web pages contains this illegal character. Just like browsers, crawlers must be forgiving in these cases; they cannot afford to discard many important pages as a strict parser would do. A wise preprocessing step taken by robust crawlers is to apply a tool such as tidy (www.w3.org/People/Raggett/tidy) to clean up the HTML content prior to parsing. To add to the complexity, there are many coexisting 8.2 Implementation Issues 317 HTML and XHTML reference versions. However, if the crawler only needs to extract links within a page and/or the text in the page, simpler parsers may suffice. The HTML parsers available in high-level languages such as Java and Perl are becoming increasingly sophisticated and robust. Fig. 8.2. Illustration of the DOM (or tag) tree built from a simple HTML page. Internal nodes (shown as ovals) represent HTML tags, with the tag as the root. Leaf nodes (shown as rectangles) correspond to text chunks. A growing portion of Web pages are written in formats other than HTML. Crawlers supporting large-scale search engines routinely parse and index documents in many open and proprietary formats such as plain text, PDF, Microsoft Word and Microsoft PowerPoint. Depending on the 318 8 Web Crawling application of the crawler, this may or may not be required. Some formats present particular difficulties as they are written exclusively for human interaction and thus are especially unfriendly to crawlers. For instance, some commercial sites use graphic animations in Flash; these are difficult for a crawler to parse in order to extract links and their textual content. Other examples include image maps and pages making heavy use of Javascript for interaction. New challenges are going to come as new standards such as Scalable Vector Graphics (SVG), Asynchronous Javascript and XML (AJAX), and other XML-based languages gain popularity. 8.2.3 Stopword Removal and Stemming When parsing a Web page to extract the content or to score new URLs suggested by the page, it is often helpful to remove so-called stopwords, i.e., terms such as articles and conjunctions, which are so common that they hinder the discrimination of pages on the basis of content. Another useful technique is stemming, by which morphological variants of terms are conflated into common roots (stems). In a topical crawler where a link is scored based on the similarity between its source page and the query, stemming both the page and the query helps improve the matches between the two sets and the accuracy of the scoring function. Both stop-word removal and stemming are standard techniques in information retrieval, and are discussed in greater detail in Chap. 6. 8.2.4 Link Extraction and Canonicalization HTML parsers provide the functionality to identify tags and associated attribute-value pairs in a given Web page. In order to extract hyperlink URLs from a page, we can use a parser to find anchor () tags and grab the values of the associated href attributes. However, the URLs thus obtained need to be further processed. First, filtering may be necessary to exclude certain file types that are not to be crawled. This can be achieved with white lists (e.g., only follow links to text/html content pages) or black lists (e.g., discard links to PDF files). The identification of a file type may rely on file extensions. However, they are often unreliable and sometimes missing altogether. We cannot afford to download a document and then decide whether we want it or not. A compromise is to send an HTTP HEAD request and inspect the content-type response header, which is usually a more reliable label. Another type of filtering has to do with the static or dynamic nature of pages. A dynamic page (e.g., generated by a CGI script) may indicate a 8.2 Implementation Issues 319 query interface for a database or some other application in which a crawler may not be interested. In the early days of the Web, such pages were few and easily recognizable, e.g., by matching URLs against the /cgi-bin/ directory name for CGI scripts, or against the special characters [?=&] used in CGI query strings. However, the use of dynamic content has become much more common; it is used in a variety of sites for content that is perfectly indexable. Most importantly, its dynamic nature is very difficult to recognize via URL inspection. For these reasons, most crawlers no longer make such distinction between static and dynamic content. While a crawler normally would not create query URLs autonomously (unless it is designed to probe the so-called deep or hidden Web, which contain databases with query interfaces), it will happily crawl URLs hard-coded in HTML source of parsed pages. In other words, if a URL is found in a Web page, it is fair game. There is one important exception to this strategy, the spider trap, which is discussed below. Before links can be added to the frontier, relative URLs must be converted to absolute URLs. For example, the relative URL news/today.html in the page http://www.somehost.com/index.html is to be transformed into the absolute form http://www.somehost.com/news/today.html. There are various rules to convert relative URLs into absolute ones. A relative URL can be expressed as a relative or absolute path relative to the Web server’s document root directory. The base URL may be specified by an HTTP header or a meta-tag within an HTML page, or not at all–in the latter case the directory of the hyperlink’s source page is used as a base URL. Converting relative URLs is just one of many steps that make up the canonicalization process, i.e., the conversion of a URL into a canonical form. The definition of canonical form is somewhat arbitrary, so that different crawlers may specify different rules. For example, one crawler may always specify the port number within the URL (e.g., http://www.somehost. com:80/), while another may specify the port number only when it is not 80 (the default HTTP port). As long as the canonical form is applied consistently by a crawler, such distinctions are inconsequential. Some programming languages such as Perl provide modules to manage URLs, including methods for absolute/relative conversion and canonicalization. However, several canonicalization steps require the application of heuristic rules, and off-the-shelf tools typically do not provide such functionalities. A crawler may also need to use heuristics to detect when two URLs point to the same page in order to minimize the likelihood that the same page is fetched multiple times. Table 8.1 lists the steps typically employed to canonicalize a URL. 320 8 Web Crawling Table 8.1. Some transformations to convert URLs to canonical forms. Stars indicate heuristic rules, where there is a tradeoff between the risk of altering the semantics of the URL (if a wrong guess is made) and the risk of missing duplicate URLs (if no transformation is applied) for the same target Description and transformation Example and canonical form Default port number http://cs.indiana.edu:80/ Remove http://cs.indiana.edu/ Root directory http://cs.indiana.edu Add trailing slash http://cs.indiana.edu/ Guessed directory* http://cs.indiana.edu/People Ad http://cs.indiana.edu/People/ Fragment http://cs.indiana.edu/faq.html#3 Remove http://cs.indiana.edu/faq.html Current or parent directory http://cs.indiana.edu/a/./../b/ Resolve path http://cs.indiana.edu/b/ Default filename* http://cs.indiana.edu/index.html Remove http://cs.indiana.edu/ Needlessly encoded characters http://cs.indiana.edu/%7Efil/ Decode http://cs.indiana.edu/~fil/ Disallowed characters http://cs.indiana.edu/My File.htm Encode http://cs.indiana.edu/My%20File.htm Mixed/upper-case host names http://CS.INDIANA.EDU/People/ Lower-case http://cs.indiana.edu/People/ 8.2.5 Spider Traps A crawler must be aware of spider traps. These are Web sites where the URLs of dynamically created links are modified based on the sequence of actions taken by the browsing user (or crawler). Some e-commerce sites such as Amazon.com may use URLs to encode which sequence of products each user views. This way, each time a user clicks a link, the server can log detailed information on the user's shopping behavior for later analysis. As an illustration, consider a dynamic page for product x, whose URL path is /x and that contains a link to product y. The URL path for this link would be /x/y to indicate that the user is going from page x to page y. Now suppose the page for y has a link back to product x. The dynamically created URL path for this link would be /x/y/x, so that the crawler would think this is a new page when in fact it is an already visited page with a new URL. As a side effect of a spider trap, the server may create an entry in a database every time the user (or crawler) clicks on certain dynamic links. An example might be a blog or message board where users can post comments. These situations create sites that appear infinite to a crawler, because the more links are followed, the more new d trailing slash 8.2 Implementation Issues 321 URLs are created. However these new “dummy” links do not lead to existing or new content, but simply to dynamically created form pages, or to pages that have already been visited. Thus a crawler could go on crawling inside the spider trap forever without actually fetching any new content. In practice spider traps are not only harmful to the crawler, which wastes bandwidth and disk space to download and store duplicate or useless data. They may be equally harmful to the server sites. Not only does the server waste its bandwidth, the side effect of a crawler caught in a spider trap may also be filling a server-side database with bogus entries. The database may eventually become filled to capacity, and the site may be disabled as a result. This is a type of denial of service attack carried out unwittingly by the crawler. In some cases a spider trap needs the client to send a cookie set by the server for the dynamic URLs to be generated. So the problem is prevented if the crawler avoids accepting or sending any cookies. However, in most cases a more proactive approach is necessary to defend a crawler against spider traps. Since the dummy URLs often become larger and larger in size as the crawler becomes entangled in a spider trap, one common heuristic approach to tackle such traps is by limiting the URL sizes to some maximum number of characters, say 256. If a longer URL is encountered, the crawler should simply ignore it. Another way is by limiting the number of pages that the crawler requests from a given domain. The code associated with the frontier can make sure that every consecutive sequence of, say, 100 URLs fetched by the crawler contains at most one URL from each fully qualified host name. This approach is also germane to the issue of crawler etiquette, discussed later. 8.2.6 Page Repository Once a page is fetched, it may be stored/indexed for the master application (e.g., a search engine). In its simplest form a page repository may store the crawled pages as separate files. In this case each page must map to a unique file name. One way to do this is to map each page's URL to a compact string using some hashing function with low probability of collisions, e.g., MD5. The resulting hash value is used as a (hopefully) unique file name. The shortcoming of this approach is that a large scale crawler would incur significant time and disk space overhead from the operating system to manage a very large number of small individual files. A more efficient solution is to combine many pages into one file. A naïve approach is to simply concatenate some number of pages (say 1,000) into each file, with some special markup to separate and identify the pages 322 8 Web Crawling within the file. This requires a separate look-up table to map URLs to file names and IDs within each file. A better method is to use a database to store the pages, indexed by (canonical) URLs. Since traditional RDBMSs impose high overhead, embedded databases such as the open-source Berkeley DB are typically preferred for fast access. Many high-level languages such as Java and Perl provide simple APIs to manage Berkeley DB files, for example as tied associative arrays. This way the storage management operations become nearly transparent to the crawler code, which can treat the page repository as an in-memory data structure. 8.2.7 Concurrency A crawler consumes three main resources: network, CPU, and disk. Each is a bottleneck with limits imposed by bandwidth, CPU speed, and disk seek/transfer times. The simple sequential crawler described in Sect. 8.1 makes a very inefficient use of these resources because at any given time two of them are idle while the crawler attends to the third. The most straightforward way to speed-up a crawler is through concurrent processes or threads. Multiprocessing may be somewhat easier to implement than multithreading depending on the programming language and platform, but it may also incur a higher overhead due to the involvement of the operating system in the management (creation and destruction) of child processes. Whether threads or processes are used, a concurrent crawler may follow a standard parallel computing model as illustrated in Fig. 8.3. Basically each thread or process works as an independent crawler, except for the fact that access to the shared data structures (mainly the frontier, and possibly the page repository) must be synchronized. In particular a frontier manager is responsible for locking and unlocking the frontier data structures so that only one process or thread can write to them at one time. Note that both enqueueing and dequeuing are write operations. Additionally, the frontier manager would maintain and synchronize access to other shared data structures such as the crawl history for fast look-up of visited URLs. It is a bit more complicated for a concurrent crawler to deal with an empty frontier than for a sequential crawler. An empty frontier no longer implies that the crawler has reached a dead-end, because other processes may be fetching pages and adding new URLs in the near future. The process or thread manager may deal with such a situation by sending a temporary sleep signal to processes that report an empty frontier. The process manager needs to keep track of the number of sleeping processes; when all the processes are asleep, the crawler must halt. 8.3 Universal Crawlers 323 Fig. 8.3. Architecture of a concurrent crawler The concurrent design can easily speed-up a crawler by a factor of 5 or 10. The concurrent architecture however does not scale up to the performance needs of a commercial search engine. We discuss in Sect. 8.3 further steps that can be taken to achieve more scalable crawlers. 8.3 Universal Crawlers General purpose search engines use Web crawlers to maintain their indices [4], amortizing the cost of crawling and indexing over the millions of queries received between successive index updates (though indexers are 324 8 Web Crawling designed for incremental updates [13]. These large-scale universal crawlers differ from the concurrent breadth-first crawlers described above along two major dimensions: 1. Performance: They need to scale up to fetching and processing hundreds of thousands of pages per second. This calls for several architectural improvements. 2. Policy: They strive to cover as much as possible of the most important pages on the Web, while maintaining their index as fresh as possible. These goals are, of course, conflicting so that the crawlers must be designed to achieve good tradeoffs between their objectives. Next we discuss the main issues in meeting these requirements. 8.3.1 Scalability Figure 8.4 illustrates the architecture of a large-scale crawler, based on the accounts in the literature [6, 8, 25]. The most important change from the concurrent model discussed earlier is the use of asynchronous sockets in place of threads or processes with synchronous sockets. Asynchronous sockets are non-blocking, so that a single process or thread can keep hundreds of network connections open simultaneously and make efficient use of network bandwidth. Not only does this eliminate the overhead due to managing threads or processes, it also makes locking access to shared data structures unnecessary. Instead, the sockets are polled to monitor their states. When an entire page has been fetched into memory, it is processed for link extraction and indexing. This “pull” model eliminates contention for resources and the need for locks. The frontier manager can improve the efficiency of the crawler by maintaining several parallel queues, where the URLs in each queue refer to a single server. In addition to spreading the load across many servers within any short time interval, this approach allows to keep connections with servers alive over many page requests, thus minimizing the overhead of TCP opening and closing handshakes. The crawler needs to resolve host names in URLs to IP addresses. The connections to the Domain Name System (DNS) servers for this purpose are one of the major bottlenecks of a naïve crawler, which opens a new TCP connection to the DNS server for each URL. To address this bottleneck, the crawler can take several steps. First, it can use UDP instead of TCP as the transport protocol for DNS requests. While UDP does not guarantee delivery of packets and a request can occasionally be dropped, this is rare. On the other hand, UDP incurs no connection overhead with a 8.3 Universal Crawlers 325 significant speed-up over TCP. Second, the DNS server should employ a large, persistent, and fast (in-memory) cache. Finally, the pre-fetching of DNS requests can be carried out when links are extracted from a page. In addition to being added to the frontier, the URLs can be scanned for host names to be sent to the DNS server. This way, when a URL is later ready to be fetched, the host IP address is likely to be found in the DNS cache, obviating the need to propagate the request through the DNS tree. Fig. 8.4. High-level architecture of a scalable universal crawler 326 8 Web Crawling In addition to making more efficient use of network bandwidth through asynchronous sockets, large-scale crawlers can increase network bandwidth by using multiple network connections switched to multiple routers, thus utilizing the networks of multiple Internet service providers. Similarly, disk I/O throughput can be boosted via a storage area network connected to a storage pool through a fibre channel switch. 8.3.2 Coverage vs. Freshness vs. Importance Given the size of the Web, it is not feasible even for the largest-scale crawlers employed by commercial search engines to index all of the content that could be accessed. Instead, search engines aim to focus on the most “important” pages, where importance is assessed based on various factors such as link popularity measures (indegree or PageRank) [14, 22]. At the time of this writing the three major commercial search engines report index sizes in the order of 1010 pages, while the indexable Web may be at least an order of magnitude larger. The simplest strategy to bias the crawler in favor of popular pages is to do nothing – given the long-tailed distribution of indegree discussed in Sect. 8.1, a simple breadth-first crawling algorithm will tend to fetch the pages with the highest PageRank by definition, as confirmed empirically [41]. In fact, one would have to apply a reverse bias to obtain a fair sample of the Web. Suppose that starting with a random Web walk, we wanted a random sample of pages drawn with uniform probability distribution across all pages. We can write the posterior probability of adding a page p to the sample as Pr(accept(p)|crawl(p))Pr(crawl(p)) where the first factor is the conditional probability of accepting the page into the sample given that it was crawled, and the second factor is the prior probability of crawling the page in the random walk. We can find the acceptance strategy to obtain a uniform sample by setting the product to a constant, yielding Pr(accept(p)|crawl(p))  1/Pr(crawl(p)). The prior Pr(crawl(p)) is given by the PageRank of p, and can be approximated during the random walk by the frequency f(p) that the crawler has encountered a link to p. So therefore, each visited page p should be accepted with probability proportional to 1/f(p). Empirical tests on a simulated Web graph validate that this strategy yields a sample of the graph that is statistically representative of the original [23]. The goal to cover as many pages as possible (among the most important ones) is in conflict with the need to maintain a fresh index. Because of the highly dynamic nature of the Web, with pages being added, deleted, and modified all the time, it is necessary for a crawler to revisit pages already 8.4 Focused Crawlers 327 in the index in order to keep the index up-to-date. Many studies have been conducted to analyze the dynamics of the Web, i.e., the statistical properties of the processes leading to change in Web structure and content 66, 101, 152, 177, 416]. They all indicate that the Web changes at very rapid rates. While early studies relied on the values reported by Web servers in the last-modified HTTP header, recently there is consensus that this information has little reliability. The most recent and exhaustive study at the time of this writing [42] reports that while new pages are created at a rate of about 8% per week, only about 62% of the content of these pages is really new because pages are often copied from existing ones. The link structure of the Web is more dynamic, with about 25% new links created per week. Once created, pages tend to change little so that most of the changes observed in the Web are due to additions and deletions rather than modifications. Finally, there is an agreement on the observation that the degree of change of a page is a better predictor of future change than the frequency of change [20, 42]. This suggests that crawler revisit strategies based on frequency of change [4, 13] may not be the most appropriate for achieving a good tradeoff between coverage and freshness. 8.4 Focused Crawlers Rather than crawling pages from the entire Web, we may want to crawl only pages in certain categories. One applications of such a preferential crawler would be to maintain a Web taxonomy such as the Yahoo! Directory (dir.yahoo.com) or the volunteer-based Open Directory Project (ODP, dmoz.org). Suppose you are the ODP editor for a certain category; you may wish to launch such a crawler from an initial seed set of pages relevant to that category, and see if any new pages discovered should be added to the directory, either directly under the category in question or one of its subcategories. A focused crawler attempts to bias the crawler towards pages in certain categories in which the user is interested. Chakrabarti et al. [11] proposed a focused crawler based on a classifier. The idea is to first build a text classifier using labeled example pages from, say, the ODP. Then the classifier would guide the crawler by preferentially selecting from the frontier those pages that appear most likely to belong to the categories of interest, according to the classifier's prediction. To train the classifier, example pages are drawn from various categories in the taxonomy as shown in Fig. 8.5. The classification algorithm used was the naïve Bayesian method (see Chap. 3). For each category c in the taxonomy we can build a Bayesian classifier to compute the probability Pr(c|p) that a crawled page p belongs to c (by definition, Pr(top|p) = 1 for the top or root 328 8 Web Crawling category). The user can select a set c* of categories of interest. Each crawled page is assigned a relevance score.   * ).|Pr()( cc pcpR (1) 3 Two strategies were explored. In the “soft” focused strategy, the crawler uses the score R(p) of each crawled page p as a priority value for all unvisited URLs extracted from p. The URLs are then added to the frontier, which is treated as a priority queue (see Sect. 8.1.2). In the “hard” focused strategy, for a crawled page p, the classifier first finds the leaf category )(ˆ pc in the taxonomy most likely to include p: ˆ c (p)  arg max c: c'c Pr(c | p). (2) If an ancestor of )(ˆ pc is a focus category, i.e., c’: )(ˆ pc  c’ c’ c*, then the URLs from the crawled page p are added to the frontier. Otherwise they are discarded. The idea is illustrated in Fig. 8.5 (left). For example, imagine a crawler focused on soccer (c' = soccer  c*) visits a page p in the FIFA World Cup Germany 2006 site. If the classifier correctly assigns p to the leaf category cˆ =Sports/Soccer/Competitions/World_Cup/2006, the Fig. 8.5. Left: A taxonomy supporting a focused crawler. The areas in gray represent the categories of interest c*. A crawler with hard focus would add to the frontier the links extracted from a page classified in the leaf category 1ˆc because its ancestor category c' is of interest to the user, while the links from a page classified in 2ˆc would be discarded. Right: A context graph with L = 3 layers constructed to train a context focused crawler from the target set in layer A = 0. 8.4 Focused Crawlers 329 links extracted from p are added to the frontier because 2006 is a subcategory of Sports/Soccer (cˆ  soccer). The soft and hard focus strategies worked equally well in experiments. Another element of the focused crawler is the use of a distiller. The distiller applies a modified version of the HITS algorithm [28] to find topical hubs. These hubs provide links to authoritative sources on a focus category. The distiller is activated at various times during the crawl and some of the top hubs are added to the frontier. Context-Focused Crawlers are another type of focused crawlers. They also use naïve Bayesian classifiers as a guide, but in this case the classifiers are trained to estimate the link distance between a crawled page and a set of relevant target pages [18]. To see why this might work, imagine looking for information on “machine learning.” One might go to the home pages of computer science departments and from there to faculty pages, which may then lead to relevant pages and papers. A department home page, however, may not contain the keywords “machine learning.” A typical focused or best-first crawler would give such a page a low priority and possibly never follow its links. However, if the crawler could estimate that pages about machine learning are only two links away from a page containing the keywords “computer science department,” then it would give the department home page a higher priority. The context-focused crawler is trained using a context graph with L layers (Fig. 8.5 right). The seed (target) pages form the layer 0 of the graph. The pages corresponding to the in-links to the seed pages are in layer 1. The in-links to the layer 1 pages make up the layer 2, and so on. The in-links to any page can be obtained by submitting a link: query to a search engine. The seed pages in layer 0 (and possibly those in layer 1) are then concatenated into a single large document, and the top few terms according to the TF-IDF weighting scheme (see Chap. 6) are selected as the vocabulary (feature space) to be used for classification. A naïve Bayesian classifier is built for each layer in the context graph. A prior probability Pr(A ) = 1/L is assigned to each layer. All the pages in a layer are used to compute Pr(t|A ), the probability of occurrence of a term t given the layer (class) A . At the crawling time, these are used to compute Pr(p|A ) for each crawled page p. The posterior probability Pr(A |p) of p belonging to layer A can then be computed for each layer from Bayes’ rule. The layer A * with highest posterior probability wins: ).|Pr(maxarg)(* pp AA A  (3) 330 8 Web Crawling If Pr(A *|p) is less than a threshold, p is classified into the “other” class, which represents pages that do not have a good fit with any of the layers in the context graph. If Pr(A *|p) exceeds the threshold, p is classified into A *. The set of classifiers corresponding to the context graph provides a mechanism to estimate the link distance of a crawled page from a relevant page. If the mechanism works, the computer science department page in our example will get classified into layer 2. The crawler maintains a separate priority queue for each layer, containing the links extracted from visited pages classified in that layer. Each queue is sorted by the scores Pr(A |p). The next URL to crawl is taken from the non-empty queue with the smallest A . So the crawler gives precedence to links that appear to be closest to relevant targets. It is shown in [18] that the context-focused crawler outperforms the standard focused crawler in experiments. While the majority of focused crawlers in the literature have employed the naïve Bayesian method as the classification algorithm to score unvisited URLs, an extensive study with hundreds of topics has provided strong evidence that classifiers based on SVM or neural networks can yield significant improvements in the quality of the crawled pages [47]. 8.5 Topical Crawlers For many preferential crawling tasks, labeled (positive and negative) examples of pages are not available in sufficient numbers to train a focused crawler before the crawl starts. Instead, we typically have a small set of seed pages and a description of a topic of interest to a user or user community. The topic can consist of one or more example pages (possibly the seeds) or even a short query. Preferential crawlers that start with only such information are often called topical crawlers [8, 14, 38]. They do not have text classifiers to guide crawling. Even without the luxury of a text classifier, a topical crawler can be smart about preferentially exploring regions of the Web that appear relevant to the target topic by comparing features collected from visited pages with cues in the topic description. To illustrate a topical crawler with its advantages and limitations, let us consider the MySpiders applet (myspiders.informatics.indiana.edu). Figure 8.6 shows a screenshot of this application. The applet is designed to demonstrate two topical crawling algorithms, best-N-first and InfoSpiders, both discussed below [45]. MySpiders is interactive in that a user submits a query just like one would do with a search engine, and the results are then shown in a 8.5 Topical Crawlers 331 window. However, unlike a search engine, this application has no index to search for results. Instead the Web is crawled in real time. As pages deemed relevant are crawled, they are displayed in a list that is kept sorted by a user-selected criterion: score or recency. The score is simply the content (cosine) similarity between a page and the query (see Chap. 6); the recency of a page is estimated by the last-modified header, if returned by the server (as noted earlier this is not a very reliable estimate). Fig. 8.6. Screenshot of the MySpiders applet in action. In this example the user has launched a population of crawlers with the query “search censorship in france” using the InfoSpiders algorithm. The crawler reports some seed pages obtained from a search engine, but also a relevant blog page (bottom left) that was not returned by the search engine. This page was found by one of the agents, called Spider2, crawling autonomously from one of the seeds. We can see that Spider2 spawned a new agent, Spider13, who started crawling for pages also containing the term “italy.” Another agent, Spider5, spawned two agents one of which, Spider11, identified and internalized the relevant term “engine.” One of the advantages of topic crawling is that all hits are fresh by definition. No stale results are returned by the crawler because the pages are visited at query time. This makes this type of crawlers suitable for 332 8 Web Crawling applications that look for very recently posted documents, which a search engine may not have indexed yet. On the down side, the search is slow compared to a traditional search engine because the user has to wait while the crawler fetches and analyzes pages. If the user's client machine (where the applet runs) has limited bandwidth, e.g., a dial-up Internet connection, the wait is likely infeasible. Another disadvantage is that the ranking algorithms cannot take advantage of global prestige measures, such as PageRank, available to a traditional search engine. Several research issues around topical crawlers have received attention. One key question is how to identify the environmental signals to which crawlers should attend in order to determine the best links to follow. Rich cues such as the markup and lexical (text) signals within Web pages, as well as features of the link graph built from pages already seen, are all reasonable sources of evidence to exploit. Crawlers can use the evidence available to them in different ways, for example more or less greedily. The goals of the application also provide crucial context. For example the desired properties of the pages to be fetched (similar pages, popular pages, authoritative pages, recent pages, and so on) can lead to significant differences in crawler design and implementation. The task could be constrained by parameters like the maximum number of pages to be fetched (long crawls vs. short crawls) or the memory available. A crawling task can thus be viewed as a constrained multi-objective search problem. The wide variety of objective functions, coupled with the lack of appropriate knowledge about the search space, make such a problem challenging. In the remainder of this section we briefly discuss the theoretical conditions necessary for topical crawlers to function, and the empirical evidence supporting the existence of such conditions. Then we review some of the machine learning techniques that have been successfully applied to identify and exploit useful cues for topical crawlers. 8.5.1 Topical Locality and Cues The central assumption behind topical crawlers is that Web pages contain reliable cues about each other’s content. This is a necessary condition for designing a crawler that has a better-than-random chance to preferentially visit pages relevant with respect to a given topic. Indeed, if no estimates could be made about unvisited pages, then all we could do is a random walk through the Web graph, or an exhaustive search (using breadth-first or depth-first search algorithms). Fortunately, crawling algorithms can use cues from words and hyperlinks, associated respectively with a lexical and 8.5 Topical Crawlers 333 a link topology. In the former, two pages are close to each other if they have similar textual content; in the latter, if there is a short path between them (we will see what “short” means). Lexical metrics are text similarity measures derived from the vector space model (see Chap. 6). The cluster hypothesis behind this model is that a document lexically close to a relevant document (with respect to the given query) is also relevant with high probability [51]. Link metrics typically look at hyperlinks as directed edges in a graph, but a path can also be defined in an undirected sense, in which case two pages have a short link distance between them if they are co-cited or co- referenced, even if there is no directed path between them. Links are a very rich source of topical information about Web pages. From a crawler's perspective, there are two central questions: 1. link-content conjecture: whether two pages that link to each other are more likely to be lexically similar to each other, compared to two randomly selected pages; 2. link-cluster conjecture: whether two pages that link to each other are more likely to be semantically related to each other, compared to two randomly selected pages. A first answer to the link-content conjecture was obtained by computing the cosine similarity between linked and random pairs of pages, showing that the similarity is an order of magnitude higher in the former case [15]. The same study also showed that the anchor text tends to be a good (similar) description of the target page. The link-content conjecture can be generalized by looking at the decay in content similarity as a function of link distance from a source page. This decay was measured by launching an exhaustive breadth-first crawl from seed sets of 100 topics in the Yahoo! directory [35]. Let us use the cosine similarity measure (p1, p2) between pages p1 and p2 (see Chap. 6). We can measure the link distance 1(p1, p2) along the shortest directed path from p1 and p2, revealed by the breadth-first crawl. Both distances 1(q, p) and similarities (q, p) were averaged for each topic q over all pages p in the crawl set q dP for each depth d: )(1),(),( 1 1 1 q i d i q iq d PNNiNpqdq p d     (4)    p d p d Pp q d P pqNpqdq ),(1),(),(  (5) 334 8 Web Crawling where Nd q is the size of the cumulative page set Pd q = {p | 1(q, p) ≤ d}. The crawlers were stopped at depth d = 3, yielding 3000 data points {(p, d): q {1, …, 100}, d {1, 2, 3}}. These points were then used for fitting an exponential decay model: 21)1()(     e (6) where  is the noise level in similarity, measured empirically by averaging across random pairs of pages. The parameters 1 and 2 are set by fitting the data. This was done for pages in various top-level domains, and the resulting similarity decay curves are plotted in Fig. 8.7. Fig. 8.7. Illustration of the link-content conjecture. The curves plot, for each top- level domain, the decay in mean cosine similarity between pages as a function of their mean directed link distance, obtained by fitting data from 100 exhaustive breadth-first crawls starting from the 100 Yahoo! directory topics [35]. The curves provide us with a rough estimate of how far in link space one can make inferences about lexical content. We see that a weak signal is still present three links away from the starting pages for all but the .com domain, and even further for the .edu domain. Such heterogeneity is not surprising – academic pages are written carefully to convey information and proper pointers, while business sites often do not link to related sites because of competition. Therefore a topical crawler in the commercial domain would have a harder task, other things being equal. A solution may 8.5 Topical Crawlers 335 be to use undirected links. More specifically, if a crawler can obtain in- links to good pages (by querying a search engine), it can use co-citation to detect hubs. If a page links to several good pages, it is probably a good hub and all its out-links should be given high priority. This strategy, related to the so-called sibling locality [1], has been used in focused crawlers [11] and in topical crawlers for business intelligence [46]. In addition to co- citation, one could look at bibliographic coupling: if several good pages link to a certain page, that target is likely to be a good authority so it and its in-links should be given high priority. Fig. 8.8 illustrates various ways in which crawlers can exploit co-citation and bibliographic coupling. Fig. 8.8. Crawling techniques exploiting co-citation (top) and bibliographic coupling (bottom). Dashed edges represent in-links, which require access to a search engine or connectivity server. Page A is a good hub, so it should be given high priority; once fetched, page B linked by it can be discovered and placed in the frontier with high priority since it is likely to be a good authority. Page C is also a good hub, so D should be given high priority. Page E is a good authority, so it should be given high priority. Its URL can also be used to discover F, which may be a good hub and should be placed in the frontier. G is also a good authority, so H should be given high priority and I should be placed in the frontier. 336 8 Web Crawling The link-cluster conjecture, also known as linkage locality [11], states that one can infer the meaning of a page by looking at its neighbors. This is actually more important than inferring lexical content, since the latter is only relevant insofar as it is correlated with the semantic content of pages. The same exhaustive crawl data used to validate the link-content conjecture can also be used to explore the link-cluster conjecture, namely the extent to which relevance is preserved within link space neighborhoods and the decay in expected relevance as one browses away from a relevant page [35]. The link-cluster conjecture can be simply formulated in terms of the conditional probability that a page p is relevant with respect to some query q, given that page r is relevant and that p is within d links from r: Rq (d)  Pr(relq (p)|relq (r)1(r, p)  d] (7) where relq() is a binary relevance assessment with respect to q. In other words a page has a higher than random probability of being about a certain topic if it is in the neighborhood of other pages about that topic. Rq(d) is the posterior relevance probability given the evidence of a relevant page nearby. The conjecture is then represented by the likelihood ratio (q, d) between Rq(d) and the prior relevance probability Gq  Pr(relq(p)), also known as the generality of the query. If semantic inferences are possible within a link radius d, then the following condition must hold: .1)(),(  q q G dRdq (8) To illustrate the meaning of the link-cluster conjecture, consider a random crawler searching for pages about a topic q. Call q(t) the probability that the crawler hits a relevant page at time t. Solving the recursion q (t 1)  q (t)Rq (1)  (1 q (t))Gq (9) for q(t+1) = q(t) yields the stationary hit rate .)1(1 * qq q q RG G  (10) The link-cluster conjecture is a necessary and sufficient condition for such a crawler to have a better than chance hit rate: .1)1,(*  qGqq  (11) Figure 8.9 plots the mean likelihood ratio (q, d) versus the mean link distance (q, d) obtained by fitting an exponential decay function 8.5 Topical Crawlers 337 () 1 3e4 5 (12) to the same 300 data points {(q, d)}. Note that this three-parameter model is more complex than the one used to validate the link-content conjecture, because ( = 0) must also be estimated from the data ((q, 0) = 1/Gq). The fitted curve reveals that being within a radius of three links from a relevant page increases the relevance probability by a factor (q, d) >>1. This is very reassuring for the design of topical crawlers. It also suggests that crawlers should attempt to remain within a few links from some relevant source. In this range hyperlinks create detectable signals about lexical and semantic content, despite the Web's apparent lack of structure. Fig. 8.9. Illustration of the link-cluster conjecture. The curve plots the decay in mean likelihood ratio as a function of mean directed link distance from a relevant page, obtained by fitting data from 100 exhaustive breadth-first crawls starting from as many Yahoo! directory topics [35]. The link-content and link-cluster conjectures can be further developed by looking at the correlation between content-based, link-based, and semantic-based similarity measures. Using the ODP as a ground truth, we can express the semantic similarity between any two pages in the taxonomy [32, 36] and see how it can be approximated by content and link similarity measures. For content one can consider for example cosine similarity based on TF or TF-IDF term weights. For link similarity one can similarly represent a page as a bag of links (in-links, out-links, or 338 8 Web Crawling both/undirected) and then apply a Jaccard coefficient or a cosine similarity. Figure 8.10 shows, for various topical domains from the ODP, the correlation between semantic similarity and two representative content and link similarity measures. We observe significant heterogeneity in the correlations, suggesting that topical crawlers have an easier job in some topics (e.g., “news”) than others (e.g., “games”). Another observation is that in some topical domains (e.g., “home”) textual content is a more reliable signal, while in others (e.g., “computers”) links are more helpful. Fig. 8.10. Pearson correlation coefficients between the semantic similarity extracted from ODP [32] and two representative content and link similarity measures. The correlations are measured using a stratified sample of 150,000 URLs from the ODP, for a total of 4 billion pairs [36]. Content similarity is cosine with TF weights, and link similarity is the Jaccard coefficient with undirected links. 8.5.2 Best-First Variations The majority of crawling algorithms in the literature are variations of the best-first scheme described in Sect. 8.1.2. The difference is in the heuristics that they use to score unvisited URLs. A very simple instance is the case where each URL is queued into the frontier with priority given by the content similarity between the topic description and the page from 8.5 Topical Crawlers 339 which the URL was extracted. Content similarity can be measured with the standard cosine similarity, using TF or TFIDF term weights (in the latter case the crawler must have global or topic-contextual term frequency information available). This simple crawler is also known as naïve best-first. Many variations of the naïve best-first crawlers are possible. Some give more importance to certain HTML markups, such as the title, or to text segments marked by special tags, such as headers. Other techniques focus on determining the most appropriate textual context to score a link. One Fig. 8.11. Link context from distance-weighted window (top) and from the DOM tree (bottom). 340 8 Web Crawling alternative to using the entire page or just the anchor text as context, used by InfoSpiders [37] and Clever [9], is a weighted window where topic keywords occurrences near the anchor count more toward the link score than those farther away, as shown in Fig. 8.11. Another approach is to consider the tag (DOM) tree of the HTML page [8]. The idea is to walk up the tree from the link anchor toward the root, stopping at an appropriate aggregation node. The link context is then obtained by the text in the tag subtree rooted at the aggregation node (Fig. 8.11). SharkSearch [24] is an improved version of the earlier FishSearch crawler [16]. It uses a similarity measure like the one used in the naïve best-first crawler as a first step for scoring unvisited URLs. The similarity is computed for anchor text, a fixed-width link context, the entire source page, and ancestor pages. The ancestors of a URL are the pages that appear on the crawl path to the URL. SharkSearch, like its predecessor FishSearch, maintains a depth bound. That is, if the crawler finds unimportant pages on a crawl path it stops crawling further along that path. To this end, each URL in the frontier is associated with a depth and a potential score. The score of an unvisited URL is obtained from a linear combination of anchor text similarity, window context similarity, and an inherited score. The inherited score is the similarity of the source page to the topic, unless it is zero, in which case it is inherited from the source's parent (and recursively from its ancestors). The implementation of SharkSearch requires to preset three similarity coefficients in addition to the depth bound. This crawler does not perform as well as others described below. Rather than (or in addition to) improving the way we assign priority scores to unvisited URLs, we can also improve on a naïve best-first crawler by altering the priority scheme. A classic trade-off in machine learning is that between exploration and exploitation of information. A crawler is no different: it can greedily pursue the best-looking leads based on noisy quality estimates, or be more explorative and visit some pages that seem less promising, but might lead to better pages. The latter approach is taken in many optimization algorithms in order to escape local optima and reach a global optimum with some probability. As it turns out, the same strategy is also advantageous for topical crawlers. Visiting some URLs with lower priority leads to a better overall quality of the crawler pages than strictly following the best-first order. This is demonstrated by best-N-first, a crawling algorithm that picks N URLs at a time from the frontier (the top N by priority score) and fetches them all. Once all N pages are visited, the newly extracted URLs are merge-sorted into the priority queue, and the cycle is repeated. The best-N-first crawler with N = 256 is a very strong competitor, outperforming most of the other topical crawlers in the literature [38, 48]. Figure 8.12 shows a comparison with two crawlers 8.5 Topical Crawlers 341 discussed thus far. Note that a concurrent implementation of a best-first crawler with N threads or processes is equivalent to a best-N-first crawler. 8.5.3 Adaptation All the crawlers discussed thus far use a static strategy both to evaluate unvisited URLs and to manage the frontier. Thus they do not learn from experience or adapt to the context of a particular topic in the course of the crawl. In this section we describe a number of machine learning techniques that have been incorporated into adaptive topical crawlers. The intelligent crawler uses a statistical model for learning to assign priorities to the URLs in the frontier, considering Bayesian interest factors derived from different features [1]. For example, imagine that the crawler is supposed to find pages about soccer and that 40% of links with the keyword football in the anchor text lead to relevant pages, versus a background or prior frequency of only 2% of crawled pages being relevant. Then the crawler assigns an interest factor Fig. 8.12. Performance of best-N-first crawler with N = 256 (BFS256) compared with a naïve best-first crawler (BFS1) and a breadth-first crawler. Recall refers to sets of relevant pages that the crawlers are supposed to discover; averages and error bars are computed across 100 crawls from as many ODP topics. 342 8 Web Crawling 20)](Pr[ )](|)(Pr[ ),(   prel panchorfootballprel anchorfootballsoccer soccer soccer  (13) to the feature “keyword football in anchor.” Recall relsoccer(p) is the binary relevance score (0 or 1) of page p to soccer. The interest factors are treated as independent sources of evidence, or likelihoods. They are combined by a linear combination of log-likelihoods, with user-defined weight parameters. The features employed by the intelligent crawler may be diverse, depending on the particular crawling task. They may include tokens extracted from candidate URLs, source page content and links, co-citation (sibling) relationships, and/or other characteristics of the visited and unvisited URLs. As more evidence is accumulated and stored throughout the crawl, the interest factors are recalculated and the priorities updated, so that the frontier is always sorted according to the most recent estimates. Thus intelligent crawlers adapt to the content and link structure of the Web neighborhoods being explored. The original focused crawlers described earlier also use machine learning, in particular a classifier that guides the crawler. However the classifier is trained before the crawl is launched, and no learning occurs during the crawl. Therefore we do not consider it an adaptive crawler. However, in a later “accelerated” version of the focused crawler [8], an online learning apprentice was added to the system; the original (baseline) classifier then acts as a critic, providing the apprentice with training examples for learning to classify outgoing links from the features of the pages from which they are extracted. Suppose page p1 is fetched and contains a link to page p2. Later, p2 is fetched and the baseline classifier assigns it to a relevant class. This information is passed to the apprentice, which uses the labeled example (“the link from p1 to p2 is good”) to learn to classify the link to p2 as good based on the textual features in the context of the anchor within p1. Future links with a similar context should be given high priority. Conversely, if p2 is deemed irrelevant by the baseline classifier, the apprentice learns to predict (“bad link”) when it encounters a link with a similar context in the future. The features used to train the apprentice were textual tokens associated with a link context based on the DOM tree, and the learning algorithm used by the apprentice was a naïve Bayesian classifier. This approach led to a significant reduction in the number of irrelevant pages fetched by the focused crawler. While the accelerated focused crawler is not a topical crawler because it still needs labeled examples to train the baseline classifier prior to the crawl, the idea of training an apprentice online during the crawl can be 8.5 Topical Crawlers 343 applied in topical crawlers as well. Indeed this is a type of reinforcement learning technique employed in several crawlers, using different features and/or different learning algorithms for the apprentice. In reinforcement learning [27] we have a network where nodes are states and directed links are actions. An action a  A (think “anchor”) moves an agent from a state p  P (think “page”) to another state according to a transition function L: P  A  P. Thus an adaptive crawler is seen as an agent moving from page to page. Actions are rewarded according to a function r: P  A  . We want to learn a policy mapping states to actions, : P  A, that maximizes future reward discounted over time:     0 0 ),()( t tt t aprpV  (14) where we follow action (link) at=(pt) from state (page) pt at each time step t. The parameter  determines how future rewards are discounted (0   < 1). If  = 0, the reinforcement learning policy is the greedy one employed by the naïve best-first crawler. To learn an optimal policy, we define the value of selecting action a from state p, and following the optimal policy thereafter: )],([),(),(* apLVaprapQ  (15) where V* is the value function of the optimal policy *(p) = argmaxaQ(p, a). The question then becomes how to estimate the function Q, i.e., to assign a value to a link a based on the context information in page p from which the link is extracted. However, the actions available to the crawler are not limited to the links from the last page visited; any of the actions corresponding to the URLs in the frontier are available. Furthermore, there is no reason why the Q value of a link should be a function of a particular source page; if links to the same target page are extracted from multiple source pages, the estimated values of the anchors can be combined, for example Q(u) = max{(p, a): L(p, a) = u}Q(p, a). This way Q values can be computed not for links (anchors), but for target pages (URLs); the state and action spaces are thus greatly reduced, basically collapsing all visited pages into a single degenerate state and all links to their target URLs. The policy  reduces to the simple selection of the URL in the frontier with the maximum Q value. One way to calculate Q values is via a naïve Bayesian classifier. This method was found to work well compared to a breadth-first crawler for the tasks of crawling computer science research papers and company directory information [33, 50]. In this case, the classifier was trained off-line rather than online while crawling, using labeled examples as in the focused 344 8 Web Crawling crawler. Training the classifier to predict future reward ( > 0) was better than only using immediate reward ( = 0). For future reward the authors use a heavy discount  = 0.5, arguing that it is optimal to be greedy in selecting URLs from the frontier, so that one can crawl toward the nearest relevant page. This assumes that all relevant targets are within reach. So there is no reason to delay reward. However, as discussed earlier, a crawler typically deals with noisy data, so the classifier’s Q estimates are not entirely reliable; more importantly, a typical crawler cannot possibly cover the entire search space. These factors suggest that it may be advantageous to occasionally give up some immediate reward in order to explore other directions, potentially leading to pockets or relevant pages unreachable by a greedy crawler (see Fig. 8.12). Using a previously trained classifier to compute Q values for URLs in the frontier means that supervised learning is combined with reinforcement learning. As for focused crawlers, labeled examples must be available prior to the start of the crawl. This may be possible in tasks such as the collection of research articles, but is not a realistic assumption for typical topical crawlers. An adaptive crawling algorithm that actually uses rein- forcement learning while crawling online, without any supervised learning, is InfoSpiders. This crawler employs various machine learning techniques to exploit various types of Web regularities. InfoSpiders are inspired by artificial life models in which a population of agents lives, learn, evolve, and die in an environment. Individual agents learn during their lifetimes based on their experiences, with the environment playing the role of a critic, providing rewards and penalties for actions. Agents may also reproduce, giving rise to new agents similar to them, and die. The environment is the Web, the actions consist of following links and visiting pages and the text and link features of pages are the signals that agents can internalize into their learned and evolved behaviors. Feedback from the environment consists of a finite energy resource, necessary for survival. Each action has an energy cost, which may be fixed or proportional to, say, the size of a fetched page or the latency of a page download [17]. Energy is gained from visiting new pages relevant to the query topic. A cache prevents an agent from accumulating energy by visiting the same page multiple times. In the recent version of InfoSpiders, each agent maintains its own frontier of unvisited URLs [38]. The agents can be implemented as concurrent processes/threads, with non-contentious access to their local frontiers. Fig. 8.13 illustrates the representation and flow of an individual agent. The adaptive representation of each InfoSpiders agent consists of a list of keywords (initialized with the topic description) and a neural net used to evaluate new links. Each input unit of the neural net receives a count of the frequency with which the keyword occurs in the vicinity of each link, 8.5 Topical Crawlers 345 weighted to give more importance to keywords occurring near the anchor and maximum for the anchor text (Fig. 8.11). The neural net has a single output unit whose activation is used as a Q value (score) for each link u in input. The agent’s neural net learns to predict the Q value of the link’s target URL u given the inputs from the link's source page p. The reward function r(u) is the cosine similarity between the agent’s terms and the target page u. The future discounted optimal value V*(u) is approximated using the highest neural net prediction among the links subsequently extracted from u. This procedure is similar to the reinforcement learning algorithm described above, except that the neural net replaces the naïve Bayesian classifier. The neural net is trained by the back-propagation algorithm [52]. This mechanism is called connectionist reinforcement learning [30]. While the neural net can in principle model nonlinear relationships between term frequencies and pages, in practice we have used a simple perceptron whose prediction is a linear combination of the keyword weights. Such a learning technique provides each InfoSpiders agent with the capability to adapt its own link-following behavior in the course of a crawl by associating relevance estimates with particular patterns of keyword frequencies around links. Fig. 8.13. A single InfoSpiders agent. The link context is the weighted window as shown in Fig. 8.11: for each newly extracted URL and for each term in the agent's term list, this produces a weight that is fed into the neural network, whose output is stored as the link's priority score in the frontier. The neural net's link scores are combined with estimates based on the cosine similarity between the agent's keyword list and the entire source page. A parameter  (0    1) regulates the relative importance given to the estimates based on the neural net versus the source page. Based on the 346 8 Web Crawling combined score  the agent uses a stochastic selector to pick one of the links in the frontier with probability       ' )'( )( )Pr( u u u e eu (16) where u is a URL in the local frontier . Parameter  regulates the greediness of the link selector. Its value can be fixed or evolved with the agent. After a new page u has been fetched, the agent receives an energy payoff proportional to the difference between the reward r(u) and the cost charged for the download. An agent dies when it runs out of energy. The energy level is also used to determine whether or not the agent should reproduce after visiting a page. An agent reproduces when the energy level passes a fixed threshold. The reproduction is meant to bias the search toward areas with pages relevant to the topic. Topical locality suggests that if an agent visits a few relevant pages in rapid sequence, more relevant pages are likely to be nearby (in the frontier). To exploit this, the accumulated energy results in a short-term doubling of the frequency with which the crawler explores this agent’s frontier. At reproduction, the agent’s energy and frontier are split in half with the offspring (new agent or thread). According to ecological theory, this way the agent population is supposed to move toward an optimal cover of the Web graph in proportion to the local density of resources, or relevant pages. In addition to the individual's reinforcement learning and the population’s evolutionary bias, InfoSpiders employ a third adaptive mechanism. At reproduction, the offspring’s keyword vector is mutated (expanded) by adding a new term. The chosen term/keyword is the one that is most frequent in the parent’s last visited page, i.e., the page that triggered the reproduction. This selective query expansion strategy, illustrated in Fig. 8.6, is designed to allow the population to diversify and expand its focus according to each agent’s local context. An InfoSpiders crawler incorporating all of these adaptive techniques has been shown to outperform various versions of naïve best-first crawlers (Fig. 8.14) when visiting a sufficiently large number of pages (more than 10,000) so that the agents have time to adapt [38, 53]. 8.5 Topical Crawlers 347 Fig. 8.14. Performance plots [38]: average target recall RT(t) (top) and average precision PD(t) (similarity to topic description, bottom). The averages are calculated over 10 ODP topics. After 50,000 pages crawled, one tailed t-tests reveal that both BFS256 and InfoSpiders outperform the breadth-first crawler on both performance metrics. InfoSpiders outperform BFS256 on recall, while the difference in precision is not statistically significant. 348 8 Web Crawling 8.6 Evaluation Given the goal of building a “good” crawler, a critical question is how to evaluate crawlers so that one can reliably compare two crawling algorithms and conclude that one is “better” than the other. Since a crawler is usually designed to support some application, e.g., a search engine, it can be indirectly evaluated through the application it supports. However, attribution is problematic; if a search engine works better than another (assuming that were easy to determine!), how can we attribute this difference in performance to the underlying crawling algorithms as opposed to the ranking or indexing schemes? Thus it is desirable to evaluate crawlers directly. Often crawler evaluation has been carried out by comparing a few crawling algorithms on a limited number of queries/tasks without considering the statistical significance. Such anecdotal results, while important, do not suffice for thorough performance comparisons. As the Web crawling field has matured, a need has emerged for evaluating and comparing disparate crawling strategies on common tasks through well-defined performance measures. Let us review the elements of such an evaluation framework, which can be applied to topical as well as focused crawlers. A comparison between crawlers must be unbiased and must allow one to measure statistically significant differences. This requires a sufficient number of crawl runs over different topics, as well as sound methodologies that consider the temporal nature of crawler outputs. Significant challenges in evaluation include the general unavailability of relevant sets for particular topics or queries. Unfortunately, meaningful experiments involving real users for assessing the relevance of pages as they are crawled are extremely problematic. In order to obtain a reasonable notion of crawl effectiveness one would have to recruit a very large number of subjects, each of whom would have to judge a very large number of pages. Furthermore, crawls against the live Web pose serious time constraints and would be overly burdensome to the subjects. To circumvent these problems, crawler evaluation typically relies on defining measures for automatically estimating page relevance and quality. The crawler literature reveals several performance measures used for these purposes. A page may be considered relevant if it contains some or all of the keywords in the topic/query. The frequency with which the keywords appear on the page may also be considered [14]. While the topic of interest to the user is often expressed as a short query, a longer description may be available in some cases. Similarity between the short or long description and each crawled page may be used to judge the page's relevance [24, 39, 53]. The pages used as the crawl's seed URLs may be combined together 8.6 Evaluation 349 into a single document, and the cosine similarity between this document and a crawled page may serve as the page’s relevance score [3]. A classifier may be trained to identify relevant pages. The training may be done using seed pages or other pre-specified relevant pages as positive examples. The trained classifier then provides boolean or continuous relevance scores for each of the crawled pages [11, 18]. Note that if the same classifier, or a classifier trained on the same labeled examples, is used both to guide a (focused) crawler and to evaluate it, the evaluation is not unbiased. Clearly the evaluating classifier would be biased in favor of crawled pages. To partially address this issue, an evaluation classifier may be trained on a different set than the crawling classifier. Ideally the training sets should be disjoint. At a minimum the training set used for evaluation must be extended with examples not available to the crawler [47]. Another approach is to start N different crawlers from the same seeds and let them run until each crawler gathers P pages. All of the NP pages collected from the crawlers are ranked against the topic query/description using a retrieval algorithm such as cosine. The rank provided by the retrieval system for each page is then used as a relevance score. Finally, one may use algorithms, such as PageRank or HITS, that provide authority or popularity estimates for each crawled page. A simpler method would be to use just the number of in-links to the crawled page to derive similar information [3, 14]. Many variations of link-based methods using topical weights may be applied to measure the topical quality of pages [5, 10]. Once each page is assessed, a method is needed to summarize the performance of a crawler across a set of crawled pages. Given a particular measure of page relevance and/or importance we can summarize the performance of the crawler with metrics that are analogous to the information retrieval notions of precision and recall (see Chap. 6). Lacking well-defined relevant sets, the classic boolean relevance is replaced by one of the scores outlined above. A few precision-like measures are found in the literature. In case we have boolean relevance scores, we could measure the rate at which “good” pages are found; if 100 relevant pages are found in the first 500 pages crawled, we have an acquisition rate or harvest rate of 20% at 500 pages [1]. If the relevance scores are continuous (e.g., from cosine similarity or a trained classifier) they can be averaged over the crawled pages. The average relevance, as shown in Fig. 8.14, may be computed over the progress of the crawl [39]. Sometimes running averages are calculated over a window of a number of pages, e.g., the last 50 pages from a current crawl point [11]. Another measure from information retrieval that has been applied to crawler evaluation is search length [37], defined as the number of pages (or the number of irrelevant pages) crawled before a certain percentage of the 350 8 Web Crawling relevant pages are found. Search length is akin to the reciprocal of precision for a preset level of recall. Recall-like measures would require normalization by the number of relevant pages. Since this number is unknown for Web crawling tasks, it might appear that recall cannot be applied to crawlers. However, even if unknown, the size of the relevant set is a constant. Therefore, it can be disregarded as a scaling factor when comparing two crawling algorithms on the same topical query. One can simply sum the quality or relevance estimates (obtained by one of the methods outlined above) over the course of a crawl, and obtain a total relevance as shown in Fig. 8.14. It is possible to design crawling experiments so that a set of relevant target pages is known by the experimenter. Then precision and recall can be calculated from the fraction of these relevant targets that are discovered by the crawler, rather than based on relevance estimates. One way to obtain a set of relevant pages is from a public directory such as the ODP. This way one can leverage the classification already carried out by the volunteer editors of the directory. The experimenter can select as topics a set of categories from the ODP, whose distance from the root of the ODP taxonomy can be determined so as to obtain topics with generality/ specificity appropriate for the crawling task [38, 53]. Figure 8.5 (left) illustrates how subtrees rooted at a chosen category can be used to harvest a set of relevant target pages. If a page is classified in a subtopic of a target topic, it can be considered relevant with respect to the target topic. If a set of known relevant target pages is used to measure the performance of a topical crawler, these same pages cannot be used as seeds for the crawl. Two approaches have been proposed to obtain suitable seed pages. One is to perform a back-crawl from the target pages [53]. By submitting link: queries to a search engine API, one can obtain a list of pages linking to each given target; the process can be repeated from these parent pages to find “grandparent” pages, and so on until a desired link distance is reached. The greater the link distance, the harder the task is for the crawler to locate the relevant targets from these ancestor seed pages. The procedure has the desired property that directed paths are guaranteed to exist from any seed page to some relevant targets. Given the potentially large fan-in of pages, sampling is likely required at each stage of the back- crawl to obtain a suitable number of seeds. The process is similar to the construction of a context graph, as shown in Fig. 8.5 (right). A second approach is to split the set of known relevant pages into two sets; one set can be used as seeds, the other as targets. While there is no guarantee that the targets are reachable from the seeds, this approach is significantly simpler because no back-crawl is necessary. Another advantage is that each of the two relevant subsets can be used in turn as seeds and targets. In 8.6 Evaluation 351 this way, one can measure the overlap between the pages crawled starting from the two disjoint sets. A large overlap is interpreted as robustness of the crawler in covering relevant portions of the Web [8, 11]. The use of known relevant pages as proxies for unknown relevant sets implies an important assumption, which we can illustrate by the Venn diagram in Fig. 8.15. Here S is a set of crawled pages and T is the set of known relevant target pages, a subset of the relevant set R. Let us consider the measure of recall. Using T as if it were the relevant set means that we are estimating the recall |R  S| / |R| by |T  S| / |T|. This approximation only holds if T is a representative, unbiased sample of R independent of the crawl process. While the crawler attempts to cover as much as possible of R, it should not have any information about how pages in T are sampled from R. If T and S are not independent, the measure is biased and unreliable. For example if a page had a higher chance of being selected in T because it was in S, or vice versa, then the recall would be overestimated. The same independence assumption holds for precision-like measures, where we estimate |R  S| / |S| by |T  S| / |S|. A consequence of the independence requirement is that if the ODP is used to obtain T, the experimenter must prevent the crawler from accessing the ODP. This would bias the results because, once a relevant ODP category page is found, all of the relevant target pages can be reached by the crawler in a short breadth-first sweep. Preventing access to the ODP may pose a challenge because so many ODP mirrors exist on the Web. They may not be known by the experimenter, and not trivial to detect. To summarize, crawler performance measures [53] can be characterized along two dimensions: the source of relevance assessments (target pages Fig. 8.15. Illustration of precision and recall measures based on known relevant target pages and underlying independence assumption/requirement. 352 8 Web Crawling vs. similarity to their descriptions) and the normalization factor (average relevance, or precision, vs. total relevance, or recall). Using target pages as the relevant sets we can define crawler precision and recall as follows: || ||),(   T TStR t T  (17) || ||),( t t T S TStP   (18) where St is the set of pages crawled at time t (t can be wall clock time, network latency, number of pages visited, number of bytes downloaded, and so on). T is the relevant target set, where  represents the parameters used to select the relevant target pages. This could include for example the depth of ODP category subtrees used to extract topic-relevant pages. Analogously we can define crawler precision and recall based on similarity to target descriptions: || ),( ),(    T Dp tR tSp D   (19) || ),( ),( t Sp D S Dp tP t    (20) where D is the textual description of the target pages, selected with parameters , and  is a text-based similarity function, e.g., cosine similarity (see Chap. 6). Figure 8.14 shows two examples of performance plots for three different crawlers discussed earlier in this chapter. The two plots depict RT and PD as a function of pages crawled. InfoSpiders and the BFS256 crawler are found to outperform the breadth-first crawler. InfoSpiders gain a slight edge in recall once the agents have had an opportunity to adapt. This evaluation involves each of the three crawlers visiting 50,000 pages for each of 10 topics, for a total of 1.5 million pages. Another set of evaluation criteria can be obtained by scaling or normalizing any of the above performance measures by the critical resources used by a crawler. This way, one can compare crawling algorithms by way of performance/cost analysis. For example, with limited network bandwidth one may see latency as a major bottleneck for a crawling task. The time spent by a crawler on network I/O can be monitored and applied as a scaling factor to normalize precision or recall. 8.7 Crawler Ethics and Conflicts 353 Using such a measure, a crawler designed to preferentially visit short pages, or pages from fast servers [17], would outperform one that can locate pages of equal or even better quality but less efficiently. 8.7 Crawler Ethics and Conflicts Crawlers, especially when efficient, can put a significant strain on the resources of Web servers, mainly on their network bandwidth. A crawler that sends many page requests to a server in rapid succession, say ten or more per second, is considered impolite. The reason is that the server would be so busy responding to the crawler that its service to other requests, including those from human browsing interactively, would deteriorate. In the extreme case a server inundated with requests from an aggressive crawler would become unable to respond to other requests, resulting in an effective denial of service attack by the crawler. To prevent such incidents, it is essential for a crawler to put in place measures to distribute its requests across many servers, and to prevent any one server (fully qualified host name) from receiving requests at more than some reasonably set maximum rate (say, one request every few seconds). In a concurrent crawler, this task can be carried out by the frontier manager, when URLs are dequeued and passed to individual threads or processes. This practice not only is required by politeness toward servers, but also has the additional benefits of limiting the impact of spider traps and not overloading the server, which will respond slowly. Preventing server overload is just one of a number of policies required of ethical Web agents [19]. Such policies are often collectively referred to as crawler etiquette. Another requirement is to disclose the nature of the crawler using the User-Agent HTTP header. The value of this header should include not only a name and version number of the crawler, but also a pointer to where Web administrators may find information about the crawler. Often a Web site is created for this purpose and its URL is included in the User-Agent field. Another piece of useful information is the email contact to be specified in the From header. Finally, crawler etiquette requires compliance with the Robot Exclusion Protocol. This is a de facto standard providing a way for Web server administrators to communicate which files may not be accessed by a crawler. This is accomplished via an optional file named robots.txt in the root directory of the Web server (e.g., http://www.somehost.com/robots.txt). The file provides access policies for different crawlers, identified by the User-agent field. For any user-agent value (or the default “*”) a number of Disallow entries identify directory subtrees to be avoided. Compliant 354 8 Web Crawling crawlers must fetch and parse a server's robots.txt file before sending requests to that server. For example, the following policy in robots.txt: User-agent: * Disallow: / directs any crawler to stay away from the entire server. Some high-level languages such as Perl provide modules to parse robots.txt files. It is wise for a crawler to cache the access policies of recently visited servers, so that the robots.txt file need not be fetched and parsed every time a request is sent to the same server. Additionally, Web authors can indicate if a page may or may not be indexed, cached, or mined by a crawler using a special HTML meta-tag. Crawlers need to fetch a page in order to parse this tag, therefore this approach is not widely used. More details on the robot exclusion protocols can be found at http://www.robotstxt.org/wc/robots.html. When discussing the interactions between information providers and search engines or other applications that rely on Web crawlers, confusion sometime arises between the ethical, technical, and legal ramifications of the Robot Exclusion Protocol. Compliance with the protocol is an ethical issue, and non-compliant crawlers can justifiably be shunned by the Web community. However, compliance is voluntary, and a robots.txt file cannot enforce it. Servers can, however, block access to a client based on its IP address. Thus it is likely that a crawler which does not comply with the Exclusion Protocol and does not follow proper etiquette will be quickly blocked by many servers. Crawlers may disguise themselves as browsers by sending a browser's identifying string in the User-Agent header. This way a server administrator may not immediately detect lack of compliance with the Exclusion Protocol, but an aggressive request profile is likely to reveal the true nature of the crawler. To avoid detection, some mischievous crawlers send requests at low and randomized rates. While such behaviors may be reprehensible, they are not illegal – at least not at the time of this writing. Nonetheless, there have been cases of businesses bringing lawsuits against search organizations for not complying with the Robot Exclusion Protocol. In a recent lawsuit involving the Internet Archive's WayBack Machine (www.archive.org), a plaintiff not only attributed legal weight to the Exclusion Protocol, but also expected that a newly added robots.txt policy should have retroactive value! Deception does not occur only by crawlers against servers. Some servers also attempt to deceive crawlers. For example, Web administrators may attempt to improve the ranking of their pages in a search engine by providing different content depending on whether a request originates from a browser or a search engine crawler, as determined by inspecting the request's User-Agent header. This technique, called cloaking, is frowned 8.7 Crawler Ethics and Conflicts 355 upon by search engines, which remove sites from their indices when such abuses are detected. For more information about Web spam, see Chap. 6. One of the most serious challenges for crawlers originates from the rising popularity of pay-per-click advertising. If a crawler is not to follow advertising links, it needs to have a robust detection algorithm to discriminate ads from other links. A bad crawler may also pretend to be a genuine user who clicks on the advertising links in order to collect more money from merchants for the hosts of advertising links. The above examples suggest a view of the Web as a new playground for artificial intelligence (AI). Crawlers need to become increasingly sophi- sticated to prevent insidious forms of spam from polluting and exploiting the Web environment. Malicious crawlers are also becoming smarter in their efforts, not only to spam but also to steal personal information and in general to deceive people and crawlers for illicit gains. One chapter of this arms race has been the development of CAPTCHAs [55], graphics-based inverse Turing tests automatically generated by server sites to keep out malicious crawlers. Maybe a stronger AI will be a positive outcome of crawler evolution; maybe a less usable Web will be a hefty price to pay. Interestingly, the gap between humans and crawlers may be narrowing from both sides. While crawlers become smarter, some humans are dumbing down their content to make it more accessible to crawlers. For example some online news providers use simpler titles than can be easily classified and interpreted by a crawler as opposed or in addition to witty titles that can only be understood by humans. Another gap that is getting narrower is the distinction between browsers and crawlers, with a growing gray area between the two. A business may wish to disallow crawlers from its site if it provides a service by which it wants to entice human users to visit the site, say to make a profit via ads on the site. A competitor crawling the information and mirroring it on its own site, with different ads, is a clear violator not only of the Robot Exclusion Protocol but also possibly of copyright law. What about an individual user who wants to access the information but automatically hide the ads? There are many browser extensions that allow users to perform all kinds of tasks that deviate from the classic browsing activity, including hiding ads, altering the appearance and content of pages, adding and deleting links, adding functionality to pages, pre-fetching pages, and so on. Such extensions have some of the functionalities of crawlers. Should they identify themselves through the User-Agent header as distinct from the browser with which they are integrated? Should a server be allowed to exclude them? And should they comply with such exclusion policies? These too are questions about ethical crawler behaviors that remain open for the moment. 356 8 Web Crawling 8.8 Some New Developments The typical use of (universal) crawlers thus far has been for creating and maintaining indexes for general purpose search engines. However a more diverse use of (topical) crawlers is emerging both for client and server based applications. Topical crawlers are becoming important tools to support applications such as specialized Web portals (a.k.a. “vertical” search engines), live crawling, and competitive intelligence. Another characteristic of the way in which crawlers have been used by search engines up to now is the one-directional relationship between users, search engines, and crawlers. Users are consumers of information provided by search engines, search engines are consumers of information provided by crawlers, and crawlers are consumers of information provided by users (authors). This one-directional loop does not allow, for example, information to flow from a search engine (say, the queries submitted by users) to a crawler. It is likely that commercial search engines will soon leverage the huge amounts of data collected from their users to focus their crawlers on the topics most important to the searching public. To investigate this idea in the context of a vertical search engine, a system was built in which the crawler and the search engine engage in a symbiotic relationship [44]. The crawler feeds the search engine which in turn helps the crawler. It was found that such a symbiosis can help the system learn about a community's interests and serve such a community with better focus. As discussed in Sect. 8.3, universal crawlers have to somehow focus on the most “important” pages given the impossibility to cover the entire Web and keep a fresh index of it. This has led to the use of global prestige measures such as PageRank to bias universal crawlers, either explicitly [14, 22] or implicitly through the long-tailed structure of the Web graph [41]. An important problem with these approaches is that the focus is dictated by popularity among “average” users and disregards the hetero- geneity of user interests. A page about a mathematical theorem may appear quite uninteresting to the average user, if one compares it to a page about a pop star using indegree or PageRank as a popularity measure. Yet the math page may be highly relevant and important to a small community of users (mathematicians). Future crawlers will have to learn to discriminate between low-quality pages and high-quality pages that are relevant to very small communities. Social networks have recently received much attention among Web users as vehicles to capture commonalities of interests and to share relevant information. We are witnessing an explosion of social and collaborative engines in which user recommendations, opinions, and annotations are aggregated and shared. Mechanisms include tagging (e.g., del.icio.us and 8.8 Some New Developments 357 flickr.com), ratings (e.g., stumbleupon.com), voting (e.g., digg.com), and hierarchical similarity (GiveALink.org). One key advantage of social systems is that they empower humans rather than depending on crawlers to discover relevant resources. Further, the aggregation of user recommendations gives rise to a natural notion of trust. Crawlers could be designed to expand the utility of information collected through social systems. For example it would be straightforward to obtain seed URLs relevant to specific communities of all sizes. Crawlers would then explore the Web for other resources in the neighborhood of these seed pages, exploiting topical locality to locate and index other pages relevant to those communities. Social networks can emerge not only by mining a central repository of user-provided resources, but also by connecting hosts associated with individual users or communities scattered across the Internet. Imagine a user creating its own micro-search engine by employing a personalized topical crawler, seeded for example with a set of bookmarked pages. Desktop search applications make it easy to also share certain local files, if so desired. Can federations of such micro-engine agents emerge on the basis of mutual interests? Peer-to-peer (P2P) networks are beginning to be seen as robust architectures ideal for brokering among individual needs and catering to communities [31]. Adaptive peer-based search systems driven by simple distributed adaptive query routing algorithms can spontaneously organize into networks with efficient communication and with emerging clusters capturing semantic locality. Specifically, in a P2P search application called 6Search (6S), each peer crawls the Web in a focused way, guided by its user’s information context. Each peer submits and responds to queries to/from its neighbors. This search process has no centralized control. Peers depend on local adaptive routing algorithms to dynamically change the topology of the peer network and search for the best neighbors to answer their queries. Machine learning techniques are being explored to improve local adaptive routing. Validation of the 6S framework and network via simulations with 70500 model users based on actual Web crawls has yielded encouraging preliminary results. The network topology rapidly converges from a random network to a small-world network, with clusters emerging to match user communities with shared interests [2]. Additionally the quality of the results is significantly better than obtained by centralized search engines built with equivalent resources, and comparable with the results from much larger search engines such as Google [57, 58]. The integration of effective personalized/topical crawlers with adaptive query routing algorithms is the key to the success of peer-based social search systems. Many synergies may be exploited in this integration by 358 8 Web Crawling leveraging contextual information about the local peer that is readily available to the crawler, as well as information about the peer's neighbors that can be mined through the stream of queries and results routed through the local peer. An open-source prototype of 6S enabling sharing of bookmarks, one-click crawling, and distributed collaborative search is available (http://homer.informatics.indiana.edu/~nan/6S/). If successful, this kind of application could create a new paradigm for crawling and searching where universal crawlers and search engines are complemented with swarms of personal crawlers and micro-engines tuned to the specialized information needs of individual users and dynamic self- organized social networks. Bibliographic Notes General ideas and techniques about crawling can be found in [6, 8], but little is known about implementation details of commercial crawlers. Focused crawling discussed in this chapter is based on [8, 11, 18] . Literature on topical crawling algorithms is extensive [e.g., [1, 14, 16, 17, 24, 33, 34, 37, 38, 46, 48, 50]. Topical crawlers have been used for building focused repositories, automating resource discovery, and supporting software agents. For example, topical crawlers are used to collect papers for building scientific literature digital libraries such as CiteSeer and Google Scholar [29, 33, 56]. Applications of topical crawlers to business and competitive intelligence are discussed in [46], and biomedical applications in [54]. Controversial applications to harvest personal information for spam and phishing purposes are illustrated in [26]. On best-first crawlers, various methods have been used to determine an appropriate textual context in which to evaluate and score unvisited links. Using the anchor text is one strategy [15]. Another strategy is to use windows of a fixed size, e.g., 50 words around the anchor, in place of/in addition to the anchor text [24]. The weighted window used by InfoSpiders [37] yields a weight for each link, which is then fed to a neural network to score each link. In the tag (DOM) tree approach [8], using the parent node of the anchor as aggregation node worked well in a business intelligence crawling task [46]. There is a tradeoff analogous to that between precision and recall when we consider the optimal size of a link context: small contexts (e.g., anchor text) have the highest average similarities to the target page, but also highest chance to miss important cues about the target. Larger contexts (e.g., parent or grand-parent aggregator node) have lower average similarities to the target, but lower chance to miss all the keywords in the target. This suggests a greedy optimization scheme: climb Bibliography 359 the DOM tree from the anchor until sufficient terms are present in the link context [43]. This approach outperformed both the fixed-window method (with optimal window size) and the DOM tree method with a fixed aggregator depth (anchor, parent, or grandparent). Early versions of InfoSpiders were described in [34, 37, 39]. Certain aspects of evolutionary computation have also been used in other topical crawlers such as the itsy bitsy spider [12]. Another adaptive mechanism for topical crawlers inspired by natural processes is ant colony optimization [21]. The idea is that a population of agents leaves a trail of pheromone along the paths that lead to relevant pages, gradually biasing the crawl toward promising portions of the Web graph. A more extensive review of adaptive topical crawling algorithms can be found in [40]. Bibliography 1. Aggarwal, C., F. Al-Garawi, and P. Yu. Intelligent crawling on the World Wide Web with arbitrary predicates. In Proceedings of 10th Internaitonal Conference on World Wide Web (WWW-2001), 2001. 2. Akavipat, R., L. Wu, and F. Menczer. Small world peer networks in distributed Web search. In Proceedings of Alternative Track Papers and Posters Proceedings of International Conference on World Wide Web, 2004. 3. Amento, B., L. Terveen, and W. Hill. Does “authority” mean quality? Predicting expert quality ratings of Web documents. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR- 2000), 2000. 4. Arasu, A., J. Cho, H. Garcia-Molina, A. Paepcke, and S. Raghavan. Searching the web. ACM Transactions on Internet Technology (TOIT), 2001, 1(1): p. 2- 43. 5. Bharat, K. and M. Henzinger. Improved algorithms for topic distillation in a hyperlinked environment. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-1998), 1998. 6. Brin, S. and P. Lawrence. The anatomy of a large-scale hypertextual web search engine. Computer Networks, 1998, 30(1-7): p. 107-117. 7. Broder, A., R. Kumar, F. Maghoul, P. Raghavan, S. Rajagopalan, R. Stata, A. Tomkins, and J. Wiener. Graph structure in the web. Computer Networks, 2000, 33(1-6): p. 309-320. 8. Chakrabarti, S. Mining the Web: discovering knowledge from hypertext data. 2003: Morgan Kaufmann Publishers. 9. Chakrabarti, S., B. Dom, S. Kumar, P. Raghavan, S. Rajagopalan, A. Tomkins, D. Gibson, and J. Kleinberg. Mining the Web's link structure. Computer, 2002, 32(8): p. 60-67. 360 8 Web Crawling 10. Chakrabarti, S., B. Dom, P. Raghavan, S. Rajagopalan, D. Gibson, and J. Kleinberg. Automatic resource compilation by analyzing hyperlink structure and associated text. Computer Networks, 1998, 30(1-7): p. 65-74. 11. Chakrabarti, S., M. Van den Berg, and B. Dom. Focused crawling: a new approach to topic-specific Web resource discovery. Computer Networks, 1999, 31(11-16): p. 1623-1640. 12. Chen, H., Y. Chung, M. Ramsey, and C. Yang. A smart itsy bitsy spider for the web. Journal of the American Society for Information Science, 1998, 49(7): p. 604-618. 13. Cho, J. and H. Garcia-Molina. The evolution of the web and implications for an incremental crawler. In Proceedings of International Conference on Very Large Data Bases (VLDB-2000), 2000. 14. Cho, J., H. Garcia-Molina, and L. Page. Efficient crawling through URL ordering. Computer Networks, 1998, 30(1-7): p. 161-172. 15. Davison, B. Topical locality in the Web. In Proceedings of ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR- 2000), 2000. 16. De Bra, P. and R. Post. Information retrieval in the World-Wide Web: making client-based searching feasible. Computer Networks, 1994, 27(2): p. 183-192. 17. Degeratu, M., G. Pant, and F. Menczer. Latency-dependent fitness in evolutionary multithreaded web agents. In Proceedings of GECCO Workshop on Evolutionary Computation and Multi-Agent Systems, 2001. 18. Diligenti, M., F. Coetzee, S. Lawrence, C. Giles, and M. Gori. Focused crawling using context graphs. In Proceedings of International Conference on Very Large Data Bases (VLDB-2000), 2000. 19. Eichmann, D. Ethical Web agents. Computer Networks and ISDN Systems, 1995, 28(1-2): p. 127-136. 20. Fetterly, D., M. Manasse, M. Najork, and J. Wiener. A large scale study of the evolution of Web pages. Software: Practice and Experience, 2004, 34(2): p. 213-237. 21. Gasparetti, F. and A. Micarelli. Swarm intelligence: Agents for adaptive web search. In Proceedings of European Conf. on Artificial Intelligence (ECAI- 2004), 2004. 22. Henzinger, M., A. Heydon, M. Mitzenmacher, and M. Najork. Measuring index quality using random walks on the Web. Computer Networks, 1999, 31(11-16): p. 1291-1303. 23. Henzinger, M., A. Heydon, M. Mitzenmacher, and M. Najork. On near- uniform URL sampling. Computer Networks, 2000, 33(1-6): p. 295-308. 24. Hersovici, M., M. Jacovi, Y. Maarek, D. Pelleg, M. Shtalhaim, and S. Ur. The shark-search algorithm. An application: tailored Web site mapping. Computer Networks, 1998, 30(1-7): p. 317-326. 25. Heydon, A. and M. Najork. Mercator: A scalable, extensible web crawler. World Wide Web, 1999, 2(4): p. 219-229. 26. Jagatic, T., N. Johnson, M. Jakobsson, and F. Menczer. Social phishing. Communications of the ACM, 2007, 50(10): p. 94-100. Bibliography 361 27. Kaelbling, L., M. Littman, and A. Moore. Reinforcement learning: A survey. Journal of Artificial Intelligence Research 1996, 4: p. 237–285. 28. Kleinberg, J. Authoritative sources in a hyperlinked environment. Journal of the ACM (JACM), 1999, 46(5): p. 604-632. 29. Lawrence, S., L. Giles, and K. Bollacker. Digital libraries and autonomous citation indexing. Computer, 2002, 32(6): p. 67-71. 30. Lin, L. Self-improving reactive agents based on reinforcement learning, planning and teaching. Machine Learning, 1992, 8(3): p. 293-321. 31. Lu, J. and J. Callan. Content-based retrieval in hybrid peer-to-peer networks. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2003), 2003. 32. Maguitman, A., F. Menczer, H. Roinestad, and A. Vespignani. Algorithmic detection of semantic similarity. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 33. McCallum, A., K. Nigam, J. Rennie, and K. Seymore. A machine learning approach to building domain-specific search engines. In Proceedings of International Joint Conference on Artificial Intelligence (IJCAI-1999), 1999. 34. Menczer, F. ARACHNID: Adaptive retrieval agents choosing heuristic neighborhoods for information discovery. In Proceedings of International Conference on Machine Learning (ICML-1997), 1997. 35. Menczer, F. Lexical and semantic clustering by web links. Journal of the American Society for Information Science and Technology, 2004, 55(14): p. 1261-1269. 36. Menczer, F. Mapping the semantics of web text and links. Internet Computing, IEEE, 2005, 9(3): p. 27-36. 37. Menczer, F. and R. Belew. Adaptive retrieval agents: Internalizing local context and scaling up to the Web. Machine Learning, 2000, 39(2): p. 203- 242. 38. Menczer, F., G. Pant, and P. Srinivasan. Topical web crawlers: Evaluating adaptive algorithms. ACM Transactions on Internet Technology (TOIT), 2004, 4(4): p. 378-419. 39. Menczer, F., G. Pant, P. Srinivasan, and M. Ruiz. Evaluating topic-driven Web crawlers. In Proceedings of ACM SIGIR Conf. on Research and Development in Information Retrieval (SIGIR-2001), 2001. 40. Micarelli, A. and F. Gasparetti. Adaptive focused crawling. In P. Brusilovsky, W. Nejdl, and A. Kobsa (eds.), Adaptive Web., 2007: Springer-Verlag. 41. Najork, M. and J. Wiener. Breadth-first crawling yields high-quality pages. In Proceedings of International Conference on World Wide Web (WWW-2001), 2001. 42. Ntoulas, A., J. Cho, and C. Olston. What's new on the web?: the evolution of the web from a search engine perspective. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. 43. Pant, G. Deriving link-context from HTML tag tree. In Proceedings of ACM SIGMOD Workshop on Research Issues in Data Mining and Knowledge Discovery (DMKD’03), 2003. 362 8 Web Crawling 44. Pant, G., S. Bradshaw, and F. Menczer. Search engine-crawler symbiosis: Adapting to community interests. Research and AdvancedTechnology for Digital Libraries, 2004: p. 221-232. 45. Pant, G. and F. Menczer. MySpiders: Evolve your own intelligent Web crawlers. Autonomous Agents and Multi-Agent Systems, 2002, 5(2): p. 221- 229. 46. Pant, G. and F. Menczer. Topical crawling for business intelligence. Research and Advanced Technology for Digital Libraries, 2004: p. 233-244. 47. Pant, G. and P. Srinivasan. Learning to crawl: Comparing classification schemes. ACM Transactions on Information Systems (TOIS), 2005, 23(4): p. 430-462. 48. Pant, G., P. Srinivasan, and F. Menczer. Exploration versus exploitation in topic driven crawlers. In Proceedings of WWW-02 Workshop on Web Dynamics, 2002. 49. Pastor-Satorras, R. and A. Vespignani. Evolution and structure of the Internet: A statistical physics approach. 2004: Cambridge Univ Press. 50. Rennie, J. and A. McCallum. Using reinforcement learning to spider the web efficiently. In Proceedings of International Conference on Machine Learning (ICML-1999), 1999. 51. Rijsbergen, C.v. Information Retrieval. 1979: Butterworths. Second edition. 52. Rumelhart, D., G. Hinton, and R. Williams. Learning internal representations by error propagation. D. Rumelhart and J. McClelland (eds.), Parallel Distributed Processing: Explorations in the Microstructure of Cognition, 1996. 53. Srinivasan, P., F. Menczer, and G. Pant. A general evaluation framework for topical crawlers. Information Retrieval, 2005, 8(3): p. 417-447. 54. Srinivasan, P., J. Mitchell, O. Bodenreider, G. Pant, F. Menczer, and P. Acd. Web crawling agents for retrieving biomedical information. In Proceedings of Workshop on Agents in Bioinformatics (NETTAB’02), 2002. 55. Von Ahn, L., M. Blum, N. Hopper, and J. Langford. CAPTCHA: Using hard AI problems for security. Advances in Cryptology—EUROCRYPT-2003, 2003: p. 646-646. 56. Witten, I., C. Nevill-Manning, and S. Cunningham. Building a digital library for computer science research: technical issues. Australian Computer Science Communications, 1996, 18 p. 534-542. 57. Wu, L., R. Akavipat, and F. Menczer. 6S: Distributing crawling and searching across Web peers. In Proceedings of IASTED Int. Conf. on Web Technologies, Applications, and Services, 2005. 58. Wu, L., R. Akavipat, and F. Menczer. Adaptive query routing in peer Web search. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 9 Structured Data Extraction: Wrapper Generation Web information extraction is the problem of extracting target information items from Web pages. There are two general problems: extracting infor- mation from natural language text and extracting structured data from Web pages. This chapter focuses on extracting structured data. A program for extracting such data is usually called a wrapper. Extracting information from text is studied mainly in the natural language processing community. Structured data on the Web are typically data records retrieved from underlying databases and displayed in Web pages following some fixed templates. In this chapter, we still call them data records. Extracting such data records is useful because it enables us to obtain and integrate data from multiple sources (Web sites and pages) to provide value-added services, e.g., customizable Web information gathering, comparative shopping, meta-search, etc. With more and more companies and organizations disseminating information on the Web, the ability to extract such data from Web pages is becoming increasingly important. At the time of writing this book, there are several companies working on extracting products sold online, product reviews, job postings, research publications, forum discussions, statistics data tables, news articles, search results, etc. Researchers and Internet companies started to work on the extraction problem from the middle of 1990s. There are three main approaches: 1. Manual approach: By observing a Web page and its source code, the human programmer finds some patterns and then writes a program to extract the target data. To make the process simpler for programmers, several pattern specification languages and user interfaces have been built. However, this approach is not scalable to a large number of sites. 2. Wrapper induction: This is the supervised learning approach, and is semi-automatic. The work started around 1995-1996. In this approach, a set of extraction rules is learned from a collection of manually labeled pages or data records. The rules are then employed to extract target data items from other similarly formatted pages. 3. Automatic extraction: This is the unsupervised approach started around 1998. Given a single or multiple pages, it automatically finds B. Liu, Web Data Mining: Exploring Hyperlinks, Contents, and Usage Data, Data-Centric Systems and Applications, DOI 10.1007/978-3-642-19460-3_9, © Springer-Verlag Berlin Heidelberg 2011 363 364 9 Structured Data Extraction: Wrapper Generation patterns or grammars from them for data extraction. Since this approach eliminates the manual labeling effort, it can scale up data extraction to a huge number of sites and pages. The first approach will not be discussed further. This chapter focuses on the last two approaches. Sects. 9.2 and 9.3 study supervised wrapper learning, and the rest of the chapter studies automatic extraction. 9.1 Preliminaries To start our discussion, let us see some real pages that contain structured data that we want to extract. We then develop a Web data model and a HTML mark-up encoding scheme for the data model. Data extraction is simply the reverse engineering task. That is, given the HTML mark-up encoded data (i.e., Web pages), the extraction system recovers the original data model and extracts data from the encoded data records. 9.1.1 Two Types of Data Rich Pages There are mainly two types of data rich pages. Data in such pages are usually retrieved from underlying databases and displayed on the Web following some fixed templates. 1. List pages: Each of such pages contains several lists of objects. Fig. 9.1 shows such a page, which has two lists of products. From a layout point of view, we see two data regions (one horizontal and one vertical). Within each region, the data records are formatted using the same template. The templates used in the two regions are different. 2. Detail pages: Such a page focuses on a single object. For example, in Fig. 9.2, the page focuses on the product “iPod Video 30GB, Black”. That is, it contains all the details of the product, name, image, price and other purchasing information, product description, customer rating, etc. Note that when we say that a page focuses on a particular object (or lists of objects), we do not mean that the page contains no other information. In fact, it almost certainly contains other information. For example, in the page for “iPod Video 30GB, Black” (Fig. 9.2), there are some related products on the right-hand side, company information at the top, and copyright notices, terms and conditions, privacy statements at the bottom, etc. They are not shown in Fig. 9.2 as we want the main part of the product clearly eligible. For list pages, it is often easy to use some heuristics to identify the main data regions, but for detail pages, it is harder. 9.1 Preliminaries 365 Fig. 9.1. A segment of a list page with two data regions Fig. 9.2. A segment of a detail page 366 9 Structured Data Extraction: Wrapper Generation In Fig. 9.1, the description of each product is called a data record. Notice that the data records in this page are all flat with no nesting. Fig. 9.3(A) contains some nested data records, which makes the problem more interesting and also harder. The first product, “Cabinet Organizers by Copco,” has two sizes (9-in. and 12-in.) with different prices. These two organizers are not at the same level as “Cabinet Organizers by Copco”. Our objective: We want to extract the data and produce the data table given in Fig. 9.3(B). “image 1” and “Cabinet Organizers by Copco” are repeated for the first two rows due to the nesting. 9.1.2 Data Model We now describe a data model commonly used for structured data on the Web. In the next sub-section, we present a HTML mark-up encoding of the model and the data, which helps extraction. Most Web data can be modeled as nested relations, which are typed objects allowing nested sets and tuples. The types are defined as follows: (A) An example of a nested data record image 1 Cabinet Organizers by Copco 9-in. Round Turntable: White ***** $4.95 image 1 Cabinet Organizers by Copco 12-in. Round Turntable: White ***** $7.95 image 2 Cabinet Organizers 14.75x9 Cabinet Organizer (Non- skid): White ***** $7.95 image 3 Cabinet Organizers 22x6 Cookware Lid Rack **** $19.95 (B) Extraction results Fig. 9.3. An example input page and output data table 9.1 Preliminaries 367  There is a set of basic types, B = {B1, B2, …, Bk}. Each Bi is an atomic type, and its domain, denoted by dom(Bi), is a set of constants;  If T1, T2, …, Tn are basic or set types, then [T1, T2, …, Tn] is a tuple type with the domain dom([T1, T2, …, Tn]) = {[v1, v2, …, vn] | vi  dom(Ti)};  If T is a tuple type, then {T} is a set type with the domain dom({T}) being the power set of dom(T). A basic type Bi is analogous to the type of an attribute in relational databases, e.g., string and int. In the context of the Web, Bi is usually a text string, image-file, etc. The example in Fig. 9.4 shows a nested tuple type product, with attributes  name (of type string),  image (of type image-file), and  differentSizes (a set type), consisting of a set of tuples with attributes:  size (of type string), and  price (of type string). product [ name: string; image: image-file; differentSizes: { [ size: string; price: string; ] } ] Fig. 9.4. An example nested type We can also define flat tuple and set types:  If T1, T2, …, Tn are basic types, then [T1, T2, …, Tn] is a flat tuple type;  If T is a flat tuple type, then {T} is a flat set type. Classic flat relations are of flat set types. Nested relations are of arbitrary set types. Types can be represented as trees.  A basic type Bi is a leaf tree or node;  A tuple type [T1, T2, …, Tn] is a tree rooted at a tuple node with n sub- trees, one for each Ti;  A set type {T} is a tree rooted at a set node with one sub-tree. An instance of a type T is simply an element of dom(T). Clearly, instances can be represented as trees as well:  An instance (constant) of a basic type is a leaf tree;  A tuple instance [v1, v2, …, vn] forms a tree rooted at a tuple node with n children or sub-trees representing attribute values v1, v2, …, vn;  A set instance {e1, e2, …, en} forms a set node with n children or sub- trees representing the set elements e1, e2, …, and en. 368 9 Structured Data Extraction: Wrapper Generation An instance of a tuple type (also known as a tuple instance) is usually called a data record in the data extraction research. An instance of a set type (also known as a set instance) is usually called a list as in an actual Web page the data records in the set are presented in a particular order. An instance of a flat tuple type is called a flat data record (no nested lists), and an instance of a flat set type is called a list of flat data records. We note that attribute names are not included in the type tree. We next introduce a labeling of a type tree, which is defined recursively:  If a set node is labeled , then its child is labeled .0, a tuple node;  If a tuple node is labeled , then its n children are labeled .1, …, .n. We can think of labels as abstract names for types or attributes. For example, in Fig. 9.4 the top level tuple type is “product”, its three children are attributes: product.name, product.image, and product.differentSizes. .0 labels a tuple node without a name of two attributes, “size” and “price”. 9.1.3 HTML Mark-Up Encoding of Data Instances In a Web page, the data is encoded or formatted with HTML mark-up tags. This sub-section discusses the encoding of data instances in the above abstract data model using HTML tags. Web pages are written in HTML consisting of plain texts, tags and links to image, audio and video files, and other pages. Most HTML tags work in pairs. Each pair consists of an open tag and a close tag indicated by < > and respectively. Within each corresponding tag-pair, there can be other pairs of tags, resulting in nested structures. Thus, HTML tags can naturally encode nested data. We note the following: 1. There are no designated tags for each type as HTML was not designed as a data encoding language. Any HTML tag can be used for any type. 2. For a tuple type, values (data items) of different attributes are usually encoded differently to distinguish them and to highlight important items. 3. A tuple may be partitioned into several groups or sub-tuples. Each group covers a disjoint subset of attributes and may be encoded differently. Based on these characteristics of the HTML language, the HTML mark-up encoding of instances is defined recursively below. We encode based on the type tree, where each node of the tree is associated with an encoding function, which will encode (or mark-up) all the instances of the type in the same way. We will use the tuple type and its attributes explicitly because values of different attributes in the tuple type are typically encoded differently. We use T.i to represent a value instance of the tuple type T and attribute i. We use enc to denote an abstract encoding function. 9.1 Preliminaries 369  For a leaf node of a basic type labeled , an instance c is encoded with enc(:c) = OPEN-TAGS c CLOSE-TAGS where OPEN-TAGS is a sequence of open HTML tags, and CLOSE- TAGS is the sequence of corresponding close HTML tags. The number of tags is greater than or equal to 0.  For a tuple node labeled  of n children or attributes, [.1, …, .n], the attributes are first partitioned into h ( 1) groups <.1, …, .e>, <.(e+1),…, .g> … <.(k+1), …, .n> and an instance [v1, …, vn] of the tuple node is encoded with enc(:[v1, …, vn]) = OPEN-TAGS1 enc(v1) … enc(ve) CLOSE-TAGS1 OPEN-TAGS2 enc(ve+1)…enc(vg) CLOSE-TAGS2 … OPEN-TAGSh enc(vk+1)…enc(vn) CLOSE-TAGSh where OPEN-TAGSi is a sequence of open HTML tags, and CLOSE- TAGSi is the sequence of corresponding close tags. The number of tags is greater than or equal to 0.  For a set node labeled , an non-empty set instance {e1, e2, …, en} is encoded with enc(:{e1, …, en}) = OPEN-TAGS enc(ej1)…enc(ejn) CLOSE-TAGS, where OPEN-TAGS is a sequence of open HTML tags, and CLOSE- TAGS is the sequence of corresponding close HTML tags. The number of tags is greater than or equal to 0. The set elements are ordered based on an ordering function <. With ordering, a set instance is called a list. An empty set instance is encoded with OPEN-TAGS CLOSE-TAGS. By no means does this mark-up encoding cover all cases in Web pages. In fact, each group of a tuple type can be further divided. Anyway, you get the idea. We should also note that in an actual Web page the encoding is usually done not only by HTML tags, but also by words and punctuation marks. For example, in Fig. 9.5, if we are interested in extracting the addresses and the area codes, the punctuation marks are useful. Fig. 9.5. Words and punctuation marks are also used in data encoding 370 9 Structured Data Extraction: Wrapper Generation 9.2 Wrapper Induction We are now ready to study the first approach to data extraction, namely wrapper induction, which is based on supervised learning. A wrapper induction system learns data extraction rules from a set of labeled training examples. Labeling is usually done manually, which simply involves marking the data items in the training pages/examples that the user wants to extract. The learned rules are then applied to extract target data from other pages with the same mark-up encoding or the same template. The algorithm discussed in this section is based on the Stalker system [28]. Related work includes WIEN [19], Softmealy [17], WL2 [10], the systems in [18] and [43], etc. The next section describes a different learning approach, which is based on the IDE system given in [38]. Stalker models the Web data as nested relations. Let us model the restaurant page in Fig. 9.5. It has four addresses in four different cities. The type tree of the data is given in Fig. 9.6 (the country code is omitted). For each type, we also added an intuitive label. The wrapper uses a tree structure based on this to facilitate extraction rule learning and data extraction. Fig. 9.6. Type tree of the restaurant page in Fig. 9.5 Below, we first introduce the data extraction process, and then describe the learning algorithm for generating extraction rules. 9.2.1 Extraction from a Page A Web page can be seen as a sequence of tokens S (e.g., words, numbers and HTML tags). The extraction is done using a tree structure called the EC tree (embedded catalog tree), which models the data embedding in a HTML page. The EC tree is based on the type tree above. The root of the tree is the document containing the whole token sequence S of the page, and the content of each child node is a subsequence of the sequence of its parent node. To extract a node of interest, the wrapper uses the EC description of the page and a set of extraction rules. Fig. 9.7 shows the EC tree of the page in Fig. 9.5. Note that we use LIST here because the set of String: Name String: Street String: City Set: Addresses Integer: Area-Code String: Phone-No. Tuple: Restaurant Tuple: Address 9.2 Wrapper Induction 371 addresses are already ordered in a page. For an extraction task, the EC tree for a data source is specified by the user (not discovered by the system). Fig. 9.7. The EC tree of the HTML page in Fig. 9.5 For each node in the tree, the wrapper identifies or extracts the content of the node from its parent, which contains the sequence of tokens of all its children. Each extraction is done using two rules, the start rule and the end rule. The start rule identifies the beginning of the node and the end rule identifies the end of the node. This strategy is applicable to both leaf nodes (which represent data items) and list nodes. For a list node, list iteration rules are needed to break the list into individual data records (tuple instances). To extract items from the data records, data extraction rules are applied to each record. All the rules are learned during wrapper induction, which will be discussed in Sect. 9.2.2. Given the EC tree and the rules, any node can be extracted by following the tree path P from the root to the node by extracting each node in P from its parent. The extraction rules are based on the idea of landmarks. Each landmark is a sequence of consecutive tokens and is used to locate the beginning or the end of a target item. Let us use the example in Fig. 9.5 to introduce extraction rules and the extraction process based on the EC tree (Fig. 9.7). Fig. 9.8 shows the HTML source code of the page in Fig. 9.5. 1:

Restaurant Name: Good Noodles

2:

  • 205 Willow, Glen, Phone 1-773-366-1987
  • 3:
  • 25 Oak, Forest, Phone (800) 234-7903
  • 4:
  • 324 Halsted St., Chicago, Phone 1-800-996-5023
  • 5:
  • 700 Lake St., Oak Park, Phone: (708) 798-0008
  • Fig. 9.8. The HTML source of the page in Fig. 9.5 Let us try to extract the restaurant name “Good Noodles”. The following rule can be used to identify the beginning of the name: R1: SkipTo() This rule means that the system should start from the beginning of the page and skip all the tokens until it sees the first tag. is a landmark. Name Street City LIST (Addresses) Area-Code Phone-No. Address Page 372 9 Structured Data Extraction: Wrapper Generation Obviously, SkipTo(:) and SkipTo() will not work. According to the EC tree in Fig. 9.7, R1 is applied to the parent of node name, which is the root node. The root node contains the token sequence of the whole page. Similarly, to identify the end of the restaurant name, we can use: R2: SkipTo() R2 is applied from the end of the page toward the beginning. R1 is called the start rule and R2 is called the end rule. Note that a rule may not be unique. For example, we can also use the following rules (and many more) to identify the beginning of the name: R3: SkiptTo(Name _Punctuation_ _HtmlTag_) or R4: SkiptTo(Name) SkipTo() R3 means that we skip everything till the word “Name” followed by a punctuation symbol and then a HTML tag. In this case, “Name _Punctuation_ _HtmlTag_” together is a landmark. _Punctuation_ and _HtmlTag_ are called wildcards. A wildcard represents a class of tokens. For example, _HtmlTag_ represents any HTML tag, i.e., any HTML tag matches the wildcard _HtmlTag_. R4 means that we skip everything till the word “Name” and then again skip everything till the tag . Since wrapper induction algorithms find simple rules first, R1 will be produced. Now, suppose that we also want to extract each area code. The wrapper needs to perform the following steps: 1. Identify the entire list of addresses. We can use the start rule SkipTo(

    ), and the end rule SkipTo(

    ). 2. Iterate through the list (lines 2-5 in Fig. 9.8) to break it into four individual records. To identify the beginning of each address, the wrapper can start from the first token of the parent and repeatedly apply the start rule SkipTo(
  • ) to the content of the list. Each successive identification of the beginning of an address starts from where the previous one ends. Similarly, to identify the end of each address, it starts from the last token of its parent and repeatedly apply the end rule SkipTo(
  • ). Once each address record is identified or extracted, we can extract the area code in it. Due to variations in the format of area codes (some are in italic and some are not), we need to use disjunctions. In this case, the disjunctive start and the end rules are respectively R5 and R6: R5: either SkipTo( ( ) R6: either SkipTo( ) ) or SkipTo(-) or SkipTo() In a disjunctive rule, the disjuncts are applied sequentially until a disjunct can identify the target node. 9.2 Wrapper Induction 373 Finally, we summarize the data extraction features of Stalker. 1. Extraction is done hierarchically based on the EC tree, which enables extraction of items at any level of the hierarchy. 2. The extraction of each node is independent of its siblings. No contextual or ordering information of siblings is used in extraction or rule learning. 3. Each extraction is done using two rules, the start rule and the end rule. Each rule consists of an ordered list of disjuncts (could be one). 9.2.2 Learning Extraction Rules We now present the wrapper learning algorithm for generating extraction rules. The basic idea is as follows: To generate the start rule for a node in the EC tree, some prefix tokens or their wildcards of the node are identified as the landmarks that can uniquely identify the beginning of the node. To generate the end rule for a node, some suffix tokens or their wildcards of the node are identified as the landmarks. The rule generation process for the start rule and the end rule is basically the same. Their applications are also similar except that to apply a start rule the system starts by consuming the first token in the sequence of the parent and goes towards the last token, while for an end rule the system starts from the last token in the sequence of the parent and goes towards the first. Without loss of generality, in this section, we will discuss only the generation of start rules. For rule learning, the user first marks or labels the target items that need to be extracted in a few training examples. For instance, we have the examples in Fig. 9.8, which are addresses from the page in Fig. 9.5. Suppose we want to generate rules to extract the area code from each address. The area codes are labeled (marked) as in Fig. 9.9. A graphic user interface can make the labeling process very easy. Given a set of labeled training examples E, the learning algorithm should generate extraction rules that extract all the target items (also called positive items) without extracting any other items (called negative items). Learning is done based on the machine learning method, sequential covering (see Sect. 3.4.1). The algorithm is given in Fig. 9.10. In each E1: 205 Willow, Glen, Phone 1-773-366-1987 E2: 25 Oak, Forest, Phone (800) 234-7903 E3: 324 Halsted St., Chicago, Phone 1-800-996-5023 E4: 700 Lake St., Oak Park, Phone: (708) 798-0008 Fig. 9.9. Training examples: four addresses with labeled area codes 374 9 Structured Data Extraction: Wrapper Generation iteration, the algorithm LearnRule() (Fig. 9.10) generates a perfect disjunct that covers as many positive items as possible and does not cover any negative item in E (Examples). Then, all the examples whose positive items are covered by the rule are removed. The next iteration starts. The input to LearnRule() is E. Once all the positive items are covered, the rule is returned (line 6), which consists of an ordered list of learned disjuncts. The function LearnDisjunct() performs the actual generation of perfect disjuncts (Fig. 9.11). It works as follows: It first chooses a Seed example (line 1), which is the shortest example. In the case of Fig. 9.9, it is E2. It then generates the initial candidate disjuncts. Let us explain using a generic Seed, which can be represented as follows: t1 t2 … tk tk+1 tk+2 … tn, where ti is a token and is a labeled target item. We call t1 t2 … tk the prefix sequence of the target item, and tk+1tk+2…tn the suffix sequence of the target item. The initial candidate disjuncts for the start rule are tk, and its matching wildcards. Let us use seven wildcards, _Numeric_, _AlphaNum_, _Alphabetic_, _Capitalized_, _AllCaps_, _HtmlTag_, and _Punctuation_. Their meanings are self-explanatory. For the example E2 of Fig. 9.9, the following candidate disjuncts are generated: D1: SkipTo( ( ) D2: SkipTo(_Punctuation_) In line 4 of LearnDisjunct(), the function BestDisjunct() selects the best disjunct using a set of heuristics given in Fig. 9.13. In this case, D1 is selected as the best disjunct. D1 is a perfect disjunct, i.e., it only covers positive items in E2 and E4 but not any negative items in E. D1 is returned from LearnDisjunct(), which also ends the first iteration of LearnRule(). E2 and E4 are removed (line 4) of Fig. 9.10. The next iteration of LearnRule() is left with E1 and E3. LearnDisjunct() will select E1 as Seed as it is shorter. Two candidates are then generated: D3: SkipTo( ) D4: SkipTo( _HtmlTag_ ) Both these two candidates match early in the uncovered examples, E1 and E3. Thus, they cannot uniquely locate the positive items. Even worse, they can match to negative items in the two already covered examples, E2 and E4. Refinement is thus needed, which aims to specialize a disjunct by adding more terminals (a token or one of its matching wildcards) to it. We hope the refined version will be able to uniquely identify the positive items in some examples without matching any negative item in any example in E. Two refinement strategies are used: 9.2 Wrapper Induction 375 Algorithm LearnRule(Examples) // Examples: training examples 1 Rule   // Rule: the returned rule 2 while Examples   do 3 Disjunct  LearnDisjunct(Examples); 4 remove all examples in Examples covered by Disjunct; 5 add Disjunct to Rule 6 return Rule Fig. 9.10. The main learning algorithm  based on sequential covering Function LearnDisjunct(Examples) 1 let Seed  Examples be the shortest example; 2 Candidates  GetInitialCandidates(Seed); 3 while Candidates   do 4 D  BestDisjunct(Candidates); 5 if D is a perfect disjunct then 6 return D 7 Candidates  Candidates  Refine(D, Seed); 8 remove D from Candidates; 9 return D Fig. 9.11. Learning disjuncts Function Refine(D, Seed) 1 D is a consecutive landmarks (l0, l1, …, ln); // li is in fact SkipTo(li) 2 TopologyRefs  LandmarkRefs  ; 3 for i = 1 to n do // t0 or t1 below may be null 4 for each sequence s = t0 li t1 before the target item in Seed do 5 LandmarkRefs  LandmarkRefs  {(l0, …, li1, t0 li, …, ln)}  {(l0, …, li1, x li, …, ln) | x is a wildcard that matches t0}  {(l0, …, li t1, li+1, …, ln)}  {(l0, …, li x, li+1, …, ln) | x is a wildcard that matches t1} 6 for each token t between li1 and li before the target item in Seed do 7 ToplogyRefs  TopologyRefs  {(l0, …, li, t, li+1, …, ln)}  {(l0, …, li, x, li+1, …, ln)} | x is a wildcard that matches t} 8 return TopologyRefs  LandmarkRefs Fig. 9.12. Refining a disjunct to generate more specialized candidates BestDisjunct () prefer candidates that have: - more correct matches - accepts fewer false positives - fewer wildcards - longer end-landmarks Fig. 9.13. Choosing the best disjunct 376 9 Structured Data Extraction: Wrapper Generation 1. Landmark refinement (lines 4-5 in Fig. 9.12): Increase the size of a landmark li by concatenating a terminal (a token t0 or t1, and its matching wildcards) at the beginning or at the end of li. If t0 or t1 does not exist, it will not be considered. We note that each landmark li in the algorithm in Fig. 9.12 actually represents the SkipTo(li). 2. Topology refinement (lines 6-7 in Fig. 9.12): Increase the number of landmarks by adding 1-terminal landmarks, i.e., t and its matching wildcards. Note that l0 is not a landmark, which is used to simplify the algorithm presentation. It represents the beginning of the Seed example. Let us go back to our running example. D3 is selected as the best disjunct (line 4 of Fig. 9.11). Clearly, D3 is not a perfect disjunct. Then, refinement is carried out. Landmark refinement produces the following candidates: D5: SkipTo( - ) D6: SkipTo( _Punctuation_ ) Topology refinement produces the 15 candidates in Fig. 9.14. We can already see that D5, D10, D12, D13, D14, D15, D18 and D21 match correctly with E1 and E3 and fail to match on E2 and E4. Using the heuristics in Fig. 9.13, D5 is selected as the final solution as it has longest last landmark (- ). D5 is then returned by LearnDisjunct(). It is possible that no perfect disjunct can be found after all possible refinements have been tried. In this case, an imperfect best disjunct will be returned (line 9 in Fig. 9.11). Since all the examples are covered, LearnRule() ends and returns the disjunctive (start) rule “either D1 or D5”, i.e., R7: either SkipTo( ( ) or SkipTo(- ) In summary, we note the following: 1. The algorithm presented in this section is by no mean the only possible algorithm. Many variations are possible. Of course, there are also many D7: SkipTo(205) SkipTo() D15: SkipTo(_Numeric_) SkipTo() D8: SkipTo(Willow) SkipTo() D16: SkipTo(_Alphabetic_) SkipTo() D9: SkipTo(,) SkipTo() D17: SkipTo(_Punctuation_) SkipTo() D10: SkipTo() SkipTo() D18: SkipTo(_HtmlTag_) SkipTo() D11: SkipTo(Glen) SkipTo() D19: SkipTo(_Capitalized_) SkipTo() D12: SkipTo(1) SkipTo() D20: SkipTo(_AlphaNum_) SkipTo() D13: SkipTo(-) SkipTo() D21: SkipTo() SkipTo() D14: SkipTo(Phone) SkipTo() Fig. 9.14. All 15 topology refinements of D3 9.2 Wrapper Induction 377 other entirely different algorithms for wrapper induction. 2. In our discussion above, we used only the SkipTo() function in extraction rules. However, in some situations it may not be sufficiently expressive. Therefore, other functions may be added. For example, Stalker also has the SkipUntil() function. Its argument is a part of the target item to be extracted, and is not consumed when the rule is applied. That is, the rule stops right before its occurrence. 9.2.3 Identifying Informative Examples One of the important issues in wrapper learning is the manual labeling of training examples. To ensure accurate learning, a large number of training examples are needed. To manually label them is labor intensive and time consuming. The question is: is it possible to automatically select (unlabelled) examples that are informative for the user to label? Clearly, examples of the same format are of limited use. Examples that represent exceptions are informative as they are different from already labeled examples. Active learning is an approach that helps identify informative unlabeled examples automatically. Given a set of unlabeled examples U, the approach works as follows in the wrapper induction context: 1. Randomly select a small subset L of unlabeled examples from U 2. Manually label the examples in L, and U = U  L; 3. Learn a wrapper W based on the labeled set L; 4. Apply W to U to find a set of informative examples L; 5. Stop if L = , otherwise go to step 2. The key is to find informative examples in step 4. In [27], Muslea et al. proposed a method, called co-testing, to identify informative examples. The idea of co-testing is simple. It exploits the fact that there are often multiple ways of extracting the same item. Thus, the system can learn different rules, forward and backward rules, to locate the same item. Let us use learning of start rules as an example. The rules learned in Sect. 9.2.2 are called forward rules because they consume tokens from the beginning of the example to the end. In a similar way, we can also learn backward rules that consume tokens from the end of the example to the beginning. Given an unlabeled example, both the forward rule and backward rule are applied. If the two rules disagree on the beginning of a target item in the example, this example is given to the user to label. The intuition behind is simple. When the two rules agree, the extraction is very likely to be correct. When the two rules do not agree on the example, one of them must 378 9 Structured Data Extraction: Wrapper Generation be wrong. By giving the user the example to label, we obtain a labeled informative training example. 9.2.4 Wrapper Maintenance Once a wrapper is generated, it is applied to other Web pages that contain similar data and are formatted in the same ways as the training examples. This introduces new problems. 1. If the site changes, does the wrapper know the change? This is called the wrapper verification problem. 2. If the change is correctly detected, how to automatically repair the wrapper? This is called the wrapper repair problem. One way to deal with both problems is to learn the characteristic patterns of the target items, which are then used to monitor the extraction to check whether the extracted items are correct. If they are incorrect, the same patterns can be used to locate the correct items assuming that the page changes are minor formatting changes. This is called re-labeling. After re- labeling, re-learning is performed to produce a new wrapper. These two tasks are very difficult because contextual and/or semantic information is often needed to detect changes and to find the new locations of the target items. Wrapper maintenance is still an active research area. 9.3. Instance-Based Wrapper Learning The wrapper induction method discussed in the previous section requires a set of labeled examples to learn extraction rules. Active learning may be applied to identify informative examples for labeling to reduce the manual labeling effort. In this section, we introduce an instance-based learning approach to wrapper building, which has been implemented by a company and is in commercial use. This method does not learn extraction rules. Instead, it extracts target items in a new instance/page by comparing their prefix and suffix token strings with those of the corresponding items in the labeled examples. At the beginning, the user needs to label only a single example, which is then used to identify target items from unlabeled examples. If some item in an unlabeled example cannot be identified, it is sent for labeling, which is active learning but with no additional mechanism. Thus, in this approach the user labels only a minimum number of training examples. The method described here is based on the IDE algorithm in [38], which is given in Fig. 9.15. It consists of three steps: 9.3. Instance-Based Wrapper Learning 379 1. A random example p from a set of unlabeled training examples S is selected for labeling (line 1). The examples here can be a set of detail pages or a set of data records identified from list pages. We will see in Sect. 9.8 that data records in list pages can be identified automatically. 2. The user labels/marks the target items in the selected example p (line 2). The system also stores a sequence of k consecutive tokens right before each labeled item (called the prefix string of the item) and a sequence of k consecutive tokens right after the labeled item (called the suffix string of the item). The prefix and suffix strings of all target items form a template. Storing the prefix and suffix strings is to avoid keeping the whole page in memory. The value of k does not affect the extraction result. If it is too small, the algorithm can always get more tokens from the original page. In practice, we can give k a large number, say 30, so that the system does not have to refer back to the original page during extraction. The variable Templates keeps all templates (line 3). 3. The algorithm then starts to extract items from unlabeled examples (line 4–9) using the function extract() (line 5). For each unlabeled example d, it compares the stored prefix and suffix strings of each target item with the token string of d to identify its corresponding item. If some item from d cannot be identified, d is passed to the user for labeling (line 6) (which is active learning), i.e., d is an informative example. Let us use an example to show what a template looks like. For example, in the page of Fig. 9.1, we are interested in extracting three items from each product, namely, name, image, and price. The template (Tj) for a labeled example j is represented with: Tj = patname, patimage, patprice Each pati in Tj consists of a prefix string and a suffix string of the item i. For example, if the product image is embedded in the following source: Algorithm IDE(S) // S is the set of unlabeled examples. 1. p  randomSelect(S); // Randomly select a page p from S 2. Tp  labeling(p); // the user labels the page p 3. Templates  Tp; // initialization 4. for each remaining unlabeled example d in S do 5. if (extract(Templates, d)) then 6. Td  labeling(d); 7. insert Td into Templates 8. end-if 9. end-for Fig. 9.15. The IDE algorithm 380 9 Structured Data Extraction: Wrapper Generation … … then we have (we use k = 3 and regard each HTML tag as a token) patimg = (img, prefix:
    ). Extract(Templates, d) function: For each unlabeled example d, extract() tries to use each saved template T ( Templates) to match with the token string of d to identify every target item in d. If a sequence of prefix (and respectively suffix) tokens of a target item g in T matches a sequence of prefix (and suffix) tokens of an item f in d that uniquely identifies f in d, f is regarded as g’s corresponding item in d. By “uniquely identifies”, we mean that only item f in d matches g based on their prefix and suffix strings. An example is given below. After item f, which corresponds to item g in T, is identified and extracted from d, we use the token strings of d before f and after f to find the remaining target items using the same template T. This process continues until all the corresponding items of those items in T are identified from d. If the corresponding item of an item in T cannot be uniquely identified from d, then the extraction using T fails on d. The next template in Templates is tried. If every template in Templates fails on d, d is sent to the user for labeling (line 6 of Fig. 9.15). The algorithm is fairly straightforward, and thus is omitted. See [38] for more details, which also discusses how to deal with some additional issues, e.g., missing items in a page. Fig. 9.16 gives an example to show how a target item is uniquely identified. Assume that 5 tokens
    , suffix:
    . (1) (2)(3) Digital camera 4mp $250.00 Digital camera 5mp $300.00 On Sale Digital camera 3mp Out of stock 418 9 Structured Data Extraction: Wrapper Generation However, the following specifications of a particular product are obviously the attributes of the product. Without knowing the semantics of the encoded data, it is difficult to know that the above three are a set instance and the following two are attributes of a tuple: . If multiple lists of the same type are available, we may have some additional information to make the decision. For instance, one pair of shoes has three colors, and another has four colors. We can be fairly confident that different sets of colors represent set instances (or lists). In the second example, if all products have both height and width, it is more likely that they are attributes. However, these heuristics do not always hold. In some cases, it is hard to decide without understanding of the data semantics. 9.11.4 Labeling and Integration Once the data is extracted from a page/site and put in tables, it is desirable to label each column (assigning an attribute name to it). Some preliminary studies have been reported in [2, 34]. However, the problem is still very much open. Furthermore, the extracted data from multiple sites may need to be integrated. There are two main integration problems. The first one is schema matching, which matches columns of data tables. The second one is data value/instance match. For example, in one site, Coca Cola is called “Coke”, but in another site it is called “Coca Cola”. The problem is: how does the system know that they are the same semantically? In Chap. 10, we will study some data integration techniques. 9.11.5 Domain Specific Extraction In most applications, the user is only interested in some specific data objects, e.g., products sold online, and for each object, only some specific items are needed, e.g., product name, image, and price. Domain specific information can be exploited to simplify and also to speed up the extraction dramatically. Such information can be utilized in at least two ways. 1. Quickly identify pages that may contain required data. For example, it is fairly easy to design some domain heuristics to determine whether a page contains a list of products (a list page). One heuristic is to detect repeated images and repeated prices in some fixed order and interval. Such heuristics are usually very efficient to execute and can be used to filter out those pages that are unlikely to contain required data. The Bibliographic Notes 419 extraction algorithm, which is slower, will only run on those pages that are very likely to contain target data. 2. Identifying target items in a data record. Based on the characteristics of target items, it may be easy to identify and label the target items. For example, it is often easy to find product names and product images based on simple heuristics. If heuristics are not reliable, machine learning methods may be applied to learn models to identify target items. For example, in [44], an extended conditional random fields method (CRF) [21] is used to learn an extraction model, which is then used to extract target items from new data records. 9.12 Discussion Finally, we discuss the main advantages and disadvantages of wrapper induction and automatic data extraction. The key advantage of wrapper induction is that it extracts only the data that the user is interested in. Due to manual labeling, there is no schema matching problem. However, data value or instance matching is still needed. The main disadvantages are that it is not scalable to a large number of sites due to significant manual efforts, and that maintenance is very costly if sites change frequently. The main advantages of automatic extraction are that it is scalable to a huge number of sites, and that there is little maintenance cost. The main disadvantage is that it can extract a large amount of unwanted data because the system does not know what is interesting to the user. Also, in some applications, the extracted data from multiple sites need integration, i.e., their schemas as well as values need to be matched, which are difficult tasks. However, if the application domain is narrow, domain heuristics may be sufficient to filter out unwanted data and to perform the integration tasks. In terms of extraction accuracy, it is reasonable to assume that wrapper induction is more accurate than automatic extraction, although there is no reported large scale study comparing the two approaches. Bibliographic Notes Web data extraction techniques can be classified into three main categories: (1) wrapper programming languages and visual platforms, (2) wrapper induction, and (3) automatic data extraction. The first approach provides some specialized pattern specification languages and visual platforms to help the user construct extraction programs. Systems that follow this approach include WICCAP [23], Wargo [29], Lixto [3], etc. 420 9 Structured Data Extraction: Wrapper Generation The second approach is wrapper induction, which uses supervised learning to learn data extraction rules from a set of manually labeled positive and negative examples. A theoretical work on wrapper learning based on the PAC learning framework was done by Kushmerick [20]. Example wrapper induction systems include WIEN [19], Softmealy [17], Stalker [28], WL2 [10], Thresher [15], IDE [38], [18], [43], etc. Most existing systems are based on inductive learning from a set of labeled examples. IDE [38] employs a simple instance-based learning technique, which performs active learning at the same time so that the user only needs to label a very small number of pages. Related ideas are also used in [7] and [15]. Most existing wrapper induction systems built wrappers based on similar pages from the same site. Zhu et al. [44, 45] reported a system that learns from labeled pages from multiple sites in a specific domain. The resulting wrapper can be used to extract data from other sites. This avoids the labor intensive work of building a wrapper for each site. The third approach is automatic extraction. In [12], Embley et al. studied the automatic identification of data record boundaries given a list page. The technique uses a set of heuristic rules and domain ontologies. In [4], Buttler et al. proposed additional heuristics to perform the task without using domain ontologies. The MDR algorithm discussed in this chapter was proposed by Liu et al. [24]. It uses string edit distance in pattern finding (incidentally, Lloyd Allison has a great page on string edit distance). An algorithm based on the visual information was given by Zhao et al. [42] for extracting search engine results. Another visual based system is given in [31]. These systems, however, do not align or extract data items from data records. Chang et al. [8] reported a semi-automatic system called IEPAD to find extraction patterns from a list page to extract data items. The DeLa system by Wang et al. [34] works similarly. The DEPTA system by Zhai and Liu [40] works in a different way. It first segments data records, and then aligns and extracts data items in the data records using the partial tree alignment algorithm. Both DEPTA and IEPAD do not deal with nested data records, which are dealt with in NET [25] and DeLa [34]. Other related work includes [5, 26, 32, 36], which use the visual information, the domain knowledge or automatically found anchor trees. The RoadRunner system, which needs multiple pages as input, was proposed by Crescenzi et al. [11]. Its theoretical foundation was given by Grumbach and Mecca [13]. Sects. 9.1 and 9.4 are influenced by this paper. The work of RoadRunner was improved by Arasu and Garcia-Molina in their EXALG system [1]. Both systems need multiple input pages with a common schema/template and assume that these pages are given. The Bibliography 421 pages can be either detail pages or list pages. The method proposed in [22] works in a similar setting. A tree-matching based method is given in [30]. Bibliography 1. Arasu, A. and H. Garcia-Molina. Extracting structured data from web pages. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-2003), 2003. 2. Arlotta, L., V. Crescenzi, G. Mecca, and P. Merialdo. Automatic annotation of data extracted from large web sites. In Proceedings of Intl. Workshop on Web and Databases, 2003. 3. Baumgartner, R., S. Flesca, and G. Gottlob. Visual web information extraction with lixto. In Proceedings of International Conference on Very Large Data Bases (VLDB-2001), 2001. 4. Buttler, D., L. Liu, and C. Pu. A fully automated object extraction system for the World Wide Web. In Proceedings of International Conference on Distributed Computing Systems (ICDCS-2001), 2002. 5. Cafarella, M., A. Halevy, D. Wang, E. Wu, and Y. Zhang. Webtables: Exploring the power of tables on the web. In Proceedings of International Conference on Very Large Data Bases (VLDB-2008), 2008. 6. Carrillo, H. and D. Lipman. The multiple sequence alignment problem in biology. SIAM Journal on Applied Mathematics, 1988, 48(5): p. 1073-1082. 7. Chang, C., M. Kayed, M. Girgis, and K. Shaalan. A survey of web information extraction systems. IEEE Transactions on Knowledge and Data Engineering, 2006: p. 1411-1428. 8. Chang, C. and S. Lui. IEPAD: information extraction based on pattern discovery. In Proceedings of International Conference on World Wide Web (WWW-2001), 2001. 9. Chen, W. New algorithm for ordered tree-to-tree correction problem. Journal of Algorithms, 2001, 40(2): p. 135-158. 10. Cohen, W., M. Hurst, and L. Jensen. A flexible learning system for wrapping tables and lists in HTML documents. In Proceedings of International Conference on World Wide Web (WWW-2002), 2002. 11. Crescenzi, V., G. Mecca, and P. Merialdo. RoadRunner: Towards automatic data extraction from large web sites. In Proceedings of International Conference on Very Large Data Bases (VLDB-2001), 2001. 12. Embley, D., Y. Jiang, and Y. Ng. Record-boundary discovery in Web documents. ACM SIGMOD Record, 1999, 28(2): p. 467-478. 13. Grumbach, S. and G. Mecca. In search of the lost schema. Database Theory— ICDT’99, 1999: p. 314-331. 14. Gusfield, D. Algorithms on strings, trees, and sequences: computer science and computational biology. 1997: Cambridge Univ Press. 422 9 Structured Data Extraction: Wrapper Generation 15. Hogue, A. and D. Karger. Thresher: automating the unwrapping of semantic content from the World Wide Web. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 16. Honavar, V. and G. Slutzki. eds. Grammatical Inference. Fourth Intl Colloquium on Grammatical Inference. 1998, LNCS 1433. Springer-Verlag. 17. Hsu, C. and M. Dung. Generating finite-state transducers for semi-structured data extraction from the Web. Information Systems, 1998, 23(8): p. 521-538. 18. Irmak, U. and T. Suel. Interactive wrapper generation with minimal user effort. In Proceedings of International Conference on World Wide Web (WWW-2006), 2006. 19. Kushmerick, N. Wrapper induction for information extraction, PhD Thesis. 1997. 20. Kushmerick, N. Wrapper induction: Efficiency and expressiveness. Artificial Intelligence, 2000, 118(1-2): p. 15-68. 21. Lafferty, J., A. McCallum, and F. Pereira. Conditional random fields: Probabilistic models for segmenting and labeling sequence data. In Proceedings of International Conference on Machine Learning (ICML-2001), 2001. 22. Lerman, K., L. Getoor, S. Minton, and C. Knoblock. Using the structure of Web sites for automatic segmentation of tables. In Proceedings of ACM SIGMOD Conference on Management of Data (SIGMOD-2004), 2004. 23. Li, Z. and W. Ng. Wiccap: From semi-structured data to structured data. In Proceedings of 11th IEEE International Conference and Workshop on the Engineering of Computer-Based Systems (ECBS'04), 2004. 24. Liu, B., R. Grossman, and Y. Zhai. Mining data records in Web pages. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2003), 2003. 25. Liu, B. and Y. Zhai. NET - A System for Extracting Web Data from Flat and Nested Data Records. In Proceedings of Intl. Conf. on Web Information Systems Engineering (WISE2005), 2005. 26. Miao, G., J. Tatemura, W. Hsiung, A. Sawires, and L. Moser. Extracting data records from the web using tag path clustering. In Proceedings of International Conference on World Wide Web (WWW-2009), 2009. 27. Muslea, I., S. Minton, and C. Knoblock. Active learning with multiple views. Journal of Artificial Intelligence Research, 2006, 27(1): p. 203-233. 28. Muslea, I., S. Minton, and C. Knoblock. A hierarchical approach to wrapper induction. In Proceedings of Intl. Conf. on Autonomous Agents (AGENTS- 1999), 1999. 29. Raposo, J., A. Pan, M. Álvarez, J. Hidalgo, and A. Vina. The wargo system: Semi-automatic wrapper generation in presence of complex data access modes. In Proceedings of Workshop on Database and Expert Systems Applications, 2002. 30. Reis, D., P. Golgher, A. Silva, and A. Laender. Automatic web news extraction using tree edit distance. In Proceedings of International Conference on World Wide Web (WWW-2004), 2004. Bibliography 423 31. Simon, K. and G. Lausen. ViPER: augmenting automatic information extraction with visual perceptions. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2005), 2005. 32. Song, X., J. Liu, Y. Cao, C. Lin, and H. Hon. Automatic extraction of web data records containing user-generated content. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2010), 2010. 33. Tai, K. The tree-to-tree correction problem. Journal of the ACM (JACM), 1979, 26(3): p. 433. 34. Wang, J. and F. Lochovsky. Data extraction and label assignment for web databases. In Proceedings of International Conference on World Wide Web (WWW-2003), 2003. 35. Wang, J., B. Shapiro, D. Shasha, K. Zhang, and K. Currey. An algorithm for finding the largest approximately common substructures of two trees. Pattern Analysis and Machine Intelligence, IEEE Transactions on, 2002, 20(8): p. 889-895. 36. Yang, J., R. Cai, Y. Wang, J. Zhu, L. Zhang, and W. Ma. Incorporating site- level knowledge to extract structured data from web forums. In Proceedings of International Conference on World Wide Web (WWW-2009), 2009. 37. Yang, W. Identifying syntactic differences between two programs. Software: Practice and Experience, 1991, 21(7): p. 739-755. 38. Zhai, Y. and B. Liu. Extracting web data using instance-based learning. World Wide Web, 2007, 10(2): p. 113-132. 39. Zhai, Y. and B. Liu. Structured data extraction from the web based on partial tree alignment. IEEE Transactions on Knowledge and Data Engineering, 2006: p. 1614-1628. 40. Zhai, Y. and B. Liu. Web data extraction based on partial tree alignment. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 41. Zhang, K., R. Statman, and D. Shasha. On the editing distance between unordered labeled trees. Information Processing Letters, 1992, 42(3): p. 133- 139. 42. Zhao, H., W. Meng, Z. Wu, V. Raghavan, and C. Yu. Fully automatic wrapper generation for search engines. In Proceedings of International Conference on World Wide Web (WWW-2005), 2005. 43. Zheng, S., R. Song, J. Wen, and C. Giles. Efficient record-level wrapper induction. In Proceedings of ACM International Conference on Information and knowledge management (CIKM-2009), 2009. 44. Zhu, J., Z. Nie, J. Wen, B. Zhang, and W. Ma. 2D conditional random fields for web information extraction. In Proceedings of International Conference on Machine Learning (ICML-2005), 2005. 45. Zhu, J., Z. Nie, J. Wen, B. Zhang, and W. Ma. Simultaneous record detection and attribute labeling in web data extraction. In Proceedings of ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (KDD-2006), 2006. 10 Information Integration In Chap. 9, we studied data extraction from Web pages. The extracted data is put in tables. For an application, it is, however, often not sufficient to extract data from only a single site. Instead, data from a large number of sites are gathered in order to provide value-added services. In such cases, extraction is only part of the story. The other part is the integration of the extracted data to produce a consistent and coherent database because different sites typically use different data formats. Intuitively, integration means to match columns in different data tables that contain the same type of information (e.g., product names) and to match values that are semantically identical but represented differently in different Web sites (e.g., “Coke” and “Coca Cola”). Unfortunately, limited integration research has been done so far in this specific context. Much of the Web information integration research has been focused on the integration of Web query interfaces. This chapter will have several sections on their integration. However, many ideas developed are also applicable to the integration of the extracted data because the problems are similar. Web query interfaces are used to formulate queries to retrieve needed data from Web databases (called the deep Web). Fig. 10.1 shows two query interfaces from two travel sites, expedia.com and vacation.com. The user who wants to buy an air ticket typically tries many sites to find the cheapest ticket. Given a large number of alternative sites, he/she has to access each individually in order to find the best price, which is tedious. To reduce the manual effort, we can construct a global query interface that allows uniform access to disparate relevant sources. The user can then fill in his/her requirements in this single global interface and all the underlying sources (or databases) will be automatically filled and searched. The retrieved results from multiple sources also need to be integrated. Both integration problems, i.e., integration of query interfaces and integration of returned results, are challenging due to the heterogeneity of Web sites. Clearly, integration is not peculiar
    are saved in the prefix string of item price from a labeled example. Given an unlabeled example, after scanning through its token string, we obtain the match situation in Fig. 9.16. That is, we find 4 ’s, three together, and only one together, which can match some prefix tokens of price. These are shown in four rows below the saved prefix string. The prefix: … … , which can be hard to fix if multiple levels of nesting exist. There are open source programs that can be used to clean up HTML pages. One popular program is called tidy (available at http://tidy.sourceforge. net/). 2. Tree building: We can follow the nested blocks of the HTML tags in the page to build the DOM tree. It is fairly straightforward. We will not discuss it further. This method works for most pages. However, for some ill-formatted tags, even the tidy program cannot fix. Then, the constructed DOM trees may be wrong, which makes it difficult for subsequent data extraction. Using Tags and Visual Cues: Instead of analyzing the HTML code to fix errors, rendering or visual information (i.e., the locations on the screen at which tags are rendered) can be used to infer the structural relationship among tags and to construct a DOM tree. This method leads to more robust tree construction due to the high error tolerance of the rendering engines of Web browsers (e.g., Internet Explorer). As long as the browser is able to render a page correctly, its tag tree can be built correctly. 9.8 Extraction Based on a Single List Page: Flat Data Records 397 In a Web browser, each HTML element (consisting of an open tag, optional attributes, optional embedded HTML content, and a close tag that may be omitted) is rendered as a rectangle. The visual information can be obtained after the HTML code is rendered by a Web browser. A DOM tree can then be constructed based on the nested rectangles (resulted from nested tags). The details are as follows: 1. Find the four boundaries of the rectangle of each HTML element by calling the rendering engine of a browser, e.g., Internet Explorer. 2. Follow the sequence of open tags and perform containment checks to build the tree. Containment check means checking if one rectangle is contained in another. Let us use an example to illustrate the process. Assume we have the HTML code on the left of Fig. 9.25. However, there are three errors in the code. The close tag for line 3 is put after the open tag for line 4. Also, the close tags in line 5 and in line 7 are missing. However, this HTML segment can be rendered correctly in a browser, with the boundary coordinates for each HTML element shown in the middle of Fig. 9.25. Using this visual information, it is easy to build the tree on the right. Fig. 9.25. A HTML code segment, boundary coordinates and the resulting tree 9.8 Extraction Based on a Single List Page: Flat Data Records We are now ready to perform the data extraction task. In this and the next sections, we study the first extraction problem in Sect. 9.4.1, i.e., extraction based on a single list page. This section focuses on a simpler case, i.e., a list (a data region) containing only flat data records (no nesting). We assume that the DOM tree has been built for the page. In Sect. 9.10, we 1
    price  (10)  (17) (18)  (23) (24) (25)  (67) (68) 1 3 4 Fig. 9.16. The price is found uniquely. … …… … 8 9 10 15 16 17 18 …
    $25.00 …... 22 23 24 25 ...
    65 66 67 68 HTML source of the 9.4 Automatic Wrapper Generation: Problems 381 number within each “( )” is the sequence id of the token (the tag) in the unlabeled example. “” means no match. The HTML source is given in the box of Fig. 9.16 with sequence id’s attached. We observe that the beginning of price is uniquely identified because the sequence of prefix tokens of price,
    , has only one match. Note that we do not need to use all the saved tokens in the prefix string of price. This technique is thus called sufficient match. We see that is not unique because there are 4 ’s. is not unique because there are 3 matches. Once the beginning of item price is found, the algorithm tried to locate the ending of item price in the same way by comparing suffix strings in the opposite direction. After item price is identified and extracted, the algorithm goes to identify other items if they are not extracted. The final set of templates and the extract() function together form a wrapper, which can be used to extract target items from future examples. Apart from performing active learning automatically, there are two other interesting features about IDE. Firstly, there is no pre-specified sequence of items to be extracted. For example, the order of items in the HTML source may be: name, price, and image. If at the beginning we can identify item price uniquely in the unlabeled example, we can then start from price and search forward to find item image and search backward to find item name. The final extraction sequence of items may be price, image and name. Secondly, the method exploits local contexts in extraction. It may be the case that from the whole page/data record we are unable to identify a particular item. However, within a local area, it is easy to identify the item. For instance, in the above example, after identifying item price, we only need to search for item image in the rest of the input. Even a similar item appears before price, it will not be considered. Evaluation results in [38] show that this simple technique works very well. 9.4 Automatic Wrapper Generation: Problems Wrapper generation using supervised learning has two main shortcomings: 1. It is not suitable for a large number of sites due to the manual labeling effort. For example, if a shopping site wants to extract all the products sold on the Web, manual labeling becomes almost an impossible task. 2. Wrapper maintenance is very costly. The Web is a dynamic environment. Sites change constantly. Since wrapper learning systems mainly rely on HTML formatting tags, if a site changes its formatting templates, the existing wrapper for the site will become invalid. As we discussed earlier, automatic verification and repair are still difficult. Doing them manually is very costly if the number of sites involved is large. 382 9 Structured Data Extraction: Wrapper Generation Due to these problems, automatic (or unsupervised) extraction has been studied by researchers. Automatic extraction is possible because data records (tuple instances) in a Web site are usually encoded using a very small number of fixed templates. It is possible to find these templates by mining repeated patterns in multiple data records. The rest of the chapter focuses on automatic extraction. Note that in general we use the term “templates” to refer to hidden templates employed by Web page designers. We use the term “patterns” to refer to regular structures that the system has discovered. 9.4.1 Two Extraction Problems In Sects. 9.1.2 and 9.1.3, we described an abstract model of structured data on the Web (i.e., nested relations), and a HTML mark-up encoding of the data model respectively. The general goal of data extraction is to recover the hidden schema from the HTML mark-up encoded data. In the rest of the chapter, we focus on two problems, which are really quite similar. Problem 1: Extraction Based on a Single List Page Input: A single HTML string S, which contains k non-overlapping substrings s1, s2, …, sk with each si encoding an instance of a set type. That is, each si contains a collection Wi of mi ( 2) non-overlapping sub- substrings encoding mi instances of a tuple type. Output: k tuple types 1, 2, …, k, and k collections C1, C2, …, Ck of instances of the tuple types such that for each collection Ci there is a HTML encoding function enci such that enci: Ci  Wi is a bijection. We use the example in Fig. 9.1 to explain. The input string S is the full Web page (only part of it is shown in Fig. 9.1). In this page, there are two substrings s1 and s2 that encode two set instances, i.e., the two sets of data records. s1 consists of four encodings (displayed horizontally) enc1(I1), enc1(I2), enc1(I3), enc1(I4) of four product instances I1, I2, I3, I4 of a tuple type 1, according to some mark-up encoding function enc1. Similarly, s2 consists of encodings of some other products (displayed vertically). One important note is that S often contains some other information (not shown in Fig. 9.1) apart from the encoded data. An algorithm needs to work on the string S to find each substring and construct the tuple type by generating a pattern from each substring representing the mark-up encoding function enci. The pattern may be represented as a regular expression. Data extraction can be done using the regular expression or the original pattern as we will see in Sect. 9.11.1. 9.4 Automatic Wrapper Generation: Problems 383 Problem 2: Extraction Based on Multiple Pages Input: A collection W of k HTML strings, which encodes k instances of the same type. Output: A type , and a collection C of instances of type , such that there is a HTML encoding enc such that enc: C  W is a bijection. The input consists of a collection of k encodings enc(I1), enc(I2), …, enc(Ik) of instances I1, I2, …, Ik of a nested type , according to some mark-up encoding function enc. An algorithm works on the encoded instances and constructs the type by generating a pattern (the encoding function enc), which again may be represented as a regular expression and used to extract data from other pages. Note that, for this problem, the input may be a set of detail pages (of a tuple type) or list pages (of a set type). The next few sections describe several techniques to solve the two problems. As we will see in Sect. 9.10, most techniques for solving problem 1 can also be used for solving problem 2. 9.4.2 Patterns as Regular Expressions A regular expression can be naturally used to model the HTML encoded version of a nested type. Given an alphabet of symbols Σ and a special token “#text” that is not in Σ, a regular expression over Σ is a string over Σ  {#text, *, ?, |, (, )} defined as follows:  The empty string ε and all elements of   {#text} are regular expressions.  If A and B are regular expressions, then AB, (A|B) and (A)? are regular expressions, where (A|B) stands for A or B and (A)? stands for (A|ε).  If A is a regular expression, (A)* is a regular expression, where (A)* stands for ε or A or AA or ... We also use (A)+ as a shortcut for A(A)*, which can be used to model the set type of a list of tuples. (A)? indicates that A is optional. (A|B) represents a disjunction. If a regular expression does not include (A|B), it is called a union-free regular expression. Regular expressions are often employed to represent extraction patterns (or encoding functions). However, extraction patterns do not have to be regular expressions, as we will see later. Given a regular expression, a nondeterministic finite-state automaton can be constructed and employed to match its occurrences in string sequences representing Web pages. In the process, data items can be extracted, which are text strings represented by #text. 384 9 Structured Data Extraction: Wrapper Generation 9.5 String Matching and Tree Matching As we can see from both problems in Sect. 9.4.1, the key is to find the encoding template from a collection of encoded instances of the same type. A natural way to do this is to detect repeated patterns from HTML encoding strings. String matching and tree matching are obvious techniques for the task. Tree matching is useful because HTML encoding strings also form nested structures due to their nested HTML tags. Such nested structures can be modeled as trees, commonly known as DOM (tag) trees. DOM stands for Document Object Model (http://www.w3.org/DOM/). Below we describe some string matching and tree matching algorithms. 9.5.1 String Edit Distance String edit distance (also known as Levenshtein distance) is perhaps the most widely used string matching/comparison technique. The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of: (1) change a character, (2) insert a character, and (3) delete a character. Assume we are given two strings s1 and s2. The following recurrence relations define the edit distance, d(s1, s2), of two strings s1 and s2: d(ε, ε) = 0 // ε represents an empty string d(s, ε) = d(ε, s) = |s| // |s| is the length of string s d(s1–+c1, s2–+c2) = min(d(s1–, s2–) + p(c1, c2), d(s1–+c1, s2–) + 1, d(s1–, s2–+c2) + 1), where c1 and c2 are the last characters of s1 (= s1–+c1) and s2 (= s2–+c2) respectively, and p(c1, c2) = 0 if c1 = c2; p(c1, c2) = 1, otherwise. The first two rules are obvious. Let us examine the last one. Since neither string is empty, each has a last character, c1 and c2 respectively. c1 and c2 have to be explained in an edit of s1–+c1 into s2–+c2. If c1 = c2, they match with no penalty, i.e., p(c1, c2) = 0, and the overall edit distance is d(s1–, s2–). If c1  c2, then c1 could be changed into c2, giving p(c1, c2) = 1 and an overall cost d(s1–, s2–)+1. Another possibility is to edit s1–+c1 into s2– and then insert c2, giving d(s1–+c1, s2–)+1. The last possibility is to delete c1 and edit s1– into s2–+c2, giving d(s1–, s2–+c2)+1. There are no other alternatives. We take the least expensive, i.e., min. of these alternatives. From the relations, we can see that d(s1, s2) depends only on d(s1, s2) where s1 is a shorter string than s1, or s2 is a shorter string than s2, or both. Thus, the dynamic programming technique can be applied to compute the edit distance of two strings. 9.5 String Matching and Tree Matching 385 We can use a two-dimensional matrix, m[0..|s1|, 0..|s2|], to hold the edit distances. The low right corner cell m(|s1|, |s2|) will furnish the required value of the edit distance d(s1, s2). We have m[0, 0] = 0 m[i, 0] = i, i =1, 2, ..., |s1| m[0, j] = j, j =1, 2, ..., |s2| m[i, j] = min(m[i1, j1] + p(s1[i], s2[j]), m[i1, j] + 1, m[i, j1] + 1 ), where i = 1, 2, ..., |s1|, j = 1, 2, ..., |s2|, and p(s1[i], s2[j]) = 0 if s1[i] = s2[j]; p(s1[i], s2[j]) = 1, otherwise. Once the edit distance computation is completed, we can find the alignment of characters that give the final distance. For this, we need to record which case in the above recursive rule minimizes the distance, and then trace back the path that corresponds to the best alignment. Note that, in many cases, the minimal choice is not unique, and different paths could have been drawn, which indicate alternative optimal alignments. Example 1: We want to compute the edit distance and find the alignment of the following two strings: s1: X G Y X Y X Y X s2: X Y X Y X Y T X The edit distance matrix is given in Fig. 9.17. The final edit distance value is 2, which is the value in the bottom right corner cell. Fig. 9.17 also shows the trace back path. Notice that a diagonal line means match or change, a vertical line means insertion, and a horizontal line means deletion. Thus, the final alignment of our two strings is: s1: X G Y X Y X Y  X s2: X  Y X Y X Y T X Fig. 9.17. The edit distance matrix and back trace path s1 X G Y X Y X Y X s2 0 1 2 3 4 5 6 7 8 X 1 0 1 2 3 4 5 6 7 Y 2 1 1 1 2 3 4 5 6 X 3 2 2 2 1 2 3 4 5 Y 4 3 3 2 2 1 2 3 4 X 5 4 4 3 2 2 1 2 3 Y 6 5 5 4 3 2 2 1 2 T 7 6 6 5 4 3 3 2 2 X 8 7 7 6 5 4 3 3 2 386 9 Structured Data Extraction: Wrapper Generation The time-complexity of the algorithm is O(|s1||s2|) (to fill the matrix). The space complexity is also O(|s1||s2|). Back trace takes O(|s1|+|s2|) time. The normalized edit distance ND(s1, s2) is defined as the edit distance divided by the mean length of the two strings: . 2/|)||(| ),(),( 21 21 21 ss ssdssND   (1) Another commonly used denominator is max(|s1|, |s2|). Finally, in data extraction, “change a character” may be undesirable (which represents a disjunction in regular expressions). A large distance may be used to disallow it. We will discuss this issue again in Sect. 9.11.2. 9.5.2 Tree Matching Like string edit distance, tree edit distance between two trees A and B (labeled ordered rooted trees) is the cost associated with the minimum set of operations needed to transform A into B. In the classic formulation, the set of operations used to define tree edit distance includes, node removal, node insertion, and node replacement. A cost is assigned to each operation. Solving the tree edit distance problem is to find a minimum-cost mapping between two trees. The concept of mapping is formally defined as: Let X be a tree and let X[i] be the ith node of tree X in a preorder walk of the tree. A mapping M between a tree A of size n1 and a tree B of size n2 is a set of ordered pairs (i, j), one from each tree, satisfying the following conditions for all (i1, j1), (i2, j2)  M: (1) i1 = i2 iff j1 = j2; (2) A[i1] is on the left of A[i2] iff B[j1] is on the left B[j2]; (3) A[i1] is an ancestor of A[i2] iff B[j1] is an ancestor of B[j2]. Intuitively, the definition requires that each node appears no more than once in a mapping and the order among siblings and the hierarchical relation among nodes are preserved. Fig. 9.18 shows a mapping example. Several algorithms have been proposed to address the problem of finding the minimum set of operations (i.e., the one with the minimum cost) to transform one tree into another. All the formulations have complexities above quadratic. In [33], a solution based on dynamic programming is presented with the complexity of O(n1n2h1h2), where n1 and n2 are the sizes of the trees and h1 and h2 are the heights of the trees. In [9, 35], two improved algorithms are presented, and in [41], it is shown that if the trees are not ordered, the problem is NP-complete. 9.5 String Matching and Tree Matching 387 Fig. 9.18. A general tree mapping example In the above general setting, mapping can cross levels, e.g., node a in tree A and node a in tree B (Fig. 9.18). Replacements are also allowed, e.g., node b in A and node h in B. We now define a restricted tree mapping [37], called simple tree matching (STM), in which no node replacement and no level crossing are allowed. In STM, the aim is to find the maximum matching between two trees (not the edit distance of two trees). This restricted model has been found quite effective for Web data extraction. Let A and B be two trees, and i  A and j  B be two nodes in A and B respectively. A matching between two trees is defined to be a mapping M such that, for every pair (i, j)  M where i and j are non-root nodes, (parent(i), parent(j))  M. A maximum matching is a matching with the maximum number of pairs. Let A = RA:A1, …, Ak and B = RB:B1,…, Bn be two trees, where RA and RB are the roots of A and B, and Ai and Bj are the ith and jth first-level sub- trees of A and B respectively. Let W(A, B) be the number of pairs in the maximum matching of trees A and B. If RA and RB contain identical symbols, the maximum matching between A and B (i.e., W(A, B)) is m(A1, …, Ak, B1, …, Bn) + 1, where m(A1, …, Ak, B1, …, Bn) is the number of pairs in the maximum matching of A1, …, Ak and B1, …, Bn. If RA  RB, W(A, B)) = 0. Formally, W(A, B) is defined as follows:      ;otherwise1),...,,,...,( if 0),( 11 nk BA BBAAm RRBAW m(, ) = 0 //  represents an empty sub-tree list. m(s, ) = m(, s) = 0 // s matches any non-empty sub-tree list m(A1, …, Ak, B1, …, Bn) = max(m(A1, …, Ak-1, B1, …, Bn-1) + W(Ak, Bn), m(A1, …, Ak, B1, …, Bn-1), m(A1, …, Ak-1, B1, …, Bn)). This definition of m is similar to that of the string edit distance except that here we compute the maximum matching rather than the distance and c b a p c h e p d a d A B 388 9 Structured Data Extraction: Wrapper Generation that W(Ak, Bn) needs to be computed recursively since Ak and Bn are sub- trees. Clearly, the dynamic programming technique is again applicable. We now give an algorithm for simple tree matching (STM), which computes W(A, B). The algorithm is also called STM (Fig. 9.19). STM is a top-down algorithm. It evaluates the similarity by producing the maximum matching through dynamic programming. The algorithm has the complexity of O(n1n2), where n1 and n2 are the sizes of trees A and B respectively. In line 1, the roots of A and B are compared first. If the roots contain distinct symbols, then the two trees do not match at all. If the roots contain identical symbols, then the algorithm recursively finds the maximum matching between first-level sub-trees of A and B and save it in a W matrix (line 8). Based on the W matrix, a dynamic programming scheme is applied to find the number of pairs in a maximum matching between two trees A and B. We use an example (Fig. 9.20) to explain the algorithm. To find the maximum matching between trees A and B, their roots (N1 and N15) are compared first. Since N1 and N15 contain identical symbols, m1,15[4, 2]+1 is returned as the maximum matching value between trees A and B (line 11). The m1,15 matrix is computed based on the W1,15 matrix. Each entry in W1,15, e.g., W1,15[i, j], is the maximum matching between the ith and jth first-level sub-trees of A and B, which is computed recursively based on its m matrix. For example, W1,15[4, 2] is computed recursively by building the matrices (E)-(H). All the relevant cells are shaded. The zero column and row in m matrices are initializations. Note that we use subscripts for both m and W to indicate the nodes that they are working on. Algorithm: STM(A, B) 1. if the roots of the two trees A and B contain distinct symbols then 2. return (0) 3. else k  the number of first-level sub-trees of A; 4. n  the number of first-level sub-trees of B; 5. Initialization: m[i, 0]  0 for i = 0, …, k; m[0, j]  0 for j = 0, …, n; 6. for i = 1 to k do 7. for j = 1 to n do 8. m[i, j]  max(m[i, j1], m[i1, j], m[i1, j1]+W[i, j]), where W[i, j]  STM(Ai, Bj) 9. end-for 10. end-for 11. return (m[k, n]+1) 12. end-if Fig. 9.19. The simple tree matching (STM) algorithm 9.5 String Matching and Tree Matching 389 Fig. 9.20. (A) Tree A; (B) Tree B; (C) m matrix for the first level sub-trees of N1 and N15; (D) W matrix for the first level sub-trees of N1 and N15; (E)-(H) m matrixes and W matrixes for the lower level sub-trees. The normalized simple tree matching NSTM(A, B) is obtained by dividing the matching score by the mean number of nodes in the two trees: 2/))()(( ),(),( BnodesAnodes BASTMBANSTM   . (2) We may also use max(nodes(A), nodes(B)) as the denominator. nodes(X) denotes the number of nodes in tree X. Similar to string edit distance, after matching computation, we can trace back in the m matrices to find the aligned nodes from the two trees. e d d e j b b c f p c g h i b p c g h d e 0 1 (N16) 2 (N16-N17) 0 0 0 0 1 (N2) 0 3 3 2 (N2-N3) 0 3 5 3 (N2-N4) 0 3 5 4 (N2-N5) 0 3 6 1 (N16) 2 (N17) 1 (N2) 3 0 2 (N3) 0 2 3 (N4) 2 0 4 (N5) 0 3 0 1 (N20) 2 (N20-N21) 0 0 0 0 1 (N11) 0 2 2 1(N20) 2(N21) 1 (N11) 2 0 1 (N22) 1 (N12) 1 2 (N13) 0 3 (N14) 0 (C) m1,15 (D) W1,15 (E) m5,17 (G) m11,20 (H) W11,20 (F) W5,17 (A) (B) N1 N15 N2 N3 N4 N5 N6 N7 N8 N9 N10 N11 N12 N13 N14 N18 N19 N20 N21 N22 N16 N17 f 0 1 (N22) 0 0 0 1 (N12) 0 1 2 (N12-N13) 0 1 3 (N12-N14) 0 1 390 9 Structured Data Extraction: Wrapper Generation 9.6 Multiple Alignment In order to find repeated patterns from HTML strings based on string edit distance or tree matching, we need alignments of strings and trees. We have discussed how to obtain the alignment of two strings or trees. However, a Web page usually contains more than two data records, thus more than two strings or trees need to be aligned. Producing a global alignment of all the strings or trees is crucial. The task is called multiple alignment. In [6], Carrillo and Lipman proposed an optimal multiple alignment based on multidimensional dynamic programming. However, its time complexity is exponential, and is thus not suitable for practical use. Many heuristic methods exist. We describe two of them: the center star method and the partial tree alignment method in [40]. 9.6.1 Center Star Method This is a classic technique [14]. It is commonly used for multiple string alignments, but can be adopted for trees. The method is applied to data extraction based on alignments of HTML strings in [8]. Let the set of strings to be aligned be S. In the method, a string sc that minimizes  Ss ici ssd ),( (3) is first selected as the center string. d(sc, si) is the distance of two strings. The algorithm then iteratively computes the alignment of rest of the strings with sc. Spaces are added when needed. The algorithm is in Fig. 9.21. CenterStar(S) 1. choose the center star sc using Equation (3); 2. initialize the multiple sequence alignment M that contains only sc; 4. for each s in S-{sc} do 5. let c* be the aligned version of sc in M; 6. let s and c* be the optimally aligned strings of s and c*; 7. add aligned strings s and c* into the multiple alignment M; 8. add spaces to each string in M, except, s and c*, at locations where new spaces are added to c* 9. endfor 10. return multiple string alignment M Fig. 9.21. The center star algorithm Example 2: We have three strings, i.e., S = {ABC, XBC, XAB}. ABC is selected as the center string sc. Let us align the other strings with ABC. 9.6 Multiple Alignment 391 Iteration 1: Align c* (= sc) with s =XBC: c* : A B C | | s : X B C Update M: A B C  A B C X B C Iteration 2: Align c* with s = XAB: c* :  A B C | | s : X A B  Update M: A B C   A B C X B C  X B C X A B  Assume there are k strings in S and all strings have length n, finding the center takes O(k2n2) time and the iterative pair-wise alignment takes O(kn2) time. Thus, the overall time complexity is O(k2n2). For our data extraction task, this method has two shortcomings: 1. the algorithm runs slowly for pages containing many data records and/or data records containing many tags (i.e., long strings) because finding the center string needs O(k2n2) time. 2. if the center string (or tree) does not have a particular data item, other data records that contain the same data item may not be aligned properly. For example, the letter X’s in the last two strings (in bold) are not aligned in the final result, but they should. Let us discuss the second point further. As we mentioned in Sect. 9.5.1, giving the cost of 1 for “changing a letter” in edit distance is problematic (e.g., A and X in the first and second strings in the final result) because of optional data items in data records. The problem can be partially dealt with by disallowing “changing a letter” (e.g., giving it a very large cost). However, this introduces another problem. For example, if we align only ABC and XBC, it is not clear which of the following alignment is better. (1) A – B C (2) – A B C – X B C X – B C If we also consider the string XAB, then (2) is better. However, the center star method does not have this global view. The partial tree alignment algorithm described below deals with both problems nicely. 9.6.2 Partial Tree Alignment This method is proposed in [40] for multiple tree alignment in the context of data extraction. It can also be used for aligning multiple strings. For 392 9 Structured Data Extraction: Wrapper Generation simplicity, we describe the method in the context of trees. The main idea is as follows: The algorithm aligns multiple trees by progressively growing a seed tree. The seed tree, denoted by Ts, is initially picked to be the tree with the maximum number of data fields, which is similar to the center string but without the O(k2n2) pair-wise tree matching to choose it. The reason for choosing this seed tree is clear as it is more likely for this tree to have a good alignment with data fields in other data records. Then, for each Ti (i ≠ s), the algorithm finds for each node in Ti a matching node in Ts. If no match can be found for a node vi, then the algorithm attempts to expand the seed tree by inserting vi into Ts. The expanded seed tree Ts is then used in subsequent matching. The insertion is done only if a position for vi can be uniquely determined in Ts. Otherwise, it is left unmatched. Thus the alignment is partial. It represents a least commitment approach. Early uncertain commitments can result in undesirable effects for later matches. Note that although the method was designed originally for aligning multiple trees, it can also be adapted for aligning multiple strings. Partial Alignment of Two Trees Before presenting the full algorithm for aligning multiple trees, let us first look at the partial alignment of two trees. As indicated above, after Ts and Ti are matched, some nodes in Ti can be aligned with their corresponding nodes of Ts because they match one another. For those nodes in Ti that are not matched, we want to insert them into Ts as they may contain optional data items. There are two possible situations when inserting a new node vi from Ti into Ts, depending on whether a location in Ts can be uniquely determined to insert vi. Instead of considering a single node vi, we can consider each set of unmatched consecutive sibling nodes vj…vm from Ti together. Without loss of generality, we assume that the parent of vj…vm has a match in Ts and we want to insert vj…vm into Ts under the same parent node. We only insert vj…vm into Ts if a position for inserting vj…vm can be uniquely determined in Ts. Otherwise, they will not be inserted into Ts and left unaligned. The location for inserting vj…vm can be uniquely decided: 1. If vj…vm have two neighboring siblings in Ti, one on the right and one on the left, that are matched with two consecutive siblings in Ts. Fig. 9.22(A) shows such a situation, which gives one part of Ts and one part of Ti. We can see that node c in Ti can be inserted into Ts between node b and node e in Ts because node b and node e in Ts and Ti match. The new (extended) Ts is also shown in Fig. 9.22(A). We note that nodes a, b, c and e may also have their own children. We did not draw them to save space. This applies to all the cases below. 9.6 Multiple Alignment 393 2. If vj…vm has only one left neighboring sibling x in Ti and x matches the right most node x in Ts, then vj…vm can be inserted after node x in Ts. Fig. 9.22(B) illustrates this case. 3. If vj…vm has only one right neighboring sibling x in Ti and it matches the left most node x in Ts, then vj…vm can be inserted before node x in Ts. This case is similar to the second case above. Otherwise, we cannot uniquely decide a location for unmatched nodes in Ti to be inserted into Ts. This is shown in Fig. 9.22(C). The unmatched node x in Ti could be inserted into Ts in two positions, between nodes a and b, or between node b and e in Ts. In this situation, we will not insert it into Ts. Fig. 9.22. Expand the seed: (A) and (B) unique insertion; (C) insertion ambiguity Partial Alignment of Multiple Trees Fig. 9.23 gives the full algorithm for multiple tree alignment based on partial alignment of two trees. S is the set of input trees. We use a simple example in Fig. 9.24 to explain the algorithm. S has three example trees. Lines 1–2 (Fig. 9.23) find the tree with the most data items. It is used as the seed tree Ts. In Fig. 9.24, the seed tree is the first tree (we omitted many nodes on the left of T1). Line 3 initializes R, which is used to store those trees that are not completely aligned with Ts in each iteration. Line 4 starts the while loop to align every other tree against Ts. Line 5 picks the next unaligned tree, and line 6 does the tree matching. Line 7 finds all the matched pairs by tracing the matrix results of line 6. This function is similar to aligning two strings using edit distance. In Fig. 9.24, Ts and T2 p Ts Ti (C) p a b e (A) c e b c e p b New Ts a a (B) b e f e g p p Ts Ti f e g p b New Ts a e a b x p p Ts Ti a e 394 9 Structured Data Extraction: Wrapper Generation produce one match, node b. Nodes w, c, k and g are not matched to Ts. Lines 8 and 9 attempt to insert the unmatched nodes into Ts. This is the partial tree alignment discussed above. In Fig. 9.24, none of the nodes w, c, k and g in T2 can be inserted into Ts because no unique location can be found. Thus, it will not pass the if-statement (InsertIntoSeed() returns false in line 9 of Fig. 9.23). Lines 13–14 inserts T2 into R, which is a list of trees that need to be re-matched since some data items are not aligned and not inserted into Ts. In Fig. 9.24, when matching T3 with Ts in the next iteration, all unmatched nodes c, h and k can be inserted into Ts (line 9). Since there are some insertions, we re-match those trees in R. Line 10 and line 11 put the trees in R into S and reinitializes R. T3 will not be inserted into R (line 13). In Fig. 9.24, T2 is the only tree in R, which will be matched to the new Ts in the next round. Now, every node in T2 can be matched or inserted, and the process completes. Line 18 of Fig. 9.23 outputs the data items from each tree according to the alignment. Note that if there are still un-matched nodes with data after the algorithm completes (e.g., R ≠ ), each un-matched data will occupy a single column by itself. Table 1 shows the data table for the trees in Fig. 9.24. We use “1” to indicate a data item. The complexity of the algorithm is O(k2n2), where k is the number of trees in S and n is the size of each tree (we assume that all the trees are of similar size). However, as reported in [40], in practice, the algorithm Algorithm PartialTreeAlignment(S) 1. Sort trees in S in descending order of the number of unaligned data items; 2. Ts  the first tree (which is the largest) and delete it from S; 3. R  ; 4. while (S ≠ ) do 5. Ti  select and delete next tree from S; // follow the sorted order 6. STM(Ts, Ti); // tree matching 7. AlignTrees(Ts, Ti); // based on the result from line 6 8. if Ti is not completely aligned with Ts then 9. if InsertIntoSeed(Ts, Ti) then // True: some insertions are done 10. S  S  R; 11. R   12 endif; 13. if there are still unaligned items in Ti that are not inserted into Ts then 14. R  R  {Ti} 15. endif; 16. endif; 17. endwhile; 18. Output data fields from each Ti to a data table based on the alignment results. Fig. 9.23. The partial tree alignment algorithm almost always goes through S only once (i.e., R = ). 9.6 Multiple Alignment 395 Fig. 9.24. Iterative tree alignment with two iterations Table 1. Final data table (“1” indicates a data item) … x b w c d h k g T1 … 1 1 1 T2 1 1 1 1 1 T3 1 1 1 1 1 In fact, to make the algorithm complete, a recursive call should be added after line 17 in Fig. 9.23 to handle the case when R ≠ , i.e., to further align only those trees in R. The following three lines can be added: 18. if R   then 19. PartialTreeAlignment(R) 20. endif This takes care of the situation where some items are not aligned and not inserted. However, it is shown in [40] that this part is usually not needed for data extraction. We make two remarks about this complete algorithm. First, the recursion will terminate even if no alignment and/or no insertion is made d x … b p c k g w p b d h k c p b d x … b p k c x … b p d h c k g w p b wx… b p c d h k S  R. R contains only T2 No node inserted c, h, and k are inserted Ts = T1 T2 T3 T2 g Ts New Ts Initial set S 396 9 Structured Data Extraction: Wrapper Generation to the seed tree because the seed tree is deleted in each recursion and thus R becomes smaller and smaller. Second, the algorithm can found multiple templates in the data. The seed tree from each recursion represents a different template. 9.7 Building DOM Trees DOM (Document Object Model) tree building from input pages is a necessary step for many data extraction algorithms. We describe two methods for building DOM trees, which are also commonly called tag trees (we will use them interchangeably in this chapter). Using Tags Alone: Most HTML tags work in pairs. Each pair consists of an open tag and a close tag (indicated by < > and respectively). Within each corresponding tag-pair, there can be other pairs of tags, resulting in a nested structure. Building a DOM tree from a page using its HTML code is thus natural. In the tree, each pair of tags is a node, and the nested pairs of tags within it are the children of the node. Two tasks need to be performed: 1. HTML code cleaning: Some tags do not require close tags (e.g.,
  • ,
    and

    ) although they have close tags. Hence, additional close tags should be inserted to ensure all tags are balanced. Ill-formatted tags also need to be fixed. Such error tags are usually close tags that cross different nested blocks, e.g.,

  • 2 3 data2 5 6 7 9
    data1 4
    data3 data4 8
    left right top bottom 100 300 200 400 100 300 200 300 100 200 200 300 200 300 200 300 100 300 300 400 100 200 300 400 200 300 300 400 table tr tr td td td td 398 9 Structured Data Extraction: Wrapper Generation will study the second extraction problem based on multiple input pages. The techniques studied in this section are based on the work in [24, 40]. Given a list page containing multiple lists and each list contains multiple data records (at least two), the following tasks are performed: 1. Identify each list (also called a data region), i.e., mine all data regions, 2. Segment data records in each list or data region, and 3. Align data items in the data records to produce a data table for each data region and also a regular expression pattern. 9.8.1 Two Observations about Data Records Data records in each list (or data region) are encoded using the same HTML mark-up encoding. Finding the data records and its hidden schema means to find repeated patterns and align them. String or tree matching/comparison are natural techniques. The problem, however, is the efficiency because a data record can start from anywhere in a page and has any length. It is prohibitive to try all possibilities. If all data records have exactly the same tag string, then the problem is easier. However, in practice, a set of data records typically does not have exactly the same tag string or data items due to missing or optional items (see Fig. 9.26). The two important observations below help to solve the problem, which are based on the DOM tree structure [24]. Observation 1: A group of data records that contains descriptions of a set of similar objects is typically presented in a contiguous region of a page and is formatted using similar HTML tags. Such a region represents a list or a data region. For example, in Fig. 9.26 two books are presented in one contiguous region. Observation 2: A list of data records in a region is formed by some child sub-trees of the same parent node. It is unlikely that a data record starts in the middle of a child sub-tree and ends in the middle of another child sub-tree. Instead, it starts from the beginning of a child sub-tree and ends at the end of the same or a later child sub-tree. For example, Fig. 9.27 shows the DOM tree of the page in Fig. 9.26 (with some parts omitted). In this tree, each data record is wrapped in five TR nodes with their sub-trees under the same parent TBODY. The two data records are in the two dash-lined boxes. It is unlikely that a data record starts from TD* and ends at TD# (Fig. 9.27). The second observation makes it possible to design an efficient algorithm to identify data records because it limits the tags from which a data record may start and end in a DOM tree. 9.8 Extraction Based on a Single List Page: Flat Data Records 399 Fig. 9.27. The DOM tree of the page segment in Fig. 9.26 9.8.2 Mining Data Regions This first step mines every data region in a Web page that contains a list of data records (a set instance). Finding data regions (or individual data records in them) directly is, however, hard. We first mine generalized nodes (defined below). A sequence of adjacent generalized nodes forms a Fig. 9.26. An example of a page segment HTML HEAD BODY TR | TD TD TD TD TD TR TR | TD TR TR TR | TD TR | TD TR | TD TR | TD TABLE P TR TD* TD TD TD TD TD TD# TD TABLE TBODY data record 1 data record 2 400 9 Structured Data Extraction: Wrapper Generation data region. From each data region, we identify the actual data records (discussed in Sect. 9.8.3). Below, we define generalized nodes and data regions using the DOM (tag) tree: Definition: A generalized node (a node combination) of length r consists of r (r  1) nodes in the DOM tree with the following two properties: (1) the nodes all have the same parent; (2) the nodes are adjacent. We introduce the generalized node to capture the situation that a data record is contained in several sibling HTML tag nodes rather than one. For example, in Fig. 9.27, we see that each notebook is contained in five table rows (or five TR nodes). We call each node in the HTML tag tree a tag node to distinguish it from a generalized node. Definition: A data region is a collection of two or more generalized nodes with the following properties: (1) the generalized nodes all have the same parent; (2) the generalized nodes all have the same length; (3) the generalized nodes are all adjacent; (4) the similarity between adjacent generalized nodes is greater than a fixed threshold. For example, in Fig. 9.27, we can form two generalized nodes. The first one consists of the first five children TR nodes of TBODY, and the second one consists of the next five children TR nodes of TBODY. We should note that although the generalized nodes in a data region have the same length (the same number of children nodes of a parent node in the tag tree), their lower level nodes in their sub-trees can be quite different. Thus, they can capture a wide variety of regularly structured objects. We also note that a generalized node may not represent a final data record (see Sect. 9.8.3), but will be used to find the final data records. To further explain different kinds of generalized nodes and data regions, we make use of an artificial DOM/tag tree in Fig. 9.28. For notational convenience, we do not use actual HTML tag names but ID numbers to denote tag nodes in a tree. The shaded areas are generalized nodes. Nodes 5 and 6 are generalized nodes of length 1 and they together define the data region labeled 1 if the similarity condition (4) is satisfied. Nodes 8, 9 and 10 are also generalized nodes of length 1 and they together define the data region labeled 2 if the similarity condition (4) is satisfied. The pairs of nodes (14, 15), (16, 17) and (18, 19) are generalized nodes of length 2. They together define the data region labeled 3 if the similarity condition (4) is satisfied. It should be emphasized that a data region includes the sub- trees of the component nodes, not just the component nodes alone. 9.8 Extraction Based on a Single List Page: Flat Data Records 401 Fig. 9.28. An illustration of generalized nodes and data regions Comparing Generalized Nodes In order to find each data region in a Web page, the mining algorithm needs to find the following: (1) Where does the first generalized node of a data region start? For example, in Region 2 of Fig. 9.28, it starts at node 8. (2) How many tag nodes or components does a generalized node in each data region have? For example, in Region 2 of Fig. 9.28, each generalized node has one tag node (or one component). Let the maximum number of tag nodes that a generalized node can have be K, which is normally a small number (< 10). In order to answer (1), we can try to find a data region starting from each node sequentially. To answer (2), we can try one node, two node combination, …, K node combination. That is, we start from each node and perform all 1-node comparisons, all 2-node comparisons, and so on (see the example below). We then use the comparison results to identify each data region. The number of comparisons is actually not very large because:  Due to the two observations in Sect. 9.8.1, we only need to perform comparisons among the children nodes of a parent node. For example, in Fig. 9.28, we do not compare node 8 with node 13.  Some comparisons done for earlier nodes are the same as for later nodes (see the example below). We use Fig. 9.29 to illustrate the comparison. There are 10 nodes below the parent node p. We start from each node and perform string (or tree) comparison of all possible combinations of component nodes. Let the maximum number of components that a generalized node can have be 3. 1 3 10 2 7 8 9 Region 2 5 6 Region 1 4 11 12 14 15 20 16 17 19 18 13 Region 3 402 9 Structured Data Extraction: Wrapper Generation Fig. 9.29. Combination and comparison Start from node 1: We compute the following string or tree comparisons.  (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)  (1-2, 3-4), (3-4, 5-6), (5-6, 7-8), (7-8, 9-10)  (1-2-3, 4-5-6), (4-5-6, 7-8-9). (1, 2) means that the tag string of node 1 is compared with the tag string of node 2. The tag string of a node includes all the tags of the sub-tree of the node. (1-2, 3-4) means that the combined tag string of nodes 1 and 2 is compared with the combined tag string of nodes 3 and 4. Start from node 2: We only compute:  (2-3, 4-5), (4-5, 6-7), (6-7, 8-9)  (2-3-4, 5-6-7), (5-6-7, 8-9-10). We do not need to do 1-node comparisons because they have been done when we started from node 1 above. Start from node 3: We only need to compute:  (3-4-5, 6-7-8). Again, we do not need to do 1-node comparisons. Also, 2-node comparisons are not necessary as they were done when we started at node 1. We do not need to start from any other node after node 3 because all the computations have been done. The Overall Algorithm The overall algorithm (called MDR) is given in Fig. 9.30. It traverses the tag tree from the root downward in a depth-first fashion (lines 5 and 6). Node is any tree node. K is the maximum number of tag nodes in a generalized node (10 is sufficient).  is the similarity threshold. The node comparison can be done either using string edit distance or tree matching (e.g., STM). The similarity threshold can be set empirically. Line 1 says that the algorithm will not search for data regions if the depth of the sub-tree at Node is 2 or 1 as it is unlikely that a data region is formed with only a single level of tag(s). 2 1 3 4 6 5 7 8 9 10 p 9.8 Extraction Based on a Single List Page: Flat Data Records 403 At each internal node, the function CombComp() (line 2) performs string (tree) comparisons of various combinations of the children sub-trees, which have been discussed above. The function IdenDRs() (line 3) uses the comparison results to find similar children node combinations (using the similarity threshold ) to obtain generalized nodes and data regions (DataRegions) under Node (i.e., among the children of Node). That is, it decides which combinations represent generalized nodes and where the beginning and end are for each data region. DataRegions consists of a set of data regions, and each data region contains a list of tag nodes organized as generalized nodes of the region. IdenDRs() is discussed further below. Line 4 says that if some nodes are not covered by discovered data regions, the algorithm will go down the tree further from these nodes to see whether they contain data regions (lines 5 and 6). We note that a generalized node may not be a data record, but may contain more than one data record. Fig. 9.31 illustrates the point. This data region has eight data records. Each row has two. However, each row will be reported as a generalized node because rows 14 are similar. We will explain how to find data records from each generalized node shortly. Fig. 9.31. A possible configuration of data records Algorithm MDR(Node, K, ) 1 if TreeDepth(Node) >= 3 then 2 CombComp(Node.Children, K); 3 DataRegions  IdenDRs(Node, K, ); 4 if (UncoveredNodes  Node.Children  UDRDataRegionsDR) ≠  then 5 for each ChildNode  UncoveredNodes do 6 DataRegions  DataRegions  MDR(ChildNode, K, ); 7 return DataRegions 8 else return  Fig. 9.30. The MDR algorithm row 1 row 2 row 3 row 4 1 2 3 4 5 6 8 7 404 9 Structured Data Extraction: Wrapper Generation Let us come back to the function IdenDRs(), which is not hard to design and it is omitted. Interested readers can refer to [24]. We only describe two issues that the function needs to consider. 1. It is clear from Fig. 9.28 that there may be several data regions under a single parent node Node. Generalized nodes in different data regions may have different number of tag node components. 2. A property about similar strings (or trees) is that if a set of strings (trees), s1, s2, s3, …., sn, is similar to one another, then a combination of any number of them is also similar to another combination of them of the same number. IdenDRs should only report generalized nodes of the smallest length that cover a data region. For Fig. 9.31, it only reports each row as a generalized node rather than a combination of two rows (rows 1-2, and rows 3-4). The computation of the algorithm is dominated by string (or tree) comparison. Assume that the total number of nodes in the tag tree is N, the number of comparisons is in the order of O(NK2). Since K is normally very small, the computation requirement of the algorithm is low. Visual information (see Sect. 9.8.5) and simple heuristics can be applied to reduce the number of string (tree) comparisons substantially. 9.8.3 Identifying Data Records in Data Regions As we have discussed above, a generalized node may consist of multiple data records. Fig. 9.31 shows an example, where each row is a generalized node that contains two data records. To find data records from each generalized node in a data region, the following observation is useful:  If a generalized node contains two or more data records, these data records must be similar in terms of their tag strings. This is clear because we assume that a data region contains descriptions of similar data records. Identifying data records from each generalized node in a data region is relatively easy because they are nodes (together with their sub-trees) at the same level as the generalized node, or nodes at a lower level of the DOM/tag tree. This can be done in two steps: 1. Produce one rooted tree for each generalized node: An artificial root node is created first, and all the components (which are sub-trees) of the generalized node are then put as its children. 2. Call the MDR algorithm using the tree built in step 1: Due to the observation above, this step will find the data records if exist. Otherwise, the generalized node is a data record. Two issues needs to be considered. 7 9.8 Extraction Based on a Single List Page: Flat Data Records 405  The discovered data records should cover all the data items in the original generalized node.  Each data record should not be too small, e.g., a single number or a piece of text, which is likely to be an entry in a spreadsheet table. Further details can be found in [24, 39], where handling non-contiguous data records is also discussed. For example, two books are described in two table rows. One row lists the names of the two books in two cells, and the next row lists the other pieces of information about the books also in two cells. This results in the following sequence in the HTML code: name 1, name 2, description 1, description 2. 9.8.4 Data Item Alignment and Extraction Once data records in each data region are discovered, they are aligned to produce an extraction pattern that can be used to extract data from the current page and also other pages that use the same encoding template. We use the partial tree alignment algorithm to perform the task in two steps: 1. Produce a rooted tree for each data record: An artificial root node is created first. The sub-trees of the data record are then put as its children. 2. Align the resulting trees: The trees of all the data records in each data region are aligned using the partial tree alignment method in Sect. 9.6.2. After alignments are done, the final seed tree can be used as the extraction pattern, or be turned into a regular expression. Conflict Resolution: In tree matching and alignment, it is possible that multiple matches can give the same maximum score, but only one match is correct. Then, we need to decide which one. For example, in Fig. 9.32, node c in tree A can match either the first or the last node c in tree B. Fig. 9.32. Two trees with more than one possible match: which is correct? To deal with this problem, we can use data content similarity. Data items that share some common substrings are more likely to match. In many cases, a data item contains both the attribute name and the attribute value. For example, “shopping in 24 hours” and “shopping within a week”. g c b a p c b a p c B A 406 9 Structured Data Extraction: Wrapper Generation The data content similarity can in fact be considered in the simple tree matching (STM) algorithm in Fig. 9.19 with minor changes to line 1 and line 11. Data contents (data items) should be included as leaves of the DOM trees. When data items are matched, their match score is computed. In [39], the longest common subsequence (LCS) is used, but cosine similarity should work too. Let q be the number of words in the LCS of the two data items, and m be the maximal number of words contained in them. Their matching score is computed with q/m. 9.8.5 Making Use of Visual Information It is quite clear that many visual features that are designed to help people locate and understand information in a Web page can help data extraction. We have already shown that visual cues can be used to construct DOM trees. In fact, they can be exploited everywhere. Here are some examples:  Reduce the number of string or tree comparisons. If two sub-trees are visually too different, they do not need to be compared.  Confirm the boundary of data records using the space gap between data records. It is usually the case that gaps between data records are larger than gaps between items within a data record.  Determine item alignment. Visual alignment (left, right or center) of items can help determine whether two data items should match. Relative positions of the data items in each data record are very helpful too.  Identify data records based on their contour shapes. This method was exploited to segment data records from search engine results [42]. 9.8.6 Some Other Techniques Both string comparison and tree comparison based methods have been used to solve the data extraction problem. The task described in Sect. 9.8.2 can be done either based on strings or based on trees. A pure string based method called IEPAD is proposed in [8]. It finds patterns from the HTML tag string and then uses the patterns to extract data. The center star method is used to align multiple strings. A more sophisticated string based method, which also deals with nested records, is proposed in [34]. The main problem with string based methods is that they can find many patterns and it is hard to determine which one is correct. Some patterns may even cross boundaries of data records. In [8], the user needs to choose the right pattern for extraction. In [34], multiple pages containing similar data records and other methods are used to choose the right pattern. This 9.9 Extraction Based on a Single List Page: Nested Data Records 407 problem is not a major issue for tree based methods because of the tree structures, which eliminate most alternatives. The observations made in Sect. 9.8.1 were also very helpful. In [22], a method based on constraints and the EM algorithm is proposed, which needs to use some information from detail pages to segment data records. Note that a data record usually (not always) has a link to its detail page, which contains the detail description of the object (e.g., a product) represented by the data record. Visual information is extensively used in [42] to segment snippets (data records) of returned pages from search engines. It uses the contour shape on the left of each snippet, distance from the left boundary of the page, and also the type of each line in the snippet (e.g., text line, link line, empty line, etc.) to determine the similarity of candidate data records. In [26], a set of visual signals is identified and clustered to help find data records. Several researchers also explored the idea of using some simple domain knowledge to help identify data records [36], e.g., a key piece of data or information that appears in every data record. In [32], Song et al. extended this idea and also MDR by proposing an automated method to identify a sub-tree (called the anchor tree) that exists in every data record. Data records are then identified by looking for other elements around the anchors. 9.9 Extraction Based on a Single List Page: Nested Data Records The problem with the method in Sect. 9.8 is that it is not suitable for nested data records, i.e., data records containing nested lists. Since the number of elements in a list of each data record can be different, using a fixed threshold to determine the similarity of data records will not work. The problem, however, can be dealt with as follows. Instead of traversing the DOM tree top down, we can traverse it post-order. This ensures that nested lists at lower levels are found first based on repeated patterns before going to higher levels. When a nested list is found, its records are collapsed to produce a single pattern which replaces the list of data records. When comparisons are made at a higher level, the algorithm only sees the pattern. Thus it is treated as a flat data record. This solves the fixed threshold problem above. We introduce an algorithm below, which is based on the NET system in [25]. A running example is also given to illustrate the process. The NET algorithm is given in Fig. 9.33. It is basically a post-order tree traversal algorithm. The observations in Sect. 9.8.1 are still applicable here. The function TraverseAndMatch() performs the post-order traversal. 408 9 Structured Data Extraction: Wrapper Generation During the process, each nested list is collapsed. The function PutDataInTables() (line 3) outputs the extracted data to the user in relational tables (a page may have multiple data regions, and data in each region are put in a separate table). Line 3 can be easily done if the function TraverseAndMatch() saves the nodes whose children form data records. Line 1 of TraverseAndMatch() says that the algorithm will not search for data records if the depth of the sub-tree from Node is 2 or 1 as it is unlikely that a data record is formed with only a single level of tag(s). This parameter can be changed. The Match() function performs tree matching on child sub-trees of Node and pattern generation.  is the threshold for a match of two trees that are considered sufficiently similar. Algorithm NET(Root, ) 1 TraverseAndMatch(Root, ); 2 for each top level node Node whose children have aligned data records do 3 PutDataInTables(Node); 4 endfor Function TraverseAndMatch (Node, ) 1 if Depth(Node)  3 then 2 for each Child  Node.Children do 3 TraverseAndMatch(Child, ); 4 endfor 5 Match(Node, ); 6 endif Fig. 9.33. The NET algorithm Function Match(Node, ) 1 Children  Node.Children; 2 while Children   do 3 ChildFirst  select and remove the first child from Children; 4 for each ChildR in Children do 5 if TreeMatch(ChildFirst, ChildR) >  then 6 AlignAndLink(); 7 Children  Children – {ChildR} 8 endfor 9 if some alignments (or links) have been made with ChildFirst then 10 GenNodePattern(ChildFirst) 11 endwhile 12 If consecutive child nodes in Children are aligned then 13 GenRecordPattern(Node) Fig. 9.34. The Match function 9.9 Extraction Based on a Single List Page: Nested Data Records 409 Match(): The Match() function is given in Fig. 9.34. Fig. 9.35 shows a running example. In this figure, Ni represents an internal node, and tj represents a terminal (leaf) node with a data item. We use the same shape or shading to indicate matching nodes. We explain the algorithm below. Given the input node Node, line 1 obtains all its children to be matched. In our example, Children of p are t1, N1, N2, N3, N4, t2, N5, N6, N7, N8, and N9 (with their sub-trees). Lines 2–4 set every pair of child nodes to be matched. The matching is done by TreeMatch(), which uses algorithm STM() in Fig. 9.19. AlignAndLink() (line 6) aligns and links all matched data items (leaf nodes) in ChildFirst and ChildR. The links are directional, i.e., from earlier data items to later (matched) data items. If ChildR matches ChildFirst, ChildR is removed from Chirdren so that it will not be matched again later (line 7). For our example, after lines 411, the resulting matches and links (dashed lines) are given in Fig. 9.35. Assume they all satisfy the match condition in line 5. In lines 910, if some alignments (or links) have been made, the GenNodePattern() function generates a node pattern for all the nodes (including their sub-trees) that match ChildFirst. This function first gets the set of matched nodes ChildR’s, and then calls PartialTreeAlignment() in Fig. 9.23 to produce a pattern which is the final seed tree. Note that PartialTreeAlignment() can be simplified here because most alignments have been done. Only insertions and matching of unaligned items are needed. A node pattern can also be represented as a regular expression. In lines 1213, it collapses the sub-trees to produce a global pattern for the data records (which are still unknown). Notice that lines 910 already produced the pattern for each child sub-tree. The GenRecordPattern() function simply produces a regular expression pattern for the list of data records. This is essentially a grammar induction problem [16]. t1 p N3 N4 t6 t7 t8 N1 t2 t3 t4 N5 t9 t10 N7 N6 t12 t13 t11 N8 N9 t14 t15 t16 N2 t5 Fig. 9.35. A running example: All matched items are linked 410 9 Structured Data Extraction: Wrapper Generation Grammar induction in our context is to infer a regular expression given a finite set of positive and negative example strings. However, we only have a single positive example (a list of hidden data records). Fortunately, structured data in Web pages are usually highly regular which enables heuristic methods to generate “simple” regular expressions. Here, we introduce such a simple method, which depends on three assumptions: 1. The nodes in the first data record at each level must be complete, e.g., in Fig. 9.35, nodes t1, N1 and N2 must all be present. Function GenRecordPattern(Node) 1 String  Assign a distinctive symbol to each set of matched children of Node; 2 Initilize a data structure for NFA N = (Q, , , q0, F), where Q is the set of states,  is the symbol set containing all symbols appeared in String,  is the transition relation that is a partial function from Q  (  {}) to Q, and F is the set of accept states, Q  {q0} (q0 is the start state),    and F  ; 3 qc  q0; // qc is the current state 4 for each symbol s in String in sequence do 5 if  a transition (qc, s) = qn then 6 qc  qn // transit to the next state; 7 else if  (qi, s) = qj, where qi, qj  Q then // s appeared before 8 if  (qf, ) = qi, where (qi, s) = qj and f  c then 9 TransitTo(qc, qf) 10 else TransitTo(qc, qi) 11 qc  qj 12 else create a new state qc+1 and a transition (qc, s) = qc+1, i.e.,     {((qc, ), qc+1)} 13 Q  Q  {qc+1}; 14 qc  qc+1 15 if s is the last symbol in String then 16 Assign the state with the largest subscript the accept state qr, F = {qr}; 17 TransitTo(qc, qr); 18 endfor 19 generate a regular expression based on the NFA N; 20 Substitute all the node patterns into the regular expression. Function TransitTo(qc, qs) 1 while qc  qs do 2 if  (qc, ) = qk and k>c then 3 qc  qk 4 else create a transition (qc, ) = qc+1, i.e.,     {((qc, ), qc+1)}; 5 qc  qc+1 6 endwhile Fig. 9.36. Generating regular expressions 9.9 Extraction Based on a Single List Page: Nested Data Records 411 2. The first node of every data record at each level must be present, e.g., at the level of t1 and t2, they both must be present, and at the next level, N1, N3, N5, N7 and N8 must be present. Note that the level here is in the hierarchical data organizational sense (not the HTML code sense). 3. Nodes within a single flat data record (no nesting) do not match one another, e.g., N1 and N3 do not appear in the same data record. The GenRecordPattern() function is given in Fig. 9.36. It generates a regular expression pattern. Line 1 in Fig. 9.36 simply produces a string for generating a regular expression. For our example, we obtain the following: t1 N1 N2 N3 N4 t2 N5 N6 N7 N8 N9 String: a b c b c a b c b b c Lines 23 initialize a NFA (non-deterministic finite automaton). Lines 418 traverses String from left to right to construct the NFA. For our example, we obtain the final NFA in Fig. 9.37. Fig. 9.37. The generated NFA and its regular expression Line 19 produces a regular expression from the NFA, which is shown in Fig. 9.37 on the right. Line 20 produces the final pattern (Fig. 9.38) by substituting the node patterns into the regular expression. Here, we use node t1 as the pattern (the seed tree) for nodes t1 and t2, the N1 sub-tree as the pattern for all the linked sub-trees rooted at N1, N3, N5, N7 and N8. The N2 sub-tree is the pattern of the sub-trees rooted at N2, N4, N6 and N9. Fig. 9.38. The regular expression produced from Fig. 9.35 Some additional notes about the algorithm are in order:  Each child node here represents a sub-tree (e.g., N1, N2, etc). Assumption 1 does not require lower level nodes of each sub-tree in the first data record to be complete (no missing items). We will see an t1 N2 t5 N1 t3 t4 ? + + (a (b c?)+ )+ a b   c  q0 q1 q3 q4 412 9 Structured Data Extraction: Wrapper Generation example in Fig. 9.40 and Fig. 9.41.  Regular expressions produced by the algorithm do not allow disjunctions (i.e., A|B) except (A|), which means that A is optional. Such regular expresses are called union-free regular expressions. However, disjunctions are possible at lower level matches of the sub- trees. We will discuss the issue of disjunction again in Sect. 9.11.2.  Function GenRecordPattern() in Fig. 9.37 assumes that under Node there is only one data region, which may not be true. The algorithm can be easily extended to take care of multiple data regions under Node. In fact, the NET() algorithm here is a simplified version to present the main ideas. For practical use, it can be significantly enhanced to remove most assumptions if not all. Finally, the function PutDataInTables() (line 3 of NET() in Fig. 9.33) simply outputs data items in a table, which is straightforward after the data record patterns are found. For the example in Fig. 9.35, the following data table is produced (only terminal nodes contain data): Fig. 9.39. The output data table for the example in Fig. 9.35 Fig. 9.42. The output data table for the example in Fig. 9.40 t1 t3 t4 t5 t1 t6 t7 t8 t2 t9 t10 t11 t2 t12 t13 t2 t14 t15 t16 N3 t8 t9 t2 N2 N4 N5 N1 N3 t8 t9 t2 N2 N5 + t5 t6 t1 N6 Fig. 9.40. Aligned data nodes are linked N6 t3 t4 t5 t6 t7 t7 N1 Fig. 9.41. Alignment after collapsing t1 ? t1 t3 t4 t1 t5 t6 t7 t2 t8 t9 9.10 Extraction Based on Multiple Pages 413 Let us use a smaller but more complete example (Fig. 9.40) to show that generating a pattern of a lower level list makes it possible for a higher level matching. At the level of N4N5 (which has the parent N2), t3t5 and t4t6 are matched (assume they satisfy the match condition, line 5 of Fig. 9.34). They are aligned and linked (dash lines). N4 and N5 are data records at this level (nested in N2 in this case), in which t7 is optional. N4 and N5 are then collapsed to produce a pattern data record using GenNodePattern() first and then GenRecordPattern(), which does not do anything in this case. t7 is marked with a “?”, indicating that it is optional. The pattern data record is N5 (selected based on the PartialTreeAlignment() function). The sub-tree at N4 is then omitted in Fig. 9.41. N5 is marked with a “+” indicating that there is one or more such data records and that the sub-tree of N5 is the pattern. We can see in Fig. 9.41 that the sub-trees rooted at N2 and N3 can now match. The final output data table is given in Fig. 9.42. 9.10 Extraction Based on Multiple Pages We now discuss the second extraction problem described in Sect. 9.4.1. Given multiple pages with the same encoding template, the system finds patterns from them to be used to extract data from other similar pages. The collection of input pages can be a set of list pages or detail pages. Below, we first see how the techniques described so far can be applied in this setting, and then describe a technique specifically designed for this setting. 9.10.1 Using Techniques in Previous Sections We discuss extraction of list pages and detail pages separately. Given a Set of List Pages Since the techniques described in previous sections are for a single list page, they can obviously be applied to multiple list pages. The pattern discovered from a single page can be used to extract data from the rest of the pages. Multiple list pages may also help improve the extraction. For example, patterns from all input pages may be found separately and merged to produce a single refined pattern. This can deal with the problem that a single page may not contain the complete information. Given a Set of Detail Pages In some applications, one needs to extract data from detail pages as they 414 9 Structured Data Extraction: Wrapper Generation contain more information. For example, in a list page, the information on each product is usually quite brief, e.g., containing only the name, image, and price. However, if an application also needs the product description and customer reviews, one has to extract them from detail pages. For extraction from detail pages, we can treat each page as a data record and apply the algorithms described in Sect. 9.8 and/or Sect. 9.9. For instance, to apply the NET algorithm, we can simply construct a rooted tree as input to NET as follows: (1) create an artificial root node, and (2) make the DOM tree of each page as a child sub-tree of the artificial root. 9.10.2 RoadRunner Algorithm We now describe the RoadRunner algorithm [11], which is designed specifically for problem 2. Given a set of pages, each containing one or more data records (i.e., the pages can be list pages or detail pages), the algorithm compares the pages to find similarities and differences, and in the process generating a union-free regular expression (i.e., a regular expression without disjunctions) extractor/wrapper. The approach works as follows:  To start, it takes a random page as the regular expression wrapper W.  The wrapper W is then refined by matching it sequentially with the HTML code of each remaining page pi. It generalizes W by solving mismatches between the wrapper W and the page pi. A mismatch occurs when some token in pi does not match the grammar of the wrapper. There are two types of mismatches: 1. Text string mismatches: They indicate data fields or items. 2. Tag mismatches: They indicate  optional items, or  iterators (a list of repeated patterns): In this case, a mismatch occurs at the beginning of a repeated pattern and the end of a list. The system finds the last token of the mismatch position and identifies some candidate repeated patterns from the wrapper and the page pi by searching forward. It then compares the candidates with the upward portion of the page pi to confirm. The algorithm is best explained with an example, which is given in Fig. 9.43. In this figure, page 1 on the left (in HTML code) is the initial wrapper. Page 2 on the right is a new page to be matched with page 1. Let us look at some matches and mismatches. Lines 13 of both pages are the same and thus match. Lines 4 of both pages are text strings and are 9.11 Some Other Issues 415 different. They are thus data items to be extracted. We go down further. Lines 6 of the pages do not match. Line 6 of page 1 matches line 7 of page 2. Thus, is likely to be optional. Line 11 of page 1 and line 12 of page 2 give another mismatch. Since they are text strings, they are thus data items to be extracted. Line 17 of page 1 and line 18 of page 2 are also data items. Another mismatch occurs at line 19 of page 1 and line 20 of page 2. Further analysis will find that we have a list here. The final refined regular expression wrapper is given at the bottom of Fig. 9.43. Fig. 9.43. A wrapper generation example The match algorithm is exponential in the input string length as it has to explore all possibilities. A set of heuristics is introduced to lower the complexity by limiting the space to explore and to backtrack. In [1], a more efficient method is given based on sophisticated tag path analysis. 9.11 Some Other Issues We now briefly discuss a few other issues that are important to automatic extraction techniques. - Wrapper (initially Page 1): - Sample (page 2) 1: parsing 1: 2: Books of: 2: Books of: 3: 3: 4: Paul Smith string mismatch 4: Mike Jones 5: 5: 6:
      tag mismatch (?) 6: 7:
        7:
      • 8:
      • 8-10: Title: 9-11: Title: 11: Web Mining string mismatch (#text) 12: Databases 12:
      • 13: 13:
      • 14:
      • 14-16: Title: 15-17: Title: 17: Data Mining string mismatch (#text) 18: HTML Premier 18:
      • 19: 19:
      tag mismatch (+) 20:
    • 20: 21-23: Title: terminal tag search and 24: Javascript square matching 25:
    • 26:
    - Wrapper after solving mismatches: 27: Books of:#text ()?
      (
    • Title:#text
    • )+
    416 9 Structured Data Extraction: Wrapper Generation 9.11.1 Extraction from Other Pages Once the encoding template pattern is found, it can be used to extract data from other pages that contain data encoded in the same way. There are three ways to perform the extraction:  Finite-state machines: An encoding template pattern is usually represented as a regular expression. A nondeterministic finite-state automaton can be constructed to match occurrences of the pattern in the input string representing a Web page. In the process, data items are extracted.  Pattern matching: It is also possible to directly match the string or tree pattern against the input to extract data. This approach is more flexible than finite-state machines because pattern matching allows partial matching. For example, in the page where the pattern is discovered, an optional item does not occur, but it occurs in some other pages. Pattern matching can deal with this easily. In the process, the pattern can be enhanced as well by inserting the new optional item in it.  Extracting each page independently: The above two approaches can be problematic if the Web site use many different templates to encode its data. If we start to extract after only finding one pattern, then the data encoded using other templates will not be extracted. One solution to this is to find patterns from each page and extract the page using only the discovered patterns from the page. However, handling each page individually is inefficient. Detecting new templates: To detect new templates without sacrificing efficiency of mining extraction patterns from each page, a pre-screening strategy may be applied. In most applications, the user is interested in only a particular kind of data, e.g., products, research publications, or job postings. It is usually possible to design some simple and efficient heuristics to check whether a page contains such data. If so, a full blown extraction is performed using already generated patterns. If no data is extracted from the page, it is an indication that the page is encoded with a different template. A new mining process can be initiated to discover the new template. 9.11.2 Disjunction or Optional In automatic extraction, it can be difficult to recognize disjunctions. For example, for the three digital cameras in Fig. 9.44, it is easy to know that “On Sale” is an optional item. However, for the prices (including “Out of stock”), it is hard to decide whether they are optional items or disjuncts of a disjunction. The HTML codes for the three fields are given below, 9.11 Some Other Issues 417 (1) $250.00 (2) $300.00 (3) Out of stock . If they are treated as optional items, they are put in three different columns in the output table, but if they are disjuncts, they are put in the same column. In this example, it is easy for a human user to see that they are disjuncts, i.e., ( #text ) | ( #text ) | ( #text). Fig. 9.44. Disjuncts or optional items There are two main pieces of information that can be used to determine whether they are optional or disjuncts: 1. Visual information: If they are in the same relative location with respect to their objects, then they are more likely to be disjuncts. In the above example, the three items are all at the same relative location. 2. Data type information: If the data items are of the same type, they are more likely to be disjuncts. “$250.00” and “$300.00” are of the same type, but “Out of stock” is not. In many cases, it can be hard to decide. Fortunately, disjunctive cases are rare on the Web. Even if an extraction system does not deal with disjunction, it does not cause a major problem. For example, if “Out of stock” is identified as optional, it is probably acceptable. 9.11.3 A Set Type or a Tuple Type Sometimes it can also be difficult to determine whether a list is a tuple type or a set type. For example, if all the lists of a set type have the same number of elements, it is hard to know if they are in fact attributes of a tuple. For instance, the following are three colors of a jacket with different prices. Clearly, they represent a set instance with a list of three tuples:
    Blue: $5.00
    Yellow: $6.50
    Pink: $10.99
    weight: 30 kg
    height: 5 m