Merge pull request #30 from rasbt/sklearn018
Scikit-learn 0.18 optional syntax
This commit is contained in:
+10
-10
File diff suppressed because one or more lines are too long
+49
-49
File diff suppressed because one or more lines are too long
@@ -0,0 +1,20 @@
|
||||
digraph Tree {
|
||||
node [shape=box] ;
|
||||
0 [label="petal width <= 0.75\nentropy = 1.5799\nsamples = 105\nvalue = [34, 32, 39]"] ;
|
||||
1 [label="entropy = 0.0\nsamples = 34\nvalue = [34, 0, 0]"] ;
|
||||
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
|
||||
2 [label="petal length <= 4.95\nentropy = 0.993\nsamples = 71\nvalue = [0, 32, 39]"] ;
|
||||
0 -> 2 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
|
||||
3 [label="petal width <= 1.65\nentropy = 0.4306\nsamples = 34\nvalue = [0, 31, 3]"] ;
|
||||
2 -> 3 ;
|
||||
4 [label="entropy = 0.0\nsamples = 30\nvalue = [0, 30, 0]"] ;
|
||||
3 -> 4 ;
|
||||
5 [label="entropy = 0.8113\nsamples = 4\nvalue = [0, 1, 3]"] ;
|
||||
3 -> 5 ;
|
||||
6 [label="petal length <= 5.05\nentropy = 0.1793\nsamples = 37\nvalue = [0, 1, 36]"] ;
|
||||
2 -> 6 ;
|
||||
7 [label="entropy = 0.8113\nsamples = 4\nvalue = [0, 1, 3]"] ;
|
||||
6 -> 7 ;
|
||||
8 [label="entropy = 0.0\nsamples = 33\nvalue = [0, 0, 33]"] ;
|
||||
6 -> 8 ;
|
||||
}
|
||||
+146
-138
File diff suppressed because one or more lines are too long
+70
-100
File diff suppressed because one or more lines are too long
+85
-245
File diff suppressed because one or more lines are too long
+109
-79
File diff suppressed because one or more lines are too long
+111
-79
@@ -34,7 +34,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -43,25 +43,23 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The watermark extension is already loaded. To reload it, use:\n",
|
||||
" %reload_ext watermark\n",
|
||||
"Sebastian Raschka \n",
|
||||
"last updated: 2016-06-30 \n",
|
||||
"last updated: 2016-09-29 \n",
|
||||
"\n",
|
||||
"CPython 3.5.1\n",
|
||||
"IPython 4.2.0\n",
|
||||
"CPython 3.5.2\n",
|
||||
"IPython 5.1.0\n",
|
||||
"\n",
|
||||
"numpy 1.11.0\n",
|
||||
"numpy 1.11.1\n",
|
||||
"pandas 0.18.1\n",
|
||||
"matplotlib 1.5.1\n",
|
||||
"scikit-learn 0.17.1\n",
|
||||
"sklearn 0.18\n",
|
||||
"nltk 3.2.1\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext watermark\n",
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,scikit-learn,nltk"
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,sklearn,nltk"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -111,6 +109,19 @@
|
||||
"<br>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Added version check for recent scikit-learn 0.18 checks\n",
|
||||
"from distutils.version import LooseVersion as Version\n",
|
||||
"from sklearn import __version__ as sklearn_version"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@@ -166,7 +177,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -177,7 +188,7 @@
|
||||
"text": [
|
||||
"0% 100%\n",
|
||||
"[##############################] | ETA: 00:00:00\n",
|
||||
"Total time elapsed: 00:06:23\n"
|
||||
"Total time elapsed: 00:09:04\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -215,7 +226,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -236,7 +247,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -247,7 +258,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -291,7 +302,7 @@
|
||||
"2 ***SPOILER*** Do not read this, if you think a... 0"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -356,7 +367,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -364,6 +375,7 @@
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"from sklearn.feature_extraction.text import CountVectorizer\n",
|
||||
"\n",
|
||||
"count = CountVectorizer()\n",
|
||||
"docs = np.array([\n",
|
||||
" 'The sun is shining',\n",
|
||||
@@ -381,7 +393,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -390,7 +402,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'sun': 4, 'and': 0, 'is': 1, 'the': 6, 'shining': 3, 'two': 7, 'sweet': 5, 'weather': 8, 'one': 2}\n"
|
||||
"{'one': 2, 'sweet': 5, 'the': 6, 'shining': 3, 'weather': 8, 'and': 0, 'two': 7, 'is': 1, 'sun': 4}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -414,7 +426,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -449,7 +461,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -478,7 +490,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -546,7 +558,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -594,7 +606,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 14,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -605,7 +617,7 @@
|
||||
"array([ 3.39, 3. , 3.39, 1.29, 1.29, 1.29, 2. , 1.69, 1.29])"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -618,7 +630,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 15,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -629,7 +641,7 @@
|
||||
"array([ 0.5 , 0.45, 0.5 , 0.19, 0.19, 0.19, 0.3 , 0.25, 0.19])"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -655,7 +667,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 16,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -666,7 +678,7 @@
|
||||
"'is seven.<br /><br />Title (Brazil): Not Available'"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -677,7 +689,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 17,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -694,7 +706,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -705,7 +717,7 @@
|
||||
"'is seven title brazil not available'"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -716,7 +728,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 19,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -727,7 +739,7 @@
|
||||
"'this is a test :) :( :)'"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -738,7 +750,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 20,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -763,7 +775,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 21,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -783,7 +795,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 22,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -794,7 +806,7 @@
|
||||
"['runners', 'like', 'running', 'and', 'thus', 'they', 'run']"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -805,7 +817,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 23,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -816,7 +828,7 @@
|
||||
"['runner', 'like', 'run', 'and', 'thu', 'they', 'run']"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -827,7 +839,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 24,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -847,7 +859,7 @@
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -860,7 +872,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 25,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -871,7 +883,7 @@
|
||||
"['runner', 'like', 'run', 'run', 'lot']"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -908,7 +920,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 26,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -922,16 +934,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 29,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.grid_search import GridSearchCV\n",
|
||||
"from sklearn.pipeline import Pipeline\n",
|
||||
"from sklearn.linear_model import LogisticRegression\n",
|
||||
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
||||
"if Version(sklearn_version) < '0.18':\n",
|
||||
" from sklearn.grid_search import GridSearchCV\n",
|
||||
"else:\n",
|
||||
" from sklearn.model_selection import GridSearchCV\n",
|
||||
"\n",
|
||||
"tfidf = TfidfVectorizer(strip_accents=None,\n",
|
||||
" lowercase=False,\n",
|
||||
@@ -963,7 +978,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"execution_count": 30,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -979,9 +994,9 @@
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Parallel(n_jobs=-1)]: Done 42 tasks | elapsed: 12.0min\n",
|
||||
"[Parallel(n_jobs=-1)]: Done 192 tasks | elapsed: 53.2min\n",
|
||||
"[Parallel(n_jobs=-1)]: Done 240 out of 240 | elapsed: 69.4min finished\n"
|
||||
"[Parallel(n_jobs=-1)]: Done 42 tasks | elapsed: 43.9min\n",
|
||||
"[Parallel(n_jobs=-1)]: Done 192 tasks | elapsed: 228.2min\n",
|
||||
"[Parallel(n_jobs=-1)]: Done 240 out of 240 | elapsed: 265.3min finished\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -995,11 +1010,12 @@
|
||||
" ...nalty='l2', random_state=0, solver='liblinear', tol=0.0001,\n",
|
||||
" verbose=0, warm_start=False))]),\n",
|
||||
" fit_params={}, iid=True, n_jobs=-1,\n",
|
||||
" param_grid=[{'vect__tokenizer': [<function tokenizer at 0x111594400>, <function tokenizer_porter at 0x111594488>], 'vect__ngram_range': [(1, 1)], 'clf__C': [1.0, 10.0, 100.0], 'clf__penalty': ['l1', 'l2'], 'vect__stop_words': [['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'you...kenizer at 0x111594400>, <function tokenizer_porter at 0x111594488>], 'clf__penalty': ['l1', 'l2']}],\n",
|
||||
" pre_dispatch='2*n_jobs', refit=True, scoring='accuracy', verbose=1)"
|
||||
" param_grid=[{'vect__tokenizer': [<function tokenizer at 0x11851c6a8>, <function tokenizer_porter at 0x11851c730>], 'vect__ngram_range': [(1, 1)], 'vect__stop_words': [['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', '...alty': ['l1', 'l2'], 'vect__norm': [None], 'vect__ngram_range': [(1, 1)], 'vect__use_idf': [False]}],\n",
|
||||
" pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
|
||||
" scoring='accuracy', verbose=1)"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1010,7 +1026,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"execution_count": 31,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1019,7 +1035,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Best parameter set: {'vect__tokenizer': <function tokenizer at 0x111594400>, 'vect__ngram_range': (1, 1), 'clf__C': 10.0, 'clf__penalty': 'l2', 'vect__stop_words': None} \n",
|
||||
"Best parameter set: {'vect__tokenizer': <function tokenizer at 0x11851c6a8>, 'clf__C': 10.0, 'vect__stop_words': None, 'clf__penalty': 'l2', 'vect__ngram_range': (1, 1)} \n",
|
||||
"CV Accuracy: 0.897\n"
|
||||
]
|
||||
}
|
||||
@@ -1031,7 +1047,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"execution_count": 32,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1068,7 +1084,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"execution_count": 36,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1079,23 +1095,33 @@
|
||||
"array([ 0.6, 0.4, 0.6, 0.2, 0.6])"
|
||||
]
|
||||
},
|
||||
"execution_count": 38,
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.cross_validation import StratifiedKFold, cross_val_score\n",
|
||||
"from sklearn.linear_model import LogisticRegression\n",
|
||||
"import numpy as np\n",
|
||||
"if Version(sklearn_version) < '0.18':\n",
|
||||
" from sklearn.cross_validation import StratifiedKFold\n",
|
||||
" from sklearn.cross_validation import cross_val_score\n",
|
||||
"else:\n",
|
||||
" from sklearn.model_selection import StratifiedKFold\n",
|
||||
" from sklearn.model_selection import cross_val_score\n",
|
||||
"\n",
|
||||
"np.random.seed(0)\n",
|
||||
"np.set_printoptions(precision=6)\n",
|
||||
"y = [np.random.randint(3) for i in range(25)]\n",
|
||||
"X = (y + np.random.randn(25)).reshape(-1, 1)\n",
|
||||
"\n",
|
||||
"cv5_idx = list(StratifiedKFold(y, n_folds=5, shuffle=False, random_state=0))\n",
|
||||
"cross_val_score(LogisticRegression(random_state=123), X, y, cv=cv5_idx)"
|
||||
"if Version(sklearn_version) < '0.18':\n",
|
||||
" cv5_idx = list(StratifiedKFold(y, n_folds=5, shuffle=False, random_state=0))\n",
|
||||
"\n",
|
||||
"else:\n",
|
||||
" cv5_idx = list(StratifiedKFold(n_splits=5, shuffle=False, random_state=0).split(X, y))\n",
|
||||
" \n",
|
||||
"cross_val_score(LogisticRegression(random_state=123), X, y, cv=cv5_idx)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1109,7 +1135,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"execution_count": 38,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1135,13 +1161,19 @@
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.0s remaining: 0.0s\n",
|
||||
"[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.0s remaining: 0.0s\n",
|
||||
"[Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 0.0s finished\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.grid_search import GridSearchCV\n",
|
||||
"gs = GridSearchCV(LogisticRegression(), {}, cv=cv5_idx, verbose=3).fit(X, y) "
|
||||
"if Version(sklearn_version) < '0.18':\n",
|
||||
" from sklearn.grid_search import GridSearchCV\n",
|
||||
"else:\n",
|
||||
" from sklearn.model_selection import GridSearchCV\n",
|
||||
"\n",
|
||||
"gs = GridSearchCV(LogisticRegression(), {}, cv=cv5_idx, verbose=3).fit(X, y) \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1160,7 +1192,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"execution_count": 39,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1171,7 +1203,7 @@
|
||||
"0.47999999999999998"
|
||||
]
|
||||
},
|
||||
"execution_count": 40,
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1189,7 +1221,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 41,
|
||||
"execution_count": 40,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1200,7 +1232,7 @@
|
||||
"0.47999999999999998"
|
||||
]
|
||||
},
|
||||
"execution_count": 41,
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1238,7 +1270,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"execution_count": 48,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -1267,7 +1299,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"execution_count": 49,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1279,7 +1311,7 @@
|
||||
" 1)"
|
||||
]
|
||||
},
|
||||
"execution_count": 33,
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1290,7 +1322,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"execution_count": 50,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1310,7 +1342,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"execution_count": 51,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1330,7 +1362,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"execution_count": 52,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1341,7 +1373,7 @@
|
||||
"text": [
|
||||
"0% 100%\n",
|
||||
"[##############################] | ETA: 00:00:00\n",
|
||||
"Total time elapsed: 00:00:33\n"
|
||||
"Total time elapsed: 00:00:44\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -1361,7 +1393,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"execution_count": 53,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1370,7 +1402,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Accuracy: 0.868\n"
|
||||
"Accuracy: 0.867\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -1382,7 +1414,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"execution_count": 54,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1423,7 +1455,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.1"
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
+65
-70
@@ -44,21 +44,22 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sebastian Raschka \n",
|
||||
"Last updated: 08/20/2015 \n",
|
||||
"last updated: 2016-09-29 \n",
|
||||
"\n",
|
||||
"CPython 3.4.3\n",
|
||||
"IPython 3.2.1\n",
|
||||
"CPython 3.5.2\n",
|
||||
"IPython 5.1.0\n",
|
||||
"\n",
|
||||
"numpy 1.9.2\n",
|
||||
"pandas 0.16.2\n",
|
||||
"matplotlib 1.4.3\n",
|
||||
"nltk 3.0.4\n"
|
||||
"numpy 1.11.1\n",
|
||||
"pandas 0.18.1\n",
|
||||
"matplotlib 1.5.1\n",
|
||||
"nltk 3.2.1\n",
|
||||
"sklearn 0.18\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext watermark\n",
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,nltk"
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,nltk,sklearn"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -148,7 +149,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -175,15 +176,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import re\n",
|
||||
"from nltk.corpus import stopwords\n",
|
||||
"from nltk.stem import PorterStemmer\n",
|
||||
"\n",
|
||||
"stop = stopwords.words('english')\n",
|
||||
"porter = PorterStemmer()\n",
|
||||
@@ -205,7 +207,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -217,7 +219,7 @@
|
||||
" 1)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -267,7 +269,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -287,7 +289,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -307,7 +309,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -317,8 +319,8 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0% 100%\n",
|
||||
"[##############################] | ETA[sec]: 0.000 \n",
|
||||
"Total time elapsed: 59.019 sec\n"
|
||||
"[##############################] | ETA: 00:00:00\n",
|
||||
"Total time elapsed: 00:00:41\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -338,7 +340,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -347,7 +349,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Accuracy: 0.868\n"
|
||||
"Accuracy: 0.867\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -359,7 +361,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -394,7 +396,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -420,7 +422,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -477,7 +479,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
@@ -489,7 +491,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -505,7 +507,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 14,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -515,7 +517,7 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Prediction: positive\n",
|
||||
"Probability: 91.56%\n"
|
||||
"Probability: 82.52%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -553,13 +555,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 15,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sqlite3\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if os.path.exists('reviews.sqlite'):\n",
|
||||
" os.remove('reviews.sqlite')\n",
|
||||
"\n",
|
||||
"conn = sqlite3.connect('reviews.sqlite')\n",
|
||||
"c = conn.cursor()\n",
|
||||
@@ -577,7 +583,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 16,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -594,7 +600,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 17,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -603,7 +609,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[('I love this movie', 1, '2015-07-15 01:25:15'), ('I disliked this movie', 0, '2015-07-15 01:25:15')]\n"
|
||||
"[('I love this movie', 1, '2016-09-30 01:31:01'), ('I disliked this movie', 0, '2016-09-30 01:31:01')]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -613,7 +619,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -625,7 +631,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 700
|
||||
@@ -635,7 +641,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_01.png', width=700) "
|
||||
"Image(filename='../images/09_01.png', width=700) "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -682,7 +688,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 19,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -694,7 +700,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 19,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 400
|
||||
@@ -704,12 +710,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_02.png', width=400) "
|
||||
"Image(filename='../images/09_02.png', width=400) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 20,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -721,7 +727,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"execution_count": 20,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 400
|
||||
@@ -731,7 +737,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_03.png', width=400) "
|
||||
"Image(filename='../images/09_03.png', width=400) "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -751,7 +757,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 21,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -763,7 +769,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"execution_count": 21,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 400
|
||||
@@ -773,12 +779,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_04.png', width=400) "
|
||||
"Image(filename='../images/09_04.png', width=400) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 22,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -790,7 +796,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"execution_count": 22,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 400
|
||||
@@ -800,12 +806,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_05.png', width=400) "
|
||||
"Image(filename='../images/09_05.png', width=400) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 23,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -817,7 +823,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"execution_count": 23,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 400
|
||||
@@ -827,12 +833,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_06.png', width=400) "
|
||||
"Image(filename='../images/09_06.png', width=400) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 24,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -844,7 +850,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"execution_count": 24,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 200
|
||||
@@ -854,7 +860,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_07.png', width=200) "
|
||||
"Image(filename='../images/09_07.png', width=200) "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -874,7 +880,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 25,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -886,7 +892,7 @@
|
||||
"<IPython.core.display.Image object>"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"execution_count": 25,
|
||||
"metadata": {
|
||||
"image/png": {
|
||||
"width": 600
|
||||
@@ -896,7 +902,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Image(filename='./images/09_08.png', width=600) "
|
||||
"Image(filename='../images/09_08.png', width=600) "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -921,18 +927,6 @@
|
||||
"Change current directory to `movieclassifier`:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.chdir('movieclassifier')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@@ -942,7 +936,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 26,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -985,7 +979,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 27,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -1035,6 +1029,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"...\n",
|
||||
"<br>"
|
||||
]
|
||||
}
|
||||
@@ -1055,7 +1050,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.1"
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -5,8 +5,8 @@ import pickle
|
||||
|
||||
cur_dir = os.path.dirname(__file__)
|
||||
stop = pickle.load(open(
|
||||
os.path.join(cur_dir,
|
||||
'pkl_objects',
|
||||
os.path.join(cur_dir,
|
||||
'pkl_objects',
|
||||
'stopwords.pkl'), 'rb'))
|
||||
|
||||
def tokenizer(text):
|
||||
@@ -21,4 +21,4 @@ def tokenizer(text):
|
||||
vect = HashingVectorizer(decode_error='ignore',
|
||||
n_features=2**21,
|
||||
preprocessor=None,
|
||||
tokenizer=tokenizer)
|
||||
tokenizer=tokenizer)
|
||||
+159
-181
File diff suppressed because one or more lines are too long
+16
-25
File diff suppressed because one or more lines are too long
+10
-20
File diff suppressed because one or more lines are too long
@@ -1518,21 +1518,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.11"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
@@ -25,6 +24,14 @@ from sklearn.tree import export_graphviz
|
||||
from matplotlib.colors import ListedColormap
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# for sklearn 0.18's alternative syntax
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.grid_search import train_test_split
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
print('Section: First steps with scikit-learn')
|
||||
@@ -191,7 +198,8 @@ plt.legend(loc='upper left')
|
||||
# plt.savefig('./figures/logistic_regression.png', dpi=300)
|
||||
plt.show()
|
||||
|
||||
print('Predicted probabilities', lr.predict_proba(X_test_std[0, :]))
|
||||
print('Predicted probabilities', lr.predict_proba(X_test_std[0, :]
|
||||
.reshape(1, -1)))
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
|
||||
@@ -18,7 +18,6 @@ from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.preprocessing import OneHotEncoder
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
@@ -27,6 +26,13 @@ from sklearn.metrics import accuracy_score
|
||||
from itertools import combinations
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# for sklearn 0.18's alternative syntax
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.grid_search import train_test_split
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
@@ -382,5 +388,11 @@ plt.xlim([-1, X_train.shape[1]])
|
||||
# plt.savefig('./random_forest.png', dpi=300)
|
||||
plt.show()
|
||||
|
||||
X_selected = forest.transform(X_train, threshold=0.15)
|
||||
if Version(sklearn_version) < '0.18':
|
||||
X_selected = forest.transform(X_train, threshold=0.15)
|
||||
else:
|
||||
from sklearn.feature_selection import SelectFromModel
|
||||
sfm = SelectFromModel(forest, threshold=0.15, prefit=True)
|
||||
X_selected = sfm.transform(X_train)
|
||||
|
||||
X_selected.shape
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.decomposition import PCA
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -27,6 +26,17 @@ from scipy import exp
|
||||
from scipy.linalg import eigh
|
||||
from matplotlib.ticker import FormatStrFormatter
|
||||
|
||||
# for sklearn 0.18's alternative syntax
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.grid_search import train_test_split
|
||||
from sklearn.lda import LDA
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
|
||||
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
print('Section: Unsupervised dimensionality reduction'
|
||||
|
||||
@@ -15,16 +15,10 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.decomposition import PCA
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sklearn.cross_validation import StratifiedKFold
|
||||
from sklearn.cross_validation import cross_val_score
|
||||
from sklearn.learning_curve import learning_curve
|
||||
from sklearn.learning_curve import validation_curve
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.metrics import confusion_matrix
|
||||
@@ -38,6 +32,24 @@ from sklearn.metrics import roc_auc_score
|
||||
from sklearn.metrics import accuracy_score
|
||||
from scipy import interp
|
||||
|
||||
# for sklearn 0.18's alternative syntax
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.grid_search import train_test_split
|
||||
from sklearn.cross_validation import StratifiedKFold
|
||||
from sklearn.cross_validation import cross_val_score
|
||||
from sklearn.learning_curve import learning_curve
|
||||
from sklearn.learning_curve import validation_curve
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.model_selection import StratifiedKFold
|
||||
from sklearn.model_selection import cross_val_score
|
||||
from sklearn.model_selection import learning_curve
|
||||
from sklearn.model_selection import validation_curve
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
print('Section: Loading the Breast Cancer Wisconsin dataset')
|
||||
@@ -83,31 +95,39 @@ print(50 * '=')
|
||||
print('Section: K-fold cross-validation')
|
||||
print(50 * '-')
|
||||
|
||||
kfold = StratifiedKFold(y=y_train,
|
||||
n_folds=10,
|
||||
random_state=1)
|
||||
if Version(sklearn_version) < '0.18':
|
||||
kfold = StratifiedKFold(y=y_train,
|
||||
n_folds=10,
|
||||
random_state=1)
|
||||
else:
|
||||
kfold = StratifiedKFold(n_splits=10,
|
||||
random_state=1).split(X_train, y_train)
|
||||
|
||||
scores = []
|
||||
for k, (train, test) in enumerate(kfold):
|
||||
pipe_lr.fit(X_train[train], y_train[train])
|
||||
score = pipe_lr.score(X_train[test], y_train[test])
|
||||
scores.append(score)
|
||||
print('Fold: %s, Class dist.: %s, Acc: %.3f' % (k+1,
|
||||
print('Fold: %s, Class dist.: %s, Acc: %.3f' % (k + 1,
|
||||
np.bincount(y_train[train]), score))
|
||||
|
||||
print('\nCV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))
|
||||
|
||||
print('Using StratifiedKFold')
|
||||
kfold = StratifiedKFold(y=y_train,
|
||||
n_folds=10,
|
||||
random_state=1)
|
||||
if Version(sklearn_version) < '0.18':
|
||||
kfold = StratifiedKFold(y=y_train,
|
||||
n_folds=10,
|
||||
random_state=1)
|
||||
else:
|
||||
kfold = StratifiedKFold(n_splits=10,
|
||||
random_state=1).split(X_train, y_train)
|
||||
|
||||
scores = []
|
||||
for k, (train, test) in enumerate(kfold):
|
||||
pipe_lr.fit(X_train[train], y_train[train])
|
||||
score = pipe_lr.score(X_train[test], y_train[test])
|
||||
scores.append(score)
|
||||
print('Fold: %s, Class dist.: %s, Acc: %.3f' % (k+1,
|
||||
print('Fold: %s, Class dist.: %s, Acc: %.3f' % (k + 1,
|
||||
np.bincount(y_train[train]), score))
|
||||
|
||||
print('\nCV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))
|
||||
@@ -134,12 +154,12 @@ pipe_lr = Pipeline([('scl', StandardScaler()),
|
||||
('clf', LogisticRegression(penalty='l2', random_state=0))])
|
||||
|
||||
train_sizes, train_scores, test_scores =\
|
||||
learning_curve(estimator=pipe_lr,
|
||||
X=X_train,
|
||||
y=y_train,
|
||||
train_sizes=np.linspace(0.1, 1.0, 10),
|
||||
cv=10,
|
||||
n_jobs=1)
|
||||
learning_curve(estimator=pipe_lr,
|
||||
X=X_train,
|
||||
y=y_train,
|
||||
train_sizes=np.linspace(0.1, 1.0, 10),
|
||||
cv=10,
|
||||
n_jobs=1)
|
||||
|
||||
train_mean = np.mean(train_scores, axis=1)
|
||||
train_std = np.std(train_scores, axis=1)
|
||||
@@ -182,12 +202,12 @@ print(50 * '-')
|
||||
|
||||
param_range = [0.001, 0.01, 0.1, 1.0, 10.0, 100.0]
|
||||
train_scores, test_scores = validation_curve(
|
||||
estimator=pipe_lr,
|
||||
X=X_train,
|
||||
y=y_train,
|
||||
param_name='clf__C',
|
||||
param_range=param_range,
|
||||
cv=10)
|
||||
estimator=pipe_lr,
|
||||
X=X_train,
|
||||
y=y_train,
|
||||
param_name='clf__C',
|
||||
param_range=param_range,
|
||||
cv=10)
|
||||
|
||||
train_mean = np.mean(train_scores, axis=1)
|
||||
train_std = np.std(train_scores, axis=1)
|
||||
@@ -345,7 +365,14 @@ pipe_lr = Pipeline([('scl', StandardScaler()),
|
||||
|
||||
X_train2 = X_train[:, [4, 14]]
|
||||
|
||||
cv = StratifiedKFold(y_train, n_folds=3, random_state=1)
|
||||
if Version(sklearn_version) < '0.18':
|
||||
cv = StratifiedKFold(y_train,
|
||||
n_folds=3,
|
||||
random_state=1)
|
||||
|
||||
else:
|
||||
cv = list(StratifiedKFold(n_splits=3,
|
||||
random_state=1).split(X_train, y_train))
|
||||
|
||||
fig = plt.figure(figsize=(7, 5))
|
||||
|
||||
@@ -367,7 +394,7 @@ for i, (train, test) in enumerate(cv):
|
||||
tpr,
|
||||
lw=1,
|
||||
label='ROC fold %d (area = %0.2f)'
|
||||
% (i+1, roc_auc))
|
||||
% (i + 1, roc_auc))
|
||||
|
||||
plt.plot([0, 1],
|
||||
[0, 1],
|
||||
|
||||
@@ -23,10 +23,8 @@ from sklearn.externals import six
|
||||
from sklearn.base import clone
|
||||
from sklearn.pipeline import _name_estimators
|
||||
from sklearn import datasets
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.cross_validation import cross_val_score
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
@@ -34,11 +32,21 @@ from sklearn.pipeline import Pipeline
|
||||
from sklearn.metrics import roc_curve
|
||||
from sklearn.metrics import auc
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
from sklearn.ensemble import BaggingClassifier
|
||||
from sklearn.ensemble import AdaBoostClassifier
|
||||
from itertools import product
|
||||
|
||||
# Added version check for recent scikit-learn 0.18 checks
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.cross_validation import cross_val_score
|
||||
from sklearn.cross_validation import GridSearchCV
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.model_selection import cross_val_score
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
@@ -48,7 +56,7 @@ print(50 * '-')
|
||||
|
||||
def ensemble_error(n_classifier, error):
|
||||
k_start = math.ceil(n_classifier / 2.0)
|
||||
probs = [comb(n_classifier, k) * error**k * (1-error)**(n_classifier - k)
|
||||
probs = [comb(n_classifier, k) * error**k * (1 - error)**(n_classifier - k)
|
||||
for k in range(k_start, n_classifier + 1)]
|
||||
return sum(probs)
|
||||
|
||||
@@ -185,11 +193,11 @@ class MajorityVoteClassifier(BaseEstimator,
|
||||
for clf in self.classifiers_]).T
|
||||
|
||||
maj_vote = np.apply_along_axis(
|
||||
lambda x:
|
||||
np.argmax(np.bincount(x,
|
||||
weights=self.weights)),
|
||||
axis=1,
|
||||
arr=predictions)
|
||||
lambda x:
|
||||
np.argmax(np.bincount(x,
|
||||
weights=self.weights)),
|
||||
axis=1,
|
||||
arr=predictions)
|
||||
maj_vote = self.lablenc_.inverse_transform(maj_vote)
|
||||
return maj_vote
|
||||
|
||||
@@ -237,9 +245,9 @@ le = LabelEncoder()
|
||||
y = le.fit_transform(y)
|
||||
|
||||
X_train, X_test, y_train, y_test =\
|
||||
train_test_split(X, y,
|
||||
test_size=0.5,
|
||||
random_state=1)
|
||||
train_test_split(X, y,
|
||||
test_size=0.5,
|
||||
random_state=1)
|
||||
|
||||
clf1 = LogisticRegression(penalty='l2',
|
||||
C=0.001,
|
||||
@@ -391,9 +399,19 @@ grid = GridSearchCV(estimator=mv_clf,
|
||||
scoring='roc_auc')
|
||||
grid.fit(X_train, y_train)
|
||||
|
||||
for params, mean_score, scores in grid.grid_scores_:
|
||||
print("%0.3f+/-%0.2f %r"
|
||||
% (mean_score, scores.std() / 2.0, params))
|
||||
if Version(sklearn_version) < '0.18':
|
||||
for params, mean_score, scores in grid.grid_scores_:
|
||||
print("%0.3f +/- %0.2f %r"
|
||||
% (mean_score, scores.std() / 2.0, params))
|
||||
|
||||
else:
|
||||
cv_keys = ('mean_test_score', 'std_test_score', 'params')
|
||||
|
||||
for r, _ in enumerate(grid.cv_results_['mean_test_score']):
|
||||
print("%0.3f +/- %0.2f %r"
|
||||
% (grid.cv_results_[cv_keys[0]][r],
|
||||
grid.cv_results_[cv_keys[1]][r] / 2.0,
|
||||
grid.cv_results_[cv_keys[2]][r]))
|
||||
|
||||
print('Best parameters: %s' % grid.best_params_)
|
||||
print('Accuracy: %.2f' % grid.best_score_)
|
||||
@@ -426,9 +444,9 @@ le = LabelEncoder()
|
||||
y = le.fit_transform(y)
|
||||
|
||||
X_train, X_test, y_train, y_test =\
|
||||
train_test_split(X, y,
|
||||
test_size=0.40,
|
||||
random_state=1)
|
||||
train_test_split(X, y,
|
||||
test_size=0.40,
|
||||
random_state=1)
|
||||
|
||||
tree = DecisionTreeClassifier(criterion='entropy',
|
||||
max_depth=None,
|
||||
|
||||
@@ -17,7 +17,6 @@ import re
|
||||
import nltk
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
from sklearn.feature_extraction.text import TfidfTransformer
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.feature_extraction.text import TfidfVectorizer
|
||||
@@ -26,6 +25,13 @@ from sklearn.linear_model import SGDClassifier
|
||||
from nltk.stem.porter import PorterStemmer
|
||||
from nltk.corpus import stopwords
|
||||
|
||||
# Added version check for recent scikit-learn 0.18 checks
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.cross_validation import GridSearchCV
|
||||
else:
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
|
||||
@@ -25,6 +25,14 @@ from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
from sklearn.ensemble import RandomForestRegressor
|
||||
|
||||
# Added version check for recent scikit-learn 0.18 checks
|
||||
from distutils.version import LooseVersion as Version
|
||||
from sklearn import __version__ as sklearn_version
|
||||
if Version(sklearn_version) < '0.18':
|
||||
from sklearn.cross_validation import train_test_split
|
||||
else:
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
#############################################################################
|
||||
print(50 * '=')
|
||||
print('Section: Exploring the Housing dataset')
|
||||
@@ -184,12 +192,20 @@ print('Section: Fitting a robust regression model using RANSAC')
|
||||
print(50 * '-')
|
||||
|
||||
|
||||
ransac = RANSACRegressor(LinearRegression(),
|
||||
max_trials=100,
|
||||
min_samples=50,
|
||||
residual_metric=lambda x: np.sum(np.abs(x), axis=1),
|
||||
residual_threshold=5.0,
|
||||
random_state=0)
|
||||
if Version(sklearn_version) < '0.18':
|
||||
ransac = RANSACRegressor(LinearRegression(),
|
||||
max_trials=100,
|
||||
min_samples=50,
|
||||
residual_metric=lambda x: np.sum(np.abs(x), axis=1),
|
||||
residual_threshold=5.0,
|
||||
random_state=0)
|
||||
else:
|
||||
ransac = RANSACRegressor(LinearRegression(),
|
||||
max_trials=100,
|
||||
min_samples=50,
|
||||
loss='absolute_loss',
|
||||
residual_threshold=5.0,
|
||||
random_state=0)
|
||||
ransac.fit(X, y)
|
||||
inlier_mask = ransac.inlier_mask_
|
||||
outlier_mask = np.logical_not(inlier_mask)
|
||||
|
||||
Reference in New Issue
Block a user