Showing posts with label machine learning. Show all posts
Showing posts with label machine learning. Show all posts

Tuesday, September 29, 2015

Practical Scala, Haskell and Category Theory

Functional programming has moved from academia to industry in the last few years. It is theoretical with a steep learning curve. I have worked with strongly typed functional programming for 4 years. I took the normal progression, first Scala then Haskell and ended with category theory.

What practical results does functional programming give me?

I typically do data mining, NLP and back end programming. How does functional programming help me with NLP, AI and math?

    Scala

    Scala is a complex language that can take quite some time to learn. For a couple of years I was unsure if it really improved my productivity compared to Java or Python.

    After 2 years my productivity in Scala went up. I find that Scala is an excellent choice for creating data mining pipelines because it is:
    • Fast
    • Stable
    • Has lot of quality libraries
    • Has a very advanced type system
    • Good DSL for Hadoop (Scalding)

    Natural Language Processing in Scala

    Before Scala I did NLP in Python. I used NLTK the Natural Language Toolkit for 3 years.

    NLTK vs. ScalaNLP


    NLTK

    • Easy to learn and very flexible
    • Gives you a lot of functionality out of the box
    • Very adaptable, handles a lot of different structured file formats

    What I did not like about NLTK was:
    • It had a very inefficient representation of a text features as a Dictionary
    • The file format readers were not producing exactly matching structures and this did not get caught by the type system
    • You have to jump between Python, NumPy and C or Fortran for low level work

    ScalaNLP

    ScalaNLP merged different Scala numeric and NLP libraries. It is a very active parent project of Breeze and Eric.

    ScalaNLP Breeze

    Breeze is a full featured, fast numeric library that uses the type system to great effect.
    • Linear algebra
    • Probability Distribution
    • Regression algorithms
    • You can drop down to the bottom level without having to program in C or Fortran

    ScalaNLP Eric

    Eric is the natural language processing part of ScalaNLP. It has become a competitive NLP library with many algorithms for several human languages:
    • Reader for text corpora
    • Tokenizer
    • Sentence splitter
    • Part-of-speech tagger
    • Named entity recognition
    • Statistical parser


    Video lecture by David Hall the Eric lead

    Machine Learning in Scala

    The most active open source Scala machine learning library is MLib which is part of the Spark project.
    Spark now has data frames like R and Pandas.
    It is easy to set up machine learning pipelines, do cross validation and optimization of hyper parameters.

    I did text classification and set it up in Spark MLib in only 100 lines of code. The result had satisfactory accuracy.

    AI Search Problem in Scala vs. in Lisp

    I loved Lisp when I learned it at the university. You could do all these cool Artificial Intelligence tree search problems. For many years I suffered from Lisp envy.

    Tree search works a little differently in Scala, let me illustrate by 2 examples.

    Example 1: Simple Tree Search for Bird Flu

    You have an input HTML page and parsed into a DOM tree. Look for the word bird and flu in a paragraph that is not part of the advertisement section.
    I can visualize what a search tree for this would look like.

    Example2: Realistic Bird Flu Medication Search

    The problems I deal with at work are often more complex:
    Given a list of medical websites, search for HTML pages with bird flu and doctor recommendations for medications to take. Then do a secondary web search to see if the doctors are credible.

    Parts of Algorithm for Example 2

    This is a composite search problem:
    • Search HTML pages for the words bird and flu close to each other in DOM structure
    • Search individual match to ensure this is not in advertisement section
    • Search for Dr names
    • Find what Dr name candidates could be matched up with the section about bird flu
    • Web search for Dr to determine popularity and credentials
    Visualizing this as a tree search is hard for me.

    Lazy Streams to the Rescue

    Implementing solutions to the Example 2 bird flu medication problem takes:
    • Feature extractors
    • Machine learning on top of that
    • Correlation of a disease and a doctor
    This lends itself well to using Scala's lazy streams. Scala makes it easy to use the lazy streams and the type system gives a lot of support, especially when plugging together various streams.

    Outline of Lazy Streams Algorithm for Example 2

    1. Stream of all web pages
    2. Stream of tokenized trees
    3. Steam of potential text matches e.g. avian influenza, H5N1
    4. Filter Stream 3 if it is an advertisement part of the DOM tree, (no Dr Mom)
    5. Stream of potential Dr text matches from Stream 2
    6. Stream of good Dr names. Detected with machine learning
    7. Merge Stream 3 and Stream 6 to get bird flu and doctor name combination
    8. Web search stream for the doctor names from Stream 7 for ranking of result

    AI Search Problem in Lisp

    Tree search is Lisp's natural domain. Lisp could certainly handle Example 2 the more complex bird flu medication search. Even using a similar lazy stream algorithm.

    Additionally, Lisp has the ability to do very advanced meta programming:
    Rules that create other rules or work on multiple levels. Things I do not know how to do in Scala.

    Lisp gives you a lot of power to handle open ended problems and it is great for knowledge representation. When you try to do the same in Scala you end up either writing Lisp or Prolog style code or using RDF or graph databases.

    Some Scala Technical Details

    Here are a few observations on working with Scala.

    Scala's Low Rent Monads

    Monads are a general way to compose functionality. They are a very important organizing principle in Scala. Except is not really monads it is just syntactic sugar.

    You give us a map and a flatMap function and we don't ask any questions.

    Due to the organization of the standard library and subtyping you can even combine an Option and a List, which should strictly not be possible. Still this give you a lot of power.
    I do use Scala monads with no shame.

    Akka and Concurrency

    Scala's monads make it convenient to work with two concurrency constructs: Futures and Promises.

    Akka is a library implementing an Erlang style actor model in Scala.
    I have used Akka for years and it is a good framework to organize a lot of concurrent computation that requires communication.

    The type system does not help you with the creation of parent actors so you are not sure that they exist. This makes it hard to write unit tests for actors.

    Akka is good but the whole Erlang actor idea is rather low level.

    Scalaz and Cake Patterns

    Scalaz is a very impressive library that implements big parts of Haskell’s standard library in Scala.
    Scalaz’s monad typeclass is invariant, which fixes the violations allowed in the standard library.

    Cake Patterns allows for recursive modules, which make dependency injection easier. This is used in the Scala compiler.

    Both of these libraries got me into trouble as a beginner Scala programmer. I would not recommend them for beginners.

    How do you determine if you should use this heavy artillery?
    Once you feel that you are spending a lot of time repeating code due to insufficient abstraction you can consider it. Otherwise:

    Keep It Simple.

    Dependent Types and Category Theory in Scala

    There are many new theoretical developments in Scala:
    • Dotty - a new compiler built on DOT a new type-theoretic foundation of Scala
    • Cats library - a simplified version of Scalaz implementing concepts from category theory
    • Shapeless library for dependent types. I am using this in my production code since Shapeless is used in Slick and Parboiled2


    Haskell

    Haskell is a research language from 1990. In 2008 its popularity started to rise. You can now find real jobs working in Haskell. Most publicized is that Facebook wrote their spam filter in Haskell.



    Why is Haskell so Hard to Learn?

    It took me around 2 years to learn to program in Haskell, which is exceptionally long. I have spoken to other people at Haskell meetups who have told me the same.

    Mathematical Precision

    Python effectively uses the Pareto principle: 20% of the features will give give you 80% of the functionality; Python has very few structures in the core language and reuses them.

    Haskell uses many more constructs. E.g. exception handling can be done in many different ways each with small advantages. You can chose the optimal exception monad transformer that has least dependencies for your problem.

    Cabal Hell and Stack

    Haskell is a fast developing language with a very deep stack of interdependent libraries.
    When I started programming in it, it was hard to set up even a simple project since you could not get the libraries to compile with versions that were compatible with each other.
    The build system is called cabal, and this phenomenon is called Cabal Hell.
    If you have been reading mailing list there are a lot of references to Cabal Hell.

    The Haskell consulting company FPComplete first released Stackage a curated list of libraries that works together. In 2015 they went further and released Stack which is a system that installs different versions of Haskell to work with Stackage versions.

    This has really made Haskell development easier.

    Dependently Typed Constructs in Haskell

    Dependently typed languages are the next step after Haskell. In normal languages the type system and the objects of the language are different systems. In dependently typed languages the objects and the types inhabits the same space. This gives more safety and greater flexibility but also makes it harder to program in.
    The type checker has to be replaced with a theorem-prover.

    You have to prove that the program is correct, and the proofs are part of the program and first order constructs.

    Haskell has a lot of activities towards emulating dependently typed languages.
    The next version of the Haskell compiler GHC 8 is making a big push for more uniform handling of types and kinds.

    Practical Haskell

    Haskell is a pioneering language and still introducing new ideas. It has clearly shown that it is production ready by being able to handle Facebook's spam filter.

    Aesthetically I prefer terse programming and like to use Haskell for non work related programming.

    There is a great Haskell community in New York City. Haskell feels like a subculture where Scala has now become the establishment. That said I do not feel Haskell envy when I program in Scala on a daily basis.

    Learning Haskell is a little like running a marathon. You get in good mental shape.


    Category Theory

    Category theory is often called Abstract Nonsense both by practitioners and detractors.
    It is a very abstract field of mathematics and its utility is pretty controversial.

    It abstracts internal properties of objects away and instead looks at relations between objects.
    Categories require very little structure and so there are categories everywhere.  Many mathematical objects can be turned into categories in many different ways. This high level of abstraction makes it hard to learn.

    There is a category Hask of Haskell types and functions.


    Steve Awodey lecture series on category theory

    Vector Spaces Described With a Few String Diagrams

    To give a glimpse of the power of category theory: In this video lecture John Baez shows how you can express the axioms of finite dimensional vector spaces with a few string diagrams.

    Video lecture by John Baez

    With 2 more simple operations you can extend it to control theory.

    Quest For a Solid Foundation of Mathematics

    At the university I embarked on a long quest for a solid scientific foundation. Fist I studied chemistry and physics. Quantum physics drove me to studying mathematics for more clarity. For higher clarity and a solid foundation I studied mathematical logic.
    I did not find clarity in mathematical logic. Instead I found:

    The Dirty Secret About the Foundation of Mathematics

    My next stop was the normal foundation for modern mathematics: ZFC, Zermelo–Fraenkel set theory with the axiom of choice.

    This was even less intuitive than logic. There were more non intuitive axioms. This was like learning computer science from a reference of x86 assembly: A big random mess. There were also an uncertain connection between the axioms of logic and the axioms set theory.

    ZFC and first order logic makes 2 strong assumptions:
    1. Law of Excluded Middle
    2. Axiom of Choice
    Law of Excluded Middle is saying that every mathematical sentence is either true or false. This is a very strong assumption that was not motivated at all. And it certainly does not extend to other sentences.

    Constructive Mathematics / Intuitionistic Logic

    There was actually a debate about what should be a foundation for mathematics at the beginning of the 20th century.
    A competing foundation of mathematics was Brouwer's constructive mathematics. In order to prove something about a mathematical object you need to be able to construct it and via the Curry-Howard correspondence this is equivalent to writing a program constructing a particular type.

    This was barely mentioned at the university. I had one professor who once briefly said that there was this other thing called intuitionistic logic, but it was so much harder to prove things in it, why should we bother.

    Recently constructive mathematics have had a revival with Homotopy Type Theory. HoTT is based on category theory, type theory, homotopy theory and intuitionistic logic.
    This holds a lot of promise and is another reason why category theory is practical for me.


    Robert Harper's lectures on type theory
    end with an introduction to HoTT

    Future of Intelligent Software

    There are roughly 2 main approaches to artificial intelligence
    • Top down or symbolic techniques e.g. logic or Lisp
    • Bottom up or machine learning techniques e.g. neural networks
    The symbolic approach was favored for a long time but did not deliver on its promise. Now machine learning is everywhere and has created many advances in modern software.

    To me it seems obvious that more intelligent software needs both. But combining them has been an elusive goal since they are very different by nature.

    Databases created a revolution in data management. They reduce data retrieval to simplified first order logic, you just write a logic expression for what you want.

    Dependently typed language is the level of abstraction where programs and logic merge.
    I think that intelligent software of the future will be a combination of dependently typed languages and machine learning.
    A promising approach is: Discovery of Bayesian network models from data. This finds causality in a form that can be combined with logic reasoning.

    Conclusion

    I invested a lot of time in statically typed functional languages and was not sure how much this would help me in my daily work. It helped a lot, especially with reuse and stability.

    Scala has made it substantially easier to create production quality software.

    MLib and ScalaNLP are 2 popular open source projects. They show me that Scala is a good environment for NLP and machine learning.

    I am only starting to see an outline of category theory, dependently typed languages and HoTT. It looks like computer science and mathematics are not mainly done, but we still have some big changes ahead of us.

    Friday, April 1, 2011

    Practical Probabilistic Topic Models for NLP

    Latent Dirichlet Allocation, LDA is a new and very powerful technique for finding the topics in a collection of texts, using unsupervised learning. LDA is a probabilistic topic models. LDA was developed in 2003 and rely on advanced math. This post is a practical guide about how to get started building LDA models and software.

    LDA will have a substantial impact on corpus based natural language processing; since it opens up for easy creation of semantic models based on machine learning.

    Motivation for topic models


    With the Internet we have large amount of text available. Having the text categorized into topics make text search much more precise and makes it possible to find similar documents.

    Text categorization is not an easy problem:
    • Texts usually deals with more than one topic
    • There is no clear standard for categorization
    • Doing it by hand is infeasible
    Nuanced categorized is a hard problem, with many moving parts, but in 2003 David M. Blei, Andrew Y. Ng and Michael I. Jordan published an article on a new approach called Latent Dirichlet Allocation. LDA can be implemented base on research articles, but if you are not a machine learning academic the math is intimidating and the material is still new.

    There is actually good material available, but finding all the pieces takes some work. Most things you need are available online for free. Here is a chronological account for what I did to understanding LDA and start implementing it.

    Need for more sophisticated hierarchical topic models


    In 2009 I needed a fine grained classification of text, using unsupervised or semi supervised training. I spend a little time thinking about it, and had some idea about making bootstrapped training in a 2 layered hierarchy. It was hackish, complex and I was not sure how numerically stable it was. I never got around to implement it.


    David Blei


    I went to 4 Annual Machine Learning Symposium in 2009 and asked around for solutions to my problem. Several attendees told me to look at David Blei work. I did but he has written a lot of math heavy articles, so I did not know were to start.

    I was lucky to see David Blei give a presentation on LDA first at the 5 Annual Machine Learning Symposium. David Blei works at Princeton and just exudes brilliance. He gave a lucid entertaining description of the LDA with examples. It wa really shocking to see the LDA algorithm find scientific topics on its own with no human intervention.

    I saw him give the same talk at the NYC Machine Learning Meetup, and luckily that was videotaped here are part 1 and part 2. I watched these videos a few times. This gave me a good intuition for the algorithm.

    I looked through his articles and found a good beginner articles BleiLafferty2009. I read through that several time, but I could not understand it.

    I went out and bought the text book that David Blei recommended: Pattern Recognition and Machine Learning by Christopher M. Bishop. After reading the introduction chapter, I read BleiLafferty2009 again and was able to understand it. On page 10 the essence of the algorithm is described in a small text box.


    Software implementation of LDA


    There are plenty open source implementation of LDA. Here are a few observations:

    lda-c in C by David Blei is an implementation in old school C. The code is readable, concise and clean.

    lda for R package by Jonathan Chang. Implementing many models with extensive documentation.

    Online LDA in Python by Matt Hoffman. Short code, but not too much documentation.

    LDA Apache Mahout in Java. Active development community works with Hadoop / MapReduce.



    No matter what language you prefer there should be a good implementation.


    Practical software considerations


    All the implementations looked good. But if you want to use LDA software then robustness, scalability and extendibility are big issues. First you just want the algorithm to run for simple text input. Next day you want the following options:
    • Better word tokenizer
    • Bigrams and collocation
    • Words stemmer
    • LDA on structured text
    • Read from database


    Programming language choice for LDA


    Here is a little common sense advice on choice of programming language for LDA programming.

    C
    C is an elegant, simple system programming language.
    C is not my first choice of a language for text processing.


    C++
    C++ is a very powerful but also complex language.
    NLP lib: The Lemur Project
    I would be happy to use C++ for text processing.


    C#
    C# is a great language.
    NLP lib:  SharpNLP. 
    You will have to implement LDA yourself or port one of the other implementations. SciPy is getting ported to C# but it does not have the best numeric libraries.


    Clojure
    Clojure is a moderate sized LISP dialect build on the Java JVM.
    NLP lib: OpenNLP through clojure-opennlp.
    LISP is classic AI language and you can use one of the Java LDA implementations.


    Java
    Java is modern object oriented programming language with access to every thinkable library.
    NLP lib: OpenNLP.


    Python
    Python is an elegant language very well suited for NLP.
    NLP lib: NLTK, using NumPy and SciPy


    R
    R is a fantastic language for statistics, but not so great for low level text processing.
    NLP lib:
    The R implementation of LDA looks great; I think that it is common to do all the preprocessing in another language say Perl. And then do all the rest of the work in R.


    Different versions of LDA


    There are now a lot of different LDA models geared towards different domains. Let me just mention a couple:

    Online LDA
    Online means that: you do learning of the models in small batches; instead of on all the documents. This is useful for a continuously running system.

    Dynamic LDA
    Good for handling text that stretches over a long time interval say 100 years.

    Hierarchical LDA
    This will handle topics are organized in hierarchies.


    Gray box approach to LDA


    The math needed for LDA is advanced. If you do not succeed in understand it I still think that you can learn to use the code, if you are willing to take something on faith and get your hands dirty.

    Tuesday, February 15, 2011

    Is IBM Watson Beginning An AI Boom?

    Artificial intelligence fell out of favor in the 1970s, the start of first artificial intelligence winter, and has mainly been out of favor since. In April 2010 I wrote a post about how you can now get a paying job doing AI, machine learning and natural language processing outside academia.

    Now barely one year later I have seen a few demonstrations that signal that artificial intelligence has taken another leap towards mainstream acceptance:
    • Yann LeCun demonstrated a computer vision system that could learn to recognize objects from his pocket after being shown a few examples, under a talk about learning feature hierarchies for computer vision 
    • Andrew Hogue demonstrated Google Squared and Google Sentiment Analysis at Google Tech Talk, those systems both show rudimentary understanding of web pages and use word association
    • IBM Watson super computer is competing against the best human players on Jeopardy 
      All these 3 systems contain some real intelligence. Rudimentary by human standard, but AI has gone from the very specialized systems to handling more general tasks. It feels like AI is picking up steam. I am seeing startups based on machine learning pop up. This reminds me of the Internet boom in 1990s. I moved to New York in 1996, at the beginning of the Internet boom. I saw firsthand the crazy gold rush where fortunes were made and lost in short time, Internet startups were everywhere and everybody was talking about IPOs. This got me thinking, are we headed towards an artificial intelligence boom, and what would it look like?

      IBM Watson

      IBM Watson is a well executed factoid extraction system, but it is a brilliant marketing move, promoting IBM's new POWER7 system and their Smart Planet consulting services. It gives some people the impression that we already have human-like AI, and in that sense it could serve as a catalyst for investments in AI. This post is not about human-like artificial intelligence, but about the spread of shallow artificial intelligence.

      Applications For Shallow Artificial Intelligence

      Both people and corporations would gain value from having AI systems that they could ask free form questions to and get answers from in very diverse topics. In particular in these fields:
      • Medical science
      • Law
      • Surveillance
      • Military

      Many people, me included, are concerned about a big brother state and military use of AI, but I do not think that is going to stop adaption. These people play for keeps.

      There are signs that the financial service industry is starting to use sentiment analysis for their pricing and risk models. Shallow AI would be a good candidate for more advanced algorithmic trading.

      Bottom Up vs. Top Down Approaches

      Here is a very brief simplified introduction to AI techniques and tools. AI is a loosely defined field, with a loose collection of techniques. You can roughly categorize them it top down and bottom up approaches.

      Top down or symbolic techniques
      • Automated reasoning
      • Logic
      • Many forms of tree search
      • Semantic networks
      • Planning
      Bottom up or machine learning techniques
      • Neural networks, computer with similar structure to the brain
      • Machine learning

      The top down systems are programmed by hand, while the bottom up systems learns themselves based on examples without human intervention, a bit like the brain.

      What Is Causing This Sudden Leap?

      Many top down techniques were developed by the 1960s. They were very good ideas, but they did not scale; they only worked for small toy problems.
      Neural networks are an important bottom up technique. They started in 1950s, but fell out of favor; they came roaring back in 1980s. In the 1990 the machine learning / statistical approaches to natural language processing beat out Chomsky's generative grammar approach.

      The technology that is needed for what we are doing now have been around for a long time. Why are these systems popping up now?

      I think that we are seeing the beginning of a combination machine learning with top down techniques. The reason why this have taken so long is that it is hard to combine top down and bottom up techniques. Let me elaborate a little bit:

      Bottom up AI / machine learning are black boxes that you give some input and expected output and it will adjust a lot of parameter numbers so it can mimic the result. Usually the numbers will not make much sense they just work.

      In top down / symbolic AI you are creating detailed algorithms for working with concepts that make sense.

      Both top down and bottom up techniques are now well developed and better understood. This makes it easier to combine them.

      Other reasons for the leap are:
      • Cheap, powerful and highly parallel computers
      • Open source software, were programmers from around the world develop free software. This makes programming into more of an industrial assembly of parts.

      Who Will Benefit From An AI Boom?

      Here are some groups of companies that made a lot of money during the Internet boom:
      • Cisco and Oracle the tool makers
      • Amazon and eBay small companies that grew to become domineering in e-commerce
      • Google and Yahoo advertisement driven information companies

      Initially big companies like IBM and Google that can create the technology should have an advantage, whether it will be in the capacity of tool makers or domineering players.

      It is hard to predict how high the barrier to entry in AI will be. AI programs are just trained on regular text found on or off the Internet. And today's super computer is tomorrow's game console. The Internet has a few domineering players, but it is generally decentralized and anybody can have a web presence.

      New York is now filled with startups using machine learning as a central element. They are "funded", but it seems like they got some seed capital. So maybe there is room for smaller companies to compete in the AI space.

      Job Skills That Will Be Required In An AI Boom

      During the Internet boom I met people with a bit of technical flair and no education beyond high school who picked up HTML in a week and next thing they were making $60/hour doing plain HTML. I think that the jobs in artificial intelligence are going to be a little more complex than those 1990s web developer jobs.

      In my own work I have noticed a move from writing programming to teaching software based on examples. This is a dramatic change, and it requires a different skill set.

      I think that there will still be plenty of need for programmers, but cognitive science, mathematics, statistics and linguistics will be skills in demand.

      My work would benefit from me having better English language skills. The topic that I am dealing with is, after all, the English language. So maybe that English literature degree could come in handy.

      Currently I feel optimistic about the field of artificial intelligence; there is progress after years of stagnation. We are wrestling a few secrets away from Mother Nature, and are making progress in understanding how the brain works. Sill, introduction of such powerful technology as artificial intelligence is going to affect society for better and worse.

      Thursday, June 24, 2010

      Orange, R, RapidMiner, Statistica and WEKA

      Review of open source and cheap software packages for Data Mining

      This blog posting is comparing the following tools, after working with them for 2 months and using them for solving a real data mining problem:
      • Orange
      • R
      • RapidMiner
      • Statistica 8 with Data Miner module
      • WEKA
      Statistica is commercial, all the other are open source. There is also a brief mention of the following Python libraries: mlpy, ffnet, NLTK.

      Summary of first impression

      This is a follow up on my previous post R, RapidMiner, Statistica, SSAS or WEKA describing my impression of the following software packages after using them for a couple of days each:
      • R
      • RapidMiner
      • SciPy
      • SQL Server Analysis Services, Business Intelligence Development Studio
      • SQL Server Analysis Services, Table Analysis Tool for Excel
      • Statistica 8 with Data Miner module
      • WEKA
      Let me summarize what I found:

      SciPy did not have what I needed. However I found a few other good Python-based solutions: Orange, mlpy, ffnet and NLTK.

      The SSAS-based solutions held promise due to their close integration with Microsoft products, but I found them to be too closely tied to data warehouses so I postponed exploring them.

      Statistica and RapidMiner had a lot of functionality and were polished, but the many features were overwhelming.

      R was harder to get started with and WEKA was less polished, so I did not spend too much time on them.

      Comparison matrix

      In order to compress my current findings I am summarizing it in this matrix. This metric is only based on limited work with the different software packages and is not very accurate. The categories are:
      Documentation; GUI and graphics; how polished the package is; ease of learning; controlling package from a script or program; how many machine learning algorithms that are available:

      DocGUIPolishedEaseScriptingAlgorithms
      Orange232332
      Python libs111332
      R322132
      RapidMiner232223
      Statistica333223
      WEKA222323

      Criteria for software package comparison

      The comparison is based on a real data mining task that is relatively simple:
      • Supervised learning for categorization.
      • Over 200 attributes mainly numeric but 2 categorical / text.
      • One of the categorical attributes is the most important predictor.
      • Data is clean, so no need to clean outliers and missing data.
      • Accuracy is a good metric.
      • GUI with good graphic to explore the data is a plus.

      General observations

      The most popular data mining packages in the industry are SAS and SPSS, but they are quite expensive. Orange, R, RapidMiner, Statistica and WEKA all can be used for doing real data mining work. While some of them are unpolished.

      There was a similar learning curve for most of the programs. Most programs took me a few days to get working, between the documentation and experimenting.

      I had to reformulate my original problem. Neural network models did not work well on my categorical / text attributes. Statistica produced an accuracy of 90%, while RapidMiner produced an accuracy of 82%.
      I replaced the 2 categorical attributes with a numeric attribute and accuracy of the best model increased to around 97%, and was much more uniform between the different tools.

      Orange

      Orange is an open source data mining package build on Python, NumPy, wrapped C, C++ and Qt.
      • Works both as a script and with an ETL work flow GUI.
      • Shortest script for doing training, cross validation, algorithms comparison and prediction.
      • I found Orange the easiest tool to learn.
      • Cross platform GUI.
      Issues:
      • Not super polished.
      • The install is big since you need to install QT.

      Python libs: ffnet, NumPy, mlpy, NLTK

      A few Python libs deserve to be mentioned here: ffnet, NumPy, mlpy and NLTK.
      • If you do not care about the graphic exploration, you can set up an ffnet neural network in few lines of code.
      • There are several machine learning algorithms in mlpy.
      • The machine learning is NLTK is very elegant if you have a text mining or NLP problem.
      • The libraries are self contained.
      Issues:
      • Limited list of machine learning algorithms.
      • Machine learning is not handled uniformly between the different libraries.

      R

      R is an open source statistical and data mining package and programming language.
      • Very extensive statistical library.
      • It is a powerful elegant array language in the tradition of APL, Mathematica and MATLAB, but also LISP/Scheme.
      • I was able to make a working machine learning program in just 40 lines of code.
      Issues:
      • Less specialized towards data mining.
      • There is a steep learning curve, unless you are familiar with array languages.

      R vs. Orange written in Python

      Python and R have a lot in common: they are both elegant, minimal, interpreted languages with good numeric libraries. Still they have a different feel. So I was interested in seeing how they compared.
      Orange / Python advantages
      • R is quite different from common programming languages.
      • Python is easier for most programmers to learn.
      • Python has better debugger.
      • Scripting data mining categorization problems is simpler in Orange.
      • Orange also has an ELT work flow GUI.
      R advantages
      • R is even more minimal than Python.
      • Numerical programming is better integrated in R, in Python where you have to use external packages NumPy and SciPy.
      • R has better graphics.
      • R is more transparent since the Orange are wrapped C++ classes.
      • Easier to combine with other statistical calculations.
      I made small script to solve my data mining problem in both Orange and R. This was my impression:

      If all you want to do is to solve a categorization problem I found Orange to be simpler. You have to become very familiar with how Orange read the spreadsheet, the different attribute types, notably the Meta attribute.

      Import and export of data from spreadsheet is easier in R, spreadsheet are stored in a data frames that the different machine learning algorithms are operating on. Programming in R really is very different, you are working on a higher abstraction level, but you do lose control over the details.

      RapidMiner

      RapidMiner is an open source statistical and data mining package written in Java.
      • Solid and complete package.
      • It easily reads and writes Excel files and different databases.
      • You program by piping components together in a graphic ETL work flows.
      • If you set up an illegal work flows RapidMiner suggest Quick Fixes to make it legal.
      Issues:
      • I only got it to works under Windows, but others have gotten it to work in other environments, see comment below.
      • There are a lot of different ETL modules; it took a while to understand how to use them.
      • First I had a hard time making a comparison between different models. Eventually I found a way: You chose a cross validation and select different models one by one. When you run the model the will all be stored on the result page and you can do comparison there.

      Statistica 8

      Statistica is a commercial statistics and data mining software package for Windows.
      There is a 90 day trial for Statistica 8 with data miner module in the textbook:
      Handbook of Statistical Analysis and Data Mining Applications. There is also a free 30 day trial.
      • Generally very polished and good at everything, but it is also the only non open source program.
      • High accuracy even when I gave it bad input.
      • You can script everything in Statistica in VB.
      • Cheap compared to SPSS and SAS.
      Issues:
      • So many options that it was hard to navigate the program.
      • The most important video about Data Miner Recipes is the very last out of 36.
      • Cost of Statistica is not available on their website.
      • It is cheap in a corporate setting, but not for private use.

      WEKA

      WEKA is an open source statistical and data mining library written in Java.
      • A lot of machine learning algorithms.
      • Easy to learn and use.
      • Good GUI.
      • Platform independent.
      Issues:
      • Worse connectivity to Excel spreadsheet and non Java based databases.
      • CSV reader not as robust as in RapidMiner.
      • Not as polished.

      RapidMiner vs. WEKA

      The most similar data mining packages are RapidMiner and WEKA. There have many similarities:
      • Written in in Java.
      • Free / open source software with GPL license.
      • RapidMiner includes many learning algorithms from WEKA.
      My first thought what that RapidMiner has everything that WEKA has, plus a lot of other functionality and is more polished. Therefore I did not spend too much time on WEKA. For the sake of completeness I took a second look at WEKA and I have to say that it was a lot easier to get WEKA to work. Sometimes less is more. Depending on what is more important functionality or ease of use.

      Conclusion

      There are several good and very different solutions. Let me finish by listing the strongest aspect of each tool:

      Orange has elegant and concise scripting and can also be run in an ETL GUI mode.
      R has elegant and concise scripting integrated with a vast statistical library.
      RapidMiner has a lot of functionality, is polished and has good connectivity.
      Statistica is the most polished product, and generally performed well in all categories. It gave good result when I gave it bad input.
      WEKA is the easiest GUI to learn and use.

      -Sami Badawi

      Thursday, March 18, 2010

      SharpNLP vs NLTK called from C# review

      C# and VB.net have fewer open source NLP libraries than languages like C++, Java, LISP and Perl. My last blog post: Open Source NLP in C# 3.5 using NLTK is about calling NLTK, which is written in Python, from IronPython embedded under C# or VB.net.

      An alternative is to use SharpNLP, which is the leading open source NLP project written in C# 2.0. SharpNLP is not as big as other Open Source NLP projects. This blog posting is a short comparison of SharpNLP and NLTK embedded in C#.

      Documentation

      NLTK has excellent documentation, including an introductory online book on NLP and Python programming.

      For SharpNLP the source code is the documentation. There is also a short introductory article by SharpNLP's author Richard J. Northedge.

      Ease of learning

      NLTK is very easy to work with under Python, but integrating it as embedded IronPython under C# took me a few days. It is still a lot simpler to get Python and C# to work together than Python and C++.

      SharpNLP's lack of documentation makes it harder to use; but it is very simple to install.

      Ease of use

      NLTK it is great to work with in the Python interpreter.

      SharpNLP simplifies life by not having to deal with the embedding of IronPython under C# and the mismatching between the 2 languages.

      Machine learning and statistical models

      NLTK comes with a variety of machine learning and statistical models: decision trees, naive Bayesian, and maximum entropy. They are very easy to train and validate, but do not preform well for large data sets.

      SharpNLP is focused on maximum entropy modeling.

      Tokenizer quality

      NLTK has a very simple RegEx based tokenizer that works well in most cases.

      SharpNLP has a more advanced maximum entropy based tokenizer that can split "don't" into "do | n't". On the other hand it sometimes makes errors and splits a normal word into 2 words.

      Development community

      NLTK has an active development community, with an active mailing list.

      SharpNLP was last release was in December 2006. It is a port of the Java based OpenNLP, and can read models from OpenNLP. SharpNLP has a low volume mailing list.

      Code quality

      NLTK lets you write programs that read from web pages, clean HTML out of text and do machine learning in a few lines of code.

      SharpNLP is written in C# 2.0 using generics. It is a port from OpenNLP and maintains a Java flavor, but it is still very readable and pleasant to work with.

      License

      NLTK's license is Apache License, Version 2.0, which should fit most people's need.

      SharpNLP's license is LGPL 2.1. This is a versatile license, but maybe a little harder to work with when the project is not active.

      Applications

      NLTK comes with a theorem prover for reasoning about semantic content of text.

      SharpNLP comes with an name, organization, time, date and percentage finder.
      It is very simple to add an advanced GUI, using WPF or WinForms.

      Conclusion

      Both packages comes with a lot of functionality. They both have weaknesses, but they are definitely usable. I have both SharpNLP and embedded NLTK in my NLP toolbox.

      -Sami Badawi

      Thursday, March 11, 2010

      Open Source NLP in C# 3.5 using NLTK

      I am working on natural language processing algorithms in a C# 3.5 environment. I did not find any open source NLP packages for C# or VB.NET.
      NLTK is a great open source NLP package written in Python. It comes with an online book. I decided to try to embed IronPython under C# and run NLTK from there. Here are a few thoughts about the experience.

      Problems with embedding IronPython and NLTK

      • Some libraries that NLTK uses are not installed in IronPython, e.g. zlib and numpy, you can mainly patch this up
      • You need a good understanding of how embedded IronPython works
      • The connection between Python and C# is not seamless
      • Sending data between Python and C# takes work
      • NLTK is pretty slow at starting up
      • Doing large scale machine learning in NLTK is slow

      C# and IronPython

      IronPython is a very good implementation of Python, but in C# 3.5 there is still a mismatch between C# and Python; this becomes an issue when you are dealing with a library as big as NLTK.
      The integration between IronPython and C# is going to improve with C# 4.0. How much remains to be seen.

      To embed or not to embed

      When is embedding IronPython and NLTK inside C# a good idea?

      Separate processes for NLTK under CPython and C#

      If your C# tasks and your NLP tasks are not interacting too much, it might be simpler to have a C# program call a NLP CPython program as an external process. E.g. you want to analyze the content of a Word document. You would open the Word document in C# create a Python process pipe the text into it and read the result back in JSON or XML and display it in ASP, WPF or WinForms.

      Small NLP tasks

      There is a learning curve for both NLTK and embedded IronPython, that slows down you down when you start work.

      Medium sized NLP projects

      The setup cost is not an issue so embedding IronPython and NLTK could work very well here.

      Big NLP projects

      The setup cost is not an issue, but at some point the mismatch between Python and C#, will start to outweigh the advantages you get.

      Prototyping in NLTK

      Start writing your application in NLTK either under CPython or IronPython. This should improve development time substantially. You might find that your prototype is good enough and you do not need to port it to C#; or you will have a working program that you can port to C#.

      References


      -Sami Badawi