{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "import important modules/packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import time\n", "import datetime\n", "import pandas as pd\n", "import numpy as np\n", "import sklearn\n", "import matplotlib.pyplot as plt\n", "import seaborn as sb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download online csv file" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "PROJECT_DIR = \"H:\\\\CS4410(Machine Learning)\\\\Samle ML Code\"\n", "DATA_DIR = \"Datasets\"\n", "mypath = os.path.join(PROJECT_DIR,DATA_DIR)\n", "\n", "from six.moves import urllib\n", "\n", "CSV_PATH=os.path.join(mypath,\"airline.csv\")\n", "DATA_URL = \"https://github.com/fivethirtyeight/data/blob/master/airline-safety/airline-safety.csv\"\n", "def fetch_data(data_url=DATA_URL, pathtosave=mypath,csv_path=CSV_PATH):\n", " if not os.path.isdir(pathtosave):\n", " os.makedirs(pathtosave)\n", " urllib.request.urlretrieve(data_url, csv_path)\n", " \n", "fetch_data()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load some data, sklearn comes with several datasets" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from sklearn import datasets\n", "boston = datasets.load_breast_cancer()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sklearn.utils.Bunch" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(boston)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from sklearn.utils import Bunch" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "Bunch?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "read keys" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data\n", "target\n", "feature_names\n", "DESCR\n", "filename\n" ] } ], "source": [ "for bos in boston:\n", " print (bos)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\".. _boston_dataset:\\n\\nBoston house prices dataset\\n---------------------------\\n\\n**Data Set Characteristics:** \\n\\n :Number of Instances: 506 \\n\\n :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\\n\\n :Attribute Information (in order):\\n - CRIM per capita crime rate by town\\n - ZN proportion of residential land zoned for lots over 25,000 sq.ft.\\n - INDUS proportion of non-retail business acres per town\\n - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\\n - NOX nitric oxides concentration (parts per 10 million)\\n - RM average number of rooms per dwelling\\n - AGE proportion of owner-occupied units built prior to 1940\\n - DIS weighted distances to five Boston employment centres\\n - RAD index of accessibility to radial highways\\n - TAX full-value property-tax rate per $10,000\\n - PTRATIO pupil-teacher ratio by town\\n - B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town\\n - LSTAT % lower status of the population\\n - MEDV Median value of owner-occupied homes in $1000's\\n\\n :Missing Attribute Values: None\\n\\n :Creator: Harrison, D. and Rubinfeld, D.L.\\n\\nThis is a copy of UCI ML housing dataset.\\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\\n\\n\\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\\n\\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\\nprices and the demand for clean air', J. Environ. Economics & Management,\\nvol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics\\n...', Wiley, 1980. N.B. Various transformations are used in the table on\\npages 244-261 of the latter.\\n\\nThe Boston house-price data has been used in many machine learning papers that address regression\\nproblems. \\n \\n.. topic:: References\\n\\n - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\\n - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.\\n\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boston.feature_names\n", "boston.DESCR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load in pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "bospd = pd.DataFrame(data = boston['data'], columns = boston['feature_names'])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "bospd['MEDV'] = boston['target']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "see correlation" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | CRIM | \n", "ZN | \n", "INDUS | \n", "CHAS | \n", "NOX | \n", "RM | \n", "AGE | \n", "DIS | \n", "RAD | \n", "TAX | \n", "PTRATIO | \n", "B | \n", "LSTAT | \n", "MEDV | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRIM | \n", "1.000000 | \n", "-0.200469 | \n", "0.406583 | \n", "-0.055892 | \n", "0.420972 | \n", "-0.219247 | \n", "0.352734 | \n", "-0.379670 | \n", "0.625505 | \n", "0.582764 | \n", "0.289946 | \n", "-0.385064 | \n", "0.455621 | \n", "-0.388305 | \n", "
| ZN | \n", "-0.200469 | \n", "1.000000 | \n", "-0.533828 | \n", "-0.042697 | \n", "-0.516604 | \n", "0.311991 | \n", "-0.569537 | \n", "0.664408 | \n", "-0.311948 | \n", "-0.314563 | \n", "-0.391679 | \n", "0.175520 | \n", "-0.412995 | \n", "0.360445 | \n", "
| INDUS | \n", "0.406583 | \n", "-0.533828 | \n", "1.000000 | \n", "0.062938 | \n", "0.763651 | \n", "-0.391676 | \n", "0.644779 | \n", "-0.708027 | \n", "0.595129 | \n", "0.720760 | \n", "0.383248 | \n", "-0.356977 | \n", "0.603800 | \n", "-0.483725 | \n", "
| CHAS | \n", "-0.055892 | \n", "-0.042697 | \n", "0.062938 | \n", "1.000000 | \n", "0.091203 | \n", "0.091251 | \n", "0.086518 | \n", "-0.099176 | \n", "-0.007368 | \n", "-0.035587 | \n", "-0.121515 | \n", "0.048788 | \n", "-0.053929 | \n", "0.175260 | \n", "
| NOX | \n", "0.420972 | \n", "-0.516604 | \n", "0.763651 | \n", "0.091203 | \n", "1.000000 | \n", "-0.302188 | \n", "0.731470 | \n", "-0.769230 | \n", "0.611441 | \n", "0.668023 | \n", "0.188933 | \n", "-0.380051 | \n", "0.590879 | \n", "-0.427321 | \n", "
| RM | \n", "-0.219247 | \n", "0.311991 | \n", "-0.391676 | \n", "0.091251 | \n", "-0.302188 | \n", "1.000000 | \n", "-0.240265 | \n", "0.205246 | \n", "-0.209847 | \n", "-0.292048 | \n", "-0.355501 | \n", "0.128069 | \n", "-0.613808 | \n", "0.695360 | \n", "
| AGE | \n", "0.352734 | \n", "-0.569537 | \n", "0.644779 | \n", "0.086518 | \n", "0.731470 | \n", "-0.240265 | \n", "1.000000 | \n", "-0.747881 | \n", "0.456022 | \n", "0.506456 | \n", "0.261515 | \n", "-0.273534 | \n", "0.602339 | \n", "-0.376955 | \n", "
| DIS | \n", "-0.379670 | \n", "0.664408 | \n", "-0.708027 | \n", "-0.099176 | \n", "-0.769230 | \n", "0.205246 | \n", "-0.747881 | \n", "1.000000 | \n", "-0.494588 | \n", "-0.534432 | \n", "-0.232471 | \n", "0.291512 | \n", "-0.496996 | \n", "0.249929 | \n", "
| RAD | \n", "0.625505 | \n", "-0.311948 | \n", "0.595129 | \n", "-0.007368 | \n", "0.611441 | \n", "-0.209847 | \n", "0.456022 | \n", "-0.494588 | \n", "1.000000 | \n", "0.910228 | \n", "0.464741 | \n", "-0.444413 | \n", "0.488676 | \n", "-0.381626 | \n", "
| TAX | \n", "0.582764 | \n", "-0.314563 | \n", "0.720760 | \n", "-0.035587 | \n", "0.668023 | \n", "-0.292048 | \n", "0.506456 | \n", "-0.534432 | \n", "0.910228 | \n", "1.000000 | \n", "0.460853 | \n", "-0.441808 | \n", "0.543993 | \n", "-0.468536 | \n", "
| PTRATIO | \n", "0.289946 | \n", "-0.391679 | \n", "0.383248 | \n", "-0.121515 | \n", "0.188933 | \n", "-0.355501 | \n", "0.261515 | \n", "-0.232471 | \n", "0.464741 | \n", "0.460853 | \n", "1.000000 | \n", "-0.177383 | \n", "0.374044 | \n", "-0.507787 | \n", "
| B | \n", "-0.385064 | \n", "0.175520 | \n", "-0.356977 | \n", "0.048788 | \n", "-0.380051 | \n", "0.128069 | \n", "-0.273534 | \n", "0.291512 | \n", "-0.444413 | \n", "-0.441808 | \n", "-0.177383 | \n", "1.000000 | \n", "-0.366087 | \n", "0.333461 | \n", "
| LSTAT | \n", "0.455621 | \n", "-0.412995 | \n", "0.603800 | \n", "-0.053929 | \n", "0.590879 | \n", "-0.613808 | \n", "0.602339 | \n", "-0.496996 | \n", "0.488676 | \n", "0.543993 | \n", "0.374044 | \n", "-0.366087 | \n", "1.000000 | \n", "-0.737663 | \n", "
| MEDV | \n", "-0.388305 | \n", "0.360445 | \n", "-0.483725 | \n", "0.175260 | \n", "-0.427321 | \n", "0.695360 | \n", "-0.376955 | \n", "0.249929 | \n", "-0.381626 | \n", "-0.468536 | \n", "-0.507787 | \n", "0.333461 | \n", "-0.737663 | \n", "1.000000 | \n", "