Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notebook for topic coherence use cases blog
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Some topic coherence use cases"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import operator\n",
"from collections import namedtuple\n",
"\n",
"from gensim.models import LdaModel\n",
"from gensim.models import CoherenceModel\n",
"from gensim.corpora import Dictionary\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"texts = [['human', 'interface', 'computer'],\n",
" ['survey', 'user', 'computer', 'system', 'response', 'time'],\n",
" ['eps', 'user', 'interface', 'system'],\n",
" ['system', 'human', 'system', 'eps'],\n",
" ['user', 'response', 'time'],\n",
" ['trees'],\n",
" ['graph', 'trees'],\n",
" ['graph', 'minors', 'trees'],\n",
" ['graph', 'minors', 'survey']]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dictionary = Dictionary(texts)\n",
"corpus = [dictionary.doc2bow(text) for text in texts]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Finding out the optimal number of topics"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def evaluate_graph(dictionary, corpus, texts, limit):\n",
" \"\"\"\n",
" Function to display num_topics - LDA graph using c_v coherence\n",
" \n",
" Parameters:\n",
" ----------\n",
" dictionary : Gensim dictionary\n",
" corpus : Gensim corpus\n",
" limit : topic limit\n",
" \n",
" Returns:\n",
" -------\n",
" lm_list : List of LDA topic models\n",
" \"\"\"\n",
" c_v = []\n",
" lm_list = []\n",
" for num_topics in range(1, limit):\n",
" lm = LdaModel(corpus=corpus, num_topics=num_topics, id2word=dictionary)\n",
" lm_list.append(lm)\n",
" cm = CoherenceModel(model=lm, texts=texts, dictionary=dictionary, coherence='c_v')\n",
" c_v.append(cm.get_coherence())\n",
" \n",
" # Show graph\n",
" x = range(1, limit)\n",
" plt.plot(x, c_v)\n",
" plt.xlabel(\"num_topics\")\n",
" plt.ylabel(\"Coherence score\")\n",
" plt.legend((\"c_v\"), loc='best')\n",
" plt.show()\n",
" \n",
" return lm_list"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAEQCAYAAABFtIg2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XecVOX1x/HPAQUlSERFbICKUUFjQQXsq2gkERFjQ2NM\nYhKJUTEaC8YYsMSo0ViTKFGxgQbBArafWDaISkkoooAgWChqMBaiICB7fn88d8MAM+zs7p25d2a+\n79drXzvlztyzsDvnPu085u6IiIhk0yTpAEREJL2UJEREJCclCRERyUlJQkREclKSEBGRnJQkREQk\np4InCTPraWazzGy2mV2ynuP2M7OVZvb9+r5WREQKwwq5TsLMmgCzgR7AImAS0NfdZ2U5bgywDLjH\n3R/N97UiIlI4hW5JdAXmuPt77r4SeBg4Nstx5wIjgH834LUiIlIghU4S2wLzM+4viB77HzPbBujj\n7n8FrD6vFRGRwkrDwPXNgMYbRERSaIMCv/9CoH3G/e2ixzLtCzxsZgZsAXzXzL7O87UAmJkKUImI\n1JO7Wz4HFewLaAq8DXQAmgFTgU7rOX4I8P36vjb8GOk2cODApEPIi+KMl+KMl+KMT/S5WefneEFb\nEu6+yszOAZ4jdG3d7e4zzaxfFODgtV9S12sLGa+IiKyp0N1NuPuzwC5rPXZnjmPPqOu1IiJSPGkY\nuK4IVVVVSYeQF8UZL8UZL8VZfAVdTFcsZubl8HOIiBSLmeU1cK2WhIhIDttvvz1mVtJf22+/faP+\nDdSSEBHJIbraTjqMRsn1M6glISIijaYkISIiOSlJiIhITkoSIiKSk5KE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AIcKK77wlsCJyV8ZJPo90V/wxkKwQ5NIp5L0Jl0Q+BU4Fno/ffE5iax88uUi9KElKJ\n3nH36dHtycD2dRz/krsvdfePCUnlyejx6XW8NrNC8S6EMtJzo/v3AYdkHPtw9P0hoPsabxKqyW7j\n7qMA3H2Fuy8jJKGfmNnvgD3c/cs6fg6RelOSkEq0POP2KkKV1q9Z/few0XqO94z7NdSvkvL6Stp7\njts5X+vuLxMSzULgXg1ySyEoSUglyvZh/S5hDAHgxJjOswRoFd1+C+hQO94A/BCozjj25Oh7X+C1\nzDeJdjKcb2bHAphZMzPbONoL4N/ufjdhz+80bxgkJaqk95MQaaBs4w83AI9Ee1E/VY/Xrs99wB1m\nthTYHzgDGGFmTQldRXdmHNvazKYBXxE211nb6cCdZnYlsIKQyA4BLjKzlcB/yTE+ItIYmgIrkrBo\nJtU+7v5J0rGIrE3dTSLJ05WapJZaEiKNZGa3AwcSPuwt+n6Lu9+XaGAiMVCSEBGRnNTdJCIiOSlJ\niIhITkoSIiKSk5KEiIjkpCQhIiI5/T+znzSFGysMFAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f3455ff9c50>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lm_list = evaluate_graph(dictionary, corpus, texts, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, 2 would be the best choice for the number of topics which is also the correct value since this corpus was designed to 2 topics, namely \"Human-Computer interaction\" and \"Graphs and trees\". Hence, just select `lm_list[1]` for your LdaModel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Making LDA behave like LSI\n",
"Here we just have 2 topics however, the same approach can be used for multiple topics."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lm = lm_list[1]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(0,\n",
" [(u'user', 0.14710733043762769),\n",
" (u'system', 0.11091383992680816),\n",
" (u'response', 0.10774688179113835),\n",
" (u'time', 0.10233047821720406),\n",
" (u'trees', 0.087948794803798927),\n",
" (u'graph', 0.079485146451697894),\n",
" (u'survey', 0.07526115085274368),\n",
" (u'interface', 0.074644398934470682),\n",
" (u'eps', 0.069599948233048423),\n",
" (u'computer', 0.058999396488198623)]),\n",
" (1,\n",
" [(u'system', 0.13282887492911646),\n",
" (u'graph', 0.11537522576174111),\n",
" (u'human', 0.11181322589854704),\n",
" (u'trees', 0.10703405658911989),\n",
" (u'minors', 0.094033312724915177),\n",
" (u'computer', 0.087136990647558726),\n",
" (u'eps', 0.076689841646505566),\n",
" (u'interface', 0.071718390218816486),\n",
" (u'survey', 0.071110563443194746),\n",
" (u'user', 0.048731616175960051)])]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lm.show_topics(formatted=False)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"coherence_values = {}\n",
"for n, topic in lm.show_topics(formatted=False):\n",
" topic = [word for word, _ in topic]\n",
" cm = CoherenceModel(topics=[topic], texts=texts, dictionary=dictionary, window_size=2)\n",
" coherence_values[n] = cm.get_coherence()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sorting in decreasing order of coherence values for ranking"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 0.40844152859807636), (1, 0.3367503796800731)]\n"
]
}
],
"source": [
"print sorted(coherence_values.items(), key=operator.itemgetter(1), reverse=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Making your own coherence pipeline\n",
"Let's modify `c_uci` to use `s_one_pre` instead of `s_one_one` segmentation"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from gensim.topic_coherence import (segmentation, probability_estimation,\n",
" direct_confirmation_measure, indirect_confirmation_measure,\n",
" aggregation)\n",
"from gensim.matutils import argsort"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"make_pipeline = namedtuple('Coherence_Measure', 'seg, prob, conf, aggr')"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"measure = make_pipeline(segmentation.s_one_one,\n",
" probability_estimation.p_boolean_sliding_window,\n",
" direct_confirmation_measure.log_ratio_measure,\n",
" aggregation.arithmetic_mean)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get topics out of the topic model"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"topics = []\n",
"for topic in lm.state.get_lambda():\n",
" bestn = argsort(topic, topn=10, reverse=True)\n",
"topics.append(bestn)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Perform segmentation\n",
"segmented_topics = measure.seg(topics)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Since this is a window-based coherence measure we will perform window based prob estimation\n",
"per_topic_postings, num_windows = measure.prob(texts=texts, segmented_topics=segmented_topics,\n",
" dictionary=dictionary, window_size=2)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"confirmed_measures = measure.conf(segmented_topics, per_topic_postings, num_windows, normalize=False)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-16.2529427009\n"
]
}
],
"source": [
"print measure.aggr(confirmed_measures)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@chaithanyateegala

This comment has been minimized.

Copy link

chaithanyateegala commented Jun 24, 2018

hi,
i tried to run the same code to find optimal no. of topics but iam getting constant straight line in the graph.
could you please help me in this.

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.