{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Naive Bayes homework with Yelp review text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1\n", "\n", "Read `yelp.csv` into a DataFrame." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
business_iddatereview_idstarstexttypeuser_idcoolusefulfunny
09yKzy9PApeiPPOUJEtnvkg2011-01-26fWKvX83p0-ka4JS3dc6E5A5My wife took me here on my birthday for breakf...reviewrLtl8ZkDX5vH5nAx9C3q5Q250
\n", "
" ], "text/plain": [ " business_id date review_id stars \\\n", "0 9yKzy9PApeiPPOUJEtnvkg 2011-01-26 fWKvX83p0-ka4JS3dc6E5A 5 \n", "\n", " text type \\\n", "0 My wife took me here on my birthday for breakf... review \n", "\n", " user_id cool useful funny \n", "0 rLtl8ZkDX5vH5nAx9C3q5Q 2 5 0 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# access yelp.csv using a relative path\n", "import pandas as pd\n", "yelp = pd.read_csv('../data/yelp.csv')\n", "yelp.head(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2\n", "\n", "Create a new DataFrame that only contains the 5-star and 1-star reviews." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# filter the DataFrame using an OR condition\n", "yelp_best_worst = yelp[(yelp.stars==5) | (yelp.stars==1)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3\n", "\n", "Split the new DataFrame into training and testing sets, using the review text as the only feature and the star rating as the response." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define X and y\n", "X = yelp_best_worst.text\n", "y = yelp_best_worst.stars" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# split into training and testing sets\n", "from sklearn.cross_validation import train_test_split\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4\n", "\n", "Use CountVectorizer to create document-term matrices from X_train and X_test." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# import and instantiate the vectorizer\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "vect = CountVectorizer()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# fit and transform X_train, but only transform X_test\n", "X_train_dtm = vect.fit_transform(X_train)\n", "X_test_dtm = vect.transform(X_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 5\n", "\n", "Use Naive Bayes to predict the star rating for reviews in the testing set, and calculate the accuracy." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# import/instantiate/fit\n", "from sklearn.naive_bayes import MultinomialNB\n", "nb = MultinomialNB()\n", "nb.fit(X_train_dtm, y_train)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# make class predictions\n", "y_pred_class = nb.predict(X_test_dtm)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.918786692759\n" ] } ], "source": [ "# calculate accuracy\n", "from sklearn import metrics\n", "print metrics.accuracy_score(y_test, y_pred_class)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 6\n", "\n", "Calculate the AUC." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([5, 5, 5, 5, 5, 1, 1, 5, 5, 5], dtype=int64)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# y_test contains fives and ones, which will confuse the roc_auc_score function\n", "y_test[:10].values" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 1, 1, 0, 0, 1, 1, 1])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create y_test_binary, which contains ones and zeros instead\n", "import numpy as np\n", "y_test_binary = np.where(y_test==5, 1, 0)\n", "y_test_binary[:10]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# predict class probabilities\n", "y_pred_prob = nb.predict_proba(X_test_dtm)[:, 1]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.940353585141\n" ] } ], "source": [ "# calculate the AUC using y_test_binary and y_pred_prob\n", "print metrics.roc_auc_score(y_test_binary, y_pred_prob)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 7\n", "\n", "Plot the ROC curve." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEPCAYAAABGP2P1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmcHFW99/HPN4FAIAkxBBQjEGUz7HuCIEwAISwKosjD\nIoJ65YVsXrlXNpe56gUR3ACvILLpg4AgXqJAEIEBDAhiQgiQYPJAZA1bQ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9SnP+fo5stq4yHY2JAsZGxLtlx+lx\nbOWh5i0YEW9JupNUyzucVIPsMIAezEdixXAbgw0DXsmSwnjSt9pOlJ5cejUifgH8gtRg+hdg95J7\n12tL2qybY5RfNO4HDpU0OPu2eihwv6QNgLcj4lrgwuw45doldfeF5gbSN9CO2geki/yXOz6TtRGs\n1c3nyWoxpwL/rXS1HEb6FgudR6VcSLrN1OGO7HNkx+kq9n+QbieV6/aiGhEPR8QO2U+npJD9W0rP\n+Q50rgkdUfLaUZv7Y1mcHff67wROKtk+vLuYuoj5LuDE7HMDlT3FVGIRnc8VpN+ji4CHY/kUtQAb\nkM6TNZATw6qn/NvYtcDOkh4j3Yue0UXZ8cCjkqaQvo3/JNJ8wscB1ykNbfwAacz7qseMiKnA1aRb\nVH8hDQ89DdgGeCi7pfNN4Ltd7OvnwGMdjc9l+/4jsCepJtMxv+8vSHMSTMm+Uf+MrmvKy/YTEY+S\nJpv/DPB90q22KaT2h45y9wBbdjQ+k2oWq2eNt48D/7XCASLmAquppJFe0hzSk0XHSXpW2WO5OQ0h\n1eCeyP4PPgy0lrz/nmz7KcC/Z9tOJf1/T5P0BHBCtv27Wfnpkh4FWro4XpQtd6yfBozPfoceAcaU\nlZ8G/CtrmD4NICKmAAvofBsJ0nzO9+X5x1tx/LiqWR1JagVmRMQNBR/nGdLjtE05B4Gk9wP3RMQW\nJdsGAFOAnUsSuzWAawxm9fVTUjtA0Zr2G5+kY0k1xbPL3jqY9NSXk0KDucZgZmaduMZgZmadODGY\nmVknTgxmZtaJE4OZmXXixGBmZp04MZiZWSf/HyifrJsO5bsLAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# plot ROC curve using y_test_binary and y_pred_prob\n", "fpr, tpr, thresholds = metrics.roc_curve(y_test_binary, y_pred_prob)\n", "plt.plot(fpr, tpr)\n", "plt.xlim([0.0, 1.0])\n", "plt.ylim([0.0, 1.0])\n", "plt.xlabel('False Positive Rate (1 - Specificity)')\n", "plt.ylabel('True Positive Rate (Sensitivity)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 8\n", "\n", "Print the confusion matrix, and calculate the sensitivity and specificity. Comment on the results." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[126 58]\n", " [ 25 813]]\n" ] } ], "source": [ "# print the confusion matrix\n", "print metrics.confusion_matrix(y_test, y_pred_class)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.9701670644391408" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calculate sensitivity\n", "813 / float(813 + 25)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.6847826086956522" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calculate specificity\n", "126 / float(126 + 58)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model is having a much easier time detecting five-star reviews than one-star reviews." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 9\n", "\n", "Browse through the review text for some of the false positives and false negatives. Based on your knowledge of how Naive Bayes works, do you have any theories about why the model is incorrectly classifying these reviews?" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2175 This has to be the worst restaurant in terms o...\n", "1781 If you like the stuck up Scottsdale vibe this ...\n", "2674 I'm sorry to be what seems to be the lone one ...\n", "9984 Went last night to Whore Foods to get basics t...\n", "3392 I found Lisa G's while driving through phoenix...\n", "8283 Don't know where I should start. Grand opening...\n", "2765 Went last week, and ordered a dozen variety. I...\n", "2839 Never Again,\\r\\nI brought my Mountain Bike in ...\n", "321 My wife and I live around the corner, hadn't e...\n", "1919 D-scust-ing.\n", "Name: text, dtype: object" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# first 10 false positives (meaning they were incorrectly classified as 5-star reviews)\n", "X_test[y_test < y_pred_class][:10]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"If you like the stuck up Scottsdale vibe this is a good place for you. The food isn't impressive. Nice outdoor seating.\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# false positive: model is reacting to the words \"good\", \"impressive\", \"nice\"\n", "X_test[1781]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"Went last night to Whore Foods to get basics to make pizza with, most clutch to the process was a three pack of yeast. Low and behold, the dirty hippie kids they have working there again didn't put something in the bag.\\r\\n\\r\\nAnd this time it was the yeast.\\r\\n\\r\\nI love the food there, but the employees are nothing more than entitled hippie kids from Scottsdale who can't be bothered to do their goddamn jobs! I am so sick of this crap with this corporation. Maybe its a Phoenix thing, or maybe its the hiring and firing processes of Whore Foods, but I am done shopping at any Whore Foods. In a place like Phoenix, where you have alternatives such as Sprouts, you'd think Whore Foods would smarten up.\\r\\n\\r\\nOr when you try to ask someone who works here where something is, and they just walk by with their nose high in the air. I understand its important to show your fellow dirt merchants that you're a super star, but I don't work with you, I contribute to your salary and over inflated set of benefits for a grocery clerk, so do your goddamn job. Useless little girl in need of a shower and orthodontist. \\r\\n\\r\\nBut alas, Whore Foods and its dirty hippie employees have alienated yet another person with a job and a degree into hating everything that place stands for.\\r\\n\\r\\nOh yea, one other thing, take the concealed firearm prohibition off of your stores. It didn't stop Jared Loughner from doing something horrid, and all it does is alienate law abiding citizens from their Constitutional rights. I understand you think that only parts of the Constitution should apply, but honestly I think you need to pull your collective heads out of your ass and take a shower.\\r\\n\\r\\nUseless motherf*&#$@s!\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# false positive: model had likely never seen many of these words in the training data\n", "X_test[9984]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'D-scust-ing.'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# false positive: model does not have enough data to work with\n", "X_test[1919]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7148 I now consider myself an Arizonian. If you dri...\n", "4963 This is by far my favourite department store, ...\n", "6318 Since I have ranted recently on poor customer ...\n", "380 This is a must try for any Mani Pedi fan. I us...\n", "5565 I`ve had work done by this shop a few times th...\n", "3448 I was there last week with my sisters and whil...\n", "6050 I went to sears today to check on a layaway th...\n", "2504 I've passed by prestige nails in walmart 100s ...\n", "2475 This place is so great! I am a nanny and had t...\n", "241 I was sad to come back to lai lai's and they n...\n", "Name: text, dtype: object" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# first 10 false negatives (meaning they were incorrectly classified as 1-star reviews)\n", "X_test[y_test > y_pred_class][:10]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'This is by far my favourite department store, hands down. I have had nothing but perfect experiences in this store, without exception, no matter what department I\\'m in. The shoe SA\\'s will bend over backwards to help you find a specific shoe, and the staff will even go so far as to send out hand-written thank you cards to your home address after you make a purchase - big or small. Tim & Anthony in the shoe salon are fabulous beyond words! \\r\\n\\r\\nI am not completely sure that I understand why people complain about the amount of merchandise on the floor or the lack of crowds in this store. Frankly, I would rather not be bombarded with merchandise and other people. One of the things I love the most about Barney\\'s is not only the prompt attention of SA\\'s, but the fact that they aren\\'t rushing around trying to help 35 people at once. The SA\\'s at Barney\\'s are incredibly friendly and will stop to have an actual conversation, regardless or whether you are purchasing something or not. I have also never experienced a \"high pressure\" sale situation here.\\r\\n\\r\\nAll in all, Barneys is pricey, and there is no getting around it. But, um, so is Neiman\\'s and that place is a crock. Anywhere that ONLY accepts American Express or their charge card and then treats you like scum if you aren\\'t carrying neither is no place that I want to spend my hard earned dollars. Yay Barneys!'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# false negative: model is reacting to the words \"complain\", \"crowds\", \"rushing\", \"pricey\", \"scum\"\n", "X_test[4963]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 10\n", "\n", "Let's pretend that you want to balance sensitivity and specificity. You can achieve this by changing the threshold for predicting a 5-star review. What threshold approximately balances sensitivity and specificity?" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# create a list that will store the results of the process below\n", "results = []\n", "\n", "# loop through the thresholds returned by the metrics.roc_curve function\n", "for threshold in thresholds:\n", " \n", " # make a class prediction of 5 if the predicted probability is higher than the threshold\n", " y_pred_class = np.where(y_pred_prob > threshold, 5, 1)\n", " \n", " # generate the confusion matrix and slice it into four pieces\n", " confusion = metrics.confusion_matrix(y_test, y_pred_class)\n", " TP = confusion[1][1]\n", " TN = confusion[0][0]\n", " FP = confusion[0][1]\n", " FN = confusion[1][0]\n", " \n", " # calculate the sensitivity and specificity\n", " sensitivity = TP / float(TP + FN)\n", " specificity = TN / float(TN + FP)\n", " \n", " # calculate the absolute difference between sensitivity and specificity\n", " difference = np.absolute(sensitivity - specificity)\n", " \n", " # append a tuple to the results list\n", " results.append((difference, sensitivity, specificity, threshold))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.0002983293556085842, 0.87470167064439142, 0.875, 0.99855196916444533)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# locate the minimum difference (at which sensitivity and specificity are balanced)\n", "min(results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At a threshold of approximately **0.9986**, the sensitivity and specificity are both approximately **0.875**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 10 (alternative solution)\n", "\n", "This solution simplifies the for loop by utilizing the \"fpr\" and \"tpr\" objects returned by the `metrics.roc_curve` function." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# create a list that will store the results of the process below\n", "results = []\n", "\n", "# loop through the thresholds returned by the metrics.roc_curve function (skipping the first threshold)\n", "for threshold in thresholds[1:]:\n", " \n", " # calculate the sensitivity and specificity\n", " sensitivity = tpr[thresholds > threshold][-1]\n", " specificity = 1 - fpr[thresholds > threshold][-1]\n", " \n", " # calculate the absolute difference between sensitivity and specificity\n", " difference = np.absolute(sensitivity - specificity)\n", " \n", " # append a tuple to the results list\n", " results.append((difference, sensitivity, specificity, threshold))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.0002983293556085842, 0.87470167064439142, 0.875, 0.99855196916444533)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# locate the minimum difference (at which sensitivity and specificity are balanced)\n", "min(results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 11\n", "\n", "Let's see how well Naive Bayes performs when all reviews are included, rather than just 1-star and 5-star reviews:\n", "\n", "- Define X and y using the original DataFrame from step 1. (y should contain 5 different classes.)\n", "- Split the data into training and testing sets.\n", "- Calculate the testing accuracy of a Naive Bayes model.\n", "- Compare the testing accuracy with the null accuracy.\n", "- Print the confusion matrix.\n", "- Comment on the results." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define X and y using the original DataFrame\n", "X = yelp.text\n", "y = yelp.stars" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# split into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# create document-term matrices\n", "X_train_dtm = vect.fit_transform(X_train)\n", "X_test_dtm = vect.transform(X_test)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# fit a Naive Bayes model\n", "nb.fit(X_train_dtm, y_train)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# make class predictions\n", "y_pred_class = nb.predict(X_test_dtm)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4712\n" ] } ], "source": [ "# calculate the testing accuary\n", "print metrics.accuracy_score(y_test, y_pred_class)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4 0.3536\n", "dtype: float64" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calculate the null accuracy\n", "y_test.value_counts().head(1) / len(y_test)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 55 14 24 65 27]\n", " [ 28 16 41 122 27]\n", " [ 5 7 35 281 37]\n", " [ 7 0 16 629 232]\n", " [ 6 4 6 373 443]]\n" ] } ], "source": [ "# print the confusion matrix\n", "print metrics.confusion_matrix(y_test, y_pred_class)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comments:\n", "\n", "- Nearly all 4-star and 5-star reviews are classified as 4 or 5 stars, but they are hard for the model to distinguish between.\n", "- 1-star, 2-star, and 3-star reviews are often classified as 4 stars, probably because it's the predominant class in the training data.\n", "- When the model predicts 1 or 2 stars, it's usually correct.\n", "- 47% accuracy is relatively impressive, given that humans would also have a hard time precisely identifying the star rating for many of these reviews." ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }