Introduction to Topic Modeling
gensim is an NLP package with an emphasis on topic modeling, so it is the first candidate when topic modeling is the goal.
Where Simple Text Vectorizers Stop
Earlier in this course you worked with CountVectorizer and TfidfVectorizer. These classes turn a collection of documents into numerical representations that a program can work with. They are useful starting points, but they implement relatively simple text-processing methods. When a task needs more linguistic analysis or a different kind of modeling, the course points you toward other Python packages.
This article introduces the three packages the course recommends for more advanced text processing, explains what each one is good for, and shows how to tell them apart. It also introduces the idea of structure in a prediction problem, which becomes important once simple text methods are no longer enough.
To see what a vectorizer actually produces, picture a tiny collection of two documents. Each document becomes one row, and each word in the shared vocabulary becomes one column. Every cell holds a number for that document and that word. CountVectorizer fills the cells with counts of how often each word appears, while TfidfVectorizer fills them with weights. The grid below uses made-up counts for two short documents to show the shape of the result.
Choosing Among spacy, nltk, and gensim
The course names three packages for more advanced text processing. Each one is recommended for a somewhat different reason, so it helps to hold on to a single defining characteristic for each. spacy is relatively new and is described as very efficient and well designed. nltk is well established and complete, but somewhat dated. gensim is a natural language processing package with an emphasis on topic modeling.
| Package | Defining characteristic | Kind of property described |
|---|---|---|
| spacy | Relatively new, very efficient, and well designed | Efficiency |
| nltk | Well established and complete, but somewhat dated | Completeness and age |
| gensim | Natural language processing package with an emphasis on topic modeling | Focus area |
Each package is described by a different kind of property
Notice that each package is described by a different kind of property. One is described by its efficiency, one by its completeness and age, and one by its focus area. These are not the same measurement, so the three packages cannot be ranked against each other on a single scale. The useful question is not which package is best in general, but which property matches the goal you have.
Here is how the matching works in practice. Suppose your goal is to group a large set of articles by the subjects they discuss. Because gensim is the package whose emphasis is topic modeling, it is the first candidate to examine for that goal. If instead your goal is to process text with a tool that is described as very efficient and well designed, spacy is the first candidate to evaluate. nltk is the package to weigh when a complete, long-established toolkit matters more than modern design, with the caveat that the course describes it as somewhat dated.
Mistakes When Picking a Package
Choosing a package is easy to get wrong when a learner reads one short description and stops there. The mistakes below come directly from the way the course describes the three packages.
Common mistake Ranking the three packages on one scale
What it looks likeDeciding that spacy is better than nltk because efficiency sounds more important than being dated.
Why it failsEfficiency, completeness and age, and focus area are different kinds of property, so they cannot be placed on one ranking.
Do this insteadStart from your goal, then ask which package's defining characteristic matches it.
Common mistake Stopping after the first short description
What it looks likeReading that gensim has an emphasis on topic modeling and then using it for every text task.
Why it failsEach package is recommended for a somewhat different reason, and one description does not describe the whole range of tasks.
Do this insteadCompare the characteristic of each package against the specific goal before committing.
Common mistake Treating completeness as the deciding factor
What it looks likeChoosing nltk for a topic modeling project only because it is well established and complete.
Why it failsThe course ties gensim specifically to topic modeling, and nltk's description concerns completeness and age rather than that focus.
Do this insteadWhen topic modeling is the goal, check the focus-area description first, and treat completeness as a secondary consideration.
Why Known Structure Beats Raw Input
A very common instinct when starting a machine learning project is to collect every available measurement, pass them all into a general-purpose learner, and trust the algorithm to work out what matters. Many software packages make this easy, since they ship predefined models that each apply one particular algorithm. The approach feels efficient, but it rests on an assumption worth examining: that the learner will discover everything useful about the problem by itself, using nothing but the raw numbers.
What do you think happens?
Imagine a navigation app for a historical park. The phone has a GPS receiver, an accelerometer, and a compass, and the developers also have an accurate map showing the paths and points of interest. Two designs are possible: one feeds every sensor reading straight into a general learner, and the other encodes what is already known about how the park and the sensors behave. Which design is more likely to produce a trustworthy position?
Reveal answer
Encode what is already known about the park and the sensors
The course's reasoning is that many real problems have regularities, such as paths that cannot pass through walls, sensors that disagree in predictable ways, and quantities that must stay within known ranges. A model that encodes this structure can use it, while a general learner has to hope the raw data reveals it.
A problem has structure when it has regularities that can be used to improve predictions. Examples include known maps, physical limits, and predictable sensor behavior.
The useful observation is that structure can often be written down in the language of probability theory. Once it is written down, it becomes something the model can use directly, rather than something a person has to hope the data will reveal on its own.
Testing Whether a Problem Is Structured
A problem is structured when there is a mathematical model of the situation being predicted, and probability theory is a natural language for expressing that structure. So the test is practical: before you rely on a generic learner, ask whether you can describe the situation you are predicting with a model. If you can, the model's regularities are something you can encode rather than something you must leave for the learner to find.
The concept map below shows the chain of ideas. A real-world problem with regularities leads to a mathematical model of the situation, which can be expressed with probability. Incorporating that expressed structure into the model is what can yield better-performing predictions.
Tracing Conflicting Sensor Readings
Return to the navigation app. Each sensor gives evidence about where the user is, but none is perfect. A probabilistic model can state how the readings relate to the true position and to each other. The goal of the model is not only to produce a single number. It is meant to reason about how much each measurement should be trusted, and to combine the measurements into a best guess for the user's location.
Two Sensors Disagree About Direction
The compass and accelerometer indicate that the user is heading north, while the GPS reports movement to the south. What should the model register?
- 1Identify the evidence
The compass and accelerometer both point north, and the GPS points south. Each reading is treated as evidence about the same underlying position.
- 2Relate the readings to each other
The model compares the sources instead of reading each one in isolation. The two directions cannot both be correct.
- 3Register the contradiction
Because the sources disagree, the model records that they contradict each other.
- 4Weigh the likelihood of error
At least one of the sources is probably wrong. The model uses probability to decide how much each source should count, rather than averaging them blindly.
The model registers a contradiction and concludes that at least one reading is probably wrong, then combines the readings according to how much each should be trusted.
The same reasoning covers a second kind of problem. Suppose a position estimate would place the user inside a wall. Given the map, that estimate is implausible, so the model can treat it with skepticism. In both cases the model is not simply accepting whatever the raw numbers say. It uses what is known about the situation to weigh the evidence.
What Probabilistic Programming Languages Provide
Once you have expressed the situation and your model of how its factors work together, you still need a way to compute predictions from that custom model. Probabilistic programming languages address this need. They are described as the most general methods for computing predictions from custom probabilistic models, and they offer a very elegant and compact way to express a learning problem. Examples named in the course are PyMC and Stan.
- What you bring: a model of how the factors relate to one another, including how much each measurement should be trusted.
- What the language provides: a compact way to define probabilistic models and to perform inference from them.
- What still stays with you: a working understanding of probability theory, which the course says these languages still require.
The course does not describe the internal mechanics of how these languages perform inference, so this article does not go into that detail. For a learner, the key point is the division of labor: you define the structure of the problem, and the language provides a general way to compute predictions from that definition.
Practice Choosing a Tool and a Model
A team needs to process a large set of documents using a tool that is described as very efficient and well designed, and topic modeling is not part of their goal. Which of the three packages should they evaluate first, and which characteristic from the course justifies that choice?
Hints
- Look for the package whose description emphasizes efficiency and design rather than topic modeling.
- The justification is the defining characteristic given in the course, not a general sense that the package is popular.
A navigation prototype receives a GPS reading that points south while the compass and accelerometer point north. Explain how a probabilistic model should treat this situation, and say why a single averaged number would be a weaker answer than a model that weighs trust.
Hints
- Think about what the model registers when two sources contradict each other.
- The course stresses that the model is meant to reason about how much each measurement should be trusted.
Books for Deeper Study
The course points to two books for deeper study. One is an overview of natural language processing that also introduces nltk. The other is a more conceptual reference covering fundamental algorithms in information retrieval, natural language processing, and machine learning. The course notes that both books have online versions that can be accessed free of charge.
- 1An overview of natural language processing that also introduces nltk (named in the course)
- 2A conceptual reference on fundamental algorithms in information retrieval, natural language processing, and machine learning (named in the course); both books have free online versions
Key Takeaways
- CountVectorizer and TfidfVectorizer turn documents into numerical grids that serve as a starting point, but they implement relatively simple methods.
- spacy, nltk, and gensim are each described by a different kind of property, so choose among them by matching the characteristic to your goal; gensim is the first candidate for topic modeling.
- A problem is structured when a mathematical model of the situation exists, and encoding that structure in a probabilistic model lets the model weigh conflicting evidence rather than relying on raw data alone.
- Probabilistic programming languages such as PyMC and Stan provide a compact way to define probabilistic models and perform inference, but they still require an understanding of probability theory.