Created
August 30, 2013 18:52
-
-
Save elyase/6393115 to your computer and use it in GitHub Desktop.
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": "stumbleupon" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import numpy as np\nimport pandas as pd\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn import metrics, cross_validation\nfrom sklearn.cross_validation import train_test_split\nfrom sklearn.grid_search import GridSearchCV", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": "Import data" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# import data\nX = pd.read_csv('trainMatrix.csv')\ny = pd.read_csv('labels.csv').values.ravel()\nX_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.5, random_state=0)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 38 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": "Grid Search" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# Set the parameters by cross-validation\ntuned_parameters = [{'alpha': [0.5, 1, 2, 5, 10, 50, 100]}] #, 1, 2, 5, 10, 50, 100]\nclf = GridSearchCV(MultinomialNB(), tuned_parameters, cv=5, scoring='roc_auc', n_jobs=-1)\nclf.fit(X_train, y_train)\n\nmodel = clf.best_estimator_\nprint 'Best model = {}'.format(model)\n#model = MultinomialNB(alpha=best_alpha)\nfor params, mean_score, scores in clf.grid_scores_:\n print(\"%0.3f (+/-%0.03f) for %r\" % (mean_score, scores.std() / 2, params))", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": "*" | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment