├── .gitignore ├── LICENSE ├── assert ├── invoice.bmp └── invoice_ali.bmp ├── classic-machine-learning ├── birch_cluster.ipynb ├── bpr.ipynb ├── dbscan_cluster.ipynb ├── decision_tree_classifier.ipynb ├── decision_tree_classifier_1.ipynb ├── fp_tree_prefixspan.ipynb ├── kmeans_cluster.ipynb ├── knn_classifier.ipynb ├── lda.ipynb ├── linear-regression.ipynb ├── lle.ipynb ├── matrix_factorization.ipynb ├── native_bayes.ipynb ├── pca.ipynb ├── regression_production_example.ipynb ├── ridge_regression.ipynb ├── ridge_regression_1.ipynb ├── spectral_cluster.ipynb └── svm_classifier.ipynb ├── data ├── CCPP.zip ├── nlp_test0.txt ├── nlp_test2.txt └── train_modified.zip ├── ensemble-learning ├── adaboost-classifier.ipynb ├── gbdt_classifier.ipynb ├── random_forest_classifier.ipynb └── xgboost-example.ipynb ├── mathematics ├── mcmc_2.ipynb ├── mcmc_3_4.ipynb └── random_data_generation.ipynb ├── model-in-product ├── sklearn-jpmml │ ├── PMML_Example.ipynb │ ├── demo.pmml │ └── pmml_demo │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── PMMLDemo.java └── tensorflow-java │ ├── TFDemoJava │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── TFjavaDemo.java │ ├── rf.pb │ └── tensorflow_model.ipynb ├── natural-language-processing ├── chinese_digging.ipynb ├── english_digging.ipynb ├── hash_trick.ipynb ├── hmm.ipynb ├── lda.ipynb ├── nmf.ipynb ├── tf-idf.ipynb └── word2vec.ipynb ├── readme.md └── reinforcement-learning ├── a3c.py ├── actor_critic.py ├── ddpg.py ├── ddqn.py ├── ddqn_prioritised_replay.py ├── dqn.py ├── duel_dqn.py ├── introduction.py ├── nature_dqn.py ├── policy_gradient.py ├── q_learning_windy_world.py └── sarsa_windy_world.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 刘建平(Pinard Liu) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /assert/invoice.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/assert/invoice.bmp -------------------------------------------------------------------------------- /assert/invoice_ali.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/assert/invoice_ali.bmp -------------------------------------------------------------------------------- /classic-machine-learning/fp_tree_prefixspan.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "用Spark学习FP Tree算法和PrefixSpan算法 https://www.cnblogs.com/pinard/p/6340162.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "import os\n", 23 | "import sys\n", 24 | "\n", 25 | "#下面这些目录都是你自己机器的Spark安装目录和Java安装目录\n", 26 | "os.environ['SPARK_HOME'] = \"C:/Tools/spark-2.2.0-bin-hadoop2.6/\"\n", 27 | "os.environ['PYSPARK_PYTHON'] = \"C:/Users/tata/AppData/Local/Programs/Python/Python36/python.exe\"\n", 28 | "\n", 29 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/bin\")\n", 30 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/python\")\n", 31 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/python/pyspark\")\n", 32 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/python/lib\")\n", 33 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/python/lib/pyspark.zip\")\n", 34 | "sys.path.append(\"C:/Tools/spark-2.2.0-bin-hadoop2.6/python/lib/py4j-0.10.4-src.zip\")\n", 35 | "sys.path.append(\"C:/Program Files/Java/jdk1.8.0_171\")\n", 36 | "\n", 37 | "from pyspark import SparkContext\n", 38 | "from pyspark import SparkConf\n", 39 | "\n", 40 | "\n", 41 | "sc = SparkContext(\"local\",\"testing\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "print (sc)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 3, 64 | "metadata": { 65 | "scrolled": false 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "from pyspark.mllib.fpm import FPGrowth\n", 70 | "data = [[\"A\", \"B\", \"C\", \"E\", \"F\",\"O\"], [\"A\", \"C\", \"G\"], [\"E\",\"I\"], [\"A\", \"C\",\"D\",\"E\",\"G\"], [\"A\", \"C\", \"E\",\"G\",\"L\"],\n", 71 | " [\"E\",\"J\"],[\"A\",\"B\",\"C\",\"E\",\"F\",\"P\"],[\"A\",\"C\",\"D\"],[\"A\",\"C\",\"E\",\"G\",\"M\"],[\"A\",\"C\",\"E\",\"G\",\"N\"]]\n", 72 | "rdd = sc.parallelize(data, 2)\n", 73 | "#支持度阈值为20%\n", 74 | "model = FPGrowth.train(rdd, 0.2, 2)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "[FreqItemset(items=['A'], freq=8),\n", 86 | " FreqItemset(items=['B'], freq=2),\n", 87 | " FreqItemset(items=['B', 'A'], freq=2),\n", 88 | " FreqItemset(items=['B', 'C'], freq=2),\n", 89 | " FreqItemset(items=['B', 'C', 'A'], freq=2),\n", 90 | " FreqItemset(items=['B', 'E'], freq=2),\n", 91 | " FreqItemset(items=['B', 'E', 'A'], freq=2),\n", 92 | " FreqItemset(items=['B', 'E', 'C'], freq=2),\n", 93 | " FreqItemset(items=['B', 'E', 'C', 'A'], freq=2),\n", 94 | " FreqItemset(items=['C'], freq=8),\n", 95 | " FreqItemset(items=['C', 'A'], freq=8),\n", 96 | " FreqItemset(items=['D'], freq=2),\n", 97 | " FreqItemset(items=['D', 'A'], freq=2),\n", 98 | " FreqItemset(items=['D', 'C'], freq=2),\n", 99 | " FreqItemset(items=['D', 'C', 'A'], freq=2),\n", 100 | " FreqItemset(items=['E'], freq=8),\n", 101 | " FreqItemset(items=['E', 'A'], freq=6),\n", 102 | " FreqItemset(items=['E', 'C'], freq=6),\n", 103 | " FreqItemset(items=['E', 'C', 'A'], freq=6),\n", 104 | " FreqItemset(items=['F'], freq=2),\n", 105 | " FreqItemset(items=['F', 'A'], freq=2),\n", 106 | " FreqItemset(items=['F', 'B'], freq=2),\n", 107 | " FreqItemset(items=['F', 'B', 'A'], freq=2),\n", 108 | " FreqItemset(items=['F', 'B', 'C'], freq=2),\n", 109 | " FreqItemset(items=['F', 'B', 'C', 'A'], freq=2),\n", 110 | " FreqItemset(items=['F', 'B', 'E'], freq=2),\n", 111 | " FreqItemset(items=['F', 'B', 'E', 'A'], freq=2),\n", 112 | " FreqItemset(items=['F', 'B', 'E', 'C'], freq=2),\n", 113 | " FreqItemset(items=['F', 'B', 'E', 'C', 'A'], freq=2),\n", 114 | " FreqItemset(items=['F', 'C'], freq=2),\n", 115 | " FreqItemset(items=['F', 'C', 'A'], freq=2),\n", 116 | " FreqItemset(items=['F', 'E'], freq=2),\n", 117 | " FreqItemset(items=['F', 'E', 'A'], freq=2),\n", 118 | " FreqItemset(items=['F', 'E', 'C'], freq=2),\n", 119 | " FreqItemset(items=['F', 'E', 'C', 'A'], freq=2),\n", 120 | " FreqItemset(items=['G'], freq=5),\n", 121 | " FreqItemset(items=['G', 'A'], freq=5),\n", 122 | " FreqItemset(items=['G', 'C'], freq=5),\n", 123 | " FreqItemset(items=['G', 'C', 'A'], freq=5),\n", 124 | " FreqItemset(items=['G', 'E'], freq=4),\n", 125 | " FreqItemset(items=['G', 'E', 'A'], freq=4),\n", 126 | " FreqItemset(items=['G', 'E', 'C'], freq=4),\n", 127 | " FreqItemset(items=['G', 'E', 'C', 'A'], freq=4)]" 128 | ] 129 | }, 130 | "execution_count": 4, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "sorted(model.freqItemsets().collect())" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "from pyspark.mllib.fpm import PrefixSpan\n", 146 | "data = [\n", 147 | " [['a'],[\"a\", \"b\", \"c\"], [\"a\",\"c\"],[\"d\"],[\"c\", \"f\"]],\n", 148 | " [[\"a\",\"d\"], [\"c\"],[\"b\", \"c\"], [\"a\", \"e\"]],\n", 149 | " [[\"e\", \"f\"], [\"a\", \"b\"], [\"d\",\"f\"],[\"c\"],[\"b\"]],\n", 150 | " [[\"e\"], [\"g\"],[\"a\", \"f\"],[\"c\"],[\"b\"],[\"c\"]]\n", 151 | " ]\n", 152 | "rdd = sc.parallelize(data, 2)\n", 153 | "model = PrefixSpan.train(rdd, 0.5,4)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 6, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "[FreqSequence(sequence=[['a']], freq=4),\n", 165 | " FreqSequence(sequence=[['a'], ['a']], freq=2),\n", 166 | " FreqSequence(sequence=[['a'], ['b']], freq=4),\n", 167 | " FreqSequence(sequence=[['a'], ['b'], ['a']], freq=2),\n", 168 | " FreqSequence(sequence=[['a'], ['b'], ['c']], freq=2),\n", 169 | " FreqSequence(sequence=[['a'], ['b', 'c']], freq=2),\n", 170 | " FreqSequence(sequence=[['a'], ['b', 'c'], ['a']], freq=2),\n", 171 | " FreqSequence(sequence=[['a'], ['c']], freq=4),\n", 172 | " FreqSequence(sequence=[['a'], ['c'], ['a']], freq=2),\n", 173 | " FreqSequence(sequence=[['a'], ['c'], ['b']], freq=3),\n", 174 | " FreqSequence(sequence=[['a'], ['c'], ['c']], freq=3),\n", 175 | " FreqSequence(sequence=[['a'], ['d']], freq=2),\n", 176 | " FreqSequence(sequence=[['a'], ['d'], ['c']], freq=2),\n", 177 | " FreqSequence(sequence=[['a'], ['f']], freq=2),\n", 178 | " FreqSequence(sequence=[['b']], freq=4),\n", 179 | " FreqSequence(sequence=[['b'], ['a']], freq=2),\n", 180 | " FreqSequence(sequence=[['b'], ['c']], freq=3),\n", 181 | " FreqSequence(sequence=[['b'], ['d']], freq=2),\n", 182 | " FreqSequence(sequence=[['b'], ['d'], ['c']], freq=2),\n", 183 | " FreqSequence(sequence=[['b'], ['f']], freq=2),\n", 184 | " FreqSequence(sequence=[['b', 'a']], freq=2),\n", 185 | " FreqSequence(sequence=[['b', 'a'], ['c']], freq=2),\n", 186 | " FreqSequence(sequence=[['b', 'a'], ['d']], freq=2),\n", 187 | " FreqSequence(sequence=[['b', 'a'], ['d'], ['c']], freq=2),\n", 188 | " FreqSequence(sequence=[['b', 'a'], ['f']], freq=2),\n", 189 | " FreqSequence(sequence=[['b', 'c']], freq=2),\n", 190 | " FreqSequence(sequence=[['b', 'c'], ['a']], freq=2),\n", 191 | " FreqSequence(sequence=[['c']], freq=4),\n", 192 | " FreqSequence(sequence=[['c'], ['a']], freq=2),\n", 193 | " FreqSequence(sequence=[['c'], ['b']], freq=3),\n", 194 | " FreqSequence(sequence=[['c'], ['c']], freq=3),\n", 195 | " FreqSequence(sequence=[['d']], freq=3),\n", 196 | " FreqSequence(sequence=[['d'], ['b']], freq=2),\n", 197 | " FreqSequence(sequence=[['d'], ['c']], freq=3),\n", 198 | " FreqSequence(sequence=[['d'], ['c'], ['b']], freq=2),\n", 199 | " FreqSequence(sequence=[['e']], freq=3),\n", 200 | " FreqSequence(sequence=[['e'], ['a']], freq=2),\n", 201 | " FreqSequence(sequence=[['e'], ['a'], ['b']], freq=2),\n", 202 | " FreqSequence(sequence=[['e'], ['a'], ['c']], freq=2),\n", 203 | " FreqSequence(sequence=[['e'], ['a'], ['c'], ['b']], freq=2),\n", 204 | " FreqSequence(sequence=[['e'], ['b']], freq=2),\n", 205 | " FreqSequence(sequence=[['e'], ['b'], ['c']], freq=2),\n", 206 | " FreqSequence(sequence=[['e'], ['c']], freq=2),\n", 207 | " FreqSequence(sequence=[['e'], ['c'], ['b']], freq=2),\n", 208 | " FreqSequence(sequence=[['e'], ['f']], freq=2),\n", 209 | " FreqSequence(sequence=[['e'], ['f'], ['b']], freq=2),\n", 210 | " FreqSequence(sequence=[['e'], ['f'], ['c']], freq=2),\n", 211 | " FreqSequence(sequence=[['e'], ['f'], ['c'], ['b']], freq=2),\n", 212 | " FreqSequence(sequence=[['f']], freq=3),\n", 213 | " FreqSequence(sequence=[['f'], ['b']], freq=2),\n", 214 | " FreqSequence(sequence=[['f'], ['b'], ['c']], freq=2),\n", 215 | " FreqSequence(sequence=[['f'], ['c']], freq=2),\n", 216 | " FreqSequence(sequence=[['f'], ['c'], ['b']], freq=2)]" 217 | ] 218 | }, 219 | "execution_count": 6, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "sorted(model.freqSequences().collect())" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [] 234 | } 235 | ], 236 | "metadata": { 237 | "kernelspec": { 238 | "display_name": "Python 3", 239 | "language": "python", 240 | "name": "python3" 241 | }, 242 | "language_info": { 243 | "codemirror_mode": { 244 | "name": "ipython", 245 | "version": 3 246 | }, 247 | "file_extension": ".py", 248 | "mimetype": "text/x-python", 249 | "name": "python", 250 | "nbconvert_exporter": "python", 251 | "pygments_lexer": "ipython3", 252 | "version": "3.6.4" 253 | } 254 | }, 255 | "nbformat": 4, 256 | "nbformat_minor": 2 257 | } 258 | -------------------------------------------------------------------------------- /classic-machine-learning/native_bayes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C)\n", 8 | "2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 9 | "\n", 10 | "https://www.cnblogs.com/pinard\n", 11 | "\n", 12 | "Permission given to modify the code as long as you keep this declaration at the top\n", 13 | "\n", 14 | "scikit-learn 朴素贝叶斯类库使用小结 https://www.cnblogs.com/pinard/p/6074222.html" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "==Predict result by predict==\n", 27 | "[1]\n", 28 | "==Predict result by predict_proba==\n", 29 | "[[ 9.99999949e-01 5.05653254e-08]]\n", 30 | "==Predict result by predict_log_proba==\n", 31 | "[[ -5.05653266e-08 -1.67999998e+01]]\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "import numpy as np\n", 37 | "X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])\n", 38 | "Y = np.array([1, 1, 1, 2, 2, 2])\n", 39 | "from sklearn.naive_bayes import GaussianNB\n", 40 | "clf = GaussianNB()\n", 41 | "#拟合数据\n", 42 | "clf.fit(X, Y)\n", 43 | "print \"==Predict result by predict==\"\n", 44 | "print(clf.predict([[-0.8, -1]]))\n", 45 | "print \"==Predict result by predict_proba==\"\n", 46 | "print(clf.predict_proba([[-0.8, -1]]))\n", 47 | "print \"==Predict result by predict_log_proba==\"\n", 48 | "print(clf.predict_log_proba([[-0.8, -1]]))" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.6.5" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 1 82 | } 83 | -------------------------------------------------------------------------------- /classic-machine-learning/ridge_regression_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C)\n", 8 | "2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 9 | "\n", 10 | "https://www.cnblogs.com/pinard\n", 11 | "\n", 12 | "Permission given to modify the code as long as you keep this declaration at the top\n", 13 | "\n", 14 | "用scikit-learn和pandas学习Ridge回归 https://www.cnblogs.com/pinard/p/6023000.html" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import matplotlib.pyplot as plt\n", 26 | "%matplotlib inline\n", 27 | "import numpy as np\n", 28 | "import pandas as pd\n", 29 | "from sklearn import datasets, linear_model" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "# read_csv里面的参数是csv在你电脑上的路径,此处csv文件放在notebook运行目录下面的CCPP目录里\n", 41 | "data = pd.read_csv('.\\CCPP\\ccpp.csv')" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stderr", 51 | "output_type": "stream", 52 | "text": [ 53 | "C:\\Python27\\lib\\site-packages\\sklearn\\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n", 54 | " \"This module will be removed in 0.20.\", DeprecationWarning)\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "X = data[['AT', 'V', 'AP', 'RH']]\n", 60 | "y = data[['PE']]\n", 61 | "from sklearn.cross_validation import train_test_split\n", 62 | "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 6, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "0.927643364697\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "from sklearn.linear_model import Ridge\n", 80 | "ridge = Ridge(alpha=1)\n", 81 | "ridge.fit(X_train, y_train)\n", 82 | "print ridge.score(X_train, y_train)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 27, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "[[-1.97373209 -0.2323016 0.06935852 -0.15806479]]\n", 95 | "[ 447.05552892]\n", 96 | "MSE: 20.0804091931\n", 97 | "RMSE: 4.4811169582\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "print ridge.coef_\n", 103 | "print ridge.intercept_\n", 104 | "y_pred = ridge.predict(X_test)\n", 105 | "from sklearn import metrics\n", 106 | "# 用scikit-learn计算MSE\n", 107 | "print \"MSE:\",metrics.mean_squared_error(y_test, y_pred)\n", 108 | "# 用scikit-learn计算RMSE\n", 109 | "print \"RMSE:\",np.sqrt(metrics.mean_squared_error(y_test, y_pred))" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 28, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "7.0" 121 | ] 122 | }, 123 | "execution_count": 28, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "from sklearn.linear_model import RidgeCV\n", 130 | "ridgecv = RidgeCV(alphas=[0.01, 0.1, 0.5, 1, 3, 5, 7, 10, 20, 100])\n", 131 | "ridgecv.fit(X_train, y_train)\n", 132 | "ridgecv.alpha_ " 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 29, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "MSE: 20.0804574476\n", 145 | "RMSE: 4.48112234241\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "y_pred = ridgecv.predict(X_test)\n", 151 | "from sklearn import metrics\n", 152 | "# 用scikit-learn计算MSE\n", 153 | "print \"MSE:\",metrics.mean_squared_error(y_test, y_pred)\n", 154 | "# 用scikit-learn计算RMSE\n", 155 | "print \"RMSE:\",np.sqrt(metrics.mean_squared_error(y_test, y_pred))" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": { 162 | "collapsed": true 163 | }, 164 | "outputs": [], 165 | "source": [] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Python 3", 171 | "language": "python", 172 | "name": "python3" 173 | }, 174 | "language_info": { 175 | "codemirror_mode": { 176 | "name": "ipython", 177 | "version": 3 178 | }, 179 | "file_extension": ".py", 180 | "mimetype": "text/x-python", 181 | "name": "python", 182 | "nbconvert_exporter": "python", 183 | "pygments_lexer": "ipython3", 184 | "version": "3.6.5" 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 1 189 | } 190 | -------------------------------------------------------------------------------- /classic-machine-learning/spectral_cluster.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "用scikit-learn学习谱聚类 https://www.cnblogs.com/pinard/p/6235920.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "import numpy as np\n", 23 | "from sklearn import datasets\n", 24 | "X, y = datasets.make_blobs(n_samples=500, n_features=6, centers=5, cluster_std=[0.4, 0.3, 0.4, 0.3, 0.4], random_state=11)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 3, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "Calinski-Harabasz Score 14910.411255265064\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "from sklearn.cluster import SpectralClustering\n", 42 | "y_pred = SpectralClustering().fit_predict(X)\n", 43 | "from sklearn import metrics\n", 44 | "print (\"Calinski-Harabasz Score\", metrics.calinski_harabaz_score(X, y_pred)) " 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 5, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 3 score: 1979.7709609161868\n", 57 | "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 4 score: 3154.0184121901607\n", 58 | "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 5 score: 23410.638949991386\n", 59 | "Calinski-Harabasz Score with gamma= 0.01 n_clusters= 6 score: 19296.861797427086\n", 60 | "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 3 score: 1979.7709609161868\n", 61 | "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 4 score: 3154.0184121901607\n", 62 | "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 5 score: 23410.638949991386\n", 63 | "Calinski-Harabasz Score with gamma= 0.1 n_clusters= 6 score: 19427.961894359105\n", 64 | "Calinski-Harabasz Score with gamma= 1 n_clusters= 3 score: 260.64607765944106\n", 65 | "Calinski-Harabasz Score with gamma= 1 n_clusters= 4 score: 2274.7475744437675\n", 66 | "Calinski-Harabasz Score with gamma= 1 n_clusters= 5 score: 23410.638949991386\n", 67 | "Calinski-Harabasz Score with gamma= 1 n_clusters= 6 score: 1575.424687869317\n" 68 | ] 69 | }, 70 | { 71 | "name": "stderr", 72 | "output_type": "stream", 73 | "text": [ 74 | "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\manifold\\spectral_embedding_.py:234: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.\n", 75 | " warnings.warn(\"Graph is not fully connected, spectral embedding\"\n" 76 | ] 77 | }, 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "Calinski-Harabasz Score with gamma= 10 n_clusters= 3 score: 26.939459595237867\n", 83 | "Calinski-Harabasz Score with gamma= 10 n_clusters= 4 score: 37.011003925097825\n", 84 | "Calinski-Harabasz Score with gamma= 10 n_clusters= 5 score: 31.922080624500378\n", 85 | "Calinski-Harabasz Score with gamma= 10 n_clusters= 6 score: 28.434790532434917\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "for index, gamma in enumerate((0.01,0.1,1,10)):\n", 91 | " for index, k in enumerate((3,4,5,6)):\n", 92 | " y_pred = SpectralClustering(n_clusters=k, gamma=gamma).fit_predict(X)\n", 93 | " print (\"Calinski-Harabasz Score with gamma=\", gamma, \"n_clusters=\", k,\"score:\", metrics.calinski_harabaz_score(X, y_pred)) " 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 7, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Calinski-Harabasz Score 14950.49397167252\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "y_pred = SpectralClustering(gamma=0.1).fit_predict(X)\n", 111 | "print (\"Calinski-Harabasz Score\", metrics.calinski_harabaz_score(X, y_pred))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "kernelspec": { 124 | "display_name": "Python 3", 125 | "language": "python", 126 | "name": "python3" 127 | }, 128 | "language_info": { 129 | "codemirror_mode": { 130 | "name": "ipython", 131 | "version": 3 132 | }, 133 | "file_extension": ".py", 134 | "mimetype": "text/x-python", 135 | "name": "python", 136 | "nbconvert_exporter": "python", 137 | "pygments_lexer": "ipython3", 138 | "version": "3.6.5" 139 | } 140 | }, 141 | "nbformat": 4, 142 | "nbformat_minor": 2 143 | } 144 | -------------------------------------------------------------------------------- /data/CCPP.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/data/CCPP.zip -------------------------------------------------------------------------------- /data/nlp_test0.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/data/nlp_test0.txt -------------------------------------------------------------------------------- /data/nlp_test2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/data/nlp_test2.txt -------------------------------------------------------------------------------- /data/train_modified.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/data/train_modified.zip -------------------------------------------------------------------------------- /ensemble-learning/random_forest_classifier.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "scikit-learn随机森林调参小结 https://www.cnblogs.com/pinard/p/6160412.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stderr", 23 | "output_type": "stream", 24 | "text": [ 25 | "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n", 26 | " \"This module will be removed in 0.20.\", DeprecationWarning)\n", 27 | "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\grid_search.py:42: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.\n", 28 | " DeprecationWarning)\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "import pandas as pd\n", 34 | "import numpy as np\n", 35 | "from sklearn.ensemble import RandomForestClassifier\n", 36 | "from sklearn.grid_search import GridSearchCV\n", 37 | "from sklearn import cross_validation, metrics\n", 38 | "\n", 39 | "import matplotlib.pylab as plt\n", 40 | "%matplotlib inline" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "0 19680\n", 52 | "1 320\n", 53 | "Name: Disbursed, dtype: int64" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "train = pd.read_csv('train_modified.csv')\n", 63 | "target='Disbursed' # Disbursed的值就是二元分类的输出\n", 64 | "IDcol = 'ID'\n", 65 | "train['Disbursed'].value_counts() " 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "x_columns = [x for x in train.columns if x not in [target, IDcol]]\n", 75 | "X = train[x_columns]\n", 76 | "y = train['Disbursed']" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 6, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "0.98005\n", 89 | "AUC Score (Train): 0.999833\n" 90 | ] 91 | }, 92 | { 93 | "name": "stderr", 94 | "output_type": "stream", 95 | "text": [ 96 | "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\ensemble\\forest.py:453: UserWarning: Some inputs do not have OOB scores. This probably means too few trees were used to compute any reliable oob estimates.\n", 97 | " warn(\"Some inputs do not have OOB scores. \"\n", 98 | "d:\\users\\pinard.liu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\ensemble\\forest.py:458: RuntimeWarning: invalid value encountered in true_divide\n", 99 | " predictions[k].sum(axis=1)[:, np.newaxis])\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "rf0 = RandomForestClassifier(oob_score=True, random_state=10)\n", 105 | "rf0.fit(X,y)\n", 106 | "print (rf0.oob_score_)\n", 107 | "y_predprob = rf0.predict_proba(X)[:,1]\n", 108 | "print (\"AUC Score (Train): %f\" % metrics.roc_auc_score(y, y_predprob))" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 8, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "([mean: 0.80681, std: 0.02236, params: {'n_estimators': 10},\n", 120 | " mean: 0.81600, std: 0.03275, params: {'n_estimators': 20},\n", 121 | " mean: 0.81818, std: 0.03136, params: {'n_estimators': 30},\n", 122 | " mean: 0.81838, std: 0.03118, params: {'n_estimators': 40},\n", 123 | " mean: 0.82034, std: 0.03001, params: {'n_estimators': 50},\n", 124 | " mean: 0.82113, std: 0.02966, params: {'n_estimators': 60},\n", 125 | " mean: 0.81992, std: 0.02836, params: {'n_estimators': 70}],\n", 126 | " {'n_estimators': 60},\n", 127 | " 0.8211334476626017)" 128 | ] 129 | }, 130 | "execution_count": 8, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "param_test1 = {'n_estimators':[10,20,30,40,50,60,70]}\n", 137 | "gsearch1 = GridSearchCV(estimator = RandomForestClassifier(min_samples_split=100,\n", 138 | " min_samples_leaf=20,max_depth=8,max_features='sqrt' ,random_state=10), \n", 139 | " param_grid = param_test1, scoring='roc_auc',cv=5)\n", 140 | "gsearch1.fit(X,y)\n", 141 | "gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 11, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "([mean: 0.79379, std: 0.02347, params: {'max_depth': 3, 'min_samples_split': 50},\n", 153 | " mean: 0.79339, std: 0.02410, params: {'max_depth': 3, 'min_samples_split': 70},\n", 154 | " mean: 0.79350, std: 0.02462, params: {'max_depth': 3, 'min_samples_split': 90},\n", 155 | " mean: 0.79367, std: 0.02493, params: {'max_depth': 3, 'min_samples_split': 110},\n", 156 | " mean: 0.79387, std: 0.02521, params: {'max_depth': 3, 'min_samples_split': 130},\n", 157 | " mean: 0.79373, std: 0.02524, params: {'max_depth': 3, 'min_samples_split': 150},\n", 158 | " mean: 0.79378, std: 0.02532, params: {'max_depth': 3, 'min_samples_split': 170},\n", 159 | " mean: 0.79349, std: 0.02542, params: {'max_depth': 3, 'min_samples_split': 190},\n", 160 | " mean: 0.80960, std: 0.02602, params: {'max_depth': 5, 'min_samples_split': 50},\n", 161 | " mean: 0.80920, std: 0.02629, params: {'max_depth': 5, 'min_samples_split': 70},\n", 162 | " mean: 0.80888, std: 0.02522, params: {'max_depth': 5, 'min_samples_split': 90},\n", 163 | " mean: 0.80923, std: 0.02777, params: {'max_depth': 5, 'min_samples_split': 110},\n", 164 | " mean: 0.80823, std: 0.02634, params: {'max_depth': 5, 'min_samples_split': 130},\n", 165 | " mean: 0.80801, std: 0.02637, params: {'max_depth': 5, 'min_samples_split': 150},\n", 166 | " mean: 0.80792, std: 0.02685, params: {'max_depth': 5, 'min_samples_split': 170},\n", 167 | " mean: 0.80771, std: 0.02587, params: {'max_depth': 5, 'min_samples_split': 190},\n", 168 | " mean: 0.81688, std: 0.02996, params: {'max_depth': 7, 'min_samples_split': 50},\n", 169 | " mean: 0.81872, std: 0.02584, params: {'max_depth': 7, 'min_samples_split': 70},\n", 170 | " mean: 0.81501, std: 0.02857, params: {'max_depth': 7, 'min_samples_split': 90},\n", 171 | " mean: 0.81476, std: 0.02552, params: {'max_depth': 7, 'min_samples_split': 110},\n", 172 | " mean: 0.81557, std: 0.02791, params: {'max_depth': 7, 'min_samples_split': 130},\n", 173 | " mean: 0.81459, std: 0.02905, params: {'max_depth': 7, 'min_samples_split': 150},\n", 174 | " mean: 0.81601, std: 0.02808, params: {'max_depth': 7, 'min_samples_split': 170},\n", 175 | " mean: 0.81704, std: 0.02757, params: {'max_depth': 7, 'min_samples_split': 190},\n", 176 | " mean: 0.82090, std: 0.02665, params: {'max_depth': 9, 'min_samples_split': 50},\n", 177 | " mean: 0.81908, std: 0.02527, params: {'max_depth': 9, 'min_samples_split': 70},\n", 178 | " mean: 0.82036, std: 0.02422, params: {'max_depth': 9, 'min_samples_split': 90},\n", 179 | " mean: 0.81889, std: 0.02927, params: {'max_depth': 9, 'min_samples_split': 110},\n", 180 | " mean: 0.81991, std: 0.02868, params: {'max_depth': 9, 'min_samples_split': 130},\n", 181 | " mean: 0.81788, std: 0.02436, params: {'max_depth': 9, 'min_samples_split': 150},\n", 182 | " mean: 0.81898, std: 0.02588, params: {'max_depth': 9, 'min_samples_split': 170},\n", 183 | " mean: 0.81746, std: 0.02716, params: {'max_depth': 9, 'min_samples_split': 190},\n", 184 | " mean: 0.82395, std: 0.02454, params: {'max_depth': 11, 'min_samples_split': 50},\n", 185 | " mean: 0.82380, std: 0.02258, params: {'max_depth': 11, 'min_samples_split': 70},\n", 186 | " mean: 0.81953, std: 0.02552, params: {'max_depth': 11, 'min_samples_split': 90},\n", 187 | " mean: 0.82254, std: 0.02366, params: {'max_depth': 11, 'min_samples_split': 110},\n", 188 | " mean: 0.81950, std: 0.02768, params: {'max_depth': 11, 'min_samples_split': 130},\n", 189 | " mean: 0.81887, std: 0.02636, params: {'max_depth': 11, 'min_samples_split': 150},\n", 190 | " mean: 0.81910, std: 0.02734, params: {'max_depth': 11, 'min_samples_split': 170},\n", 191 | " mean: 0.81564, std: 0.02622, params: {'max_depth': 11, 'min_samples_split': 190},\n", 192 | " mean: 0.82291, std: 0.02092, params: {'max_depth': 13, 'min_samples_split': 50},\n", 193 | " mean: 0.82177, std: 0.02513, params: {'max_depth': 13, 'min_samples_split': 70},\n", 194 | " mean: 0.82415, std: 0.02480, params: {'max_depth': 13, 'min_samples_split': 90},\n", 195 | " mean: 0.82420, std: 0.02417, params: {'max_depth': 13, 'min_samples_split': 110},\n", 196 | " mean: 0.82209, std: 0.02481, params: {'max_depth': 13, 'min_samples_split': 130},\n", 197 | " mean: 0.81852, std: 0.02227, params: {'max_depth': 13, 'min_samples_split': 150},\n", 198 | " mean: 0.81955, std: 0.02885, params: {'max_depth': 13, 'min_samples_split': 170},\n", 199 | " mean: 0.82092, std: 0.02600, params: {'max_depth': 13, 'min_samples_split': 190}],\n", 200 | " {'max_depth': 13, 'min_samples_split': 110},\n", 201 | " 0.8242016800050813)" 202 | ] 203 | }, 204 | "execution_count": 11, 205 | "metadata": {}, 206 | "output_type": "execute_result" 207 | } 208 | ], 209 | "source": [ 210 | "param_test2 = {'max_depth':[3,5,7,9,11,13], 'min_samples_split':[50,70,90,110,130,150,170,190]}\n", 211 | "gsearch2 = GridSearchCV(estimator = RandomForestClassifier(n_estimators= 60, \n", 212 | " min_samples_leaf=20,max_features='sqrt' ,oob_score=True, random_state=10),\n", 213 | " param_grid = param_test2, scoring='roc_auc',iid=False, cv=5)\n", 214 | "gsearch2.fit(X,y)\n", 215 | "gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 13, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "0.984\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "rf1 = RandomForestClassifier(n_estimators= 60, max_depth=13, min_samples_split=110,\n", 233 | " min_samples_leaf=20,max_features='sqrt' ,oob_score=True, random_state=10)\n", 234 | "rf1.fit(X,y)\n", 235 | "print (rf1.oob_score_)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 14, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "([mean: 0.82093, std: 0.02287, params: {'min_samples_leaf': 10, 'min_samples_split': 80},\n", 247 | " mean: 0.81913, std: 0.02141, params: {'min_samples_leaf': 10, 'min_samples_split': 100},\n", 248 | " mean: 0.82048, std: 0.02328, params: {'min_samples_leaf': 10, 'min_samples_split': 120},\n", 249 | " mean: 0.81798, std: 0.02099, params: {'min_samples_leaf': 10, 'min_samples_split': 140},\n", 250 | " mean: 0.82094, std: 0.02535, params: {'min_samples_leaf': 20, 'min_samples_split': 80},\n", 251 | " mean: 0.82097, std: 0.02327, params: {'min_samples_leaf': 20, 'min_samples_split': 100},\n", 252 | " mean: 0.82487, std: 0.02110, params: {'min_samples_leaf': 20, 'min_samples_split': 120},\n", 253 | " mean: 0.82169, std: 0.02406, params: {'min_samples_leaf': 20, 'min_samples_split': 140},\n", 254 | " mean: 0.82352, std: 0.02271, params: {'min_samples_leaf': 30, 'min_samples_split': 80},\n", 255 | " mean: 0.82164, std: 0.02381, params: {'min_samples_leaf': 30, 'min_samples_split': 100},\n", 256 | " mean: 0.82070, std: 0.02528, params: {'min_samples_leaf': 30, 'min_samples_split': 120},\n", 257 | " mean: 0.82141, std: 0.02508, params: {'min_samples_leaf': 30, 'min_samples_split': 140},\n", 258 | " mean: 0.82278, std: 0.02294, params: {'min_samples_leaf': 40, 'min_samples_split': 80},\n", 259 | " mean: 0.82141, std: 0.02547, params: {'min_samples_leaf': 40, 'min_samples_split': 100},\n", 260 | " mean: 0.82043, std: 0.02724, params: {'min_samples_leaf': 40, 'min_samples_split': 120},\n", 261 | " mean: 0.82162, std: 0.02348, params: {'min_samples_leaf': 40, 'min_samples_split': 140},\n", 262 | " mean: 0.82225, std: 0.02431, params: {'min_samples_leaf': 50, 'min_samples_split': 80},\n", 263 | " mean: 0.82225, std: 0.02431, params: {'min_samples_leaf': 50, 'min_samples_split': 100},\n", 264 | " mean: 0.81890, std: 0.02458, params: {'min_samples_leaf': 50, 'min_samples_split': 120},\n", 265 | " mean: 0.81917, std: 0.02528, params: {'min_samples_leaf': 50, 'min_samples_split': 140}],\n", 266 | " {'min_samples_leaf': 20, 'min_samples_split': 120},\n", 267 | " 0.8248650279471544)" 268 | ] 269 | }, 270 | "execution_count": 14, 271 | "metadata": {}, 272 | "output_type": "execute_result" 273 | } 274 | ], 275 | "source": [ 276 | "param_test3 = {'min_samples_split':[80,100,120,140], 'min_samples_leaf':[10,20,30,40,50]}\n", 277 | "gsearch3 = GridSearchCV(estimator = RandomForestClassifier(n_estimators= 60, max_depth=13,\n", 278 | " max_features='sqrt' ,oob_score=True, random_state=10),\n", 279 | " param_grid = param_test3, scoring='roc_auc',iid=False, cv=5)\n", 280 | "gsearch3.fit(X,y)\n", 281 | "gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 16, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "data": { 291 | "text/plain": [ 292 | "([mean: 0.81981, std: 0.02586, params: {'max_features': 3},\n", 293 | " mean: 0.81639, std: 0.02533, params: {'max_features': 5},\n", 294 | " mean: 0.82487, std: 0.02110, params: {'max_features': 7},\n", 295 | " mean: 0.81704, std: 0.02209, params: {'max_features': 9}],\n", 296 | " {'max_features': 7},\n", 297 | " 0.8248650279471544)" 298 | ] 299 | }, 300 | "execution_count": 16, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "param_test4 = {'max_features':[3,5,7,9]}\n", 307 | "gsearch4 = GridSearchCV(estimator = RandomForestClassifier(n_estimators= 60, max_depth=13, min_samples_split=120,\n", 308 | " min_samples_leaf=20 ,oob_score=True, random_state=10),\n", 309 | " param_grid = param_test4, scoring='roc_auc',iid=False, cv=5)\n", 310 | "gsearch4.fit(X,y)\n", 311 | "gsearch4.grid_scores_, gsearch4.best_params_, gsearch4.best_score_" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 18, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "0.984\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "rf2 = RandomForestClassifier(n_estimators= 60, max_depth=13, min_samples_split=120,\n", 329 | " min_samples_leaf=20,max_features=7 ,oob_score=True, random_state=10)\n", 330 | "rf2.fit(X,y)\n", 331 | "print (rf2.oob_score_)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [] 340 | } 341 | ], 342 | "metadata": { 343 | "kernelspec": { 344 | "display_name": "Python 3", 345 | "language": "python", 346 | "name": "python3" 347 | }, 348 | "language_info": { 349 | "codemirror_mode": { 350 | "name": "ipython", 351 | "version": 3 352 | }, 353 | "file_extension": ".py", 354 | "mimetype": "text/x-python", 355 | "name": "python", 356 | "nbconvert_exporter": "python", 357 | "pygments_lexer": "ipython3", 358 | "version": "3.6.5" 359 | } 360 | }, 361 | "nbformat": 4, 362 | "nbformat_minor": 2 363 | } 364 | -------------------------------------------------------------------------------- /ensemble-learning/xgboost-example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "import xgboost as xgb\n", 12 | "import matplotlib.pylab as plt\n", 13 | "%matplotlib inline\n", 14 | "\n", 15 | "from sklearn.model_selection import GridSearchCV\n", 16 | "from sklearn.model_selection import train_test_split" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "from sklearn.datasets.samples_generator import make_classification\n", 26 | "# X为样本特征,y为样本类别输出, 共10000个样本,每个样本20个特征,输出有2个类别,没有冗余特征,每个类别一个簇\n", 27 | "X, y = make_classification(n_samples=10000, n_features=20, n_redundant=0,\n", 28 | " n_clusters_per_class=1, n_classes=2, flip_y=0.1)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "(7500, 20)\n", 41 | "(7500,)\n", 42 | "(2500, 20)\n", 43 | "(2500,)\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)\n", 49 | "print (X_train.shape)\n", 50 | "print (y_train.shape)\n", 51 | "print (X_test.shape)\n", 52 | "print (y_test.shape)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "

XGBoost 使用原生API

" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "dtrain = xgb.DMatrix(X_train,y_train)\n", 69 | "dtest = xgb.DMatrix(X_test,y_test)\n", 70 | "param = {'max_depth':5, 'eta':0.5, 'verbosity':1, 'objective':'binary:logistic'}\n", 71 | "raw_model = xgb.train(param, dtrain, num_boost_round=20)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "0.9664\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "from sklearn.metrics import accuracy_score\n", 89 | "pred_train_raw = raw_model.predict(dtrain)\n", 90 | "for i in range(len(pred_train_raw)):\n", 91 | " if pred_train_raw[i] > 0.5:\n", 92 | " pred_train_raw[i]=1\n", 93 | " else:\n", 94 | " pred_train_raw[i]=0 \n", 95 | "print (accuracy_score(dtrain.get_label(), pred_train_raw))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "0.9408\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "pred_test_raw = raw_model.predict(dtest)\n", 113 | "for i in range(len(pred_test_raw)):\n", 114 | " if pred_test_raw[i] > 0.5:\n", 115 | " pred_test_raw[i]=1\n", 116 | " else:\n", 117 | " pred_test_raw[i]=0 \n", 118 | "print (accuracy_score(dtest.get_label(), pred_test_raw))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "

XGBoost 使用sklearn wrapper,仍然使用原始API的参数

" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 7, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "[0]\tvalidation_0-error:0.0636\n", 138 | "Will train until validation_0-error hasn't improved in 10 rounds.\n", 139 | "[1]\tvalidation_0-error:0.062\n", 140 | "[2]\tvalidation_0-error:0.0624\n", 141 | "[3]\tvalidation_0-error:0.062\n", 142 | "[4]\tvalidation_0-error:0.062\n", 143 | "[5]\tvalidation_0-error:0.062\n", 144 | "[6]\tvalidation_0-error:0.062\n", 145 | "[7]\tvalidation_0-error:0.062\n", 146 | "[8]\tvalidation_0-error:0.062\n", 147 | "[9]\tvalidation_0-error:0.0608\n", 148 | "[10]\tvalidation_0-error:0.0608\n", 149 | "[11]\tvalidation_0-error:0.0608\n", 150 | "[12]\tvalidation_0-error:0.0608\n", 151 | "[13]\tvalidation_0-error:0.0604\n", 152 | "[14]\tvalidation_0-error:0.0604\n", 153 | "[15]\tvalidation_0-error:0.0604\n", 154 | "[16]\tvalidation_0-error:0.0604\n", 155 | "[17]\tvalidation_0-error:0.0604\n", 156 | "[18]\tvalidation_0-error:0.0608\n", 157 | "[19]\tvalidation_0-error:0.0608\n", 158 | "[20]\tvalidation_0-error:0.06\n", 159 | "[21]\tvalidation_0-error:0.06\n", 160 | "[22]\tvalidation_0-error:0.06\n", 161 | "[23]\tvalidation_0-error:0.0592\n", 162 | "[24]\tvalidation_0-error:0.0588\n", 163 | "[25]\tvalidation_0-error:0.0588\n", 164 | "[26]\tvalidation_0-error:0.0584\n", 165 | "[27]\tvalidation_0-error:0.0584\n", 166 | "[28]\tvalidation_0-error:0.0584\n", 167 | "[29]\tvalidation_0-error:0.0584\n", 168 | "[30]\tvalidation_0-error:0.0584\n", 169 | "[31]\tvalidation_0-error:0.0584\n", 170 | "[32]\tvalidation_0-error:0.0584\n", 171 | "[33]\tvalidation_0-error:0.0584\n", 172 | "[34]\tvalidation_0-error:0.0584\n", 173 | "[35]\tvalidation_0-error:0.0584\n", 174 | "[36]\tvalidation_0-error:0.0584\n", 175 | "Stopping. Best iteration:\n", 176 | "[26]\tvalidation_0-error:0.0584\n", 177 | "\n" 178 | ] 179 | }, 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", 184 | " colsample_bynode=1, colsample_bytree=1, eta=0.5, gamma=0,\n", 185 | " learning_rate=0.1, max_delta_step=0, max_depth=5,\n", 186 | " min_child_weight=1, missing=None, n_estimators=100, n_jobs=1,\n", 187 | " nthread=None, objective='binary:logistic', random_state=0,\n", 188 | " reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,\n", 189 | " silent=None, subsample=1, verbosity=1)" 190 | ] 191 | }, 192 | "execution_count": 7, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "sklearn_model_raw = xgb.XGBClassifier(**param)\n", 199 | "sklearn_model_raw.fit(X_train, y_train, early_stopping_rounds=10, eval_metric=\"error\",\n", 200 | " eval_set=[(X_test, y_test)])" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "

XGBoost 使用sklearn wrapper,使用sklearn风格的参数(推荐)

" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 8, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "sklearn_model_new = xgb.XGBClassifier(max_depth=5,learning_rate= 0.5, verbosity=1, objective='binary:logistic',random_state=1)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 9, 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "[0]\tvalidation_0-error:0.0636\n", 229 | "Will train until validation_0-error hasn't improved in 10 rounds.\n", 230 | "[1]\tvalidation_0-error:0.0624\n", 231 | "[2]\tvalidation_0-error:0.0604\n", 232 | "[3]\tvalidation_0-error:0.0592\n", 233 | "[4]\tvalidation_0-error:0.0592\n", 234 | "[5]\tvalidation_0-error:0.0584\n", 235 | "[6]\tvalidation_0-error:0.058\n", 236 | "[7]\tvalidation_0-error:0.0588\n", 237 | "[8]\tvalidation_0-error:0.0588\n", 238 | "[9]\tvalidation_0-error:0.0588\n", 239 | "[10]\tvalidation_0-error:0.0588\n", 240 | "[11]\tvalidation_0-error:0.0588\n", 241 | "[12]\tvalidation_0-error:0.0588\n", 242 | "[13]\tvalidation_0-error:0.058\n", 243 | "[14]\tvalidation_0-error:0.0584\n", 244 | "[15]\tvalidation_0-error:0.0584\n", 245 | "[16]\tvalidation_0-error:0.0584\n", 246 | "Stopping. Best iteration:\n", 247 | "[6]\tvalidation_0-error:0.058\n", 248 | "\n" 249 | ] 250 | }, 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", 255 | " colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.5,\n", 256 | " max_delta_step=0, max_depth=5, min_child_weight=1, missing=None,\n", 257 | " n_estimators=100, n_jobs=1, nthread=None,\n", 258 | " objective='binary:logistic', random_state=1, reg_alpha=0,\n", 259 | " reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,\n", 260 | " subsample=1, verbosity=1)" 261 | ] 262 | }, 263 | "execution_count": 9, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "sklearn_model_new.fit(X_train, y_train, early_stopping_rounds=10, eval_metric=\"error\",\n", 270 | " eval_set=[(X_test, y_test)])" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "

使用sklearn网格搜索调参

\n", 278 | "\n", 279 | "一般固定步长,先调好框架参数n_estimators,再调弱学习器参数max_depth,min_child_weight,gamma等,接着调正则化相关参数subsample,colsample_byXXX, reg_alpha以及reg_lambda,最后固定前面调好的参数,来调步长learning_rate" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 10, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stderr", 289 | "output_type": "stream", 290 | "text": [ 291 | ] 292 | }, 293 | { 294 | "name": "stderr", 295 | "output_type": "stream", 296 | "text": [ 297 | ] 298 | }, 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "GridSearchCV(cv=None, error_score='raise',\n", 303 | " estimator=XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", 304 | " colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.5,\n", 305 | " max_delta_step=0, max_depth=5, min_child_weight=1, missing=None,\n", 306 | " n_estimators=100, n_jobs=1, nthread=None,\n", 307 | " objective='binary:logistic', random_state=1, reg_alpha=0,\n", 308 | " reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,\n", 309 | " subsample=1, verbosity=1),\n", 310 | " fit_params=None, iid=True, n_jobs=1,\n", 311 | " param_grid={'max_depth': [4, 5, 6], 'n_estimators': [5, 10, 20]},\n", 312 | " pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n", 313 | " scoring=None, verbose=0)" 314 | ] 315 | }, 316 | "execution_count": 10, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "gsCv = GridSearchCV(sklearn_model_new,\n", 323 | " {'max_depth': [4,5,6],\n", 324 | " 'n_estimators': [5,10,20]})\n", 325 | "gsCv.fit(X_train,y_train)" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 11, 331 | "metadata": {}, 332 | "outputs": [ 333 | { 334 | "name": "stdout", 335 | "output_type": "stream", 336 | "text": [ 337 | "0.9533333333333334\n", 338 | "{'max_depth': 4, 'n_estimators': 10}\n" 339 | ] 340 | } 341 | ], 342 | "source": [ 343 | "print(gsCv.best_score_)\n", 344 | "print(gsCv.best_params_)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 12, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stderr", 354 | "output_type": "stream", 355 | "text": [ 356 | ] 357 | }, 358 | { 359 | "data": { 360 | "text/plain": [ 361 | "GridSearchCV(cv=None, error_score='raise',\n", 362 | " estimator=XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", 363 | " colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.1,\n", 364 | " max_delta_step=0, max_depth=4, min_child_weight=1, missing=None,\n", 365 | " n_estimators=10, n_jobs=1, nthread=None,\n", 366 | " objective='binary:logistic', random_state=1, reg_alpha=0,\n", 367 | " reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,\n", 368 | " subsample=1, verbosity=1),\n", 369 | " fit_params=None, iid=True, n_jobs=1,\n", 370 | " param_grid={'learning_rate ': [0.3, 0.5, 0.7]},\n", 371 | " pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n", 372 | " scoring=None, verbose=0)" 373 | ] 374 | }, 375 | "execution_count": 12, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "sklearn_model_new2 = xgb.XGBClassifier(max_depth=4,n_estimators=10,verbosity=1, objective='binary:logistic',random_state=1)\n", 382 | "gsCv2 = GridSearchCV(sklearn_model_new2, \n", 383 | " {'learning_rate ': [0.3,0.5,0.7]})\n", 384 | "gsCv2.fit(X_train,y_train)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 13, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | "0.9516\n", 397 | "{'learning_rate ': 0.3}\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "print(gsCv2.best_score_)\n", 403 | "print(gsCv2.best_params_)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 14, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "[0]\tvalidation_0-error:0.062\n", 416 | "Will train until validation_0-error hasn't improved in 10 rounds.\n", 417 | "[1]\tvalidation_0-error:0.0592\n", 418 | "[2]\tvalidation_0-error:0.0608\n", 419 | "[3]\tvalidation_0-error:0.0608\n", 420 | "[4]\tvalidation_0-error:0.0608\n", 421 | "[5]\tvalidation_0-error:0.0604\n", 422 | "[6]\tvalidation_0-error:0.0592\n", 423 | "[7]\tvalidation_0-error:0.0588\n", 424 | "[8]\tvalidation_0-error:0.0588\n", 425 | "[9]\tvalidation_0-error:0.0588\n" 426 | ] 427 | }, 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", 432 | " colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.3,\n", 433 | " max_delta_step=0, max_depth=4, min_child_weight=1, missing=None,\n", 434 | " n_estimators=10, n_jobs=1, nthread=None,\n", 435 | " objective='binary:logistic', random_state=0, reg_alpha=0,\n", 436 | " reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,\n", 437 | " subsample=1, verbosity=1)" 438 | ] 439 | }, 440 | "execution_count": 14, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "sklearn_model_new2 = xgb.XGBClassifier(max_depth=4,learning_rate= 0.3, verbosity=1, objective='binary:logistic',n_estimators=10)\n", 447 | "sklearn_model_new2.fit(X_train, y_train, early_stopping_rounds=10, eval_metric=\"error\",\n", 448 | " eval_set=[(X_test, y_test)])" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 15, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "name": "stdout", 458 | "output_type": "stream", 459 | "text": [ 460 | "0.9412\n" 461 | ] 462 | }, 463 | { 464 | "name": "stderr", 465 | "output_type": "stream", 466 | "text": [ 467 | 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "pred_test_new = sklearn_model_new2.predict(X_test)\n", 473 | "print (accuracy_score(dtest.get_label(), pred_test_new))" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": null, 479 | "metadata": {}, 480 | "outputs": [], 481 | "source": [] 482 | } 483 | ], 484 | "metadata": { 485 | "kernelspec": { 486 | "display_name": "Python 3", 487 | "language": "python", 488 | "name": "python3" 489 | }, 490 | "language_info": { 491 | "codemirror_mode": { 492 | "name": "ipython", 493 | "version": 3 494 | }, 495 | "file_extension": ".py", 496 | "mimetype": "text/x-python", 497 | "name": "python", 498 | "nbconvert_exporter": "python", 499 | "pygments_lexer": "ipython3", 500 | "version": "3.6.5" 501 | } 502 | }, 503 | "nbformat": 4, 504 | "nbformat_minor": 2 505 | } 506 | -------------------------------------------------------------------------------- /model-in-product/sklearn-jpmml/PMML_Example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "用PMML实现机器学习模型的跨平台上线 https://www.cnblogs.com/pinard/p/9220199.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 5, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "import numpy as np\n", 23 | "import matplotlib.pyplot as plt\n", 24 | "%matplotlib inline\n", 25 | "import pandas as pd\n", 26 | "from sklearn import tree\n", 27 | "from sklearn2pmml.pipeline import PMMLPipeline\n", 28 | "from sklearn2pmml import sklearn2pmml\n", 29 | "\n", 30 | "import os\n", 31 | "os.environ[\"PATH\"] += os.pathsep + 'C:/Program Files/Java/jdk1.8.0_171/bin'" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 14, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "PMMLPipeline(steps=[('classifier', DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n", 43 | " max_features=None, max_leaf_nodes=None,\n", 44 | " min_impurity_decrease=0.0, min_impurity_split=None,\n", 45 | " min_samples_leaf=1, min_samples_split=2,\n", 46 | " min_weight_fraction_leaf=0.0, presort=False, random_state=9,\n", 47 | " splitter='best'))])" 48 | ] 49 | }, 50 | "execution_count": 14, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "X=[[1,2,3,1],[2,4,1,5],[7,8,3,6],[4,8,4,7],[2,5,6,9]]\n", 57 | "y=[0,1,0,2,1]\n", 58 | "pipeline = PMMLPipeline([(\"classifier\", tree.DecisionTreeClassifier(random_state=9))]);\n", 59 | "pipeline.fit(X,y)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 15, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "sklearn2pmml(pipeline, \".\\demo.pmml\", with_repr = True)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [] 84 | } 85 | ], 86 | "metadata": { 87 | "kernelspec": { 88 | "display_name": "Python 3", 89 | "language": "python", 90 | "name": "python3" 91 | }, 92 | "language_info": { 93 | "codemirror_mode": { 94 | "name": "ipython", 95 | "version": 3 96 | }, 97 | "file_extension": ".py", 98 | "mimetype": "text/x-python", 99 | "name": "python", 100 | "nbconvert_exporter": "python", 101 | "pygments_lexer": "ipython3", 102 | "version": "3.6.4" 103 | } 104 | }, 105 | "nbformat": 4, 106 | "nbformat_minor": 2 107 | } 108 | -------------------------------------------------------------------------------- /model-in-product/sklearn-jpmml/demo.pmml: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 2018-06-24T05:47:17Z 6 |
7 | 8 | PMMLPipeline(steps=[('classifier', DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None, 9 | max_features=None, max_leaf_nodes=None, 10 | min_impurity_decrease=0.0, min_impurity_split=None, 11 | min_samples_leaf=1, min_samples_split=2, 12 | min_weight_fraction_leaf=0.0, presort=False, random_state=9, 13 | splitter='best'))]) 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 |
75 | -------------------------------------------------------------------------------- /model-in-product/sklearn-jpmml/pmml_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | test 8 | test-demo 9 | 1.0 10 | 11 | 12 | org.jpmml 13 | pmml-evaluator 14 | 1.4.1 15 | 16 | 17 | org.jpmml 18 | pmml-evaluator-extension 19 | 1.4.1 20 | 21 | 22 | -------------------------------------------------------------------------------- /model-in-product/sklearn-jpmml/pmml_demo/src/main/java/PMMLDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com) 3 | 4 | https://www.cnblogs.com/pinard 5 | 6 | Permission given to modify the code as long as you keep this declaration at the top 7 | 8 | 用PMML实现机器学习模型的跨平台上线 https://www.cnblogs.com/pinard/p/9220199.html 9 | */ 10 | 11 | import org.dmg.pmml.FieldName; 12 | import org.dmg.pmml.PMML; 13 | import org.jpmml.evaluator.*; 14 | import org.xml.sax.SAXException; 15 | 16 | import javax.xml.bind.JAXBException; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.util.HashMap; 21 | import java.util.LinkedHashMap; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | /** 26 | * Created by 刘建平Pinard on 2018/6/24. 27 | */ 28 | public class PMMLDemo { 29 | private Evaluator loadPmml(){ 30 | PMML pmml = new PMML(); 31 | InputStream inputStream = null; 32 | try { 33 | inputStream = new FileInputStream("D:/demo.pmml"); 34 | } catch (IOException e) { 35 | e.printStackTrace(); 36 | } 37 | if(inputStream == null){ 38 | return null; 39 | } 40 | InputStream is = inputStream; 41 | try { 42 | pmml = org.jpmml.model.PMMLUtil.unmarshal(is); 43 | } catch (SAXException e1) { 44 | e1.printStackTrace(); 45 | } catch (JAXBException e1) { 46 | e1.printStackTrace(); 47 | }finally { 48 | //关闭输入流 49 | try { 50 | is.close(); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance(); 56 | Evaluator evaluator = modelEvaluatorFactory.newModelEvaluator(pmml); 57 | pmml = null; 58 | return evaluator; 59 | } 60 | private int predict(Evaluator evaluator,int a, int b, int c, int d) { 61 | Map data = new HashMap(); 62 | data.put("x1", a); 63 | data.put("x2", b); 64 | data.put("x3", c); 65 | data.put("x4", d); 66 | List inputFields = evaluator.getInputFields(); 67 | //过模型的原始特征,从画像中获取数据,作为模型输入 68 | Map arguments = new LinkedHashMap(); 69 | for (InputField inputField : inputFields) { 70 | FieldName inputFieldName = inputField.getName(); 71 | Object rawValue = data.get(inputFieldName.getValue()); 72 | FieldValue inputFieldValue = inputField.prepare(rawValue); 73 | arguments.put(inputFieldName, inputFieldValue); 74 | } 75 | 76 | Map results = evaluator.evaluate(arguments); 77 | List targetFields = evaluator.getTargetFields(); 78 | 79 | TargetField targetField = targetFields.get(0); 80 | FieldName targetFieldName = targetField.getName(); 81 | 82 | Object targetFieldValue = results.get(targetFieldName); 83 | System.out.println("target: " + targetFieldName.getValue() + " value: " + targetFieldValue); 84 | int primitiveValue = -1; 85 | if (targetFieldValue instanceof Computable) { 86 | Computable computable = (Computable) targetFieldValue; 87 | primitiveValue = (Integer)computable.getResult(); 88 | } 89 | System.out.println(a + " " + b + " " + c + " " + d + ":" + primitiveValue); 90 | return primitiveValue; 91 | } 92 | public static void main(String args[]){ 93 | PMMLDemo demo = new PMMLDemo(); 94 | Evaluator model = demo.loadPmml(); 95 | demo.predict(model,1,8,99,1); 96 | demo.predict(model,111,89,9,11); 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /model-in-product/tensorflow-java/TFDemoJava/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | tfDemo 8 | tfDemo 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.tensorflow 13 | tensorflow 14 | 1.7.0 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /model-in-product/tensorflow-java/TFDemoJava/src/main/java/TFjavaDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com) 3 | 4 | https://www.cnblogs.com/pinard 5 | 6 | Permission given to modify the code as long as you keep this declaration at the top 7 | 8 | tensorflow机器学习模型的跨平台上线 https://www.cnblogs.com/pinard/p/9251296.html 9 | */ 10 | 11 | import org.tensorflow.*; 12 | import org.tensorflow.Graph; 13 | 14 | import java.io.IOException; 15 | import java.nio.file.Files; 16 | import java.nio.file.Paths; 17 | 18 | 19 | /** 20 | * Created by 刘建平pinard on 2018/7/1. 21 | */ 22 | public class TFjavaDemo { 23 | public static void main(String args[]){ 24 | byte[] graphDef = loadTensorflowModel("D:/rf.pb"); 25 | float inputs[][] = new float[4][6]; 26 | for(int i = 0; i< 4; i++){ 27 | for(int j =0; j< 6;j++){ 28 | if(i<2) { 29 | inputs[i][j] = 2 * i - 5 * j - 6; 30 | } 31 | else{ 32 | inputs[i][j] = 2 * i + 5 * j - 6; 33 | } 34 | } 35 | } 36 | Tensor input = covertArrayToTensor(inputs); 37 | Graph g = new Graph(); 38 | g.importGraphDef(graphDef); 39 | Session s = new Session(g); 40 | Tensor result = s.runner().feed("input", input).fetch("output").run().get(0); 41 | 42 | long[] rshape = result.shape(); 43 | int rs = (int) rshape[0]; 44 | long realResult[] = new long[rs]; 45 | result.copyTo(realResult); 46 | 47 | for(long a: realResult ) { 48 | System.out.println(a); 49 | } 50 | } 51 | static private byte[] loadTensorflowModel(String path){ 52 | try { 53 | return Files.readAllBytes(Paths.get(path)); 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } 57 | return null; 58 | } 59 | 60 | static private Tensor covertArrayToTensor(float inputs[][]){ 61 | return Tensors.create(inputs); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /model-in-product/tensorflow-java/rf.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/model-in-product/tensorflow-java/rf.pb -------------------------------------------------------------------------------- /model-in-product/tensorflow-java/tensorflow_model.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "tensorflow机器学习模型的跨平台上线 https://www.cnblogs.com/pinard/p/9251296.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "import numpy as np\n", 23 | "import matplotlib.pyplot as plt\n", 24 | "%matplotlib inline\n", 25 | "from sklearn.datasets.samples_generator import make_classification\n", 26 | "import tensorflow as tf\n", 27 | "X1, y1 = make_classification(n_samples=4000, n_features=6, n_redundant=0,\n", 28 | " n_clusters_per_class=1, n_classes=3)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "(4000, 6)\n", 41 | "(4000,)\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "print (X1.shape)\n", 47 | "print (y1.shape)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "learning_rate = 0.01\n", 57 | "training_epochs = 600\n", 58 | "batch_size = 100\n", 59 | "\n", 60 | "x = tf.placeholder(tf.float32, [None, 6],name='input') # 6 features\n", 61 | "y = tf.placeholder(tf.float32, [None, 3]) # 3 classes\n", 62 | "\n", 63 | "W = tf.Variable(tf.zeros([6, 3]))\n", 64 | "b = tf.Variable(tf.zeros([3]))\n", 65 | "\n", 66 | "# softmax回归\n", 67 | "pred = tf.nn.softmax(tf.matmul(x, W) + b, name=\"softmax\") \n", 68 | "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))\n", 69 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n", 70 | "\n", 71 | "prediction_labels = tf.argmax(pred, axis=1, name=\"output\")\n", 72 | "\n", 73 | "init = tf.global_variables_initializer()" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 4, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "(4000, 3)\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "sess = tf.Session()\n", 91 | "sess.run(init)\n", 92 | "y2 = tf.one_hot(y1, 3)\n", 93 | "y2 = sess.run(y2)\n", 94 | "print (y2.shape)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 5, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "Epoch: 0010 cost= 1.050110102\n", 107 | "Epoch: 0020 cost= 1.001712561\n", 108 | "Epoch: 0030 cost= 0.958428741\n", 109 | "Epoch: 0040 cost= 0.919634283\n", 110 | "Epoch: 0050 cost= 0.884774268\n", 111 | "Epoch: 0060 cost= 0.853360713\n", 112 | "Epoch: 0070 cost= 0.824967980\n", 113 | "Epoch: 0080 cost= 0.799225986\n", 114 | "Epoch: 0090 cost= 0.775815248\n", 115 | "Epoch: 0100 cost= 0.754458249\n", 116 | "Epoch: 0110 cost= 0.734916270\n", 117 | "Epoch: 0120 cost= 0.716981947\n", 118 | "Epoch: 0130 cost= 0.700476527\n", 119 | "Epoch: 0140 cost= 0.685244024\n", 120 | "Epoch: 0150 cost= 0.671149671\n", 121 | "Epoch: 0160 cost= 0.658075333\n", 122 | "Epoch: 0170 cost= 0.645918190\n", 123 | "Epoch: 0180 cost= 0.634587705\n", 124 | "Epoch: 0190 cost= 0.624004900\n", 125 | "Epoch: 0200 cost= 0.614099383\n", 126 | "Epoch: 0210 cost= 0.604809642\n", 127 | "Epoch: 0220 cost= 0.596081078\n", 128 | "Epoch: 0230 cost= 0.587864757\n", 129 | "Epoch: 0240 cost= 0.580117583\n", 130 | "Epoch: 0250 cost= 0.572800457\n", 131 | "Epoch: 0260 cost= 0.565879166\n", 132 | "Epoch: 0270 cost= 0.559322178\n", 133 | "Epoch: 0280 cost= 0.553101718\n", 134 | "Epoch: 0290 cost= 0.547192395\n", 135 | "Epoch: 0300 cost= 0.541571438\n", 136 | "Epoch: 0310 cost= 0.536218166\n", 137 | "Epoch: 0320 cost= 0.531113505\n", 138 | "Epoch: 0330 cost= 0.526240945\n", 139 | "Epoch: 0340 cost= 0.521584213\n", 140 | "Epoch: 0350 cost= 0.517129600\n", 141 | "Epoch: 0360 cost= 0.512863755\n", 142 | "Epoch: 0370 cost= 0.508774936\n", 143 | "Epoch: 0380 cost= 0.504852176\n", 144 | "Epoch: 0390 cost= 0.501085103\n", 145 | "Epoch: 0400 cost= 0.497464716\n", 146 | "Epoch: 0410 cost= 0.493982226\n", 147 | "Epoch: 0420 cost= 0.490630001\n", 148 | "Epoch: 0430 cost= 0.487400532\n", 149 | "Epoch: 0440 cost= 0.484286994\n", 150 | "Epoch: 0450 cost= 0.481283128\n", 151 | "Epoch: 0460 cost= 0.478383124\n", 152 | "Epoch: 0470 cost= 0.475581378\n", 153 | "Epoch: 0480 cost= 0.472873092\n", 154 | "Epoch: 0490 cost= 0.470253319\n", 155 | "Epoch: 0500 cost= 0.467717826\n", 156 | "Epoch: 0510 cost= 0.465262204\n", 157 | "Epoch: 0520 cost= 0.462882727\n", 158 | "Epoch: 0530 cost= 0.460575819\n", 159 | "Epoch: 0540 cost= 0.458337963\n", 160 | "Epoch: 0550 cost= 0.456166118\n", 161 | "Epoch: 0560 cost= 0.454057366\n", 162 | "Epoch: 0570 cost= 0.452008665\n", 163 | "Epoch: 0580 cost= 0.450017571\n", 164 | "Epoch: 0590 cost= 0.448081255\n", 165 | "Epoch: 0600 cost= 0.446197867\n", 166 | "优化完毕!\n", 167 | "0.8675\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "for epoch in range(training_epochs):\n", 173 | "\n", 174 | " _, c = sess.run([optimizer, cost], feed_dict={x: X1, y: y2})\n", 175 | " if (epoch+1) % 10 == 0:\n", 176 | " print (\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(c))\n", 177 | " \n", 178 | "print (\"优化完毕!\")\n", 179 | "correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y2, 1))\n", 180 | "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 181 | "acc = sess.run(accuracy, feed_dict={x: X1, y: y2})\n", 182 | "print (acc)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 6, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "INFO:tensorflow:Froze 2 variables.\n", 195 | "Converted 2 variables to const ops.\n" 196 | ] 197 | }, 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "'.\\\\rf.pb'" 202 | ] 203 | }, 204 | "execution_count": 6, 205 | "metadata": {}, 206 | "output_type": "execute_result" 207 | } 208 | ], 209 | "source": [ 210 | "graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, [\"output\"])\n", 211 | "tf.train.write_graph(graph, '.', 'rf.pb', as_text=False)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [] 220 | } 221 | ], 222 | "metadata": { 223 | "kernelspec": { 224 | "display_name": "Python 3", 225 | "language": "python", 226 | "name": "python3" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 3 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython3", 238 | "version": "3.6.4" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 2 243 | } 244 | -------------------------------------------------------------------------------- /natural-language-processing/chinese_digging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "中文文本挖掘预处理流程总结 https://www.cnblogs.com/pinard/p/6744056.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 44, 19 | "metadata": { 20 | "scrolled": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "# -*- coding: utf-8 -*-\n", 25 | "\n", 26 | "import jieba\n", 27 | "\n", 28 | "with open('./nlp_test0.txt') as f:\n", 29 | " document = f.read()\n", 30 | " \n", 31 | " document_decode = document.decode('GBK')\n", 32 | " document_cut = jieba.cut(document_decode)\n", 33 | " #print ' '.join(jieba_cut) //如果打印结果,则分词效果消失,后面的result无法显示\n", 34 | " result = ' '.join(document_cut)\n", 35 | " result = result.encode('utf-8')\n", 36 | " with open('./nlp_test1.txt', 'w') as f2:\n", 37 | " f2.write(result)\n", 38 | "f.close()\n", 39 | "f2.close()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 45, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "4" 51 | ] 52 | }, 53 | "execution_count": 45, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "jieba.suggest_freq('沙瑞金', True)\n", 60 | "jieba.suggest_freq('易学习', True)\n", 61 | "jieba.suggest_freq('王大路', True)\n", 62 | "jieba.suggest_freq('京州', True)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 46, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "with open('./nlp_test0.txt') as f:\n", 72 | " document = f.read()\n", 73 | " \n", 74 | " document_decode = document.decode('GBK')\n", 75 | " document_cut = jieba.cut(document_decode)\n", 76 | " #print ' '.join(jieba_cut)\n", 77 | " result = ' '.join(document_cut)\n", 78 | " result = result.encode('utf-8')\n", 79 | " with open('./nlp_test1.txt', 'w') as f2:\n", 80 | " f2.write(result)\n", 81 | "f.close()\n", 82 | "f2.close() " 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 47, 88 | "metadata": { 89 | "collapsed": true 90 | }, 91 | "outputs": [], 92 | "source": [ 93 | "#从文件导入停用词表\n", 94 | "stpwrdpath = \"stop_words.txt\"\n", 95 | "stpwrd_dic = open(stpwrdpath, 'rb')\n", 96 | "stpwrd_content = stpwrd_dic.read()\n", 97 | "#将停用词表转换为list \n", 98 | "stpwrdlst = stpwrd_content.splitlines()\n", 99 | "stpwrd_dic.close()" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 48, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "沙瑞金 赞叹 易学习 的 胸怀 , 是 金山 的 百姓 有福 , 可是 这件 事对 李达康 的 触动 很大 。 易学习 又 回忆起 他们 三人 分开 的 前一晚 , 大家 一起 喝酒 话别 , 易学习 被 降职 到 道口 县当 县长 , 王大路 下海经商 , 李达康 连连 赔礼道歉 , 觉得 对不起 大家 , 他 最 对不起 的 是 王大路 , 就 和 易学习 一起 给 王大路 凑 了 5 万块 钱 , 王大路 自己 东挪西撮 了 5 万块 , 开始 下海经商 。 没想到 后来 王大路 竟然 做 得 风生水 起 。 沙瑞金 觉得 他们 三人 , 在 困难 时期 还 能 以沫 相助 , 很 不 容易 。\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "with open('./nlp_test1.txt') as f3:\n", 117 | " res1 = f3.read()\n", 118 | "print res1" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 49, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "with open('./nlp_test2.txt') as f:\n", 128 | " document2 = f.read()\n", 129 | " \n", 130 | " document2_decode = document2.decode('GBK')\n", 131 | " document2_cut = jieba.cut(document2_decode)\n", 132 | " #print ' '.join(jieba_cut)\n", 133 | " result = ' '.join(document2_cut)\n", 134 | " result = result.encode('utf-8')\n", 135 | " with open('./nlp_test3.txt', 'w') as f2:\n", 136 | " f2.write(result)\n", 137 | "f.close()\n", 138 | "f2.close() " 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 50, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "沙瑞金 向 毛娅 打听 他们 家 在 京州 的 别墅 , 毛娅 笑 着 说 , 王大路 事业有成 之后 , 要 给 欧阳 菁 和 她 公司 的 股权 , 她们 没有 要 , 王大路 就 在 京州 帝豪园 买 了 三套 别墅 , 可是 李达康 和 易学习 都 不要 , 这些 房子 都 在 王大路 的 名下 , 欧阳 菁 好像 去 住 过 , 毛娅 不想 去 , 她 觉得 房子 太大 很 浪费 , 自己 家住 得 就 很 踏实 。\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "with open('./nlp_test3.txt') as f4:\n", 156 | " res2 = f4.read()\n", 157 | "print res2" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 51, 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [ 166 | "jieba.suggest_freq('桓温', True)\n", 167 | "with open('./nlp_test4.txt') as f:\n", 168 | " document3 = f.read()\n", 169 | " \n", 170 | " document3_decode = document3.decode('GBK')\n", 171 | " document3_cut = jieba.cut(document3_decode)\n", 172 | " #print ' '.join(jieba_cut)\n", 173 | " result = ' '.join(document3_cut)\n", 174 | " result = result.encode('utf-8')\n", 175 | " with open('./nlp_test5.txt', 'w') as f3:\n", 176 | " f3.write(result)\n", 177 | "f.close()\n", 178 | "f3.close() " 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 53, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | " (0, 44)\t0.154467434933\n", 191 | " (0, 59)\t0.108549295069\n", 192 | " (0, 39)\t0.308934869866\n", 193 | " (0, 53)\t0.108549295069\n", 194 | " (0, 65)\t0.108549295069\n", 195 | " (0, 49)\t0.108549295069\n", 196 | " (0, 40)\t0.108549295069\n", 197 | " (0, 20)\t0.0772337174664\n", 198 | " (0, 62)\t0.108549295069\n", 199 | " (0, 10)\t0.108549295069\n", 200 | " (0, 41)\t0.154467434933\n", 201 | " (0, 56)\t0.108549295069\n", 202 | " (0, 35)\t0.108549295069\n", 203 | " (0, 24)\t0.108549295069\n", 204 | " (0, 12)\t0.154467434933\n", 205 | " (0, 2)\t0.217098590137\n", 206 | " (0, 15)\t0.108549295069\n", 207 | " (0, 17)\t0.108549295069\n", 208 | " (0, 26)\t0.217098590137\n", 209 | " (0, 0)\t0.217098590137\n", 210 | " (0, 23)\t0.108549295069\n", 211 | " (0, 57)\t0.108549295069\n", 212 | " (0, 66)\t0.108549295069\n", 213 | " (0, 64)\t0.108549295069\n", 214 | " (0, 18)\t0.108549295069\n", 215 | " :\t:\n", 216 | " (1, 55)\t0.0995336411066\n", 217 | " (1, 54)\t0.0995336411066\n", 218 | " (1, 43)\t0.419673178975\n", 219 | " (1, 37)\t0.139891059658\n", 220 | " (1, 11)\t0.279782119316\n", 221 | " (1, 16)\t0.279782119316\n", 222 | " (1, 9)\t0.139891059658\n", 223 | " (1, 8)\t0.139891059658\n", 224 | " (1, 42)\t0.279782119316\n", 225 | " (1, 14)\t0.139891059658\n", 226 | " (1, 52)\t0.139891059658\n", 227 | " (1, 28)\t0.139891059658\n", 228 | " (1, 46)\t0.139891059658\n", 229 | " (1, 33)\t0.139891059658\n", 230 | " (1, 3)\t0.139891059658\n", 231 | " (1, 6)\t0.139891059658\n", 232 | " (1, 61)\t0.139891059658\n", 233 | " (1, 36)\t0.279782119316\n", 234 | " (1, 21)\t0.139891059658\n", 235 | " (1, 29)\t0.139891059658\n", 236 | " (1, 5)\t0.139891059658\n", 237 | " (1, 27)\t0.139891059658\n", 238 | " (1, 47)\t0.139891059658\n", 239 | " (1, 30)\t0.139891059658\n", 240 | " (1, 60)\t0.139891059658\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "from sklearn.feature_extraction.text import TfidfVectorizer\n", 246 | "corpus = [res1,res2]\n", 247 | "vector = TfidfVectorizer(stop_words=stpwrdlst)\n", 248 | "tfidf = vector.fit_transform(corpus)\n", 249 | "print tfidf" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 54, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "-------第 0 段文本的词语tf-idf权重------\n", 262 | "一起 0.217098590137\n", 263 | "万块 0.217098590137\n", 264 | "三人 0.217098590137\n", 265 | "三套 0.0\n", 266 | "下海经商 0.217098590137\n", 267 | "不想 0.0\n", 268 | "不要 0.0\n", 269 | "东挪西撮 0.108549295069\n", 270 | "之后 0.0\n", 271 | "事业有成 0.0\n", 272 | "事对 0.108549295069\n", 273 | "京州 0.0\n", 274 | "他们 0.154467434933\n", 275 | "以沫 0.108549295069\n", 276 | "公司 0.0\n", 277 | "分开 0.108549295069\n", 278 | "别墅 0.0\n", 279 | "前一晚 0.108549295069\n", 280 | "县当 0.108549295069\n", 281 | "县长 0.108549295069\n", 282 | "可是 0.0772337174664\n", 283 | "名下 0.0\n", 284 | "后来 0.108549295069\n", 285 | "喝酒 0.108549295069\n", 286 | "回忆起 0.108549295069\n", 287 | "困难 0.108549295069\n", 288 | "大家 0.217098590137\n", 289 | "太大 0.0\n", 290 | "她们 0.0\n", 291 | "好像 0.0\n", 292 | "家住 0.0\n", 293 | "容易 0.108549295069\n", 294 | "对不起 0.217098590137\n", 295 | "帝豪园 0.0\n", 296 | "开始 0.108549295069\n", 297 | "很大 0.108549295069\n", 298 | "房子 0.0\n", 299 | "打听 0.0\n", 300 | "时期 0.108549295069\n", 301 | "易学习 0.308934869866\n", 302 | "有福 0.108549295069\n", 303 | "李达康 0.154467434933\n", 304 | "欧阳 0.0\n", 305 | "毛娅 0.0\n", 306 | "沙瑞金 0.154467434933\n", 307 | "没想到 0.108549295069\n", 308 | "没有 0.0\n", 309 | "浪费 0.0\n", 310 | "王大路 0.386168587332\n", 311 | "百姓 0.108549295069\n", 312 | "相助 0.108549295069\n", 313 | "竟然 0.108549295069\n", 314 | "股权 0.0\n", 315 | "胸怀 0.108549295069\n", 316 | "自己 0.0772337174664\n", 317 | "觉得 0.154467434933\n", 318 | "触动 0.108549295069\n", 319 | "话别 0.108549295069\n", 320 | "赔礼道歉 0.108549295069\n", 321 | "赞叹 0.108549295069\n", 322 | "踏实 0.0\n", 323 | "这些 0.0\n", 324 | "这件 0.108549295069\n", 325 | "连连 0.108549295069\n", 326 | "道口 0.108549295069\n", 327 | "金山 0.108549295069\n", 328 | "降职 0.108549295069\n", 329 | "风生水 0.108549295069\n", 330 | "-------第 1 段文本的词语tf-idf权重------\n", 331 | "一起 0.0\n", 332 | "万块 0.0\n", 333 | "三人 0.0\n", 334 | "三套 0.139891059658\n", 335 | "下海经商 0.0\n", 336 | "不想 0.139891059658\n", 337 | "不要 0.139891059658\n", 338 | "东挪西撮 0.0\n", 339 | "之后 0.139891059658\n", 340 | "事业有成 0.139891059658\n", 341 | "事对 0.0\n", 342 | "京州 0.279782119316\n", 343 | "他们 0.0995336411066\n", 344 | "以沫 0.0\n", 345 | "公司 0.139891059658\n", 346 | "分开 0.0\n", 347 | "别墅 0.279782119316\n", 348 | "前一晚 0.0\n", 349 | "县当 0.0\n", 350 | "县长 0.0\n", 351 | "可是 0.0995336411066\n", 352 | "名下 0.139891059658\n", 353 | "后来 0.0\n", 354 | "喝酒 0.0\n", 355 | "回忆起 0.0\n", 356 | "困难 0.0\n", 357 | "大家 0.0\n", 358 | "太大 0.139891059658\n", 359 | "她们 0.139891059658\n", 360 | "好像 0.139891059658\n", 361 | "家住 0.139891059658\n", 362 | "容易 0.0\n", 363 | "对不起 0.0\n", 364 | "帝豪园 0.139891059658\n", 365 | "开始 0.0\n", 366 | "很大 0.0\n", 367 | "房子 0.279782119316\n", 368 | "打听 0.139891059658\n", 369 | "时期 0.0\n", 370 | "易学习 0.0995336411066\n", 371 | "有福 0.0\n", 372 | "李达康 0.0995336411066\n", 373 | "欧阳 0.279782119316\n", 374 | "毛娅 0.419673178975\n", 375 | "沙瑞金 0.0995336411066\n", 376 | "没想到 0.0\n", 377 | "没有 0.139891059658\n", 378 | "浪费 0.139891059658\n", 379 | "王大路 0.29860092332\n", 380 | "百姓 0.0\n", 381 | "相助 0.0\n", 382 | "竟然 0.0\n", 383 | "股权 0.139891059658\n", 384 | "胸怀 0.0\n", 385 | "自己 0.0995336411066\n", 386 | "觉得 0.0995336411066\n", 387 | "触动 0.0\n", 388 | "话别 0.0\n", 389 | "赔礼道歉 0.0\n", 390 | "赞叹 0.0\n", 391 | "踏实 0.139891059658\n", 392 | "这些 0.139891059658\n", 393 | "这件 0.0\n", 394 | "连连 0.0\n", 395 | "道口 0.0\n", 396 | "金山 0.0\n", 397 | "降职 0.0\n", 398 | "风生水 0.0\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "wordlist = vector.get_feature_names()#获取词袋模型中的所有词 \n", 404 | "# tf-idf矩阵 元素a[i][j]表示j词在i类文本中的tf-idf权重\n", 405 | "weightlist = tfidf.toarray() \n", 406 | "#打印每类文本的tf-idf词语权重,第一个for遍历所有文本,第二个for便利某一类文本下的词语权重\n", 407 | "for i in range(len(weightlist)): \n", 408 | " print \"-------第\",i,\"段文本的词语tf-idf权重------\" \n", 409 | " for j in range(len(wordlist)): \n", 410 | " print wordlist[j],weightlist[i][j] " 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": { 417 | "collapsed": true 418 | }, 419 | "outputs": [], 420 | "source": [] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": { 426 | "collapsed": true 427 | }, 428 | "outputs": [], 429 | "source": [] 430 | } 431 | ], 432 | "metadata": { 433 | "kernelspec": { 434 | "display_name": "Python 3", 435 | "language": "python", 436 | "name": "python3" 437 | }, 438 | "language_info": { 439 | "codemirror_mode": { 440 | "name": "ipython", 441 | "version": 3 442 | }, 443 | "file_extension": ".py", 444 | "mimetype": "text/x-python", 445 | "name": "python", 446 | "nbconvert_exporter": "python", 447 | "pygments_lexer": "ipython3", 448 | "version": "3.6.4" 449 | } 450 | }, 451 | "nbformat": 4, 452 | "nbformat_minor": 1 453 | } 454 | -------------------------------------------------------------------------------- /natural-language-processing/english_digging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "英文文本挖掘预处理流程总结 https://www.cnblogs.com/pinard/p/6756534.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml\n" 26 | ] 27 | }, 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "True" 32 | ] 33 | }, 34 | "execution_count": 1, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "import nltk\n", 41 | "nltk.download()" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "imaging\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "from nltk.stem import WordNetLemmatizer\n", 59 | "wnl = WordNetLemmatizer()\n", 60 | "print(wnl.lemmatize('imaging')) " 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "arabic danish dutch english finnish french german hungarian italian norwegian porter portuguese romanian russian spanish swedish\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "from nltk.stem import SnowballStemmer\n", 78 | "print(\" \".join(SnowballStemmer.languages)) # See which languages are suppo" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "'countri'" 90 | ] 91 | }, 92 | "execution_count": 4, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "stemmer = SnowballStemmer(\"english\") # Choose a language\n", 99 | "stemmer.stem(\"countries\") # Stem a word" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "country\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "from nltk.stem import WordNetLemmatizer\n", 117 | "wnl = WordNetLemmatizer()\n", 118 | "print(wnl.lemmatize('countries')) " 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 29, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "ERROR: peope\n", 131 | "ERROR: likee\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "from enchant.checker import SpellChecker\n", 137 | "chkr = SpellChecker(\"en_US\")\n", 138 | "chkr.set_text(\"Many peope likee to watch In the Name of People.\")\n", 139 | "for err in chkr:\n", 140 | " print \"ERROR:\", err.word" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": { 154 | "collapsed": true 155 | }, 156 | "outputs": [], 157 | "source": [] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.6.4" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 1 181 | } 182 | -------------------------------------------------------------------------------- /natural-language-processing/hash_trick.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "文本挖掘预处理之向量化与Hash Trick https://www.cnblogs.com/pinard/p/6688348.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | " (0, 16)\t1\n", 26 | " (0, 3)\t1\n", 27 | " (0, 15)\t2\n", 28 | " (0, 4)\t1\n", 29 | " (1, 5)\t1\n", 30 | " (1, 9)\t1\n", 31 | " (1, 2)\t1\n", 32 | " (1, 6)\t1\n", 33 | " (1, 14)\t1\n", 34 | " (1, 3)\t1\n", 35 | " (2, 1)\t1\n", 36 | " (2, 0)\t1\n", 37 | " (2, 12)\t1\n", 38 | " (2, 7)\t1\n", 39 | " (3, 10)\t1\n", 40 | " (3, 8)\t1\n", 41 | " (3, 11)\t1\n", 42 | " (3, 18)\t1\n", 43 | " (3, 17)\t1\n", 44 | " (3, 13)\t1\n", 45 | " (3, 5)\t1\n", 46 | " (3, 6)\t1\n", 47 | " (3, 15)\t1\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "from sklearn.feature_extraction.text import CountVectorizer \n", 53 | "vectorizer=CountVectorizer()\n", 54 | "corpus=[\"I come to China to travel\", \n", 55 | " \"This is a car polupar in China\", \n", 56 | " \"I love tea and Apple \", \n", 57 | " \"The work is to write some papers in science\"] \n", 58 | "print (vectorizer.fit_transform(corpus))" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "[[0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 2 1 0 0]\n", 71 | " [0 0 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0]\n", 72 | " [1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0]\n", 73 | " [0 0 0 0 0 1 1 0 1 0 1 1 0 1 0 1 0 1 1]]\n", 74 | "['and', 'apple', 'car', 'china', 'come', 'in', 'is', 'love', 'papers', 'polupar', 'science', 'some', 'tea', 'the', 'this', 'to', 'travel', 'work', 'write']\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "print (vectorizer.fit_transform(corpus).toarray())\n", 80 | "print (vectorizer.get_feature_names())" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | " (0, 1)\t2.0\n", 93 | " (0, 2)\t-1.0\n", 94 | " (0, 4)\t1.0\n", 95 | " (0, 5)\t-1.0\n", 96 | " (1, 0)\t1.0\n", 97 | " (1, 1)\t1.0\n", 98 | " (1, 2)\t-1.0\n", 99 | " (1, 5)\t-1.0\n", 100 | " (2, 0)\t2.0\n", 101 | " (2, 5)\t-2.0\n", 102 | " (3, 0)\t0.0\n", 103 | " (3, 1)\t4.0\n", 104 | " (3, 2)\t-1.0\n", 105 | " (3, 3)\t1.0\n", 106 | " (3, 5)\t-1.0\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "from sklearn.feature_extraction.text import HashingVectorizer \n", 112 | "vectorizer2=HashingVectorizer(n_features = 6,norm = None)\n", 113 | "print (vectorizer2.fit_transform(corpus))" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.4" 141 | } 142 | }, 143 | "nbformat": 4, 144 | "nbformat_minor": 2 145 | } 146 | -------------------------------------------------------------------------------- /natural-language-processing/hmm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "用hmmlearn学习隐马尔科夫模型HMM https://www.cnblogs.com/pinard/p/7001397.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 11, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "('The ball picked:', 'red, white, red')\n", 26 | "('The hidden box', 'box3, box3, box3')\n" 27 | ] 28 | }, 29 | { 30 | "name": "stderr", 31 | "output_type": "stream", 32 | "text": [ 33 | "C:\\Python27\\lib\\site-packages\\IPython\\kernel\\__main__.py:31: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "import numpy as np\n", 39 | "from hmmlearn import hmm\n", 40 | "\n", 41 | "states = [\"box 1\", \"box 2\", \"box3\"]\n", 42 | "n_states = len(states)\n", 43 | "\n", 44 | "observations = [\"red\", \"white\"]\n", 45 | "n_observations = len(observations)\n", 46 | "\n", 47 | "start_probability = np.array([0.2, 0.4, 0.4])\n", 48 | "\n", 49 | "transition_probability = np.array([\n", 50 | " [0.5, 0.2, 0.3],\n", 51 | " [0.3, 0.5, 0.2],\n", 52 | " [0.2, 0.3, 0.5]\n", 53 | "])\n", 54 | "\n", 55 | "emission_probability = np.array([\n", 56 | " [0.5, 0.5],\n", 57 | " [0.4, 0.6],\n", 58 | " [0.7, 0.3]\n", 59 | "])\n", 60 | "\n", 61 | "model = hmm.MultinomialHMM(n_components=n_states)\n", 62 | "model.startprob_=start_probability\n", 63 | "model.transmat_=transition_probability\n", 64 | "model.emissionprob_=emission_probability\n", 65 | "\n", 66 | "seen = np.array([[0,1,0]]).T\n", 67 | "logprob, box = model.decode(seen, algorithm=\"viterbi\")\n", 68 | "print(\"The ball picked:\", \", \".join(map(lambda x: observations[x], seen)))\n", 69 | "print(\"The hidden box\", \", \".join(map(lambda x: states[x], box)))" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 14, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "('The ball picked:', 'red, white, red')\n", 82 | "('The hidden box', 'box3, box3, box3')\n" 83 | ] 84 | }, 85 | { 86 | "name": "stderr", 87 | "output_type": "stream", 88 | "text": [ 89 | "C:\\Python27\\lib\\site-packages\\IPython\\kernel\\__main__.py:2: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future\n", 90 | " from IPython.kernel.zmq import kernelapp as app\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "box2 = model.predict(seen)\n", 96 | "print(\"The ball picked:\", \", \".join(map(lambda x: observations[x], seen)))\n", 97 | "print(\"The hidden box\", \", \".join(map(lambda x: states[x], box2)))" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 3, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "-2.03854530992\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "print model.score(seen)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 9, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "[ 1.31697183e-12 3.07279825e-15 1.00000000e+00]\n", 127 | "[[ 1.48161608e-01 1.50331031e-01 7.01507361e-01]\n", 128 | " [ 2.19355550e-01 2.31744543e-01 5.48899907e-01]\n", 129 | " [ 4.55650613e-01 5.44338342e-01 1.10450680e-05]]\n", 130 | "[[ 2.19032398e-01 7.80967602e-01]\n", 131 | " [ 1.15445252e-01 8.84554748e-01]\n", 132 | " [ 9.99192283e-01 8.07717131e-04]]\n", 133 | "-6.51866416797\n", 134 | "[ 9.99999997e-01 3.19111963e-09 2.84005362e-23]\n", 135 | "[[ 8.42932318e-09 4.12422449e-02 9.58757747e-01]\n", 136 | " [ 1.35449734e-01 5.71715372e-01 2.92834894e-01]\n", 137 | " [ 5.77139011e-01 9.57292050e-02 3.27131784e-01]]\n", 138 | "[[ 9.99987996e-01 1.20035621e-05]\n", 139 | " [ 5.22916524e-01 4.77083476e-01]\n", 140 | " [ 1.51107630e-01 8.48892370e-01]]\n", 141 | "-6.61149770824\n", 142 | "[ 1.00000000e+00 1.30722027e-10 3.08257576e-12]\n", 143 | "[[ 1.11200351e-05 4.22408201e-01 5.77580679e-01]\n", 144 | " [ 6.44472673e-01 1.69915233e-01 1.85612094e-01]\n", 145 | " [ 5.92638365e-01 1.91056314e-01 2.16305321e-01]]\n", 146 | "[[ 9.99515188e-01 4.84812380e-04]\n", 147 | " [ 1.87229564e-01 8.12770436e-01]\n", 148 | " [ 1.49192055e-01 8.50807945e-01]]\n", 149 | "-6.54281680533\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "import numpy as np\n", 155 | "from hmmlearn import hmm\n", 156 | "\n", 157 | "states = [\"box 1\", \"box 2\", \"box3\"]\n", 158 | "n_states = len(states)\n", 159 | "\n", 160 | "observations = [\"red\", \"white\"]\n", 161 | "n_observations = len(observations)\n", 162 | "model2 = hmm.MultinomialHMM(n_components=n_states, n_iter=20, tol=0.01)\n", 163 | "X2 = np.array([[0,1,0,1],[0,0,0,1],[1,0,1,1]])\n", 164 | "model2.fit(X2)\n", 165 | "print model2.startprob_\n", 166 | "print model2.transmat_\n", 167 | "print model2.emissionprob_\n", 168 | "print model2.score(X2)\n", 169 | "model2.fit(X2)\n", 170 | "print model2.startprob_\n", 171 | "print model2.transmat_\n", 172 | "print model2.emissionprob_\n", 173 | "print model2.score(X2)\n", 174 | "model2.fit(X2)\n", 175 | "print model2.startprob_\n", 176 | "print model2.transmat_\n", 177 | "print model2.emissionprob_\n", 178 | "print model2.score(X2)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 17, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "startprob = np.array([0.6, 0.3, 0.1, 0.0])\n", 188 | "# The transition matrix, note that there are no transitions possible\n", 189 | "# between component 1 and 3\n", 190 | "transmat = np.array([[0.7, 0.2, 0.0, 0.1],\n", 191 | " [0.3, 0.5, 0.2, 0.0],\n", 192 | " [0.0, 0.3, 0.5, 0.2],\n", 193 | " [0.2, 0.0, 0.2, 0.6]])\n", 194 | "# The means of each component\n", 195 | "means = np.array([[0.0, 0.0],\n", 196 | " [0.0, 11.0],\n", 197 | " [9.0, 10.0],\n", 198 | " [11.0, -1.0]])\n", 199 | "# The covariance of each component\n", 200 | "covars = .5 * np.tile(np.identity(2), (4, 1, 1))\n", 201 | "\n", 202 | "# Build an HMM instance and set parameters\n", 203 | "model3 = hmm.GaussianHMM(n_components=4, covariance_type=\"full\")\n", 204 | "\n", 205 | "# Instead of fitting it from the data, we directly set the estimated\n", 206 | "# parameters, the means and covariance of the components\n", 207 | "model3.startprob_ = startprob\n", 208 | "model3.transmat_ = transmat\n", 209 | "model3.means_ = means\n", 210 | "model3.covars_ = covars" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 20, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stderr", 220 | "output_type": "stream", 221 | "text": [ 222 | "C:\\Python27\\lib\\site-packages\\sklearn\\utils\\deprecation.py:70: DeprecationWarning: Function log_multivariate_normal_density is deprecated; The function log_multivariate_normal_density is deprecated in 0.18 and will be removed in 0.20.\n", 223 | " warnings.warn(msg, category=DeprecationWarning)\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "seen = np.array([[1.1,2.0],[-1,2.0],[3,7]])\n", 229 | "logprob, state = model.decode(seen, algorithm=\"viterbi\")" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 21, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "[0 0 1]\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "print state" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 22, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "-41.1211281377\n" 259 | ] 260 | }, 261 | { 262 | "name": "stderr", 263 | "output_type": "stream", 264 | "text": [ 265 | "C:\\Python27\\lib\\site-packages\\sklearn\\utils\\deprecation.py:70: DeprecationWarning: Function log_multivariate_normal_density is deprecated; The function log_multivariate_normal_density is deprecated in 0.18 and will be removed in 0.20.\n", 266 | " warnings.warn(msg, category=DeprecationWarning)\n", 267 | "C:\\Python27\\lib\\site-packages\\hmmlearn\\base.py:459: RuntimeWarning: divide by zero encountered in log\n", 268 | " np.log(self.startprob_),\n", 269 | "C:\\Python27\\lib\\site-packages\\hmmlearn\\base.py:460: RuntimeWarning: divide by zero encountered in log\n", 270 | " np.log(self.transmat_),\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "print model3.score(seen)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": { 282 | "collapsed": true 283 | }, 284 | "outputs": [], 285 | "source": [] 286 | } 287 | ], 288 | "metadata": { 289 | "kernelspec": { 290 | "display_name": "Python 3", 291 | "language": "python", 292 | "name": "python3" 293 | }, 294 | "language_info": { 295 | "codemirror_mode": { 296 | "name": "ipython", 297 | "version": 3 298 | }, 299 | "file_extension": ".py", 300 | "mimetype": "text/x-python", 301 | "name": "python", 302 | "nbconvert_exporter": "python", 303 | "pygments_lexer": "ipython3", 304 | "version": "3.6.4" 305 | } 306 | }, 307 | "nbformat": 4, 308 | "nbformat_minor": 1 309 | } 310 | -------------------------------------------------------------------------------- /natural-language-processing/nmf.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "文本主题模型之非负矩阵分解(NMF) https://www.cnblogs.com/pinard/p/6812011.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import numpy as np\n", 25 | "X = np.array([[1,1,5,2,3], [0,6,2,1,1], [3, 4,0,3,1], [4, 1,5,6,3]])\n", 26 | "from sklearn.decomposition import NMF\n", 27 | "model = NMF(n_components=2, alpha=0.01)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "[[ 1.67371185 0.02013017]\n", 40 | " [ 0.40564826 2.17004352]\n", 41 | " [ 0.77627836 1.5179425 ]\n", 42 | " [ 2.66991709 0.00940262]]\n", 43 | "[[ 1.32014421 0.40901559 2.10322743 1.99087019 1.29852389]\n", 44 | " [ 0.25859086 2.59911791 0.00488947 0.37089193 0.14622829]]\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "W = model.fit_transform(X)\n", 50 | "H = model.components_\n", 51 | "print W\n", 52 | "print H" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.6.4" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 1 86 | } 87 | -------------------------------------------------------------------------------- /natural-language-processing/tf-idf.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "文本挖掘预处理之TF-IDF https://www.cnblogs.com/pinard/p/6693230.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | " (0, 4)\t0.4424621378947393\n", 26 | " (0, 15)\t0.697684463383976\n", 27 | " (0, 3)\t0.348842231691988\n", 28 | " (0, 16)\t0.4424621378947393\n", 29 | " (1, 3)\t0.3574550433419527\n", 30 | " (1, 14)\t0.45338639737285463\n", 31 | " (1, 6)\t0.3574550433419527\n", 32 | " (1, 2)\t0.45338639737285463\n", 33 | " (1, 9)\t0.45338639737285463\n", 34 | " (1, 5)\t0.3574550433419527\n", 35 | " (2, 7)\t0.5\n", 36 | " (2, 12)\t0.5\n", 37 | " (2, 0)\t0.5\n", 38 | " (2, 1)\t0.5\n", 39 | " (3, 15)\t0.2811316284405006\n", 40 | " (3, 6)\t0.2811316284405006\n", 41 | " (3, 5)\t0.2811316284405006\n", 42 | " (3, 13)\t0.3565798233381452\n", 43 | " (3, 17)\t0.3565798233381452\n", 44 | " (3, 18)\t0.3565798233381452\n", 45 | " (3, 11)\t0.3565798233381452\n", 46 | " (3, 8)\t0.3565798233381452\n", 47 | " (3, 10)\t0.3565798233381452\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "from sklearn.feature_extraction.text import TfidfTransformer \n", 53 | "from sklearn.feature_extraction.text import CountVectorizer \n", 54 | "\n", 55 | "corpus=[\"I come to China to travel\", \n", 56 | " \"This is a car polupar in China\", \n", 57 | " \"I love tea and Apple \", \n", 58 | " \"The work is to write some papers in science\"] \n", 59 | "\n", 60 | "vectorizer=CountVectorizer()\n", 61 | "\n", 62 | "transformer = TfidfTransformer()\n", 63 | "tfidf = transformer.fit_transform(vectorizer.fit_transform(corpus)) \n", 64 | "print (tfidf)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 4, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | " (0, 4)\t0.4424621378947393\n", 77 | " (0, 15)\t0.697684463383976\n", 78 | " (0, 3)\t0.348842231691988\n", 79 | " (0, 16)\t0.4424621378947393\n", 80 | " (1, 3)\t0.3574550433419527\n", 81 | " (1, 14)\t0.45338639737285463\n", 82 | " (1, 6)\t0.3574550433419527\n", 83 | " (1, 2)\t0.45338639737285463\n", 84 | " (1, 9)\t0.45338639737285463\n", 85 | " (1, 5)\t0.3574550433419527\n", 86 | " (2, 7)\t0.5\n", 87 | " (2, 12)\t0.5\n", 88 | " (2, 0)\t0.5\n", 89 | " (2, 1)\t0.5\n", 90 | " (3, 15)\t0.2811316284405006\n", 91 | " (3, 6)\t0.2811316284405006\n", 92 | " (3, 5)\t0.2811316284405006\n", 93 | " (3, 13)\t0.3565798233381452\n", 94 | " (3, 17)\t0.3565798233381452\n", 95 | " (3, 18)\t0.3565798233381452\n", 96 | " (3, 11)\t0.3565798233381452\n", 97 | " (3, 8)\t0.3565798233381452\n", 98 | " (3, 10)\t0.3565798233381452\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "from sklearn.feature_extraction.text import TfidfVectorizer\n", 104 | "tfidf2 = TfidfVectorizer()\n", 105 | "re = tfidf2.fit_transform(corpus)\n", 106 | "print (re)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.6.4" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 2 138 | } 139 | -------------------------------------------------------------------------------- /natural-language-processing/word2vec.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n", 8 | "\n", 9 | "https://www.cnblogs.com/pinard\n", 10 | "\n", 11 | "Permission given to modify the code as long as you keep this declaration at the top\n", 12 | "\n", 13 | "用gensim学习word2vec https://www.cnblogs.com/pinard/p/7278324.html" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "# -*- coding: utf-8 -*-\n", 23 | "\n", 24 | "import jieba\n", 25 | "import jieba.analyse\n", 26 | "\n", 27 | "jieba.suggest_freq('沙瑞金', True)\n", 28 | "jieba.suggest_freq('田国富', True)\n", 29 | "jieba.suggest_freq('高育良', True)\n", 30 | "jieba.suggest_freq('侯亮平', True)\n", 31 | "jieba.suggest_freq('钟小艾', True)\n", 32 | "jieba.suggest_freq('陈岩石', True)\n", 33 | "jieba.suggest_freq('欧阳菁', True)\n", 34 | "jieba.suggest_freq('易学习', True)\n", 35 | "jieba.suggest_freq('王大路', True)\n", 36 | "jieba.suggest_freq('蔡成功', True)\n", 37 | "jieba.suggest_freq('孙连城', True)\n", 38 | "jieba.suggest_freq('季昌明', True)\n", 39 | "jieba.suggest_freq('丁义珍', True)\n", 40 | "jieba.suggest_freq('郑西坡', True)\n", 41 | "jieba.suggest_freq('赵东来', True)\n", 42 | "jieba.suggest_freq('高小琴', True)\n", 43 | "jieba.suggest_freq('赵瑞龙', True)\n", 44 | "jieba.suggest_freq('林华华', True)\n", 45 | "jieba.suggest_freq('陆亦可', True)\n", 46 | "jieba.suggest_freq('刘新建', True)\n", 47 | "jieba.suggest_freq('刘庆祝', True)\n", 48 | "\n", 49 | "with open('./in_the_name_of_people.txt') as f:\n", 50 | " document = f.read()\n", 51 | " \n", 52 | " #document_decode = document.decode('GBK')\n", 53 | " \n", 54 | " document_cut = jieba.cut(document)\n", 55 | " #print ' '.join(jieba_cut) //如果打印结果,则分词效果消失,后面的result无法显示\n", 56 | " result = ' '.join(document_cut)\n", 57 | " result = result.encode('utf-8')\n", 58 | " with open('./in_the_name_of_people_segment.txt', 'w') as f2:\n", 59 | " f2.write(result)\n", 60 | "f.close()\n", 61 | "f2.close()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 64, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "# import modules & set up logging\n", 71 | "import logging\n", 72 | "import os\n", 73 | "from gensim.models import word2vec\n", 74 | "\n", 75 | "logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)\n", 76 | "\n", 77 | "sentences = word2vec.LineSentence('./in_the_name_of_people_segment.txt') \n", 78 | "\n", 79 | "model = word2vec.Word2Vec(sentences, hs=1,min_count=1,window=3,size=100) " 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 65, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "祁同伟 0.977946519852\n", 92 | "赵东来 0.969956457615\n", 93 | "侯亮平 0.96396112442\n", 94 | "沙瑞金 0.959288835526\n", 95 | "易学习 0.95690870285\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "req_count = 5\n", 101 | "for key in model.wv.similar_by_word('李达康'.decode('utf-8'), topn =100):\n", 102 | " if len(key[0])==3:\n", 103 | " req_count -= 1\n", 104 | " print key[0], key[1]\n", 105 | " if req_count == 0:\n", 106 | " break;" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 66, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "李达康 0.969956457615\n", 119 | "陆亦可 0.969528734684\n", 120 | "赵瑞龙 0.966745913029\n", 121 | "祁同伟 0.965694904327\n", 122 | "蔡成功 0.961919903755\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "req_count = 5\n", 128 | "for key in model.wv.similar_by_word('赵东来'.decode('utf-8'), topn =100):\n", 129 | " if len(key[0])==3:\n", 130 | " req_count -= 1\n", 131 | " print key[0], key[1]\n", 132 | " if req_count == 0:\n", 133 | " break;" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 67, 139 | "metadata": { 140 | "scrolled": true 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "沙瑞金 0.961137533188\n", 148 | "开玩笑 0.935248732567\n", 149 | "祁同伟 0.931046843529\n", 150 | "李达康 0.921290516853\n", 151 | "打电话 0.916399776936\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "req_count = 5\n", 157 | "for key in model.wv.similar_by_word('高育良'.decode('utf-8'), topn =100):\n", 158 | " if len(key[0])==3:\n", 159 | " req_count -= 1\n", 160 | " print key[0], key[1]\n", 161 | " if req_count == 0:\n", 162 | " break;" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 63, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "高育良 0.967257142067\n", 175 | "李达康 0.959131598473\n", 176 | "田国富 0.953414440155\n", 177 | "易学习 0.943500876427\n", 178 | "祁同伟 0.942932963371\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "req_count = 5\n", 184 | "for key in model.wv.similar_by_word('沙瑞金'.decode('utf-8'), topn =100):\n", 185 | " if len(key[0])==3:\n", 186 | " req_count -= 1\n", 187 | " print key[0], key[1]\n", 188 | " if req_count == 0:\n", 189 | " break;" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 77, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "0.961137455325\n", 202 | "0.935589365706\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print model.wv.similarity('沙瑞金'.decode('utf-8'), '高育良'.decode('utf-8'))\n", 208 | "print model.wv.similarity('李达康'.decode('utf-8'), '王大路'.decode('utf-8'))" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 76, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "刘庆祝\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "print model.wv.doesnt_match(u\"沙瑞金 高育良 李达康 刘庆祝\".split())" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": { 232 | "collapsed": true 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | " " 237 | ] 238 | } 239 | ], 240 | "metadata": { 241 | "kernelspec": { 242 | "display_name": "Python 3", 243 | "language": "python", 244 | "name": "python3" 245 | }, 246 | "language_info": { 247 | "codemirror_mode": { 248 | "name": "ipython", 249 | "version": 3 250 | }, 251 | "file_extension": ".py", 252 | "mimetype": "text/x-python", 253 | "name": "python", 254 | "nbconvert_exporter": "python", 255 | "pygments_lexer": "ipython3", 256 | "version": "3.6.4" 257 | } 258 | }, 259 | "nbformat": 4, 260 | "nbformat_minor": 1 261 | } 262 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 刘建平Pinard的博客配套代码 2 | 3 | http://www.cnblogs.com/pinard 刘建平Pinard 4 | 5 | 之前不少朋友反应我博客中的代码都是连续的片段,不好学习,因此这里把文章和代码做一个整理。 6 | 代码有部分来源于网络,已加上相关方版权信息。部分为自己原创,已加上我的版权信息。 7 | 8 | ## 目录 9 | 10 | * [机器学习基础与回归算法](#2) 11 | 12 | * [机器学习分类算法](#3) 13 | 14 | * [机器学习聚类算法](#4) 15 | 16 | * [机器学习降维算法](#5) 17 | 18 | * [机器学习集成学习算法](#6) 19 | 20 | * [数学统计学](#7) 21 | 22 | * [机器学习关联算法](#8) 23 | 24 | * [机器学习推荐算法](#9) 25 | 26 | * [深度学习算法](#10) 27 | 28 | * [自然语言处理算法](#11) 29 | 30 | * [强化学习算法](#1) 31 | 32 | * [特征工程与算法落地](#12) 33 | 34 | ## 注意 35 | 36 | 2016-2017年写的博客使用的python版本是2.7, 2018年因为TensorFlow对Python3的一些要求,所以写博客使用的Python版本是3.6。少部分2016,2017年的博客代码无法找到,重新用Python3.6跑过上传,因此可能会出现和博客中代码稍有不一致的地方,主要涉及到print的语法和range的用法,若遇到问题,稍微修改即可跑通。 37 | 38 | ## [赞助我](#13) 39 | 40 |

强化学习文章与代码::

41 | 42 | |文章 | 代码| 43 | ---|--- 44 | [强化学习(一)模型基础](https://www.cnblogs.com/pinard/p/9385570.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/introduction.py) 45 | [强化学习(二)马尔科夫决策过程(MDP)](https://www.cnblogs.com/pinard/p/9426283.html) | 无 46 | [强化学习(三)用动态规划(DP)求解](https://www.cnblogs.com/pinard/p/9463815.html) | 无 47 | [强化学习(四)用蒙特卡罗法(MC)求解](https://www.cnblogs.com/pinard/p/9492980.html) | 无 48 | [强化学习(五)用时序差分法(TD)求解](https://www.cnblogs.com/pinard/p/9529828.html) | 无 49 | [强化学习(六)时序差分在线控制算法SARSA](https://www.cnblogs.com/pinard/p/9614290.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/sarsa_windy_world.py) 50 | [强化学习(七)时序差分离线控制算法Q-Learning](https://www.cnblogs.com/pinard/p/9669263.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/q_learning_windy_world.py) 51 | [强化学习(八)价值函数的近似表示与Deep Q-Learning](https://www.cnblogs.com/pinard/p/9714655.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/dqn.py) 52 | [强化学习(九)Deep Q-Learning进阶之Nature DQN](https://www.cnblogs.com/pinard/p/9756075.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/nature_dqn.py) 53 | [强化学习(十)Double DQN (DDQN)](https://www.cnblogs.com/pinard/p/9778063.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/ddqn.py) 54 | [强化学习(十一) Prioritized Replay DQN](https://www.cnblogs.com/pinard/p/9797695.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/ddqn_prioritised_replay.py) 55 | [强化学习(十二) Dueling DQN](https://www.cnblogs.com/pinard/p/9923859.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/duel_dqn.py) 56 | [强化学习(十三) 策略梯度(Policy Gradient)](https://www.cnblogs.com/pinard/p/10137696.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/policy_gradient.py) 57 | [强化学习(十四) Actor-Critic](https://www.cnblogs.com/pinard/p/10272023.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/actor_critic.py) 58 | [强化学习(十五) A3C](https://www.cnblogs.com/pinard/p/10334127.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/a3c.py) 59 | [强化学习(十六) 深度确定性策略梯度(DDPG)](https://www.cnblogs.com/pinard/p/10345762.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/reinforcement-learning/ddpg.py) 60 | [强化学习(十七) 基于模型的强化学习与Dyna算法框架](https://www.cnblogs.com/pinard/p/10384424.html) | 无 61 | [强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)](https://www.cnblogs.com/pinard/p/10470571.html) | 无 62 | [强化学习(十九) AlphaGo Zero强化学习原理](https://www.cnblogs.com/pinard/p/10609228.html) | 无 63 | 64 | 65 |

机器学习基础与回归算法文章与代码:

66 | 67 | |文章 | 代码| 68 | ---|--- 69 | [梯度下降(Gradient Descent)小结](https://www.cnblogs.com/pinard/p/5970503.html) | 无 70 | [最小二乘法小结](https://www.cnblogs.com/pinard/p/5976811.html) |无 71 | [交叉验证(Cross Validation)原理小结](https://www.cnblogs.com/pinard/p/5992719.html) | 无 72 | [精确率与召回率,RoC曲线与PR曲线](https://www.cnblogs.com/pinard/p/5993450.html) |无 73 | [线性回归原理小结](https://www.cnblogs.com/pinard/p/6004041.html) |无 74 | [机器学习研究与开发平台的选择](https://www.cnblogs.com/pinard/p/6007200.html) | 无 75 | [scikit-learn 和pandas 基于windows单机机器学习环境的搭建](https://www.cnblogs.com/pinard/p/6013484.html) |无 76 | [用scikit-learn和pandas学习线性回归](https://www.cnblogs.com/pinard/p/6016029.html) |[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/linear-regression.ipynb) 77 | [Lasso回归算法: 坐标轴下降法与最小角回归法小结](https://www.cnblogs.com/pinard/p/6018889.html) | 无 78 | [用scikit-learn和pandas学习Ridge回归](https://www.cnblogs.com/pinard/p/6023000.html) | [代码1](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/ridge_regression_1.ipynb) [代码2](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/ridge_regression.ipynb) 79 | [scikit-learn 线性回归算法库小结](https://www.cnblogs.com/pinard/p/6026343.html)|无 80 | [异常点检测算法小结](https://www.cnblogs.com/pinard/p/9314198.html)|无 81 | 82 |

机器学习分类算法文章与代码:

83 | 84 | |文章 | 代码| 85 | ---|--- 86 | [逻辑回归原理小结](https://www.cnblogs.com/pinard/p/6029432.html) |无 87 | [scikit-learn 逻辑回归类库使用小结](https://www.cnblogs.com/pinard/p/6035872.html) |无 88 | [感知机原理小结](https://www.cnblogs.com/pinard/p/6042320.html) |无 89 | [决策树算法原理(上)](https://www.cnblogs.com/pinard/p/6050306.html) |无 90 | [决策树算法原理(下)](https://www.cnblogs.com/pinard/p/6053344.html)|无 91 | [scikit-learn决策树算法类库使用小结](https://www.cnblogs.com/pinard/p/6056319.html) |[代码1](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/decision_tree_classifier.ipynb) [代码2](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/decision_tree_classifier_1.ipynb) 92 | [K近邻法(KNN)原理小结](https://www.cnblogs.com/pinard/p/6061661.html) |无 93 | [scikit-learn K近邻法类库使用小结](https://www.cnblogs.com/pinard/p/6065607.html) |[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/knn_classifier.ipynb) 94 | [朴素贝叶斯算法原理小结](https://www.cnblogs.com/pinard/p/6069267.html) |无 95 | [scikit-learn 朴素贝叶斯类库使用小结](https://www.cnblogs.com/pinard/p/6074222.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/native_bayes.ipynb) 96 | [最大熵模型原理小结](https://www.cnblogs.com/pinard/p/6093948.html)|无 97 | [支持向量机原理(一) 线性支持向量机](https://www.cnblogs.com/pinard/p/6097604.html)|无 98 | [支持向量机原理(二) 线性支持向量机的软间隔最大化模型](https://www.cnblogs.com/pinard/p/6100722.html)|无 99 | [支持向量机原理(三)线性不可分支持向量机与核函数](https://www.cnblogs.com/pinard/p/6103615.html)|无 100 | [支持向量机原理(四)SMO算法原理](https://www.cnblogs.com/pinard/p/6111471.html)|无 101 | [支持向量机原理(五)线性支持回归](https://www.cnblogs.com/pinard/p/6113120.html)|无 102 | [scikit-learn 支持向量机算法库使用小结](https://www.cnblogs.com/pinard/p/6117515.html)|无 103 | [支持向量机高斯核调参小结](https://www.cnblogs.com/pinard/p/6126077.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/svm_classifier.ipynb) 104 | 105 |

数学统计学文章与代码:

106 | 107 | |文章 | 代码| 108 | ---|--- 109 | [机器学习算法的随机数据生成](https://www.cnblogs.com/pinard/p/6047802.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/mathematics/random_data_generation.ipynb) 110 | [MCMC(一)蒙特卡罗方法](https://www.cnblogs.com/pinard/p/6625739.html)|无 111 | [MCMC(二)马尔科夫链](https://www.cnblogs.com/pinard/p/6632399.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/mathematics/mcmc_2.ipynb) 112 | [MCMC(三)MCMC采样和M-H采样](https://www.cnblogs.com/pinard/p/6638955.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/mathematics/mcmc_3_4.ipynb) 113 | [MCMC(四)Gibbs采样](https://www.cnblogs.com/pinard/p/6645766.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/mathematics/mcmc_3_4.ipynb) 114 | [机器学习中的矩阵向量求导(一) 求导定义与求导布局](https://www.cnblogs.com/pinard/p/10750718.html)|无 115 | [机器学习中的矩阵向量求导(二) 矩阵向量求导之定义法](https://www.cnblogs.com/pinard/p/10773942.html)|无 116 | [机器学习中的矩阵向量求导(三) 矩阵向量求导之微分法](https://www.cnblogs.com/pinard/p/10791506.html)|无 117 | [机器学习中的矩阵向量求导(四) 矩阵向量求导链式法则](https://www.cnblogs.com/pinard/p/10825264.html)|无 118 | [机器学习中的矩阵向量求导(五) 矩阵对矩阵的求导](https://www.cnblogs.com/pinard/p/10930902.html)|无 119 | 120 | 121 |

机器学习集成学习文章与代码:

122 | 123 | |文章 | 代码| 124 | ---|--- 125 | [集成学习原理小结](https://www.cnblogs.com/pinard/p/6131423.html) | 无 126 | [集成学习之Adaboost算法原理小结](https://www.cnblogs.com/pinard/p/6133937.html) | 无 127 | [scikit-learn Adaboost类库使用小结](https://www.cnblogs.com/pinard/p/6136914.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/ensemble-learning/adaboost-classifier.ipynb) 128 | [梯度提升树(GBDT)原理小结](https://www.cnblogs.com/pinard/p/6140514.html) | 无 129 | [scikit-learn 梯度提升树(GBDT)调参小结](https://www.cnblogs.com/pinard/p/6143927.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/ensemble-learning/gbdt_classifier.ipynb) 130 | [Bagging与随机森林算法原理小结](https://www.cnblogs.com/pinard/p/6156009.html) | 无 131 | [scikit-learn随机森林调参小结](https://www.cnblogs.com/pinard/p/6160412.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/ensemble-learning/random_forest_classifier.ipynb) 132 | [XGBoost算法原理小结](https://www.cnblogs.com/pinard/p/10979808.html) | 无 133 | [XGBoost类库使用小结](https://www.cnblogs.com/pinard/p/11114748.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/ensemble-learning/xgboost-example.ipynb) 134 | 135 | 136 |

机器学习聚类算法文章与代码:

137 | 138 | |文章 | 代码| 139 | ---|--- 140 | [K-Means聚类算法原理](https://www.cnblogs.com/pinard/p/6164214.html)|无 141 | [用scikit-learn学习K-Means聚类](https://www.cnblogs.com/pinard/p/6169370.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/kmeans_cluster.ipynb) 142 | [BIRCH聚类算法原理](https://www.cnblogs.com/pinard/p/6179132.html)|无 143 | [用scikit-learn学习BIRCH聚类](https://www.cnblogs.com/pinard/p/6200579.html) | [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/birch_cluster.ipynb) 144 | [DBSCAN密度聚类算法](https://www.cnblogs.com/pinard/p/6208966.html)|无 145 | [用scikit-learn学习DBSCAN聚类](https://www.cnblogs.com/pinard/p/6217852.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/dbscan_cluster.ipynb) 146 | [谱聚类(spectral clustering)原理总结](https://www.cnblogs.com/pinard/p/6221564.html) |无 147 | [用scikit-learn学习谱聚类](https://www.cnblogs.com/pinard/p/6235920.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/spectral_cluster.ipynb) 148 | 149 |

机器学习降维算法文章与代码:

150 | 151 | |文章 | 代码| 152 | ---|--- 153 | [主成分分析(PCA)原理总结](https://www.cnblogs.com/pinard/p/6239403.html)|无 154 | [用scikit-learn学习主成分分析(PCA)](https://www.cnblogs.com/pinard/p/6243025.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/pca.ipynb) 155 | [线性判别分析LDA原理总结](https://www.cnblogs.com/pinard/p/6244265.html)|无 156 | [用scikit-learn进行LDA降维](https://www.cnblogs.com/pinard/p/6249328.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/lda.ipynb) 157 | [奇异值分解(SVD)原理与在降维中的应用](https://www.cnblogs.com/pinard/p/6251584.html)|无 158 | [局部线性嵌入(LLE)原理总结](https://www.cnblogs.com/pinard/p/6266408.html)|无 159 | [用scikit-learn研究局部线性嵌入(LLE)](https://www.cnblogs.com/pinard/p/6273377.html) |[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/lle.ipynb) 160 | 161 |

机器学习关联算法文章与代码:

162 | 163 | |文章 | 代码| 164 | ---|--- 165 | [典型关联分析(CCA)原理总结](https://www.cnblogs.com/pinard/p/6288716.html)|无 166 | [Apriori算法原理总结](https://www.cnblogs.com/pinard/p/6293298.html)|无 167 | [FP Tree算法原理总结](https://www.cnblogs.com/pinard/p/6307064.html)|无 168 | [PrefixSpan算法原理总结](https://www.cnblogs.com/pinard/p/6323182.html)|无 169 | [用Spark学习FP Tree算法和PrefixSpan算法](https://www.cnblogs.com/pinard/p/6340162.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/fp_tree_prefixspan.ipynb) 170 | [日志和告警数据挖掘经验谈](https://www.cnblogs.com/pinard/p/6039099.html) | 无 171 | 172 |

机器学习推荐算法文章与代码:

173 | 174 | |文章 | 代码| 175 | ---|--- 176 | [协同过滤推荐算法总结](https://www.cnblogs.com/pinard/p/6349233.html)|无 177 | [矩阵分解在协同过滤推荐算法中的应用](https://www.cnblogs.com/pinard/p/6351319.html)|无 178 | [SimRank协同过滤推荐算法](https://www.cnblogs.com/pinard/p/6362647.html)|无 179 | [用Spark学习矩阵分解推荐算法](https://www.cnblogs.com/pinard/p/6364932.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/matrix_factorization.ipynb) 180 | [分解机(Factorization Machines)推荐算法原理](https://www.cnblogs.com/pinard/p/6370127.html)|无 181 | [贝叶斯个性化排序(BPR)算法小结](https://www.cnblogs.com/pinard/p/9128682.html)|无 182 | [用tensorflow学习贝叶斯个性化排序(BPR)](https://www.cnblogs.com/pinard/p/9163481.html)| [代码](https://github.com/ljpzzz/machinelearning/blob/master/classic-machine-learning/bpr.ipynb) 183 | 184 |

深度学习算法文章与代码:

185 | 186 | |文章 | 代码| 187 | ---|--- 188 | [深度神经网络(DNN)模型与前向传播算法](https://www.cnblogs.com/pinard/p/6418668.html)|无 189 | [深度神经网络(DNN)反向传播算法(BP)](https://www.cnblogs.com/pinard/p/6422831.html)|无 190 | [深度神经网络(DNN)损失函数和激活函数的选择](https://www.cnblogs.com/pinard/p/6437495.html)|无 191 | [深度神经网络(DNN)的正则化](https://www.cnblogs.com/pinard/p/6472666.html)|无 192 | [卷积神经网络(CNN)模型结构](https://www.cnblogs.com/pinard/p/6483207.html)|无 193 | [卷积神经网络(CNN)前向传播算法](https://www.cnblogs.com/pinard/p/6489633.html)|无 194 | [卷积神经网络(CNN)反向传播算法](https://www.cnblogs.com/pinard/p/6494810.html)|无 195 | [循环神经网络(RNN)模型与前向反向传播算法](https://www.cnblogs.com/pinard/p/6509630.html)|无 196 | [LSTM模型与前向反向传播算法](https://www.cnblogs.com/pinard/p/6519110.html)|无 197 | [受限玻尔兹曼机(RBM)原理总结](https://www.cnblogs.com/pinard/p/6530523.html)|无 198 | 199 |

自然语言处理文章与代码:

200 | 201 | |文章 | 代码| 202 | ---|--- 203 | [文本挖掘的分词原理](https://www.cnblogs.com/pinard/p/6677078.html)|无 204 | [文本挖掘预处理之向量化与Hash Trick](https://www.cnblogs.com/pinard/p/6688348.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/hash_trick.ipynb) 205 | [文本挖掘预处理之TF-IDF](https://www.cnblogs.com/pinard/p/6693230.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/tf-idf.ipynb) 206 | [中文文本挖掘预处理流程总结](https://www.cnblogs.com/pinard/p/6744056.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/chinese_digging.ipynb) 207 | [英文文本挖掘预处理流程总结](https://www.cnblogs.com/pinard/p/6756534.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/english_digging.ipynb) 208 | [文本主题模型之潜在语义索引(LSI)](https://www.cnblogs.com/pinard/p/6805861.html)|无 209 | [文本主题模型之非负矩阵分解(NMF)](https://www.cnblogs.com/pinard/p/6812011.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/nmf.ipynb) 210 | [文本主题模型之LDA(一) LDA基础](https://www.cnblogs.com/pinard/p/6831308.html)|无 211 | [文本主题模型之LDA(二) LDA求解之Gibbs采样算法](https://www.cnblogs.com/pinard/p/6867828.html)|无 212 | [文本主题模型之LDA(三) LDA求解之变分推断EM算法](https://www.cnblogs.com/pinard/p/6873703.html)|无 213 | [用scikit-learn学习LDA主题模型](https://www.cnblogs.com/pinard/p/6908150.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/lda.ipynb) 214 | [EM算法原理总结](https://www.cnblogs.com/pinard/p/6912636.html)|无 215 | [隐马尔科夫模型HMM(一)HMM模型](https://www.cnblogs.com/pinard/p/6945257.html)|无 216 | [隐马尔科夫模型HMM(二)前向后向算法评估观察序列概率](https://www.cnblogs.com/pinard/p/6955871.html)|无 217 | [隐马尔科夫模型HMM(三)鲍姆-韦尔奇算法求解HMM参数](https://www.cnblogs.com/pinard/p/6972299.html)|无 218 | [隐马尔科夫模型HMM(四)维特比算法解码隐藏状态序列](https://www.cnblogs.com/pinard/p/6991852.html)|无 219 | [用hmmlearn学习隐马尔科夫模型HMM](https://www.cnblogs.com/pinard/p/7001397.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/hmm.ipynb) 220 | [条件随机场CRF(一)从随机场到线性链条件随机场](https://www.cnblogs.com/pinard/p/7048333.html)|无 221 | [条件随机场CRF(二) 前向后向算法评估标记序列概率](https://www.cnblogs.com/pinard/p/7055072.html)|无 222 | [条件随机场CRF(三) 模型学习与维特比算法解码](https://www.cnblogs.com/pinard/p/7068574.html)|无 223 | [word2vec原理(一) CBOW与Skip-Gram模型基础](https://www.cnblogs.com/pinard/p/7160330.html)|无 224 | [word2vec原理(二) 基于Hierarchical Softmax的模型](https://www.cnblogs.com/pinard/p/7243513.html)|无 225 | [word2vec原理(三) 基于Negative Sampling的模型](https://www.cnblogs.com/pinard/p/7249903.html)|无 226 | [用gensim学习word2vec](https://www.cnblogs.com/pinard/p/7278324.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/natural-language-processing/word2vec.ipynb) 227 | 228 |

特征工程与算法落地文章与代码:

229 | 230 | |文章 | 代码| 231 | ---|--- 232 | [特征工程之特征选择](https://www.cnblogs.com/pinard/p/9032759.html)|无 233 | [特征工程之特征表达](https://www.cnblogs.com/pinard/p/9061549.html)|无 234 | [特征工程之特征预处理](https://www.cnblogs.com/pinard/p/9093890.html)|无 235 | [用PMML实现机器学习模型的跨平台上线](https://www.cnblogs.com/pinard/p/9220199.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/model-in-product/sklearn-jpmml) 236 | [tensorflow机器学习模型的跨平台上线](https://www.cnblogs.com/pinard/p/9251296.html)|[代码](https://github.com/ljpzzz/machinelearning/blob/master/model-in-product/tensorflow-java) 237 | 238 |

赞助我

239 | 240 | 你的支持是我写作的动力(1.微信/2.支付宝): 241 | 242 | ![微信赞助](./assert/invoice.bmp) 243 | 244 | 245 | ![支付宝赞助](./assert/invoice_ali.bmp) 246 | 247 | License MIT. 248 | -------------------------------------------------------------------------------- /reinforcement-learning/a3c.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ## reference from MorvanZhou's A3C code on Github, minor update:## 9 | ##https://github.com/MorvanZhou/Reinforcement-learning-with-tensorflow/blob/master/contents/10_A3C/A3C_discrete_action.py ## 10 | 11 | ## https://www.cnblogs.com/pinard/p/10334127.html ## 12 | ## 强化学习(十五) A3C ## 13 | 14 | import threading 15 | import tensorflow as tf 16 | import numpy as np 17 | import gym 18 | import os 19 | import shutil 20 | import matplotlib.pyplot as plt 21 | 22 | 23 | GAME = 'CartPole-v0' 24 | OUTPUT_GRAPH = True 25 | LOG_DIR = './log' 26 | N_WORKERS = 3 27 | MAX_GLOBAL_EP = 3000 28 | GLOBAL_NET_SCOPE = 'Global_Net' 29 | UPDATE_GLOBAL_ITER = 100 30 | GAMMA = 0.9 31 | ENTROPY_BETA = 0.001 32 | LR_A = 0.001 # learning rate for actor 33 | LR_C = 0.001 # learning rate for critic 34 | GLOBAL_RUNNING_R = [] 35 | GLOBAL_EP = 0 36 | STEP = 3000 # Step limitation in an episode 37 | TEST = 10 # The number of experiment test every 100 episode 38 | 39 | env = gym.make(GAME) 40 | N_S = env.observation_space.shape[0] 41 | N_A = env.action_space.n 42 | 43 | 44 | class ACNet(object): 45 | def __init__(self, scope, globalAC=None): 46 | 47 | if scope == GLOBAL_NET_SCOPE: # get global network 48 | with tf.variable_scope(scope): 49 | self.s = tf.placeholder(tf.float32, [None, N_S], 'S') 50 | self.a_params, self.c_params = self._build_net(scope)[-2:] 51 | else: # local net, calculate losses 52 | with tf.variable_scope(scope): 53 | self.s = tf.placeholder(tf.float32, [None, N_S], 'S') 54 | self.a_his = tf.placeholder(tf.int32, [None, ], 'A') 55 | self.v_target = tf.placeholder(tf.float32, [None, 1], 'Vtarget') 56 | 57 | self.a_prob, self.v, self.a_params, self.c_params = self._build_net(scope) 58 | 59 | td = tf.subtract(self.v_target, self.v, name='TD_error') 60 | with tf.name_scope('c_loss'): 61 | self.c_loss = tf.reduce_mean(tf.square(td)) 62 | 63 | with tf.name_scope('a_loss'): 64 | log_prob = tf.reduce_sum(tf.log(self.a_prob + 1e-5) * tf.one_hot(self.a_his, N_A, dtype=tf.float32), axis=1, keep_dims=True) 65 | exp_v = log_prob * tf.stop_gradient(td) 66 | entropy = -tf.reduce_sum(self.a_prob * tf.log(self.a_prob + 1e-5), 67 | axis=1, keep_dims=True) # encourage exploration 68 | self.exp_v = ENTROPY_BETA * entropy + exp_v 69 | self.a_loss = tf.reduce_mean(-self.exp_v) 70 | 71 | with tf.name_scope('local_grad'): 72 | self.a_grads = tf.gradients(self.a_loss, self.a_params) 73 | self.c_grads = tf.gradients(self.c_loss, self.c_params) 74 | 75 | with tf.name_scope('sync'): 76 | with tf.name_scope('pull'): 77 | self.pull_a_params_op = [l_p.assign(g_p) for l_p, g_p in zip(self.a_params, globalAC.a_params)] 78 | self.pull_c_params_op = [l_p.assign(g_p) for l_p, g_p in zip(self.c_params, globalAC.c_params)] 79 | with tf.name_scope('push'): 80 | self.update_a_op = OPT_A.apply_gradients(zip(self.a_grads, globalAC.a_params)) 81 | self.update_c_op = OPT_C.apply_gradients(zip(self.c_grads, globalAC.c_params)) 82 | 83 | def _build_net(self, scope): 84 | w_init = tf.random_normal_initializer(0., .1) 85 | with tf.variable_scope('actor'): 86 | l_a = tf.layers.dense(self.s, 200, tf.nn.relu6, kernel_initializer=w_init, name='la') 87 | a_prob = tf.layers.dense(l_a, N_A, tf.nn.softmax, kernel_initializer=w_init, name='ap') 88 | with tf.variable_scope('critic'): 89 | l_c = tf.layers.dense(self.s, 100, tf.nn.relu6, kernel_initializer=w_init, name='lc') 90 | v = tf.layers.dense(l_c, 1, kernel_initializer=w_init, name='v') # state value 91 | a_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/actor') 92 | c_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/critic') 93 | return a_prob, v, a_params, c_params 94 | 95 | def update_global(self, feed_dict): # run by a local 96 | SESS.run([self.update_a_op, self.update_c_op], feed_dict) # local grads applies to global net 97 | 98 | def pull_global(self): # run by a local 99 | SESS.run([self.pull_a_params_op, self.pull_c_params_op]) 100 | 101 | def choose_action(self, s): # run by a local 102 | prob_weights = SESS.run(self.a_prob, feed_dict={self.s: s[np.newaxis, :]}) 103 | action = np.random.choice(range(prob_weights.shape[1]), 104 | p=prob_weights.ravel()) # select action w.r.t the actions prob 105 | return action 106 | 107 | 108 | class Worker(object): 109 | def __init__(self, name, globalAC): 110 | self.env = gym.make(GAME).unwrapped 111 | self.name = name 112 | self.AC = ACNet(name, globalAC) 113 | 114 | def work(self): 115 | global GLOBAL_RUNNING_R, GLOBAL_EP 116 | total_step = 1 117 | buffer_s, buffer_a, buffer_r = [], [], [] 118 | while not COORD.should_stop() and GLOBAL_EP < MAX_GLOBAL_EP: 119 | s = self.env.reset() 120 | ep_r = 0 121 | while True: 122 | # if self.name == 'W_0': 123 | # self.env.render() 124 | a = self.AC.choose_action(s) 125 | s_, r, done, info = self.env.step(a) 126 | if done: r = -5 127 | ep_r += r 128 | buffer_s.append(s) 129 | buffer_a.append(a) 130 | buffer_r.append(r) 131 | 132 | if total_step % UPDATE_GLOBAL_ITER == 0 or done: # update global and assign to local net 133 | if done: 134 | v_s_ = 0 # terminal 135 | else: 136 | v_s_ = SESS.run(self.AC.v, {self.AC.s: s_[np.newaxis, :]})[0, 0] 137 | buffer_v_target = [] 138 | for r in buffer_r[::-1]: # reverse buffer r 139 | v_s_ = r + GAMMA * v_s_ 140 | buffer_v_target.append(v_s_) 141 | buffer_v_target.reverse() 142 | 143 | buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.array(buffer_a), np.vstack(buffer_v_target) 144 | feed_dict = { 145 | self.AC.s: buffer_s, 146 | self.AC.a_his: buffer_a, 147 | self.AC.v_target: buffer_v_target, 148 | } 149 | self.AC.update_global(feed_dict) 150 | 151 | buffer_s, buffer_a, buffer_r = [], [], [] 152 | self.AC.pull_global() 153 | 154 | s = s_ 155 | total_step += 1 156 | if done: 157 | if len(GLOBAL_RUNNING_R) == 0: # record running episode reward 158 | GLOBAL_RUNNING_R.append(ep_r) 159 | else: 160 | GLOBAL_RUNNING_R.append(0.99 * GLOBAL_RUNNING_R[-1] + 0.01 * ep_r) 161 | print( 162 | self.name, 163 | "Ep:", GLOBAL_EP, 164 | "| Ep_r: %i" % GLOBAL_RUNNING_R[-1], 165 | ) 166 | GLOBAL_EP += 1 167 | break 168 | 169 | if __name__ == "__main__": 170 | SESS = tf.Session() 171 | 172 | with tf.device("/cpu:0"): 173 | OPT_A = tf.train.RMSPropOptimizer(LR_A, name='RMSPropA') 174 | OPT_C = tf.train.RMSPropOptimizer(LR_C, name='RMSPropC') 175 | GLOBAL_AC = ACNet(GLOBAL_NET_SCOPE) # we only need its params 176 | workers = [] 177 | # Create worker 178 | for i in range(N_WORKERS): 179 | i_name = 'W_%i' % i # worker name 180 | workers.append(Worker(i_name, GLOBAL_AC)) 181 | 182 | COORD = tf.train.Coordinator() 183 | SESS.run(tf.global_variables_initializer()) 184 | 185 | if OUTPUT_GRAPH: 186 | if os.path.exists(LOG_DIR): 187 | shutil.rmtree(LOG_DIR) 188 | tf.summary.FileWriter(LOG_DIR, SESS.graph) 189 | 190 | worker_threads = [] 191 | for worker in workers: 192 | job = lambda: worker.work() 193 | t = threading.Thread(target=job) 194 | t.start() 195 | worker_threads.append(t) 196 | COORD.join(worker_threads) 197 | 198 | testWorker = Worker("test", GLOBAL_AC) 199 | testWorker.AC.pull_global() 200 | 201 | total_reward = 0 202 | for i in range(TEST): 203 | state = env.reset() 204 | for j in range(STEP): 205 | env.render() 206 | action = testWorker.AC.choose_action(state) # direct action for test 207 | state, reward, done, _ = env.step(action) 208 | total_reward += reward 209 | if done: 210 | break 211 | ave_reward = total_reward / TEST 212 | print('episode: ', GLOBAL_EP, 'Evaluation Average Reward:', ave_reward) 213 | 214 | plt.plot(np.arange(len(GLOBAL_RUNNING_R)), GLOBAL_RUNNING_R) 215 | plt.xlabel('step') 216 | plt.ylabel('Total moving reward') 217 | plt.show() -------------------------------------------------------------------------------- /reinforcement-learning/actor_critic.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ## https://www.cnblogs.com/pinard/p/10272023.html ## 9 | ## 强化学习(十四) Actor-Critic ## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters 18 | GAMMA = 0.95 # discount factor 19 | LEARNING_RATE=0.01 20 | 21 | class Actor(): 22 | def __init__(self, env, sess): 23 | # init some parameters 24 | self.time_step = 0 25 | self.state_dim = env.observation_space.shape[0] 26 | self.action_dim = env.action_space.n 27 | self.create_softmax_network() 28 | 29 | # Init session 30 | self.session = sess 31 | self.session.run(tf.global_variables_initializer()) 32 | 33 | def create_softmax_network(self): 34 | # network weights 35 | W1 = self.weight_variable([self.state_dim, 20]) 36 | b1 = self.bias_variable([20]) 37 | W2 = self.weight_variable([20, self.action_dim]) 38 | b2 = self.bias_variable([self.action_dim]) 39 | # input layer 40 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 41 | self.tf_acts = tf.placeholder(tf.int32, [None,2], name="actions_num") 42 | self.td_error = tf.placeholder(tf.float32, None, "td_error") # TD_error 43 | # hidden layers 44 | h_layer = tf.nn.relu(tf.matmul(self.state_input, W1) + b1) 45 | # softmax layer 46 | self.softmax_input = tf.matmul(h_layer, W2) + b2 47 | # softmax output 48 | self.all_act_prob = tf.nn.softmax(self.softmax_input, name='act_prob') 49 | 50 | self.neg_log_prob = tf.nn.softmax_cross_entropy_with_logits(logits=self.softmax_input, 51 | labels=self.tf_acts) 52 | self.exp = tf.reduce_mean(self.neg_log_prob * self.td_error) 53 | 54 | #这里需要最大化当前策略的价值,因此需要最大化self.exp,即最小化-self.exp 55 | self.train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(-self.exp) 56 | 57 | def weight_variable(self, shape): 58 | initial = tf.truncated_normal(shape) 59 | return tf.Variable(initial) 60 | 61 | def bias_variable(self, shape): 62 | initial = tf.constant(0.01, shape=shape) 63 | return tf.Variable(initial) 64 | 65 | def choose_action(self, observation): 66 | prob_weights = self.session.run(self.all_act_prob, feed_dict={self.state_input: observation[np.newaxis, :]}) 67 | action = np.random.choice(range(prob_weights.shape[1]), p=prob_weights.ravel()) # select action w.r.t the actions prob 68 | return action 69 | 70 | 71 | def learn(self, state, action, td_error): 72 | s = state[np.newaxis, :] 73 | one_hot_action = np.zeros(self.action_dim) 74 | one_hot_action[action] = 1 75 | a = one_hot_action[np.newaxis, :] 76 | # train on episode 77 | self.session.run(self.train_op, feed_dict={ 78 | self.state_input: s, 79 | self.tf_acts: a, 80 | self.td_error: td_error, 81 | }) 82 | 83 | EPSILON = 0.01 # final value of epsilon 84 | REPLAY_SIZE = 10000 # experience replay buffer size 85 | BATCH_SIZE = 32 # size of minibatch 86 | REPLACE_TARGET_FREQ = 10 # frequency to update target Q network 87 | 88 | class Critic(): 89 | def __init__(self, env, sess): 90 | # init some parameters 91 | self.time_step = 0 92 | self.epsilon = EPSILON 93 | self.state_dim = env.observation_space.shape[0] 94 | self.action_dim = env.action_space.n 95 | 96 | self.create_Q_network() 97 | self.create_training_method() 98 | 99 | # Init session 100 | self.session = sess 101 | self.session.run(tf.global_variables_initializer()) 102 | 103 | def create_Q_network(self): 104 | # network weights 105 | W1q = self.weight_variable([self.state_dim, 20]) 106 | b1q = self.bias_variable([20]) 107 | W2q = self.weight_variable([20, 1]) 108 | b2q = self.bias_variable([1]) 109 | self.state_input = tf.placeholder(tf.float32, [1, self.state_dim], "state") 110 | # hidden layers 111 | h_layerq = tf.nn.relu(tf.matmul(self.state_input, W1q) + b1q) 112 | # Q Value layer 113 | self.Q_value = tf.matmul(h_layerq, W2q) + b2q 114 | 115 | def create_training_method(self): 116 | self.next_value = tf.placeholder(tf.float32, [1,1], "v_next") 117 | self.reward = tf.placeholder(tf.float32, None, 'reward') 118 | 119 | with tf.variable_scope('squared_TD_error'): 120 | self.td_error = self.reward + GAMMA * self.next_value - self.Q_value 121 | self.loss = tf.square(self.td_error) 122 | with tf.variable_scope('train'): 123 | self.train_op = tf.train.AdamOptimizer(self.epsilon).minimize(self.loss) 124 | 125 | def train_Q_network(self, state, reward, next_state): 126 | s, s_ = state[np.newaxis, :], next_state[np.newaxis, :] 127 | v_ = self.session.run(self.Q_value, {self.state_input: s_}) 128 | td_error, _ = self.session.run([self.td_error, self.train_op], 129 | {self.state_input: s, self.next_value: v_, self.reward: reward}) 130 | return td_error 131 | 132 | def weight_variable(self,shape): 133 | initial = tf.truncated_normal(shape) 134 | return tf.Variable(initial) 135 | 136 | def bias_variable(self,shape): 137 | initial = tf.constant(0.01, shape = shape) 138 | return tf.Variable(initial) 139 | 140 | # Hyper Parameters 141 | ENV_NAME = 'CartPole-v0' 142 | EPISODE = 3000 # Episode limitation 143 | STEP = 3000 # Step limitation in an episode 144 | TEST = 10 # The number of experiment test every 100 episode 145 | 146 | def main(): 147 | # initialize OpenAI Gym env and dqn agent 148 | sess = tf.InteractiveSession() 149 | env = gym.make(ENV_NAME) 150 | actor = Actor(env, sess) 151 | critic = Critic(env, sess) 152 | 153 | for episode in range(EPISODE): 154 | # initialize task 155 | state = env.reset() 156 | # Train 157 | for step in range(STEP): 158 | action = actor.choose_action(state) # e-greedy action for train 159 | next_state,reward,done,_ = env.step(action) 160 | td_error = critic.train_Q_network(state, reward, next_state) # gradient = grad[r + gamma * V(s_) - V(s)] 161 | actor.learn(state, action, td_error) # true_gradient = grad[logPi(s,a) * td_error] 162 | state = next_state 163 | if done: 164 | break 165 | 166 | # Test every 100 episodes 167 | if episode % 100 == 0: 168 | total_reward = 0 169 | for i in range(TEST): 170 | state = env.reset() 171 | for j in range(STEP): 172 | env.render() 173 | action = actor.choose_action(state) # direct action for test 174 | state,reward,done,_ = env.step(action) 175 | total_reward += reward 176 | if done: 177 | break 178 | ave_reward = total_reward/TEST 179 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 180 | 181 | if __name__ == '__main__': 182 | main() -------------------------------------------------------------------------------- /reinforcement-learning/ddpg.py: -------------------------------------------------------------------------------- 1 | """ 2 | Deep Deterministic Policy Gradient (DDPG), Reinforcement Learning. 3 | DDPG is Actor Critic based algorithm. 4 | Pendulum example. 5 | View more on my tutorial page: https://morvanzhou.github.io/tutorials/ 6 | Using: 7 | tensorflow 1.0 8 | gym 0.8.0 9 | """ 10 | ####################################################################### 11 | # Copyright (C) # 12 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 13 | # https://www.cnblogs.com/pinard # 14 | # Permission given to modify the code as long as you keep this # 15 | # declaration at the top # 16 | ####################################################################### 17 | 18 | ## https://www.cnblogs.com/pinard/p/10345762.html.html ## 19 | ## 强化学习(十六) 深度确定性策略梯度(DDPG) ## 20 | 21 | 22 | import tensorflow as tf 23 | import numpy as np 24 | import gym 25 | import time 26 | 27 | 28 | ##################### hyper parameters #################### 29 | 30 | MAX_EPISODES = 2000 31 | MAX_EP_STEPS = 200 32 | LR_A = 0.001 # learning rate for actor 33 | LR_C = 0.002 # learning rate for critic 34 | GAMMA = 0.9 # reward discount 35 | TAU = 0.01 # soft replacement 36 | MEMORY_CAPACITY = 10000 37 | BATCH_SIZE = 32 38 | 39 | RENDER = False 40 | ENV_NAME = 'Pendulum-v0' 41 | 42 | ############################### DDPG #################################### 43 | 44 | class DDPG(object): 45 | def __init__(self, a_dim, s_dim, a_bound,): 46 | self.memory = np.zeros((MEMORY_CAPACITY, s_dim * 2 + a_dim + 1), dtype=np.float32) 47 | self.pointer = 0 48 | self.sess = tf.Session() 49 | 50 | self.a_dim, self.s_dim, self.a_bound = a_dim, s_dim, a_bound, 51 | self.S = tf.placeholder(tf.float32, [None, s_dim], 's') 52 | self.S_ = tf.placeholder(tf.float32, [None, s_dim], 's_') 53 | self.R = tf.placeholder(tf.float32, [None, 1], 'r') 54 | 55 | with tf.variable_scope('Actor'): 56 | self.a = self._build_a(self.S, scope='eval', trainable=True) 57 | a_ = self._build_a(self.S_, scope='target', trainable=False) 58 | with tf.variable_scope('Critic'): 59 | # assign self.a = a in memory when calculating q for td_error, 60 | # otherwise the self.a is from Actor when updating Actor 61 | q = self._build_c(self.S, self.a, scope='eval', trainable=True) 62 | q_ = self._build_c(self.S_, a_, scope='target', trainable=False) 63 | 64 | # networks parameters 65 | self.ae_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/eval') 66 | self.at_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/target') 67 | self.ce_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/eval') 68 | self.ct_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/target') 69 | 70 | # target net replacement 71 | self.soft_replace = [tf.assign(t, (1 - TAU) * t + TAU * e) 72 | for t, e in zip(self.at_params + self.ct_params, self.ae_params + self.ce_params)] 73 | 74 | q_target = self.R + GAMMA * q_ 75 | # in the feed_dic for the td_error, the self.a should change to actions in memory 76 | td_error = tf.losses.mean_squared_error(labels=q_target, predictions=q) 77 | self.ctrain = tf.train.AdamOptimizer(LR_C).minimize(td_error, var_list=self.ce_params) 78 | 79 | a_loss = - tf.reduce_mean(q) # maximize the q 80 | self.atrain = tf.train.AdamOptimizer(LR_A).minimize(a_loss, var_list=self.ae_params) 81 | 82 | self.sess.run(tf.global_variables_initializer()) 83 | 84 | def choose_action(self, s): 85 | return self.sess.run(self.a, {self.S: s[np.newaxis, :]})[0] 86 | 87 | def learn(self): 88 | # soft target replacement 89 | self.sess.run(self.soft_replace) 90 | 91 | indices = np.random.choice(MEMORY_CAPACITY, size=BATCH_SIZE) 92 | bt = self.memory[indices, :] 93 | bs = bt[:, :self.s_dim] 94 | ba = bt[:, self.s_dim: self.s_dim + self.a_dim] 95 | br = bt[:, -self.s_dim - 1: -self.s_dim] 96 | bs_ = bt[:, -self.s_dim:] 97 | 98 | self.sess.run(self.atrain, {self.S: bs}) 99 | self.sess.run(self.ctrain, {self.S: bs, self.a: ba, self.R: br, self.S_: bs_}) 100 | 101 | def store_transition(self, s, a, r, s_): 102 | transition = np.hstack((s, a, [r], s_)) 103 | index = self.pointer % MEMORY_CAPACITY # replace the old memory with new memory 104 | self.memory[index, :] = transition 105 | self.pointer += 1 106 | 107 | def _build_a(self, s, scope, trainable): 108 | with tf.variable_scope(scope): 109 | net = tf.layers.dense(s, 30, activation=tf.nn.relu, name='l1', trainable=trainable) 110 | a = tf.layers.dense(net, self.a_dim, activation=tf.nn.tanh, name='a', trainable=trainable) 111 | return tf.multiply(a, self.a_bound, name='scaled_a') 112 | 113 | def _build_c(self, s, a, scope, trainable): 114 | with tf.variable_scope(scope): 115 | n_l1 = 30 116 | w1_s = tf.get_variable('w1_s', [self.s_dim, n_l1], trainable=trainable) 117 | w1_a = tf.get_variable('w1_a', [self.a_dim, n_l1], trainable=trainable) 118 | b1 = tf.get_variable('b1', [1, n_l1], trainable=trainable) 119 | net = tf.nn.relu(tf.matmul(s, w1_s) + tf.matmul(a, w1_a) + b1) 120 | return tf.layers.dense(net, 1, trainable=trainable) # Q(s,a) 121 | 122 | ############################### training #################################### 123 | 124 | env = gym.make(ENV_NAME) 125 | env = env.unwrapped 126 | env.seed(1) 127 | 128 | s_dim = env.observation_space.shape[0] 129 | a_dim = env.action_space.shape[0] 130 | a_bound = env.action_space.high 131 | 132 | ddpg = DDPG(a_dim, s_dim, a_bound) 133 | 134 | var = 3 # control exploration 135 | t1 = time.time() 136 | for episode in range(MAX_EPISODES): 137 | s = env.reset() 138 | ep_reward = 0 139 | for j in range(MAX_EP_STEPS): 140 | if RENDER: 141 | env.render() 142 | 143 | # Add exploration noise 144 | a = ddpg.choose_action(s) 145 | a = np.clip(np.random.normal(a, var), -2, 2) # add randomness to action selection for exploration 146 | s_, r, done, info = env.step(a) 147 | 148 | ddpg.store_transition(s, a, r / 10, s_) 149 | 150 | if ddpg.pointer > MEMORY_CAPACITY: 151 | var *= .9995 # decay the action randomness 152 | ddpg.learn() 153 | 154 | s = s_ 155 | ep_reward += r 156 | if j == MAX_EP_STEPS-1: 157 | print('Episode:', episode, ' Reward: %i' % int(ep_reward), 'Explore: %.2f' % var, ) 158 | # if ep_reward > -300:RENDER = True 159 | break 160 | if episode % 100 == 0: 161 | total_reward = 0 162 | for i in range(10): 163 | state = env.reset() 164 | for j in range(MAX_EP_STEPS): 165 | env.render() 166 | action = ddpg.choose_action(state) # direct action for test 167 | state,reward,done,_ = env.step(action) 168 | total_reward += reward 169 | if done: 170 | break 171 | ave_reward = total_reward/300 172 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 173 | print('Running time: ', time.time() - t1) -------------------------------------------------------------------------------- /reinforcement-learning/ddqn.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ## https://www.cnblogs.com/pinard/p/9778063.html ## 9 | ## 强化学习(十)Double DQN (DDQN) ## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters for DQN 18 | GAMMA = 0.9 # discount factor for target Q 19 | INITIAL_EPSILON = 0.5 # starting value of epsilon 20 | FINAL_EPSILON = 0.01 # final value of epsilon 21 | REPLAY_SIZE = 10000 # experience replay buffer size 22 | BATCH_SIZE = 32 # size of minibatch 23 | REPLACE_TARGET_FREQ = 10 # frequency to update target Q network 24 | 25 | class DQN(): 26 | # DQN Agent 27 | def __init__(self, env): 28 | # init experience replay 29 | self.replay_buffer = deque() 30 | # init some parameters 31 | self.time_step = 0 32 | self.epsilon = INITIAL_EPSILON 33 | self.state_dim = env.observation_space.shape[0] 34 | self.action_dim = env.action_space.n 35 | 36 | self.create_Q_network() 37 | self.create_training_method() 38 | 39 | # Init session 40 | self.session = tf.InteractiveSession() 41 | self.session.run(tf.global_variables_initializer()) 42 | 43 | def create_Q_network(self): 44 | # input layer 45 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 46 | # network weights 47 | with tf.variable_scope('current_net'): 48 | W1 = self.weight_variable([self.state_dim,20]) 49 | b1 = self.bias_variable([20]) 50 | W2 = self.weight_variable([20,self.action_dim]) 51 | b2 = self.bias_variable([self.action_dim]) 52 | 53 | # hidden layers 54 | h_layer = tf.nn.relu(tf.matmul(self.state_input,W1) + b1) 55 | # Q Value layer 56 | self.Q_value = tf.matmul(h_layer,W2) + b2 57 | 58 | with tf.variable_scope('target_net'): 59 | W1t = self.weight_variable([self.state_dim,20]) 60 | b1t = self.bias_variable([20]) 61 | W2t = self.weight_variable([20,self.action_dim]) 62 | b2t = self.bias_variable([self.action_dim]) 63 | 64 | # hidden layers 65 | h_layer_t = tf.nn.relu(tf.matmul(self.state_input,W1t) + b1t) 66 | # Q Value layer 67 | self.target_Q_value = tf.matmul(h_layer_t,W2t) + b2t 68 | 69 | t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net') 70 | e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='current_net') 71 | 72 | with tf.variable_scope('soft_replacement'): 73 | self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)] 74 | 75 | def create_training_method(self): 76 | self.action_input = tf.placeholder("float",[None,self.action_dim]) # one hot presentation 77 | self.y_input = tf.placeholder("float",[None]) 78 | Q_action = tf.reduce_sum(tf.multiply(self.Q_value,self.action_input),reduction_indices = 1) 79 | self.cost = tf.reduce_mean(tf.square(self.y_input - Q_action)) 80 | self.optimizer = tf.train.AdamOptimizer(0.0001).minimize(self.cost) 81 | 82 | def perceive(self,state,action,reward,next_state,done): 83 | one_hot_action = np.zeros(self.action_dim) 84 | one_hot_action[action] = 1 85 | self.replay_buffer.append((state,one_hot_action,reward,next_state,done)) 86 | if len(self.replay_buffer) > REPLAY_SIZE: 87 | self.replay_buffer.popleft() 88 | 89 | if len(self.replay_buffer) > BATCH_SIZE: 90 | self.train_Q_network() 91 | 92 | def train_Q_network(self): 93 | self.time_step += 1 94 | # Step 1: obtain random minibatch from replay memory 95 | minibatch = random.sample(self.replay_buffer,BATCH_SIZE) 96 | state_batch = [data[0] for data in minibatch] 97 | action_batch = [data[1] for data in minibatch] 98 | reward_batch = [data[2] for data in minibatch] 99 | next_state_batch = [data[3] for data in minibatch] 100 | 101 | # Step 2: calculate y 102 | y_batch = [] 103 | current_Q_batch = self.Q_value.eval(feed_dict={self.state_input: next_state_batch}) 104 | max_action_next = np.argmax(current_Q_batch, axis=1) 105 | target_Q_batch = self.target_Q_value.eval(feed_dict={self.state_input: next_state_batch}) 106 | 107 | for i in range(0,BATCH_SIZE): 108 | done = minibatch[i][4] 109 | if done: 110 | y_batch.append(reward_batch[i]) 111 | else : 112 | target_Q_value = target_Q_batch[i, max_action_next[i]] 113 | y_batch.append(reward_batch[i] + GAMMA * target_Q_value) 114 | 115 | self.optimizer.run(feed_dict={ 116 | self.y_input:y_batch, 117 | self.action_input:action_batch, 118 | self.state_input:state_batch 119 | }) 120 | 121 | def egreedy_action(self,state): 122 | Q_value = self.Q_value.eval(feed_dict = { 123 | self.state_input:[state] 124 | })[0] 125 | if random.random() <= self.epsilon: 126 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 127 | return random.randint(0,self.action_dim - 1) 128 | else: 129 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 130 | return np.argmax(Q_value) 131 | 132 | def action(self,state): 133 | return np.argmax(self.Q_value.eval(feed_dict = { 134 | self.state_input:[state] 135 | })[0]) 136 | 137 | def update_target_q_network(self, episode): 138 | # update target Q netowrk 139 | if episode % REPLACE_TARGET_FREQ == 0: 140 | self.session.run(self.target_replace_op) 141 | #print('episode '+str(episode) +', target Q network params replaced!') 142 | 143 | def weight_variable(self,shape): 144 | initial = tf.truncated_normal(shape) 145 | return tf.Variable(initial) 146 | 147 | def bias_variable(self,shape): 148 | initial = tf.constant(0.01, shape = shape) 149 | return tf.Variable(initial) 150 | # --------------------------------------------------------- 151 | # Hyper Parameters 152 | ENV_NAME = 'CartPole-v0' 153 | EPISODE = 3000 # Episode limitation 154 | STEP = 300 # Step limitation in an episode 155 | TEST = 5 # The number of experiment test every 100 episode 156 | 157 | def main(): 158 | # initialize OpenAI Gym env and dqn agent 159 | env = gym.make(ENV_NAME) 160 | agent = DQN(env) 161 | 162 | for episode in range(EPISODE): 163 | # initialize task 164 | state = env.reset() 165 | # Train 166 | for step in range(STEP): 167 | action = agent.egreedy_action(state) # e-greedy action for train 168 | next_state,reward,done,_ = env.step(action) 169 | # Define reward for agent 170 | reward = -1 if done else 0.1 171 | agent.perceive(state,action,reward,next_state,done) 172 | state = next_state 173 | if done: 174 | break 175 | # Test every 100 episodes 176 | if episode % 100 == 0: 177 | total_reward = 0 178 | for i in range(TEST): 179 | state = env.reset() 180 | for j in range(STEP): 181 | env.render() 182 | action = agent.action(state) # direct action for test 183 | state,reward,done,_ = env.step(action) 184 | total_reward += reward 185 | if done: 186 | break 187 | ave_reward = total_reward/TEST 188 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 189 | agent.update_target_q_network(episode) 190 | 191 | if __name__ == '__main__': 192 | main() -------------------------------------------------------------------------------- /reinforcement-learning/ddqn_prioritised_replay.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | # SumTree and Memory class are referred from https://github.com/MorvanZhou # 9 | 10 | ## https://www.cnblogs.com/pinard/p/9797695.html ## 11 | ## 强化学习(十一) Prioritized Replay DQN ## 12 | 13 | import gym 14 | import tensorflow as tf 15 | import numpy as np 16 | import random 17 | from collections import deque 18 | 19 | # Hyper Parameters for DQN 20 | GAMMA = 0.9 # discount factor for target Q 21 | INITIAL_EPSILON = 0.5 # starting value of epsilon 22 | FINAL_EPSILON = 0.01 # final value of epsilon 23 | REPLAY_SIZE = 10000 # experience replay buffer size 24 | BATCH_SIZE = 128 # size of minibatch 25 | REPLACE_TARGET_FREQ = 10 # frequency to update target Q network 26 | 27 | class SumTree(object): 28 | """ 29 | This SumTree code is a modified version and the original code is from: 30 | https://github.com/jaara/AI-blog/blob/master/SumTree.py 31 | Story data with its priority in the tree. 32 | """ 33 | data_pointer = 0 34 | 35 | def __init__(self, capacity): 36 | self.capacity = capacity # for all priority values 37 | self.tree = np.zeros(2 * capacity - 1) 38 | # [--------------Parent nodes-------------][-------leaves to recode priority-------] 39 | # size: capacity - 1 size: capacity 40 | self.data = np.zeros(capacity, dtype=object) # for all transitions 41 | # [--------------data frame-------------] 42 | # size: capacity 43 | 44 | def add(self, p, data): 45 | tree_idx = self.data_pointer + self.capacity - 1 46 | self.data[self.data_pointer] = data # update data_frame 47 | self.update(tree_idx, p) # update tree_frame 48 | 49 | self.data_pointer += 1 50 | if self.data_pointer >= self.capacity: # replace when exceed the capacity 51 | self.data_pointer = 0 52 | 53 | def update(self, tree_idx, p): 54 | change = p - self.tree[tree_idx] 55 | self.tree[tree_idx] = p 56 | # then propagate the change through tree 57 | while tree_idx != 0: # this method is faster than the recursive loop in the reference code 58 | tree_idx = (tree_idx - 1) // 2 59 | self.tree[tree_idx] += change 60 | 61 | def get_leaf(self, v): 62 | """ 63 | Tree structure and array storage: 64 | Tree index: 65 | 0 -> storing priority sum 66 | / \ 67 | 1 2 68 | / \ / \ 69 | 3 4 5 6 -> storing priority for transitions 70 | Array type for storing: 71 | [0,1,2,3,4,5,6] 72 | """ 73 | parent_idx = 0 74 | while True: # the while loop is faster than the method in the reference code 75 | cl_idx = 2 * parent_idx + 1 # this leaf's left and right kids 76 | cr_idx = cl_idx + 1 77 | if cl_idx >= len(self.tree): # reach bottom, end search 78 | leaf_idx = parent_idx 79 | break 80 | else: # downward search, always search for a higher priority node 81 | if v <= self.tree[cl_idx]: 82 | parent_idx = cl_idx 83 | else: 84 | v -= self.tree[cl_idx] 85 | parent_idx = cr_idx 86 | 87 | data_idx = leaf_idx - self.capacity + 1 88 | return leaf_idx, self.tree[leaf_idx], self.data[data_idx] 89 | 90 | @property 91 | def total_p(self): 92 | return self.tree[0] # the root 93 | 94 | 95 | class Memory(object): # stored as ( s, a, r, s_ ) in SumTree 96 | """ 97 | This Memory class is modified based on the original code from: 98 | https://github.com/jaara/AI-blog/blob/master/Seaquest-DDQN-PER.py 99 | """ 100 | epsilon = 0.01 # small amount to avoid zero priority 101 | alpha = 0.6 # [0~1] convert the importance of TD error to priority 102 | beta = 0.4 # importance-sampling, from initial value increasing to 1 103 | beta_increment_per_sampling = 0.001 104 | abs_err_upper = 1. # clipped abs error 105 | 106 | def __init__(self, capacity): 107 | self.tree = SumTree(capacity) 108 | 109 | def store(self, transition): 110 | max_p = np.max(self.tree.tree[-self.tree.capacity:]) 111 | if max_p == 0: 112 | max_p = self.abs_err_upper 113 | self.tree.add(max_p, transition) # set the max p for new p 114 | 115 | def sample(self, n): 116 | b_idx, b_memory, ISWeights = np.empty((n,), dtype=np.int32), np.empty((n, self.tree.data[0].size)), np.empty((n, 1)) 117 | pri_seg = self.tree.total_p / n # priority segment 118 | self.beta = np.min([1., self.beta + self.beta_increment_per_sampling]) # max = 1 119 | 120 | min_prob = np.min(self.tree.tree[-self.tree.capacity:]) / self.tree.total_p # for later calculate ISweight 121 | if min_prob == 0: 122 | min_prob = 0.00001 123 | for i in range(n): 124 | a, b = pri_seg * i, pri_seg * (i + 1) 125 | v = np.random.uniform(a, b) 126 | idx, p, data = self.tree.get_leaf(v) 127 | prob = p / self.tree.total_p 128 | ISWeights[i, 0] = np.power(prob/min_prob, -self.beta) 129 | b_idx[i], b_memory[i, :] = idx, data 130 | return b_idx, b_memory, ISWeights 131 | 132 | def batch_update(self, tree_idx, abs_errors): 133 | abs_errors += self.epsilon # convert to abs and avoid 0 134 | clipped_errors = np.minimum(abs_errors, self.abs_err_upper) 135 | ps = np.power(clipped_errors, self.alpha) 136 | for ti, p in zip(tree_idx, ps): 137 | self.tree.update(ti, p) 138 | 139 | class DQN(): 140 | # DQN Agent 141 | def __init__(self, env): 142 | # init experience replay 143 | self.replay_total = 0 144 | # init some parameters 145 | self.time_step = 0 146 | self.epsilon = INITIAL_EPSILON 147 | self.state_dim = env.observation_space.shape[0] 148 | self.action_dim = env.action_space.n 149 | self.memory = Memory(capacity=REPLAY_SIZE) 150 | 151 | self.create_Q_network() 152 | self.create_training_method() 153 | 154 | # Init session 155 | self.session = tf.InteractiveSession() 156 | self.session.run(tf.global_variables_initializer()) 157 | 158 | def create_Q_network(self): 159 | # input layer 160 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 161 | self.ISWeights = tf.placeholder(tf.float32, [None, 1]) 162 | # network weights 163 | with tf.variable_scope('current_net'): 164 | W1 = self.weight_variable([self.state_dim,20]) 165 | b1 = self.bias_variable([20]) 166 | W2 = self.weight_variable([20,self.action_dim]) 167 | b2 = self.bias_variable([self.action_dim]) 168 | 169 | # hidden layers 170 | h_layer = tf.nn.relu(tf.matmul(self.state_input,W1) + b1) 171 | # Q Value layer 172 | self.Q_value = tf.matmul(h_layer,W2) + b2 173 | 174 | with tf.variable_scope('target_net'): 175 | W1t = self.weight_variable([self.state_dim,20]) 176 | b1t = self.bias_variable([20]) 177 | W2t = self.weight_variable([20,self.action_dim]) 178 | b2t = self.bias_variable([self.action_dim]) 179 | 180 | # hidden layers 181 | h_layer_t = tf.nn.relu(tf.matmul(self.state_input,W1t) + b1t) 182 | # Q Value layer 183 | self.target_Q_value = tf.matmul(h_layer_t,W2t) + b2t 184 | 185 | t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net') 186 | e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='current_net') 187 | 188 | with tf.variable_scope('soft_replacement'): 189 | self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)] 190 | 191 | def create_training_method(self): 192 | self.action_input = tf.placeholder("float",[None,self.action_dim]) # one hot presentation 193 | self.y_input = tf.placeholder("float",[None]) 194 | Q_action = tf.reduce_sum(tf.multiply(self.Q_value,self.action_input),reduction_indices = 1) 195 | self.cost = tf.reduce_mean(self.ISWeights *(tf.square(self.y_input - Q_action))) 196 | self.abs_errors =tf.abs(self.y_input - Q_action) 197 | self.optimizer = tf.train.AdamOptimizer(0.0001).minimize(self.cost) 198 | 199 | def store_transition(self, s, a, r, s_, done): 200 | transition = np.hstack((s, a, r, s_, done)) 201 | self.memory.store(transition) # have high priority for newly arrived transition 202 | 203 | def perceive(self,state,action,reward,next_state,done): 204 | one_hot_action = np.zeros(self.action_dim) 205 | one_hot_action[action] = 1 206 | #print(state,one_hot_action,reward,next_state,done) 207 | self.store_transition(state,one_hot_action,reward,next_state,done) 208 | self.replay_total += 1 209 | if self.replay_total > BATCH_SIZE: 210 | self.train_Q_network() 211 | 212 | def train_Q_network(self): 213 | self.time_step += 1 214 | # Step 1: obtain random minibatch from replay memory 215 | tree_idx, minibatch, ISWeights = self.memory.sample(BATCH_SIZE) 216 | state_batch = minibatch[:,0:4] 217 | action_batch = minibatch[:,4:6] 218 | reward_batch = [data[6] for data in minibatch] 219 | next_state_batch = minibatch[:,7:11] 220 | # Step 2: calculate y 221 | y_batch = [] 222 | current_Q_batch = self.Q_value.eval(feed_dict={self.state_input: next_state_batch}) 223 | max_action_next = np.argmax(current_Q_batch, axis=1) 224 | target_Q_batch = self.target_Q_value.eval(feed_dict={self.state_input: next_state_batch}) 225 | 226 | for i in range(0,BATCH_SIZE): 227 | done = minibatch[i][11] 228 | if done: 229 | y_batch.append(reward_batch[i]) 230 | else : 231 | target_Q_value = target_Q_batch[i, max_action_next[i]] 232 | y_batch.append(reward_batch[i] + GAMMA * target_Q_value) 233 | 234 | self.optimizer.run(feed_dict={ 235 | self.y_input:y_batch, 236 | self.action_input:action_batch, 237 | self.state_input:state_batch, 238 | self.ISWeights: ISWeights 239 | }) 240 | _, abs_errors, _ = self.session.run([self.optimizer, self.abs_errors, self.cost], feed_dict={ 241 | self.y_input: y_batch, 242 | self.action_input: action_batch, 243 | self.state_input: state_batch, 244 | self.ISWeights: ISWeights 245 | }) 246 | self.memory.batch_update(tree_idx, abs_errors) # update priority 247 | 248 | def egreedy_action(self,state): 249 | Q_value = self.Q_value.eval(feed_dict = { 250 | self.state_input:[state] 251 | })[0] 252 | if random.random() <= self.epsilon: 253 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 254 | return random.randint(0,self.action_dim - 1) 255 | else: 256 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 257 | return np.argmax(Q_value) 258 | 259 | def action(self,state): 260 | return np.argmax(self.Q_value.eval(feed_dict = { 261 | self.state_input:[state] 262 | })[0]) 263 | 264 | def update_target_q_network(self, episode): 265 | # update target Q netowrk 266 | if episode % REPLACE_TARGET_FREQ == 0: 267 | self.session.run(self.target_replace_op) 268 | #print('episode '+str(episode) +', target Q network params replaced!') 269 | 270 | def weight_variable(self,shape): 271 | initial = tf.truncated_normal(shape) 272 | return tf.Variable(initial) 273 | 274 | def bias_variable(self,shape): 275 | initial = tf.constant(0.01, shape = shape) 276 | return tf.Variable(initial) 277 | # --------------------------------------------------------- 278 | # Hyper Parameters 279 | ENV_NAME = 'CartPole-v0' 280 | EPISODE = 3000 # Episode limitation 281 | STEP = 300 # Step limitation in an episode 282 | TEST = 5 # The number of experiment test every 100 episode 283 | 284 | def main(): 285 | # initialize OpenAI Gym env and dqn agent 286 | env = gym.make(ENV_NAME) 287 | agent = DQN(env) 288 | 289 | for episode in range(EPISODE): 290 | # initialize task 291 | state = env.reset() 292 | # Train 293 | for step in range(STEP): 294 | action = agent.egreedy_action(state) # e-greedy action for train 295 | next_state,reward,done,_ = env.step(action) 296 | # Define reward for agent 297 | reward = -1 if done else 0.1 298 | agent.perceive(state,action,reward,next_state,done) 299 | state = next_state 300 | if done: 301 | break 302 | # Test every 100 episodes 303 | if episode % 100 == 0: 304 | total_reward = 0 305 | for i in range(TEST): 306 | state = env.reset() 307 | for j in range(STEP): 308 | env.render() 309 | action = agent.action(state) # direct action for test 310 | state,reward,done,_ = env.step(action) 311 | total_reward += reward 312 | if done: 313 | break 314 | ave_reward = total_reward/TEST 315 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 316 | agent.update_target_q_network(episode) 317 | 318 | if __name__ == '__main__': 319 | main() -------------------------------------------------------------------------------- /reinforcement-learning/dqn.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ##https://www.cnblogs.com/pinard/p/9714655.html ## 9 | ## 强化学习(八)价值函数的近似表示与Deep Q-Learning ## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters for DQN 18 | GAMMA = 0.9 # discount factor for target Q 19 | INITIAL_EPSILON = 0.5 # starting value of epsilon 20 | FINAL_EPSILON = 0.01 # final value of epsilon 21 | REPLAY_SIZE = 10000 # experience replay buffer size 22 | BATCH_SIZE = 32 # size of minibatch 23 | 24 | class DQN(): 25 | # DQN Agent 26 | def __init__(self, env): 27 | # init experience replay 28 | self.replay_buffer = deque() 29 | # init some parameters 30 | self.time_step = 0 31 | self.epsilon = INITIAL_EPSILON 32 | self.state_dim = env.observation_space.shape[0] 33 | self.action_dim = env.action_space.n 34 | 35 | self.create_Q_network() 36 | self.create_training_method() 37 | 38 | # Init session 39 | self.session = tf.InteractiveSession() 40 | self.session.run(tf.global_variables_initializer()) 41 | 42 | def create_Q_network(self): 43 | # network weights 44 | W1 = self.weight_variable([self.state_dim,20]) 45 | b1 = self.bias_variable([20]) 46 | W2 = self.weight_variable([20,self.action_dim]) 47 | b2 = self.bias_variable([self.action_dim]) 48 | # input layer 49 | self.state_input = tf.placeholder("float",[None,self.state_dim]) 50 | # hidden layers 51 | h_layer = tf.nn.relu(tf.matmul(self.state_input,W1) + b1) 52 | # Q Value layer 53 | self.Q_value = tf.matmul(h_layer,W2) + b2 54 | 55 | def create_training_method(self): 56 | self.action_input = tf.placeholder("float",[None,self.action_dim]) # one hot presentation 57 | self.y_input = tf.placeholder("float",[None]) 58 | Q_action = tf.reduce_sum(tf.multiply(self.Q_value,self.action_input),reduction_indices = 1) 59 | self.cost = tf.reduce_mean(tf.square(self.y_input - Q_action)) 60 | self.optimizer = tf.train.AdamOptimizer(0.0001).minimize(self.cost) 61 | 62 | def perceive(self,state,action,reward,next_state,done): 63 | one_hot_action = np.zeros(self.action_dim) 64 | one_hot_action[action] = 1 65 | self.replay_buffer.append((state,one_hot_action,reward,next_state,done)) 66 | if len(self.replay_buffer) > REPLAY_SIZE: 67 | self.replay_buffer.popleft() 68 | 69 | if len(self.replay_buffer) > BATCH_SIZE: 70 | self.train_Q_network() 71 | 72 | def train_Q_network(self): 73 | self.time_step += 1 74 | # Step 1: obtain random minibatch from replay memory 75 | minibatch = random.sample(self.replay_buffer,BATCH_SIZE) 76 | state_batch = [data[0] for data in minibatch] 77 | action_batch = [data[1] for data in minibatch] 78 | reward_batch = [data[2] for data in minibatch] 79 | next_state_batch = [data[3] for data in minibatch] 80 | 81 | # Step 2: calculate y 82 | y_batch = [] 83 | Q_value_batch = self.Q_value.eval(feed_dict={self.state_input:next_state_batch}) 84 | for i in range(0,BATCH_SIZE): 85 | done = minibatch[i][4] 86 | if done: 87 | y_batch.append(reward_batch[i]) 88 | else : 89 | y_batch.append(reward_batch[i] + GAMMA * np.max(Q_value_batch[i])) 90 | 91 | self.optimizer.run(feed_dict={ 92 | self.y_input:y_batch, 93 | self.action_input:action_batch, 94 | self.state_input:state_batch 95 | }) 96 | 97 | def egreedy_action(self,state): 98 | Q_value = self.Q_value.eval(feed_dict = { 99 | self.state_input:[state] 100 | })[0] 101 | if random.random() <= self.epsilon: 102 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 103 | return random.randint(0,self.action_dim - 1) 104 | else: 105 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 106 | return np.argmax(Q_value) 107 | 108 | def action(self,state): 109 | return np.argmax(self.Q_value.eval(feed_dict = { 110 | self.state_input:[state] 111 | })[0]) 112 | 113 | def weight_variable(self,shape): 114 | initial = tf.truncated_normal(shape) 115 | return tf.Variable(initial) 116 | 117 | def bias_variable(self,shape): 118 | initial = tf.constant(0.01, shape = shape) 119 | return tf.Variable(initial) 120 | # --------------------------------------------------------- 121 | # Hyper Parameters 122 | ENV_NAME = 'CartPole-v0' 123 | EPISODE = 3000 # Episode limitation 124 | STEP = 300 # Step limitation in an episode 125 | TEST = 10 # The number of experiment test every 100 episode 126 | 127 | def main(): 128 | # initialize OpenAI Gym env and dqn agent 129 | env = gym.make(ENV_NAME) 130 | agent = DQN(env) 131 | 132 | for episode in range(EPISODE): 133 | # initialize task 134 | state = env.reset() 135 | # Train 136 | for step in range(STEP): 137 | action = agent.egreedy_action(state) # e-greedy action for train 138 | next_state,reward,done,_ = env.step(action) 139 | # Define reward for agent 140 | reward = -1 if done else 0.1 141 | agent.perceive(state,action,reward,next_state,done) 142 | state = next_state 143 | if done: 144 | break 145 | # Test every 100 episodes 146 | if episode % 100 == 0: 147 | total_reward = 0 148 | for i in range(TEST): 149 | state = env.reset() 150 | for j in range(STEP): 151 | env.render() 152 | action = agent.action(state) # direct action for test 153 | state,reward,done,_ = env.step(action) 154 | total_reward += reward 155 | if done: 156 | break 157 | ave_reward = total_reward/TEST 158 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 159 | 160 | if __name__ == '__main__': 161 | main() -------------------------------------------------------------------------------- /reinforcement-learning/duel_dqn.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ## https://www.cnblogs.com/pinard/p/9923859.html ## 9 | ## 强化学习(十二) Dueling DQN ## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters for DQN 18 | GAMMA = 0.9 # discount factor for target Q 19 | INITIAL_EPSILON = 0.5 # starting value of epsilon 20 | FINAL_EPSILON = 0.01 # final value of epsilon 21 | REPLAY_SIZE = 10000 # experience replay buffer size 22 | BATCH_SIZE = 128 # size of minibatch 23 | REPLACE_TARGET_FREQ = 10 # frequency to update target Q network 24 | 25 | class DQN(): 26 | # DQN Agent 27 | def __init__(self, env): 28 | # init experience replay 29 | self.replay_buffer = deque() 30 | # init some parameters 31 | self.time_step = 0 32 | self.epsilon = INITIAL_EPSILON 33 | self.state_dim = env.observation_space.shape[0] 34 | self.action_dim = env.action_space.n 35 | 36 | self.create_Q_network() 37 | self.create_training_method() 38 | 39 | # Init session 40 | self.session = tf.InteractiveSession() 41 | self.session.run(tf.global_variables_initializer()) 42 | 43 | def create_Q_network(self): 44 | # input layer 45 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 46 | # network weights 47 | with tf.variable_scope('current_net'): 48 | W1 = self.weight_variable([self.state_dim,20]) 49 | b1 = self.bias_variable([20]) 50 | 51 | # hidden layer 1 52 | h_layer_1 = tf.nn.relu(tf.matmul(self.state_input,W1) + b1) 53 | 54 | # hidden layer for state value 55 | with tf.variable_scope('Value'): 56 | W21= self.weight_variable([20,1]) 57 | b21 = self.bias_variable([1]) 58 | self.V = tf.matmul(h_layer_1, W21) + b21 59 | 60 | # hidden layer for action value 61 | with tf.variable_scope('Advantage'): 62 | W22 = self.weight_variable([20,self.action_dim]) 63 | b22 = self.bias_variable([self.action_dim]) 64 | self.A = tf.matmul(h_layer_1, W22) + b22 65 | 66 | # Q Value layer 67 | self.Q_value = self.V + (self.A - tf.reduce_mean(self.A, axis=1, keep_dims=True)) 68 | 69 | with tf.variable_scope('target_net'): 70 | W1t = self.weight_variable([self.state_dim,20]) 71 | b1t = self.bias_variable([20]) 72 | 73 | # hidden layer 1 74 | h_layer_1t = tf.nn.relu(tf.matmul(self.state_input,W1t) + b1t) 75 | 76 | # hidden layer for state value 77 | with tf.variable_scope('Value'): 78 | W2v = self.weight_variable([20,1]) 79 | b2v = self.bias_variable([1]) 80 | self.VT = tf.matmul(h_layer_1t, W2v) + b2v 81 | 82 | # hidden layer for action value 83 | with tf.variable_scope('Advantage'): 84 | W2a = self.weight_variable([20,self.action_dim]) 85 | b2a = self.bias_variable([self.action_dim]) 86 | self.AT = tf.matmul(h_layer_1t, W2a) + b2a 87 | 88 | # Q Value layer 89 | self.target_Q_value = self.VT + (self.AT - tf.reduce_mean(self.AT, axis=1, keep_dims=True)) 90 | 91 | t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net') 92 | e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='current_net') 93 | 94 | with tf.variable_scope('soft_replacement'): 95 | self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)] 96 | 97 | def create_training_method(self): 98 | self.action_input = tf.placeholder("float",[None,self.action_dim]) # one hot presentation 99 | self.y_input = tf.placeholder("float",[None]) 100 | Q_action = tf.reduce_sum(tf.multiply(self.Q_value,self.action_input),reduction_indices = 1) 101 | self.cost = tf.reduce_mean(tf.square(self.y_input - Q_action)) 102 | self.optimizer = tf.train.AdamOptimizer(0.0001).minimize(self.cost) 103 | 104 | def perceive(self,state,action,reward,next_state,done): 105 | one_hot_action = np.zeros(self.action_dim) 106 | one_hot_action[action] = 1 107 | self.replay_buffer.append((state,one_hot_action,reward,next_state,done)) 108 | if len(self.replay_buffer) > REPLAY_SIZE: 109 | self.replay_buffer.popleft() 110 | 111 | if len(self.replay_buffer) > BATCH_SIZE: 112 | self.train_Q_network() 113 | 114 | def train_Q_network(self): 115 | self.time_step += 1 116 | # Step 1: obtain random minibatch from replay memory 117 | minibatch = random.sample(self.replay_buffer,BATCH_SIZE) 118 | state_batch = [data[0] for data in minibatch] 119 | action_batch = [data[1] for data in minibatch] 120 | reward_batch = [data[2] for data in minibatch] 121 | next_state_batch = [data[3] for data in minibatch] 122 | 123 | # Step 2: calculate y 124 | y_batch = [] 125 | Q_value_batch = self.target_Q_value.eval(feed_dict={self.state_input:next_state_batch}) 126 | for i in range(0,BATCH_SIZE): 127 | done = minibatch[i][4] 128 | if done: 129 | y_batch.append(reward_batch[i]) 130 | else : 131 | y_batch.append(reward_batch[i] + GAMMA * np.max(Q_value_batch[i])) 132 | 133 | self.optimizer.run(feed_dict={ 134 | self.y_input:y_batch, 135 | self.action_input:action_batch, 136 | self.state_input:state_batch 137 | }) 138 | 139 | def egreedy_action(self,state): 140 | Q_value = self.Q_value.eval(feed_dict = { 141 | self.state_input:[state] 142 | })[0] 143 | if random.random() <= self.epsilon: 144 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 145 | return random.randint(0,self.action_dim - 1) 146 | else: 147 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 148 | return np.argmax(Q_value) 149 | 150 | def action(self,state): 151 | return np.argmax(self.Q_value.eval(feed_dict = { 152 | self.state_input:[state] 153 | })[0]) 154 | 155 | def update_target_q_network(self, episode): 156 | # update target Q netowrk 157 | if episode % REPLACE_TARGET_FREQ == 0: 158 | self.session.run(self.target_replace_op) 159 | #print('episode '+str(episode) +', target Q network params replaced!') 160 | 161 | def weight_variable(self,shape): 162 | initial = tf.truncated_normal(shape) 163 | return tf.Variable(initial) 164 | 165 | def bias_variable(self,shape): 166 | initial = tf.constant(0.01, shape = shape) 167 | return tf.Variable(initial) 168 | # --------------------------------------------------------- 169 | # Hyper Parameters 170 | ENV_NAME = 'CartPole-v0' 171 | EPISODE = 3000 # Episode limitation 172 | STEP = 300 # Step limitation in an episode 173 | TEST = 5 # The number of experiment test every 100 episode 174 | 175 | def main(): 176 | # initialize OpenAI Gym env and dqn agent 177 | env = gym.make(ENV_NAME) 178 | agent = DQN(env) 179 | 180 | for episode in range(EPISODE): 181 | # initialize task 182 | state = env.reset() 183 | # Train 184 | for step in range(STEP): 185 | action = agent.egreedy_action(state) # e-greedy action for train 186 | next_state,reward,done,_ = env.step(action) 187 | # Define reward for agent 188 | reward = -1 if done else 0.1 189 | agent.perceive(state,action,reward,next_state,done) 190 | state = next_state 191 | if done: 192 | break 193 | # Test every 100 episodes 194 | if episode % 100 == 0: 195 | total_reward = 0 196 | for i in range(TEST): 197 | state = env.reset() 198 | for j in range(STEP): 199 | env.render() 200 | action = agent.action(state) # direct action for test 201 | state,reward,done,_ = env.step(action) 202 | total_reward += reward 203 | if done: 204 | break 205 | ave_reward = total_reward/TEST 206 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 207 | agent.update_target_q_network(episode) 208 | 209 | if __name__ == '__main__': 210 | main() -------------------------------------------------------------------------------- /reinforcement-learning/introduction.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ljpzzz/machinelearning/17302f708146ad46838b3782bf735364fa3cf16d/reinforcement-learning/introduction.py -------------------------------------------------------------------------------- /reinforcement-learning/nature_dqn.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ##https://www.cnblogs.com/pinard/p/9756075.html## 9 | ##强化学习(九)Deep Q-Learning进阶之Nature DQN## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters for DQN 18 | GAMMA = 0.9 # discount factor for target Q 19 | INITIAL_EPSILON = 0.5 # starting value of epsilon 20 | FINAL_EPSILON = 0.01 # final value of epsilon 21 | REPLAY_SIZE = 10000 # experience replay buffer size 22 | BATCH_SIZE = 32 # size of minibatch 23 | REPLACE_TARGET_FREQ = 10 # frequency to update target Q network 24 | 25 | class DQN(): 26 | # DQN Agent 27 | def __init__(self, env): 28 | # init experience replay 29 | self.replay_buffer = deque() 30 | # init some parameters 31 | self.time_step = 0 32 | self.epsilon = INITIAL_EPSILON 33 | self.state_dim = env.observation_space.shape[0] 34 | self.action_dim = env.action_space.n 35 | 36 | self.create_Q_network() 37 | self.create_training_method() 38 | 39 | # Init session 40 | self.session = tf.InteractiveSession() 41 | self.session.run(tf.global_variables_initializer()) 42 | 43 | def create_Q_network(self): 44 | # input layer 45 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 46 | # network weights 47 | with tf.variable_scope('current_net'): 48 | W1 = self.weight_variable([self.state_dim,20]) 49 | b1 = self.bias_variable([20]) 50 | W2 = self.weight_variable([20,self.action_dim]) 51 | b2 = self.bias_variable([self.action_dim]) 52 | 53 | # hidden layers 54 | h_layer = tf.nn.relu(tf.matmul(self.state_input,W1) + b1) 55 | # Q Value layer 56 | self.Q_value = tf.matmul(h_layer,W2) + b2 57 | 58 | with tf.variable_scope('target_net'): 59 | W1t = self.weight_variable([self.state_dim,20]) 60 | b1t = self.bias_variable([20]) 61 | W2t = self.weight_variable([20,self.action_dim]) 62 | b2t = self.bias_variable([self.action_dim]) 63 | 64 | # hidden layers 65 | h_layer_t = tf.nn.relu(tf.matmul(self.state_input,W1t) + b1t) 66 | # Q Value layer 67 | self.target_Q_value = tf.matmul(h_layer_t,W2t) + b2t 68 | 69 | t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net') 70 | e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='current_net') 71 | 72 | with tf.variable_scope('soft_replacement'): 73 | self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)] 74 | 75 | def create_training_method(self): 76 | self.action_input = tf.placeholder("float",[None,self.action_dim]) # one hot presentation 77 | self.y_input = tf.placeholder("float",[None]) 78 | Q_action = tf.reduce_sum(tf.multiply(self.Q_value,self.action_input),reduction_indices = 1) 79 | self.cost = tf.reduce_mean(tf.square(self.y_input - Q_action)) 80 | self.optimizer = tf.train.AdamOptimizer(0.0001).minimize(self.cost) 81 | 82 | def perceive(self,state,action,reward,next_state,done): 83 | one_hot_action = np.zeros(self.action_dim) 84 | one_hot_action[action] = 1 85 | self.replay_buffer.append((state,one_hot_action,reward,next_state,done)) 86 | if len(self.replay_buffer) > REPLAY_SIZE: 87 | self.replay_buffer.popleft() 88 | 89 | if len(self.replay_buffer) > BATCH_SIZE: 90 | self.train_Q_network() 91 | 92 | def train_Q_network(self): 93 | self.time_step += 1 94 | # Step 1: obtain random minibatch from replay memory 95 | minibatch = random.sample(self.replay_buffer,BATCH_SIZE) 96 | state_batch = [data[0] for data in minibatch] 97 | action_batch = [data[1] for data in minibatch] 98 | reward_batch = [data[2] for data in minibatch] 99 | next_state_batch = [data[3] for data in minibatch] 100 | 101 | # Step 2: calculate y 102 | y_batch = [] 103 | Q_value_batch = self.target_Q_value.eval(feed_dict={self.state_input:next_state_batch}) 104 | for i in range(0,BATCH_SIZE): 105 | done = minibatch[i][4] 106 | if done: 107 | y_batch.append(reward_batch[i]) 108 | else : 109 | y_batch.append(reward_batch[i] + GAMMA * np.max(Q_value_batch[i])) 110 | 111 | self.optimizer.run(feed_dict={ 112 | self.y_input:y_batch, 113 | self.action_input:action_batch, 114 | self.state_input:state_batch 115 | }) 116 | 117 | def egreedy_action(self,state): 118 | Q_value = self.Q_value.eval(feed_dict = { 119 | self.state_input:[state] 120 | })[0] 121 | if random.random() <= self.epsilon: 122 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 123 | return random.randint(0,self.action_dim - 1) 124 | else: 125 | self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / 10000 126 | return np.argmax(Q_value) 127 | 128 | def action(self,state): 129 | return np.argmax(self.Q_value.eval(feed_dict = { 130 | self.state_input:[state] 131 | })[0]) 132 | 133 | def update_target_q_network(self, episode): 134 | # update target Q netowrk 135 | if episode % REPLACE_TARGET_FREQ == 0: 136 | self.session.run(self.target_replace_op) 137 | #print('episode '+str(episode) +', target Q network params replaced!') 138 | 139 | def weight_variable(self,shape): 140 | initial = tf.truncated_normal(shape) 141 | return tf.Variable(initial) 142 | 143 | def bias_variable(self,shape): 144 | initial = tf.constant(0.01, shape = shape) 145 | return tf.Variable(initial) 146 | # --------------------------------------------------------- 147 | # Hyper Parameters 148 | ENV_NAME = 'CartPole-v0' 149 | EPISODE = 3000 # Episode limitation 150 | STEP = 300 # Step limitation in an episode 151 | TEST = 5 # The number of experiment test every 100 episode 152 | 153 | def main(): 154 | # initialize OpenAI Gym env and dqn agent 155 | env = gym.make(ENV_NAME) 156 | agent = DQN(env) 157 | 158 | for episode in range(EPISODE): 159 | # initialize task 160 | state = env.reset() 161 | # Train 162 | for step in range(STEP): 163 | action = agent.egreedy_action(state) # e-greedy action for train 164 | next_state,reward,done,_ = env.step(action) 165 | # Define reward for agent 166 | reward = -1 if done else 0.1 167 | agent.perceive(state,action,reward,next_state,done) 168 | state = next_state 169 | if done: 170 | break 171 | # Test every 100 episodes 172 | if episode % 100 == 0: 173 | total_reward = 0 174 | for i in range(TEST): 175 | state = env.reset() 176 | for j in range(STEP): 177 | env.render() 178 | action = agent.action(state) # direct action for test 179 | state,reward,done,_ = env.step(action) 180 | total_reward += reward 181 | if done: 182 | break 183 | ave_reward = total_reward/TEST 184 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 185 | agent.update_target_q_network(episode) 186 | 187 | if __name__ == '__main__': 188 | main() -------------------------------------------------------------------------------- /reinforcement-learning/policy_gradient.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016 - 2019 Pinard Liu(liujianping-ok@163.com) # 4 | # https://www.cnblogs.com/pinard # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ## https://www.cnblogs.com/pinard/p/10137696.html ## 9 | ## 强化学习(十三) 策略梯度(Policy Gradient) ## 10 | 11 | import gym 12 | import tensorflow as tf 13 | import numpy as np 14 | import random 15 | from collections import deque 16 | 17 | # Hyper Parameters 18 | GAMMA = 0.95 # discount factor 19 | LEARNING_RATE=0.01 20 | 21 | class Policy_Gradient(): 22 | def __init__(self, env): 23 | # init some parameters 24 | self.time_step = 0 25 | self.state_dim = env.observation_space.shape[0] 26 | self.action_dim = env.action_space.n 27 | self.ep_obs, self.ep_as, self.ep_rs = [], [], [] 28 | self.create_softmax_network() 29 | 30 | # Init session 31 | self.session = tf.InteractiveSession() 32 | self.session.run(tf.global_variables_initializer()) 33 | 34 | def create_softmax_network(self): 35 | # network weights 36 | W1 = self.weight_variable([self.state_dim, 20]) 37 | b1 = self.bias_variable([20]) 38 | W2 = self.weight_variable([20, self.action_dim]) 39 | b2 = self.bias_variable([self.action_dim]) 40 | # input layer 41 | self.state_input = tf.placeholder("float", [None, self.state_dim]) 42 | self.tf_acts = tf.placeholder(tf.int32, [None, ], name="actions_num") 43 | self.tf_vt = tf.placeholder(tf.float32, [None, ], name="actions_value") 44 | # hidden layers 45 | h_layer = tf.nn.relu(tf.matmul(self.state_input, W1) + b1) 46 | # softmax layer 47 | self.softmax_input = tf.matmul(h_layer, W2) + b2 48 | #softmax output 49 | self.all_act_prob = tf.nn.softmax(self.softmax_input, name='act_prob') 50 | self.neg_log_prob = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.softmax_input, 51 | labels=self.tf_acts) 52 | self.loss = tf.reduce_mean(self.neg_log_prob * self.tf_vt) # reward guided loss 53 | 54 | self.train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(self.loss) 55 | 56 | def weight_variable(self, shape): 57 | initial = tf.truncated_normal(shape) 58 | return tf.Variable(initial) 59 | 60 | def bias_variable(self, shape): 61 | initial = tf.constant(0.01, shape=shape) 62 | return tf.Variable(initial) 63 | 64 | def choose_action(self, observation): 65 | prob_weights = self.session.run(self.all_act_prob, feed_dict={self.state_input: observation[np.newaxis, :]}) 66 | action = np.random.choice(range(prob_weights.shape[1]), p=prob_weights.ravel()) # select action w.r.t the actions prob 67 | return action 68 | 69 | def store_transition(self, s, a, r): 70 | self.ep_obs.append(s) 71 | self.ep_as.append(a) 72 | self.ep_rs.append(r) 73 | 74 | def learn(self): 75 | 76 | discounted_ep_rs = np.zeros_like(self.ep_rs) 77 | running_add = 0 78 | for t in reversed(range(0, len(self.ep_rs))): 79 | running_add = running_add * GAMMA + self.ep_rs[t] 80 | discounted_ep_rs[t] = running_add 81 | 82 | discounted_ep_rs -= np.mean(discounted_ep_rs) 83 | discounted_ep_rs /= np.std(discounted_ep_rs) 84 | 85 | # train on episode 86 | self.session.run(self.train_op, feed_dict={ 87 | self.state_input: np.vstack(self.ep_obs), 88 | self.tf_acts: np.array(self.ep_as), 89 | self.tf_vt: discounted_ep_rs, 90 | }) 91 | 92 | self.ep_obs, self.ep_as, self.ep_rs = [], [], [] # empty episode data 93 | # Hyper Parameters 94 | ENV_NAME = 'CartPole-v0' 95 | EPISODE = 3000 # Episode limitation 96 | STEP = 3000 # Step limitation in an episode 97 | TEST = 10 # The number of experiment test every 100 episode 98 | 99 | def main(): 100 | # initialize OpenAI Gym env and dqn agent 101 | env = gym.make(ENV_NAME) 102 | agent = Policy_Gradient(env) 103 | 104 | for episode in range(EPISODE): 105 | # initialize task 106 | state = env.reset() 107 | # Train 108 | for step in range(STEP): 109 | action = agent.choose_action(state) # e-greedy action for train 110 | next_state,reward,done,_ = env.step(action) 111 | agent.store_transition(state, action, reward) 112 | state = next_state 113 | if done: 114 | #print("stick for ",step, " steps") 115 | agent.learn() 116 | break 117 | 118 | # Test every 100 episodes 119 | if episode % 100 == 0: 120 | total_reward = 0 121 | for i in range(TEST): 122 | state = env.reset() 123 | for j in range(STEP): 124 | env.render() 125 | action = agent.choose_action(state) # direct action for test 126 | state,reward,done,_ = env.step(action) 127 | total_reward += reward 128 | if done: 129 | break 130 | ave_reward = total_reward/TEST 131 | print ('episode: ',episode,'Evaluation Average Reward:',ave_reward) 132 | 133 | if __name__ == '__main__': 134 | main() -------------------------------------------------------------------------------- /reinforcement-learning/q_learning_windy_world.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016-2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com) # 4 | # 2016 Kenta Shimada(hyperkentakun@gmail.com) # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ##https://www.cnblogs.com/pinard/p/9669263.html ## 9 | ## 强化学习(七)时序差分离线控制算法Q-Learning ## 10 | 11 | import numpy as np 12 | import matplotlib 13 | matplotlib.use('Agg') 14 | import matplotlib.pyplot as plt 15 | 16 | # world height 17 | WORLD_HEIGHT = 7 18 | 19 | # world width 20 | WORLD_WIDTH = 10 21 | 22 | # wind strength for each column 23 | WIND = [0, 0, 0, 1, 1, 1, 2, 2, 1, 0] 24 | 25 | # possible actions 26 | ACTION_UP = 0 27 | ACTION_DOWN = 1 28 | ACTION_LEFT = 2 29 | ACTION_RIGHT = 3 30 | 31 | # probability for exploration 32 | EPSILON = 0.1 33 | 34 | # Sarsa step size 35 | ALPHA = 0.5 36 | 37 | # reward for each step 38 | REWARD = -1.0 39 | 40 | START = [3, 0] 41 | GOAL = [3, 7] 42 | ACTIONS = [ACTION_UP, ACTION_DOWN, ACTION_LEFT, ACTION_RIGHT] 43 | 44 | def step(state, action): 45 | i, j = state 46 | if action == ACTION_UP: 47 | return [max(i - 1 - WIND[j], 0), j] 48 | elif action == ACTION_DOWN: 49 | return [max(min(i + 1 - WIND[j], WORLD_HEIGHT - 1), 0), j] 50 | elif action == ACTION_LEFT: 51 | return [max(i - WIND[j], 0), max(j - 1, 0)] 52 | elif action == ACTION_RIGHT: 53 | return [max(i - WIND[j], 0), min(j + 1, WORLD_WIDTH - 1)] 54 | else: 55 | assert False 56 | 57 | # play for an episode 58 | def episode(q_value): 59 | # track the total time steps in this episode 60 | time = 0 61 | 62 | # initialize state 63 | state = START 64 | 65 | while state != GOAL: 66 | # choose an action based on epsilon-greedy algorithm 67 | if np.random.binomial(1, EPSILON) == 1: 68 | action = np.random.choice(ACTIONS) 69 | else: 70 | values_ = q_value[state[0], state[1], :] 71 | action = np.random.choice([action_ for action_, value_ in enumerate(values_) if value_ == np.max(values_)]) 72 | 73 | # keep going until get to the goal state 74 | 75 | next_state = step(state, action) 76 | #if np.random.binomial(1, EPSILON) == 1: 77 | # next_action = np.random.choice(ACTIONS) 78 | #else: 79 | values_ = q_value[next_state[0], next_state[1], :] 80 | next_action = np.random.choice([action_ for action_, value_ in enumerate(values_) if value_ == np.max(values_)]) 81 | 82 | # Sarsa update 83 | q_value[state[0], state[1], action] += \ 84 | ALPHA * (REWARD + q_value[next_state[0], next_state[1], next_action] - 85 | q_value[state[0], state[1], action]) 86 | state = next_state 87 | #action = next_action 88 | time += 1 89 | return time 90 | 91 | def q_learning(): 92 | q_value = np.zeros((WORLD_HEIGHT, WORLD_WIDTH, 4)) 93 | episode_limit = 500 94 | 95 | steps = [] 96 | ep = 0 97 | while ep < episode_limit: 98 | steps.append(episode(q_value)) 99 | # time = episode(q_value) 100 | # episodes.extend([ep] * time) 101 | ep += 1 102 | 103 | steps = np.add.accumulate(steps) 104 | 105 | plt.plot(steps, np.arange(1, len(steps) + 1)) 106 | plt.xlabel('Time steps') 107 | plt.ylabel('Episodes') 108 | 109 | plt.savefig('./q-learning.png') 110 | plt.close() 111 | 112 | # display the optimal policy 113 | optimal_policy = [] 114 | for i in range(0, WORLD_HEIGHT): 115 | optimal_policy.append([]) 116 | for j in range(0, WORLD_WIDTH): 117 | if [i, j] == GOAL: 118 | optimal_policy[-1].append('G') 119 | continue 120 | bestAction = np.argmax(q_value[i, j, :]) 121 | if bestAction == ACTION_UP: 122 | optimal_policy[-1].append('U') 123 | elif bestAction == ACTION_DOWN: 124 | optimal_policy[-1].append('D') 125 | elif bestAction == ACTION_LEFT: 126 | optimal_policy[-1].append('L') 127 | elif bestAction == ACTION_RIGHT: 128 | optimal_policy[-1].append('R') 129 | print('Optimal policy is:') 130 | for row in optimal_policy: 131 | print(row) 132 | print('Wind strength for each column:\n{}'.format([str(w) for w in WIND])) 133 | 134 | if __name__ == '__main__': 135 | q_learning() -------------------------------------------------------------------------------- /reinforcement-learning/sarsa_windy_world.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Copyright (C) # 3 | # 2016-2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com) # 4 | # 2016 Kenta Shimada(hyperkentakun@gmail.com) # 5 | # Permission given to modify the code as long as you keep this # 6 | # declaration at the top # 7 | ####################################################################### 8 | ##https://www.cnblogs.com/pinard/p/9614290.html ## 9 | ## 强化学习(六)时序差分在线控制算法SARSA ## 10 | 11 | import numpy as np 12 | import matplotlib 13 | matplotlib.use('Agg') 14 | import matplotlib.pyplot as plt 15 | 16 | # world height 17 | WORLD_HEIGHT = 7 18 | 19 | # world width 20 | WORLD_WIDTH = 10 21 | 22 | # wind strength for each column 23 | WIND = [0, 0, 0, 1, 1, 1, 2, 2, 1, 0] 24 | 25 | # possible actions 26 | ACTION_UP = 0 27 | ACTION_DOWN = 1 28 | ACTION_LEFT = 2 29 | ACTION_RIGHT = 3 30 | 31 | # probability for exploration 32 | EPSILON = 0.1 33 | 34 | # Sarsa step size 35 | ALPHA = 0.5 36 | 37 | # reward for each step 38 | REWARD = -1.0 39 | 40 | START = [3, 0] 41 | GOAL = [3, 7] 42 | ACTIONS = [ACTION_UP, ACTION_DOWN, ACTION_LEFT, ACTION_RIGHT] 43 | 44 | def step(state, action): 45 | i, j = state 46 | if action == ACTION_UP: 47 | return [max(i - 1 - WIND[j], 0), j] 48 | elif action == ACTION_DOWN: 49 | return [max(min(i + 1 - WIND[j], WORLD_HEIGHT - 1), 0), j] 50 | elif action == ACTION_LEFT: 51 | return [max(i - WIND[j], 0), max(j - 1, 0)] 52 | elif action == ACTION_RIGHT: 53 | return [max(i - WIND[j], 0), min(j + 1, WORLD_WIDTH - 1)] 54 | else: 55 | assert False 56 | 57 | # play for an episode 58 | def episode(q_value): 59 | # track the total time steps in this episode 60 | time = 0 61 | 62 | # initialize state 63 | state = START 64 | 65 | # choose an action based on epsilon-greedy algorithm 66 | if np.random.binomial(1, EPSILON) == 1: 67 | action = np.random.choice(ACTIONS) 68 | else: 69 | values_ = q_value[state[0], state[1], :] 70 | action = np.random.choice([action_ for action_, value_ in enumerate(values_) if value_ == np.max(values_)]) 71 | 72 | # keep going until get to the goal state 73 | while state != GOAL: 74 | next_state = step(state, action) 75 | if np.random.binomial(1, EPSILON) == 1: 76 | next_action = np.random.choice(ACTIONS) 77 | else: 78 | values_ = q_value[next_state[0], next_state[1], :] 79 | next_action = np.random.choice([action_ for action_, value_ in enumerate(values_) if value_ == np.max(values_)]) 80 | 81 | # Sarsa update 82 | q_value[state[0], state[1], action] += \ 83 | ALPHA * (REWARD + q_value[next_state[0], next_state[1], next_action] - 84 | q_value[state[0], state[1], action]) 85 | state = next_state 86 | action = next_action 87 | time += 1 88 | return time 89 | 90 | def sarsa(): 91 | q_value = np.zeros((WORLD_HEIGHT, WORLD_WIDTH, 4)) 92 | episode_limit = 500 93 | 94 | steps = [] 95 | ep = 0 96 | while ep < episode_limit: 97 | steps.append(episode(q_value)) 98 | # time = episode(q_value) 99 | # episodes.extend([ep] * time) 100 | ep += 1 101 | 102 | steps = np.add.accumulate(steps) 103 | 104 | plt.plot(steps, np.arange(1, len(steps) + 1)) 105 | plt.xlabel('Time steps') 106 | plt.ylabel('Episodes') 107 | 108 | plt.savefig('./sarsa.png') 109 | plt.close() 110 | 111 | # display the optimal policy 112 | optimal_policy = [] 113 | for i in range(0, WORLD_HEIGHT): 114 | optimal_policy.append([]) 115 | for j in range(0, WORLD_WIDTH): 116 | if [i, j] == GOAL: 117 | optimal_policy[-1].append('G') 118 | continue 119 | bestAction = np.argmax(q_value[i, j, :]) 120 | if bestAction == ACTION_UP: 121 | optimal_policy[-1].append('U') 122 | elif bestAction == ACTION_DOWN: 123 | optimal_policy[-1].append('D') 124 | elif bestAction == ACTION_LEFT: 125 | optimal_policy[-1].append('L') 126 | elif bestAction == ACTION_RIGHT: 127 | optimal_policy[-1].append('R') 128 | print('Optimal policy is:') 129 | for row in optimal_policy: 130 | print(row) 131 | print('Wind strength for each column:\n{}'.format([str(w) for w in WIND])) 132 | 133 | if __name__ == '__main__': 134 | sarsa() --------------------------------------------------------------------------------