├── .ipynb_checkpoints ├── CA_HOUSE_LINEAR_REGRESSION-checkpoint.ipynb ├── MNIST_LINEAR_CLASSIFIER-checkpoint.ipynb └── MNIST_MLN_CLASSIFIER-checkpoint.ipynb ├── CA_HOUSE_LINEAR_REGRESSION.ipynb ├── DIGIT_MLN_DROPOUT.ipynb ├── MNIST_AUTOENCODER.ipynb ├── MNIST_BILSTM.ipynb ├── MNIST_CNN.ipynb ├── MNIST_LINEAR_CLASSIFIER.ipynb ├── MNIST_LSTM.ipynb ├── MNIST_MLN_BN.ipynb ├── MNIST_MLN_CLASSIFIER.ipynb ├── MNIST_MLN_CLASSIFIER_DROPOUT.ipynb ├── README.md └── datasets └── housing.csv /.ipynb_checkpoints/CA_HOUSE_LINEAR_REGRESSION-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pandas as pd\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "housing_data = pd.read_csv('datasets/housing.csv')" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/html": [ 33 | "
\n", 34 | "\n", 47 | "\n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | "
longitudelatitudehousing_median_agetotal_roomstotal_bedroomspopulationhouseholdsmedian_incomemedian_house_valueocean_proximity
0-122.2337.8841.0880.0129.0322.0126.08.3252452600.0NEAR BAY
1-122.2237.8621.07099.01106.02401.01138.08.3014358500.0NEAR BAY
2-122.2437.8552.01467.0190.0496.0177.07.2574352100.0NEAR BAY
3-122.2537.8552.01274.0235.0558.0219.05.6431341300.0NEAR BAY
4-122.2537.8552.01627.0280.0565.0259.03.8462342200.0NEAR BAY
\n", 131 | "
" 132 | ], 133 | "text/plain": [ 134 | " longitude latitude housing_median_age total_rooms total_bedrooms \\\n", 135 | "0 -122.23 37.88 41.0 880.0 129.0 \n", 136 | "1 -122.22 37.86 21.0 7099.0 1106.0 \n", 137 | "2 -122.24 37.85 52.0 1467.0 190.0 \n", 138 | "3 -122.25 37.85 52.0 1274.0 235.0 \n", 139 | "4 -122.25 37.85 52.0 1627.0 280.0 \n", 140 | "\n", 141 | " population households median_income median_house_value ocean_proximity \n", 142 | "0 322.0 126.0 8.3252 452600.0 NEAR BAY \n", 143 | "1 2401.0 1138.0 8.3014 358500.0 NEAR BAY \n", 144 | "2 496.0 177.0 7.2574 352100.0 NEAR BAY \n", 145 | "3 558.0 219.0 5.6431 341300.0 NEAR BAY \n", 146 | "4 565.0 259.0 3.8462 342200.0 NEAR BAY " 147 | ] 148 | }, 149 | "execution_count": 3, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "housing_data.head()\n", 156 | "# 社区数据" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 4, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "\n", 169 | "RangeIndex: 20640 entries, 0 to 20639\n", 170 | "Data columns (total 10 columns):\n", 171 | "longitude 20640 non-null float64\n", 172 | "latitude 20640 non-null float64\n", 173 | "housing_median_age 20640 non-null float64\n", 174 | "total_rooms 20640 non-null float64\n", 175 | "total_bedrooms 20433 non-null float64\n", 176 | "population 20640 non-null float64\n", 177 | "households 20640 non-null float64\n", 178 | "median_income 20640 non-null float64\n", 179 | "median_house_value 20640 non-null float64\n", 180 | "ocean_proximity 20640 non-null object\n", 181 | "dtypes: float64(9), object(1)\n", 182 | "memory usage: 1.6+ MB\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "housing_data.info()" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 5, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "\n", 200 | "RangeIndex: 20640 entries, 0 to 20639\n", 201 | "Data columns (total 10 columns):\n", 202 | "longitude 20640 non-null float64\n", 203 | "latitude 20640 non-null float64\n", 204 | "housing_median_age 20640 non-null float64\n", 205 | "total_rooms 20640 non-null float64\n", 206 | "total_bedrooms 20640 non-null float64\n", 207 | "population 20640 non-null float64\n", 208 | "households 20640 non-null float64\n", 209 | "median_income 20640 non-null float64\n", 210 | "median_house_value 20640 non-null float64\n", 211 | "ocean_proximity 20640 non-null object\n", 212 | "dtypes: float64(9), object(1)\n", 213 | "memory usage: 1.6+ MB\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "median = housing_data['total_bedrooms'].median()\n", 219 | "housing_data = housing_data.fillna(median)\n", 220 | "\n", 221 | "housing_data.info()" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 6, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "housing_data = housing_data.drop('ocean_proximity', axis=1)\n", 233 | "\n" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 7, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "\n", 246 | "RangeIndex: 20640 entries, 0 to 20639\n", 247 | "Data columns (total 9 columns):\n", 248 | "longitude 20640 non-null float64\n", 249 | "latitude 20640 non-null float64\n", 250 | "housing_median_age 20640 non-null float64\n", 251 | "total_rooms 20640 non-null float64\n", 252 | "total_bedrooms 20640 non-null float64\n", 253 | "population 20640 non-null float64\n", 254 | "households 20640 non-null float64\n", 255 | "median_income 20640 non-null float64\n", 256 | "median_house_value 20640 non-null float64\n", 257 | "dtypes: float64(9)\n", 258 | "memory usage: 1.4 MB\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "housing_data.info()" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 8, 269 | "metadata": { 270 | "collapsed": true 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "\n", 275 | "from sklearn.model_selection import train_test_split\n", 276 | "\n", 277 | "train_set, test_set = train_test_split(housing_data, test_size=0.2, random_state=33)" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 9, 283 | "metadata": { 284 | "collapsed": true 285 | }, 286 | "outputs": [], 287 | "source": [ 288 | "y_train = train_set['median_house_value']\n", 289 | "X_train = train_set.drop('median_house_value', axis=1)\n", 290 | "\n", 291 | "y_test = test_set['median_house_value']\n", 292 | "X_test = test_set.drop('median_house_value', axis=1)\n", 293 | "\n", 294 | "from sklearn.linear_model import LinearRegression\n", 295 | "\n", 296 | "lr = LinearRegression()\n", 297 | "\n", 298 | "lr.fit(X_train, y_train)\n", 299 | "\n", 300 | "from sklearn.metrics.regression import mean_squared_error\n", 301 | "\n", 302 | "y_predict = lr.predict(X_test)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 10, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "69990.825384776152" 314 | ] 315 | }, 316 | "execution_count": 10, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "import numpy as np\n", 323 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 11, 329 | "metadata": { 330 | "collapsed": true 331 | }, 332 | "outputs": [], 333 | "source": [ 334 | "from sklearn.preprocessing import StandardScaler\n", 335 | "\n", 336 | "ss = StandardScaler()\n", 337 | "\n", 338 | "X_train = ss.fit_transform(X_train)\n", 339 | "X_test = ss.fit_transform(X_test)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 13, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "69620.6890544\n", 352 | "69901.4152052\n" 353 | ] 354 | } 355 | ], 356 | "source": [ 357 | "lr = LinearRegression()\n", 358 | "\n", 359 | "lr.fit(X_train, y_train)\n", 360 | "\n", 361 | "from sklearn.metrics.regression import mean_squared_error\n", 362 | "\n", 363 | "\n", 364 | "y_predict = lr.predict(X_train)\n", 365 | "\n", 366 | "import numpy as np\n", 367 | "\n", 368 | "\n", 369 | "print np.sqrt(mean_squared_error(y_train, y_predict))\n", 370 | "\n", 371 | "y_predict = lr.predict(X_test)\n", 372 | "\n", 373 | "\n", 374 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 14, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "name": "stdout", 384 | "output_type": "stream", 385 | "text": [ 386 | "0.0\n", 387 | "79017.4707616\n" 388 | ] 389 | } 390 | ], 391 | "source": [ 392 | "from sklearn.tree import DecisionTreeRegressor\n", 393 | "\n", 394 | "dtr = DecisionTreeRegressor()\n", 395 | "\n", 396 | "dtr.fit(X_train, y_train)\n", 397 | "\n", 398 | "y_predict = dtr.predict(X_train)\n", 399 | "\n", 400 | "print np.sqrt(mean_squared_error(y_train, y_predict))\n", 401 | "\n", 402 | "from sklearn.metrics.regression import mean_squared_error\n", 403 | "\n", 404 | "y_predict = dtr.predict(X_test)\n", 405 | "\n", 406 | "import numpy as np\n", 407 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": null, 413 | "metadata": { 414 | "collapsed": true 415 | }, 416 | "outputs": [], 417 | "source": [] 418 | } 419 | ], 420 | "metadata": { 421 | "kernelspec": { 422 | "display_name": "Python 2", 423 | "language": "python", 424 | "name": "python2" 425 | }, 426 | "language_info": { 427 | "codemirror_mode": { 428 | "name": "ipython", 429 | "version": 2 430 | }, 431 | "file_extension": ".py", 432 | "mimetype": "text/x-python", 433 | "name": "python", 434 | "nbconvert_exporter": "python", 435 | "pygments_lexer": "ipython2", 436 | "version": "2.7.13" 437 | } 438 | }, 439 | "nbformat": 4, 440 | "nbformat_minor": 2 441 | } 442 | -------------------------------------------------------------------------------- /CA_HOUSE_LINEAR_REGRESSION.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pandas as pd\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "housing_data = pd.read_csv('datasets/housing.csv')" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/html": [ 33 | "
\n", 34 | "\n", 47 | "\n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | "
longitudelatitudehousing_median_agetotal_roomstotal_bedroomspopulationhouseholdsmedian_incomemedian_house_valueocean_proximity
0-122.2337.8841.0880.0129.0322.0126.08.3252452600.0NEAR BAY
1-122.2237.8621.07099.01106.02401.01138.08.3014358500.0NEAR BAY
2-122.2437.8552.01467.0190.0496.0177.07.2574352100.0NEAR BAY
3-122.2537.8552.01274.0235.0558.0219.05.6431341300.0NEAR BAY
4-122.2537.8552.01627.0280.0565.0259.03.8462342200.0NEAR BAY
\n", 131 | "
" 132 | ], 133 | "text/plain": [ 134 | " longitude latitude housing_median_age total_rooms total_bedrooms \\\n", 135 | "0 -122.23 37.88 41.0 880.0 129.0 \n", 136 | "1 -122.22 37.86 21.0 7099.0 1106.0 \n", 137 | "2 -122.24 37.85 52.0 1467.0 190.0 \n", 138 | "3 -122.25 37.85 52.0 1274.0 235.0 \n", 139 | "4 -122.25 37.85 52.0 1627.0 280.0 \n", 140 | "\n", 141 | " population households median_income median_house_value ocean_proximity \n", 142 | "0 322.0 126.0 8.3252 452600.0 NEAR BAY \n", 143 | "1 2401.0 1138.0 8.3014 358500.0 NEAR BAY \n", 144 | "2 496.0 177.0 7.2574 352100.0 NEAR BAY \n", 145 | "3 558.0 219.0 5.6431 341300.0 NEAR BAY \n", 146 | "4 565.0 259.0 3.8462 342200.0 NEAR BAY " 147 | ] 148 | }, 149 | "execution_count": 3, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "housing_data.head()\n", 156 | "# 社区数据" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 4, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "\n", 169 | "RangeIndex: 20640 entries, 0 to 20639\n", 170 | "Data columns (total 10 columns):\n", 171 | "longitude 20640 non-null float64\n", 172 | "latitude 20640 non-null float64\n", 173 | "housing_median_age 20640 non-null float64\n", 174 | "total_rooms 20640 non-null float64\n", 175 | "total_bedrooms 20433 non-null float64\n", 176 | "population 20640 non-null float64\n", 177 | "households 20640 non-null float64\n", 178 | "median_income 20640 non-null float64\n", 179 | "median_house_value 20640 non-null float64\n", 180 | "ocean_proximity 20640 non-null object\n", 181 | "dtypes: float64(9), object(1)\n", 182 | "memory usage: 1.6+ MB\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "housing_data.info()" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 5, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "\n", 200 | "RangeIndex: 20640 entries, 0 to 20639\n", 201 | "Data columns (total 10 columns):\n", 202 | "longitude 20640 non-null float64\n", 203 | "latitude 20640 non-null float64\n", 204 | "housing_median_age 20640 non-null float64\n", 205 | "total_rooms 20640 non-null float64\n", 206 | "total_bedrooms 20640 non-null float64\n", 207 | "population 20640 non-null float64\n", 208 | "households 20640 non-null float64\n", 209 | "median_income 20640 non-null float64\n", 210 | "median_house_value 20640 non-null float64\n", 211 | "ocean_proximity 20640 non-null object\n", 212 | "dtypes: float64(9), object(1)\n", 213 | "memory usage: 1.6+ MB\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "median = housing_data['total_bedrooms'].median()\n", 219 | "housing_data = housing_data.fillna(median)\n", 220 | "\n", 221 | "housing_data.info()" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 6, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "housing_data = housing_data.drop('ocean_proximity', axis=1)\n", 233 | "\n" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 7, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "\n", 246 | "RangeIndex: 20640 entries, 0 to 20639\n", 247 | "Data columns (total 9 columns):\n", 248 | "longitude 20640 non-null float64\n", 249 | "latitude 20640 non-null float64\n", 250 | "housing_median_age 20640 non-null float64\n", 251 | "total_rooms 20640 non-null float64\n", 252 | "total_bedrooms 20640 non-null float64\n", 253 | "population 20640 non-null float64\n", 254 | "households 20640 non-null float64\n", 255 | "median_income 20640 non-null float64\n", 256 | "median_house_value 20640 non-null float64\n", 257 | "dtypes: float64(9)\n", 258 | "memory usage: 1.4 MB\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "housing_data.info()" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 8, 269 | "metadata": { 270 | "collapsed": true 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "\n", 275 | "from sklearn.model_selection import train_test_split\n", 276 | "\n", 277 | "train_set, test_set = train_test_split(housing_data, test_size=0.2, random_state=33)" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 9, 283 | "metadata": { 284 | "collapsed": true 285 | }, 286 | "outputs": [], 287 | "source": [ 288 | "y_train = train_set['median_house_value']\n", 289 | "X_train = train_set.drop('median_house_value', axis=1)\n", 290 | "\n", 291 | "y_test = test_set['median_house_value']\n", 292 | "X_test = test_set.drop('median_house_value', axis=1)\n", 293 | "\n", 294 | "from sklearn.linear_model import LinearRegression\n", 295 | "\n", 296 | "lr = LinearRegression()\n", 297 | "\n", 298 | "lr.fit(X_train, y_train)\n", 299 | "\n", 300 | "from sklearn.metrics.regression import mean_squared_error\n", 301 | "\n", 302 | "y_predict = lr.predict(X_test)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 10, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "69990.825384776152" 314 | ] 315 | }, 316 | "execution_count": 10, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "import numpy as np\n", 323 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 11, 329 | "metadata": { 330 | "collapsed": true 331 | }, 332 | "outputs": [], 333 | "source": [ 334 | "from sklearn.preprocessing import StandardScaler\n", 335 | "\n", 336 | "ss = StandardScaler()\n", 337 | "\n", 338 | "X_train = ss.fit_transform(X_train)\n", 339 | "X_test = ss.fit_transform(X_test)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 13, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "69620.6890544\n", 352 | "69901.4152052\n" 353 | ] 354 | } 355 | ], 356 | "source": [ 357 | "lr = LinearRegression()\n", 358 | "\n", 359 | "lr.fit(X_train, y_train)\n", 360 | "\n", 361 | "from sklearn.metrics.regression import mean_squared_error\n", 362 | "\n", 363 | "\n", 364 | "y_predict = lr.predict(X_train)\n", 365 | "\n", 366 | "import numpy as np\n", 367 | "\n", 368 | "\n", 369 | "print np.sqrt(mean_squared_error(y_train, y_predict))\n", 370 | "\n", 371 | "y_predict = lr.predict(X_test)\n", 372 | "\n", 373 | "\n", 374 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 14, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "name": "stdout", 384 | "output_type": "stream", 385 | "text": [ 386 | "0.0\n", 387 | "79017.4707616\n" 388 | ] 389 | } 390 | ], 391 | "source": [ 392 | "from sklearn.tree import DecisionTreeRegressor\n", 393 | "\n", 394 | "dtr = DecisionTreeRegressor()\n", 395 | "\n", 396 | "dtr.fit(X_train, y_train)\n", 397 | "\n", 398 | "y_predict = dtr.predict(X_train)\n", 399 | "\n", 400 | "print np.sqrt(mean_squared_error(y_train, y_predict))\n", 401 | "\n", 402 | "from sklearn.metrics.regression import mean_squared_error\n", 403 | "\n", 404 | "y_predict = dtr.predict(X_test)\n", 405 | "\n", 406 | "import numpy as np\n", 407 | "print np.sqrt(mean_squared_error(y_test, y_predict))" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": null, 413 | "metadata": { 414 | "collapsed": true 415 | }, 416 | "outputs": [], 417 | "source": [] 418 | } 419 | ], 420 | "metadata": { 421 | "kernelspec": { 422 | "display_name": "Python 2", 423 | "language": "python", 424 | "name": "python2" 425 | }, 426 | "language_info": { 427 | "codemirror_mode": { 428 | "name": "ipython", 429 | "version": 2 430 | }, 431 | "file_extension": ".py", 432 | "mimetype": "text/x-python", 433 | "name": "python", 434 | "nbconvert_exporter": "python", 435 | "pygments_lexer": "ipython2", 436 | "version": "2.7.13" 437 | } 438 | }, 439 | "nbformat": 4, 440 | "nbformat_minor": 2 441 | } 442 | -------------------------------------------------------------------------------- /MNIST_AUTOENCODER.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data\n", 13 | "import numpy as np\n", 14 | "import matplotlib\n", 15 | "matplotlib.use('nbagg')\n", 16 | "\n", 17 | "import matplotlib.pyplot as plt" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Extracting MNIST_data/train-images-idx3-ubyte.gz\n", 32 | "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n", 33 | "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n", 34 | "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "(55000, 784) (55000, 10)\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "print mnist.train.images.shape, mnist.train.labels.shape" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "INPUT_UNITS = 784\n", 70 | "HIDDEN_UNITS = [256]\n", 71 | "ENCODED_UNITS = 128\n", 72 | "\n", 73 | "LEARNING_RATE = 1e-3\n", 74 | "TRAINING_ITER = 2000\n", 75 | "BATCH_SIZE = 100\n", 76 | "\n", 77 | "def init_weights(shape):\n", 78 | " return tf.Variable(tf.truncated_normal(shape, stddev=0.1))\n", 79 | "\n", 80 | "def init_biases(shape):\n", 81 | " return tf.Variable(tf.zeros(shape) + 0.1)\n", 82 | "\n", 83 | "def add_layer(x, weights, biases, activation_function = None):\n", 84 | " scores = tf.matmul(x, weights) + biases\n", 85 | " \n", 86 | " if activation_function:\n", 87 | " return activation_function(scores)\n", 88 | " else:\n", 89 | " return scores\n", 90 | "\n", 91 | "weights = {\n", 92 | " 'encoder_l1': init_weights([INPUT_UNITS, HIDDEN_UNITS[0]]), \n", 93 | " 'encoder_l2': init_weights([HIDDEN_UNITS[0], ENCODED_UNITS]),\n", 94 | " 'decoder_l1': init_weights([ENCODED_UNITS, HIDDEN_UNITS[0]]),\n", 95 | " 'decoder_l2': init_weights([HIDDEN_UNITS[0], INPUT_UNITS])\n", 96 | "}\n", 97 | "\n", 98 | "biases = {\n", 99 | " 'encoder_l1': init_biases([HIDDEN_UNITS[0]]), \n", 100 | " 'encoder_l2': init_biases([ENCODED_UNITS]),\n", 101 | " 'decoder_l1': init_biases(HIDDEN_UNITS[0]),\n", 102 | " 'decoder_l2': init_biases([INPUT_UNITS])\n", 103 | "}\n", 104 | "\n", 105 | "def encoder(x):\n", 106 | " encoder_h1 = add_layer(x, weights['encoder_l1'], biases['encoder_l1'], tf.nn.relu)\n", 107 | " return add_layer(encoder_h1, weights['encoder_l2'], biases['encoder_l2'], tf.nn.relu) \n", 108 | "\n", 109 | "def decoder(x):\n", 110 | " decoder_h1 = add_layer(x, weights['decoder_l1'], biases['decoder_l1'], tf.nn.relu)\n", 111 | " return add_layer(decoder_h1, weights['decoder_l2'], biases['decoder_l2'], tf.nn.relu)\n", 112 | "\n", 113 | "x = tf.placeholder(tf.float32, [None, 784])\n", 114 | "\n", 115 | "encoder_op = encoder(x)\n", 116 | "x_ = decoder(encoder_op)\n", 117 | "\n", 118 | "loss = tf.reduce_mean(tf.square(x - x_))\n", 119 | "\n", 120 | "train_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 5, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "0.200395\n", 135 | "0.0587475\n", 136 | "0.039646\n", 137 | "0.0379133\n", 138 | "0.0267893\n", 139 | "0.026916\n", 140 | "0.0215286\n", 141 | "0.0191278\n", 142 | "0.0185347\n", 143 | "0.0168091\n", 144 | "0.0186432\n", 145 | "0.018459\n", 146 | "0.0159615\n", 147 | "0.0166012\n", 148 | "0.0161047\n", 149 | "0.0142662\n", 150 | "0.0149797\n", 151 | "0.0151406\n", 152 | "0.0143366\n", 153 | "0.0143979\n", 154 | "0.0138252\n", 155 | "0.0138254\n", 156 | "0.0128702\n", 157 | "0.0133265\n", 158 | "0.0130669\n", 159 | "0.0127066\n", 160 | "0.0140361\n", 161 | "0.012641\n", 162 | "0.013777\n", 163 | "0.0137268\n", 164 | "0.0132932\n", 165 | "0.0122787\n", 166 | "0.011369\n", 167 | "0.0119215\n", 168 | "0.0114884\n", 169 | "0.0122204\n", 170 | "0.0121677\n", 171 | "0.0114692\n", 172 | "0.0120117\n", 173 | "0.0117096\n" 174 | ] 175 | }, 176 | { 177 | "data": { 178 | "application/javascript": [ 179 | "/* Put everything inside the global mpl namespace */\n", 180 | "window.mpl = {};\n", 181 | "\n", 182 | "\n", 183 | "mpl.get_websocket_type = function() {\n", 184 | " if (typeof(WebSocket) !== 'undefined') {\n", 185 | " return WebSocket;\n", 186 | " } else if (typeof(MozWebSocket) !== 'undefined') {\n", 187 | " return MozWebSocket;\n", 188 | " } else {\n", 189 | " alert('Your browser does not have WebSocket support.' +\n", 190 | " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", 191 | " 'Firefox 4 and 5 are also supported but you ' +\n", 192 | " 'have to enable WebSockets in about:config.');\n", 193 | " };\n", 194 | "}\n", 195 | "\n", 196 | "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", 197 | " this.id = figure_id;\n", 198 | "\n", 199 | " this.ws = websocket;\n", 200 | "\n", 201 | " this.supports_binary = (this.ws.binaryType != undefined);\n", 202 | "\n", 203 | " if (!this.supports_binary) {\n", 204 | " var warnings = document.getElementById(\"mpl-warnings\");\n", 205 | " if (warnings) {\n", 206 | " warnings.style.display = 'block';\n", 207 | " warnings.textContent = (\n", 208 | " \"This browser does not support binary websocket messages. \" +\n", 209 | " \"Performance may be slow.\");\n", 210 | " }\n", 211 | " }\n", 212 | "\n", 213 | " this.imageObj = new Image();\n", 214 | "\n", 215 | " this.context = undefined;\n", 216 | " this.message = undefined;\n", 217 | " this.canvas = undefined;\n", 218 | " this.rubberband_canvas = undefined;\n", 219 | " this.rubberband_context = undefined;\n", 220 | " this.format_dropdown = undefined;\n", 221 | "\n", 222 | " this.image_mode = 'full';\n", 223 | "\n", 224 | " this.root = $('
');\n", 225 | " this._root_extra_style(this.root)\n", 226 | " this.root.attr('style', 'display: inline-block');\n", 227 | "\n", 228 | " $(parent_element).append(this.root);\n", 229 | "\n", 230 | " this._init_header(this);\n", 231 | " this._init_canvas(this);\n", 232 | " this._init_toolbar(this);\n", 233 | "\n", 234 | " var fig = this;\n", 235 | "\n", 236 | " this.waiting = false;\n", 237 | "\n", 238 | " this.ws.onopen = function () {\n", 239 | " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", 240 | " fig.send_message(\"send_image_mode\", {});\n", 241 | " if (mpl.ratio != 1) {\n", 242 | " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", 243 | " }\n", 244 | " fig.send_message(\"refresh\", {});\n", 245 | " }\n", 246 | "\n", 247 | " this.imageObj.onload = function() {\n", 248 | " if (fig.image_mode == 'full') {\n", 249 | " // Full images could contain transparency (where diff images\n", 250 | " // almost always do), so we need to clear the canvas so that\n", 251 | " // there is no ghosting.\n", 252 | " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", 253 | " }\n", 254 | " fig.context.drawImage(fig.imageObj, 0, 0);\n", 255 | " };\n", 256 | "\n", 257 | " this.imageObj.onunload = function() {\n", 258 | " this.ws.close();\n", 259 | " }\n", 260 | "\n", 261 | " this.ws.onmessage = this._make_on_message_function(this);\n", 262 | "\n", 263 | " this.ondownload = ondownload;\n", 264 | "}\n", 265 | "\n", 266 | "mpl.figure.prototype._init_header = function() {\n", 267 | " var titlebar = $(\n", 268 | " '
');\n", 270 | " var titletext = $(\n", 271 | " '
');\n", 273 | " titlebar.append(titletext)\n", 274 | " this.root.append(titlebar);\n", 275 | " this.header = titletext[0];\n", 276 | "}\n", 277 | "\n", 278 | "\n", 279 | "\n", 280 | "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", 281 | "\n", 282 | "}\n", 283 | "\n", 284 | "\n", 285 | "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", 286 | "\n", 287 | "}\n", 288 | "\n", 289 | "mpl.figure.prototype._init_canvas = function() {\n", 290 | " var fig = this;\n", 291 | "\n", 292 | " var canvas_div = $('
');\n", 293 | "\n", 294 | " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", 295 | "\n", 296 | " function canvas_keyboard_event(event) {\n", 297 | " return fig.key_event(event, event['data']);\n", 298 | " }\n", 299 | "\n", 300 | " canvas_div.keydown('key_press', canvas_keyboard_event);\n", 301 | " canvas_div.keyup('key_release', canvas_keyboard_event);\n", 302 | " this.canvas_div = canvas_div\n", 303 | " this._canvas_extra_style(canvas_div)\n", 304 | " this.root.append(canvas_div);\n", 305 | "\n", 306 | " var canvas = $('');\n", 307 | " canvas.addClass('mpl-canvas');\n", 308 | " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", 309 | "\n", 310 | " this.canvas = canvas[0];\n", 311 | " this.context = canvas[0].getContext(\"2d\");\n", 312 | "\n", 313 | " var backingStore = this.context.backingStorePixelRatio ||\n", 314 | "\tthis.context.webkitBackingStorePixelRatio ||\n", 315 | "\tthis.context.mozBackingStorePixelRatio ||\n", 316 | "\tthis.context.msBackingStorePixelRatio ||\n", 317 | "\tthis.context.oBackingStorePixelRatio ||\n", 318 | "\tthis.context.backingStorePixelRatio || 1;\n", 319 | "\n", 320 | " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", 321 | "\n", 322 | " var rubberband = $('');\n", 323 | " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", 324 | "\n", 325 | " var pass_mouse_events = true;\n", 326 | "\n", 327 | " canvas_div.resizable({\n", 328 | " start: function(event, ui) {\n", 329 | " pass_mouse_events = false;\n", 330 | " },\n", 331 | " resize: function(event, ui) {\n", 332 | " fig.request_resize(ui.size.width, ui.size.height);\n", 333 | " },\n", 334 | " stop: function(event, ui) {\n", 335 | " pass_mouse_events = true;\n", 336 | " fig.request_resize(ui.size.width, ui.size.height);\n", 337 | " },\n", 338 | " });\n", 339 | "\n", 340 | " function mouse_event_fn(event) {\n", 341 | " if (pass_mouse_events)\n", 342 | " return fig.mouse_event(event, event['data']);\n", 343 | " }\n", 344 | "\n", 345 | " rubberband.mousedown('button_press', mouse_event_fn);\n", 346 | " rubberband.mouseup('button_release', mouse_event_fn);\n", 347 | " // Throttle sequential mouse events to 1 every 20ms.\n", 348 | " rubberband.mousemove('motion_notify', mouse_event_fn);\n", 349 | "\n", 350 | " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", 351 | " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", 352 | "\n", 353 | " canvas_div.on(\"wheel\", function (event) {\n", 354 | " event = event.originalEvent;\n", 355 | " event['data'] = 'scroll'\n", 356 | " if (event.deltaY < 0) {\n", 357 | " event.step = 1;\n", 358 | " } else {\n", 359 | " event.step = -1;\n", 360 | " }\n", 361 | " mouse_event_fn(event);\n", 362 | " });\n", 363 | "\n", 364 | " canvas_div.append(canvas);\n", 365 | " canvas_div.append(rubberband);\n", 366 | "\n", 367 | " this.rubberband = rubberband;\n", 368 | " this.rubberband_canvas = rubberband[0];\n", 369 | " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", 370 | " this.rubberband_context.strokeStyle = \"#000000\";\n", 371 | "\n", 372 | " this._resize_canvas = function(width, height) {\n", 373 | " // Keep the size of the canvas, canvas container, and rubber band\n", 374 | " // canvas in synch.\n", 375 | " canvas_div.css('width', width)\n", 376 | " canvas_div.css('height', height)\n", 377 | "\n", 378 | " canvas.attr('width', width * mpl.ratio);\n", 379 | " canvas.attr('height', height * mpl.ratio);\n", 380 | " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", 381 | "\n", 382 | " rubberband.attr('width', width);\n", 383 | " rubberband.attr('height', height);\n", 384 | " }\n", 385 | "\n", 386 | " // Set the figure to an initial 600x600px, this will subsequently be updated\n", 387 | " // upon first draw.\n", 388 | " this._resize_canvas(600, 600);\n", 389 | "\n", 390 | " // Disable right mouse context menu.\n", 391 | " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", 392 | " return false;\n", 393 | " });\n", 394 | "\n", 395 | " function set_focus () {\n", 396 | " canvas.focus();\n", 397 | " canvas_div.focus();\n", 398 | " }\n", 399 | "\n", 400 | " window.setTimeout(set_focus, 100);\n", 401 | "}\n", 402 | "\n", 403 | "mpl.figure.prototype._init_toolbar = function() {\n", 404 | " var fig = this;\n", 405 | "\n", 406 | " var nav_element = $('
')\n", 407 | " nav_element.attr('style', 'width: 100%');\n", 408 | " this.root.append(nav_element);\n", 409 | "\n", 410 | " // Define a callback function for later on.\n", 411 | " function toolbar_event(event) {\n", 412 | " return fig.toolbar_button_onclick(event['data']);\n", 413 | " }\n", 414 | " function toolbar_mouse_event(event) {\n", 415 | " return fig.toolbar_button_onmouseover(event['data']);\n", 416 | " }\n", 417 | "\n", 418 | " for(var toolbar_ind in mpl.toolbar_items) {\n", 419 | " var name = mpl.toolbar_items[toolbar_ind][0];\n", 420 | " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", 421 | " var image = mpl.toolbar_items[toolbar_ind][2];\n", 422 | " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", 423 | "\n", 424 | " if (!name) {\n", 425 | " // put a spacer in here.\n", 426 | " continue;\n", 427 | " }\n", 428 | " var button = $('