保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。

Revisit consent button We value your privacy We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies. Customise Reject All Accept All Customise Consent Preferences We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below. The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more NecessaryAlways Active Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytics Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Advertisement Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

Uncategorised Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet. No cookies to display. Reject All Save My Preferences Accept All Toggle SearchToggle Mobile Navigation Publish AI, ML & data-science insights to a global community of data professionals. Sign inSubmit an Article LatestEditor's PicksNewsletterShipAI


Write For TDS LinkedInXToggle Search Machine Learning

Language Models: N-Gram

A step into statistical language modeling Shashank Kapadia March 26, 20194 min read Containers on display N-Gram concept Photo by Erik Eastman on Unsplash

Introduction

Statistical language models, in its essence, are the type of models that assign probabilities to the sequences of words. In this article, we’ll understand the simplest model that assigns probabilities to sentences and sequences of words, the n-gram. You can think of an N-gram as the sequence of N words, by that notion, a 2-gram (or bigram) is a two-word sequence of words like “please turn,” “turn your,” or ”your homework,” and a 3-gram (or trigram) is a three-word sequence of words like, “please turn your,” or “turn your homework.”

Intuitive formulation

Let’s start with equation P(w|h), the probability of word w , given some history, h. For example, Learn this step by step with the interactive Machine Learning roadmap.

Here,
w = The
h = its water is so transparent that And, one way to estimate the above probability function is through the relative frequency count approach, where you would take a substantially large corpus, count the number of times you see its water is so transparent that , and then count the number of times it is followed by the. In other words, you are answering the question: Out of the times you saw the history h, how many times did the word w follow it? Now, you can imagine it is not feasible to perform this over an entire corpus, especially if it is of a significant size. This shortcoming and ways to decompose the probability function using the chain rule serve as the base intuition of the N-gram model. Here, you, instead of computing probability using the entire corpus, would approximate it by just a few historical words.

The bigram model

As the name suggests, the bigram model approximates the probability of a word given all the previous words by using only the conditional probability of one preceding word. In other words, you approximate it with the probability: P(the | that). And so, when you use a bigram model to predict the conditional probability of the next word, you are thus making the following approximation:

This assumption that the probability of a word depends only on the previous word is also known as Markov assumption. Markov models are the class of probabilisitic models that assume that we can predict the probability of some future unit without looking too far in the past. You can further generalize the bigram model to the trigram model which looks two words into the past and can thus be further generalized to the N-gram model.

Probability estimation

Now that we understand the underlying base for N-gram models, you’d think, how can we estimate the probability function? One of the most straightforward and intuitive ways to do so is Maximum Likelihood Estimation (MLE). For example, to compute a particular bigram probability of a word y given a previous word x , you can determine the count of the bigram C(xy) and normalize it by the sum of all the bigrams that share the same first-word x.

Challenges

There are, of course, challenges, as with every modeling approach and estimation method. Let’s look at the key ones affecting the N-gram model, as well as the use of MLE Sensitivity to the training corpus The N-gram model, like many statistical models, is significantly dependent on the training corpus. As a result**,** the probabilities often encode particular facts about a given training corpus. Besides, the performance of the N-gram model varies with the change in the value of N. Moreover, you may have a language task in which you know all the words that can occur, and hence, we know the vocabulary size V in advance. The closed vocabulary assumption assumes there are no unknown words, which is unlikely in practical scenarios. Smoothing A notable problem with the MLE approach is sparse data. Meaning any N-gram that appeared a sufficient number of times might have a reasonable estimate for its probability. But because any corpus is limited, some perfectly acceptable English word sequences are bound to be missing from it. As a result, the N-gram matrix for any training corpus is bound to have a substantial number of cases of putative “zero probability N-grams.”

Sources:

  1. CHAPTER DRAFT — | Stanford Lagunita. https://lagunita.stanford.edu/c4x/Engineering/CS-224N/asset/slp4.pdf
  2. Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Thanks for reading. If you have any feedback, please feel free to reach out by messaging me on LinkedIn or shooting me an email (shmkapadia[at]gmail.com).

Register Now to Continue Reading

Unlock Towards Data Science with a Free Account

All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more. Register for freeSign in to your account


SK Written By

Shashank Kapadia

See all from Shashank Kapadia Data ScienceLanguage ModelNgramsNLPStatistics Share This Article

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. Write for TDS

Related Articles

Review of Reinforcement Learning Papers #14

Artificial Intelligence I present 4 publications from my research area: reinforcement learning. Let's discuss it! Quentin Gallouédec December 19, 20220 min read

Using Bike-Share Data to Find the Most Popular Bike Routes in Your City

Programming Combining numerical and spatial analysis with Python Ben Chamblee January 27, 20225 min read

The Machine Learning Lessons I’ve Learned Last Month

Machine Learning Delayed January: deadlines, downtimes, and flow times Pascal Janetzky February 9, 20265 min read

Don't Over-Invest: How to efficiently stand up ML capabilities

Machine Learning Instead of setting up big projects to create the perfect model, organizations should create rapid prototypes and start accessing value from... Paul Yang @ Einblick Analytics August 10, 20213 min read

A Data Product View on Conversational AI

Machine Learning An overview of Conversational AI, how it shows up as products, different companies offering solutions and link sample code Richard Sheng November 2, 20208 min read

Data Science 101: Life Cycle of a Data Science Project

Data Science In this new post, I demystified data science and talked about the lifecycle of a data science project. It's a good read for everyone. Abraham Enyo-one Musa March 28, 202110 min read

How to Extract and Convert Tables From PDF Files to Pandas Dataframe?

Artificial Intelligence So you have some pdf files with tables in them and want to read them into a pandas data frame. Let me show you how. Saed Hussain April 19, 20223 min read

Where Online Learning Outperforms Conventional Models: How Recursive Least Squares Works

Machine Learning A simple model that learns on the fly updating its weights as new data comes in. Diego Unzueta September 17, 20214 min read

Understanding MapReduce with the Help of Harry Potter

Data Science MapReduce is an algorithm that allows large data sets to be processed in parallel, i.e. on multiple computers simultaneously. This greatly... Niklas Lang January 30, 20226 min read

Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. Subscribe to Our NewsletterWrite For TDS•About•Advertise•Privacy Policy•Terms of Use © Insight Media Group, LLC 2026•The New Stack•roadmap.sh•Creator Network

Recommended Articles

Close