Last active
January 27, 2016 19:48
-
-
Save fnielsen/e07d388d2d8d4d4e3519 to your computer and use it in GitHub Desktop.
Nielsen2014Python_case.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "", | |
| "signature": "sha256:b715e6a82defdc0fb73551197edeab91474dff90db7d955ecf5565b9f7890524" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Import packages to extended the functionality of basic Python\n", | |
| "\n", | |
| "# Let Python 2 behave like Python 3\n", | |
| "from __future__ import division, unicode_literals, print_function\n", | |
| "\n", | |
| "# Utility data structures\n", | |
| "from collections import Counter\n", | |
| "\n", | |
| "# Plotting\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import matplotlib.cm as cm\n", | |
| "% matplotlib inline\n", | |
| "\n", | |
| "# Natural language processing package\n", | |
| "import nltk\n", | |
| "import nltk.corpus\n", | |
| "\n", | |
| "# Numerical \n", | |
| "import numpy as np\n", | |
| "\n", | |
| "# Operating system functions\n", | |
| "import os\n", | |
| "\n", | |
| "# Data analysis package\n", | |
| "import pandas as pd\n", | |
| "\n", | |
| "# Kenneth Reitz' module to download data from the Web. (instead of urllib)\n", | |
| "import requests" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 4 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Specify a URL to download information from the Semantic MediaWiki 'WikiLit'\n", | |
| "# Such a query can be constructed on the web site and the resulting URL copy-and-pasted here.\n", | |
| "url = (\"http://wikilit.referata.com/\" \n", | |
| " \"wiki/Special:Ask/\"\n", | |
| " \"-5B-5BCategory:Publications-5D-5D/\" \n", | |
| " \"-3FHas-20author%3DAuthor(s)/-3FYear/\"\n", | |
| " \"-3FPublished-20in/-3FAbstract/-3FHas-20topic%3DTopic(s)/\" \n", | |
| " \"-3FHas-20domain%3DDomain(s)/\" \n", | |
| " \"format%3D-20csv/limit%3D-20600/offset%3D0\")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Download and read data as comma-separated values (CSV) information into a Pandas DataFrame\n", | |
| "documents = pd.read_csv(url)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "filename = os.path.expanduser('~/data/dtu02819/wikilit.csv')" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 6 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Write to a comma-separated values file at the local file system\n", | |
| "documents.to_csv(filename)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 7 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Read the comma-separated values file from the local file system\n", | |
| "documents = pd.read_csv(filename, index_col=0)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 12 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "documents.head()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "html": [ | |
| "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Unnamed: 0.1</th>\n", | |
| " <th>Author(s)</th>\n", | |
| " <th>Year</th>\n", | |
| " <th>Published in</th>\n", | |
| " <th>Abstract</th>\n", | |
| " <th>Topic(s)</th>\n", | |
| " <th>Domain(s)</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td> 'Wikipedia, the free encyclopedia' as a role m...</td>\n", | |
| " <td> Gordon M\u00fcller-Seitz,Guido Reger</td>\n", | |
| " <td> 2010</td>\n", | |
| " <td> International Journal of Technology Management</td>\n", | |
| " <td> Accounts of open source software (OSS) develop...</td>\n", | |
| " <td> Contributor motivation,Policies and governance...</td>\n", | |
| " <td> Information systems</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td> A 'resource review' of Wikipedia</td>\n", | |
| " <td> Cormac Lawler</td>\n", | |
| " <td> 2006</td>\n", | |
| " <td> Counselling & Psychotherapy Research</td>\n", | |
| " <td> The article offers information on Wikipedia, a...</td>\n", | |
| " <td> Miscellaneous topics</td>\n", | |
| " <td> Information systems</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td> A Persian web page classifier applying a combi...</td>\n", | |
| " <td> Mojgan Farhoodi,Alireza Yari,Maryam Mahmoudi</td>\n", | |
| " <td> 2009</td>\n", | |
| " <td> International Journal of Information Studies</td>\n", | |
| " <td> There are many automatic classification method...</td>\n", | |
| " <td> Text classification</td>\n", | |
| " <td> Computer science</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td> A Wikipedia literature review</td>\n", | |
| " <td> Owen S. Martin</td>\n", | |
| " <td> 2010</td>\n", | |
| " <td> ArXiv</td>\n", | |
| " <td> This paper was originally designed as a litera...</td>\n", | |
| " <td> Literature review</td>\n", | |
| " <td> Mathematics</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td> A Wikipedia matching approach to contextual ad...</td>\n", | |
| " <td> Alexander N. Pak,Chin-Wan Chung</td>\n", | |
| " <td> 2010</td>\n", | |
| " <td> World Wide Web</td>\n", | |
| " <td> Contextual advertising is an important part of...</td>\n", | |
| " <td> Other information retrieval topics</td>\n", | |
| " <td> Computer science</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 14, | |
| "text": [ | |
| " Unnamed: 0.1 \\\n", | |
| "0 'Wikipedia, the free encyclopedia' as a role m... \n", | |
| "1 A 'resource review' of Wikipedia \n", | |
| "2 A Persian web page classifier applying a combi... \n", | |
| "3 A Wikipedia literature review \n", | |
| "4 A Wikipedia matching approach to contextual ad... \n", | |
| "\n", | |
| " Author(s) Year \\\n", | |
| "0 Gordon M\u00fcller-Seitz,Guido Reger 2010 \n", | |
| "1 Cormac Lawler 2006 \n", | |
| "2 Mojgan Farhoodi,Alireza Yari,Maryam Mahmoudi 2009 \n", | |
| "3 Owen S. Martin 2010 \n", | |
| "4 Alexander N. Pak,Chin-Wan Chung 2010 \n", | |
| "\n", | |
| " Published in \\\n", | |
| "0 International Journal of Technology Management \n", | |
| "1 Counselling & Psychotherapy Research \n", | |
| "2 International Journal of Information Studies \n", | |
| "3 ArXiv \n", | |
| "4 World Wide Web \n", | |
| "\n", | |
| " Abstract \\\n", | |
| "0 Accounts of open source software (OSS) develop... \n", | |
| "1 The article offers information on Wikipedia, a... \n", | |
| "2 There are many automatic classification method... \n", | |
| "3 This paper was originally designed as a litera... \n", | |
| "4 Contextual advertising is an important part of... \n", | |
| "\n", | |
| " Topic(s) Domain(s) \n", | |
| "0 Contributor motivation,Policies and governance... Information systems \n", | |
| "1 Miscellaneous topics Information systems \n", | |
| "2 Text classification Computer science \n", | |
| "3 Literature review Mathematics \n", | |
| "4 Other information retrieval topics Computer science " | |
| ] | |
| } | |
| ], | |
| "prompt_number": 14 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Example on word tokenization of the first sentence in the first abstract\n", | |
| "sentences = nltk.sent_tokenize(documents.ix[0, 'Abstract'])\n", | |
| "\n", | |
| "# Show the sentences as a Python list of strings\n", | |
| "print(nltk.word_tokenize(sentences[0]))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "['Accounts', 'of', 'open', 'source', 'software', '(', 'OSS', ')', 'development', 'projects', 'frequently', 'stress', 'their', 'democratic', ',', 'sometimes', 'even', 'anarchic', 'nature', ',', 'in', 'contrast', 'to', 'for-profit', 'organisations', '.']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 16 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Tokenize all the text and count tokens\n", | |
| "\n", | |
| "# A extra attribute to contain the new data in the documents object\n", | |
| "documents.data = []\n", | |
| "\n", | |
| "# Token counter\n", | |
| "token_counts = Counter()\n", | |
| "\n", | |
| "# Iterate over all documents and all sentence and all words\n", | |
| "for abstract in documents['Abstract']:\n", | |
| " datum = {}\n", | |
| " datum['sentences'] = nltk.sent_tokenize(abstract)\n", | |
| " datum['tokenlist'] = [word.lower() for sent in datum['sentences'] \n", | |
| " for word in nltk.word_tokenize(sent)]\n", | |
| " token_counts.update(datum['tokenlist'])\n", | |
| " documents.data.append(datum)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 17 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# The five most common tokens in the entire WikiLit corpus\n", | |
| "token_counts.most_common(5)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 18, | |
| "text": [ | |
| "[('the', 4866), ('of', 3826), (',', 3785), ('.', 3585), ('and', 2856)]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 18 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Read stopword list ('the', 'a', 'for')\n", | |
| "stopwords = nltk.corpus.stopwords.words('english')\n", | |
| "relevant_tokens = {token: count for token, count in token_counts.items()\n", | |
| " if count > 2 and token not in stopwords and token.isalpha()}" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 19 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Show the most common tokens in the reduced token set\n", | |
| "Counter(relevant_tokens).most_common(5)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 20, | |
| "text": [ | |
| "[('wikipedia', 1428),\n", | |
| " ('information', 486),\n", | |
| " ('knowledge', 341),\n", | |
| " ('articles', 279),\n", | |
| " ('online', 265)]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 20 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Exclude the work 'wikipedia'\n", | |
| "relevant_tokens.pop('wikipedia', 0)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 21, | |
| "text": [ | |
| "1428" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 21 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Construct a dense document-term matrix with word counts in the elements\n", | |
| "# as a Numpy matrix\n", | |
| "tokens = relevant_tokens.keys() # as list\n", | |
| "M = np.asmatrix(np.zeros([len(documents), len(tokens)]))\n", | |
| "for n in range(len(documents)):\n", | |
| " for m, token in enumerate(tokens):\n", | |
| " M[n, m] = documents.data[n]['tokenlist'].count(token)\n", | |
| "M.shape" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 22, | |
| "text": [ | |
| "(525, 2899)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 22 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Value of the element in the first row in the column corresponding to the word 'software'.\n", | |
| "M[0, tokens.index('software')]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 23, | |
| "text": [ | |
| "1.0" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 23 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Plot part of the matrix as an image\n", | |
| "plt.imshow(M[:100, :100], cmap=cm.gray_r, interpolation='nearest')\n", | |
| "plt.xlabel('Tokens')\n", | |
| "plt.ylabel('Documents')\n", | |
| "plt.show()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "display_data", | |
| "png": "iVBORw0KGgoAAAANSUhEUgAAAQoAAAEJCAYAAABsX2lnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGd5JREFUeJzt3XtQVOf5B/DvCsaaxgRUWEYxv6UqICAIap1xbFwki8UG\ng5qodSTrrZexNZpkai/TFGinAuM4wUydtukY3KEZjG0zlsl4SYnFmqoxFiSpsSGjbKHIYiuQaESB\n5f39kWHDyuXAnrN73rP7/cw47i67nOfsLs953st5j0kIIUBENIJxegdARPJjoiAiRUwURKSIiYKI\nFDFREJEiJgoiUhTQRHHixAkkJiZi9uzZKC0tDeSmiUgFU6DmUbjdbiQkJKC6uhrTp0/HwoULUVlZ\niTlz5gRi80SkQnigNnThwgXMmjULFosFALB+/Xr8+c9/9koUJpMpUOEQ0X1GqhkClihaWlowY8YM\nz/3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>" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 24 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def nmf(M, components=5, iterations=500):\n", | |
| " \"\"\"Factorize matrix with non-negative matrix factorization.\"\"\"\n", | |
| " # Initialize to matrices\n", | |
| " W = np.asmatrix(np.random.random(([M.shape[0], components])))\n", | |
| " H = np.asmatrix(np.random.random(([components, M.shape[1]])))\n", | |
| " for n in range(0, iterations):\n", | |
| " H = np.multiply(H, (W.T * M) / (W.T * W * H + 0.001))\n", | |
| " W = np.multiply(W, (M * H.T) / (W * (H * H.T) + 0.001))\n", | |
| " return (W, H)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 25 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Perform the actual computation\n", | |
| "W, H = nmf(M, iterations=50, components=3)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 26 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "W.max(), H.max()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 27, | |
| "text": [ | |
| "(15.094951905539212, 1.8585767533145232)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 27 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Show the results in some format - This could be written nicer, using Jinja2\n", | |
| "for component in range(W.shape[1]):\n", | |
| " print(\"=\" * 80)\n", | |
| " print(\"COMPONENT %d: \" % (component + 1,))\n", | |
| " indices = (-H[component, :]).getA1().argsort()\n", | |
| " print(\" - \".join([tokens[i] for i in indices[:6] ]))\n", | |
| " print(\"-\")\n", | |
| " indices = (-W[:, component]).getA1().argsort()\n", | |
| " print(\"\\n\".join([documents.ix[i, 0][:80] for i in indices[:5] ]))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "================================================================================\n", | |
| "COMPONENT 1: \n", | |
| "authors - content - community - number - articles - analysis\n", | |
| "-\n", | |
| "Wikipedia - a quantitative analysis\n", | |
| "Open content and value creation\n", | |
| "Sharing knowledge and building communities: a narrative of the formation, develo\n", | |
| "Extracting content holes by comparing community-type content with Wikipedia\n", | |
| "An analysis of open content systems\n", | |
| "================================================================================\n", | |
| "COMPONENT 2: \n", | |
| "knowledge - document - clustering - linkage - topic - algorithm\n", | |
| "-\n", | |
| "Exploiting external/domain knowledge to enhance traditional text mining using gr\n", | |
| "Wikitology: a novel hybrid knowledge base derived from Wikipedia\n", | |
| "The WikiID: an alternative approach to the body of knowledge\n", | |
| "Breaking the knowledge acquisition bottleneck through conversational knowledge m\n", | |
| "Extracting lexical semantic knowledge from Wikipedia and Wiktionary\n", | |
| "================================================================================\n", | |
| "COMPONENT 3: \n", | |
| "information - web - use - students - search - results\n", | |
| "-\n", | |
| "Gender differences in information behavior concerning Wikipedia, an unorthodox i\n", | |
| "How and why do college students use Wikipedia?\n", | |
| "Where does the information come from? Information source use patterns in Wikiped\n", | |
| "What is the quality of surgery-related information on the Internet? Lessons lear\n", | |
| "Reliability of Wikipedia as a medication information source for pharmacy student\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 28 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 137 | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment