From 20d445947e8b6571dcfa3dac78915ee51698d5bb Mon Sep 17 00:00:00 2001 From: Nelio Machado Date: Mon, 9 Sep 2019 02:12:08 -0300 Subject: [PATCH 1/5] Created using Colaboratory --- Notebooks/Supervised Learning.ipynb | 4005 +++++++++++++++++++++++++++ 1 file changed, 4005 insertions(+) create mode 100644 Notebooks/Supervised Learning.ipynb diff --git a/Notebooks/Supervised Learning.ipynb b/Notebooks/Supervised Learning.ipynb new file mode 100644 index 000000000..425ed21f6 --- /dev/null +++ b/Notebooks/Supervised Learning.ipynb @@ -0,0 +1,4005 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.1" + }, + "colab": { + "name": "Supervised Learning - Part I.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ndC9Y0VtbhG9", + "colab_type": "text" + }, + "source": [ + "# Supervised Learning\n", + "\n", + "![SupervisedLearning](https://github.com/MathMachado/Python_RFB/blob/DS_Python/Material/Supervised_X_Unsupervised.jpeg?raw=true)\n", + "\n", + "Source: [Supervised vs Unsupervised Learning](https://www.linkedin.com/posts/asif-bhat_machinelearningalgorithms-machinelearing-activity-6576071130854055936-2mIn/)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "M87Q8zeLiqqB", + "colab_type": "text" + }, + "source": [ + "# Exemplo 1" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "25cO5AhXfV7z", + "colab_type": "text" + }, + "source": [ + "## Carregar bibliotecas básicas" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "P6cHHy_abP42", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%matplotlib notebook\n", + "import numpy as np\n", + "np.set_printoptions(precision=2)\n", + "\n", + "import pandas as pd\n", + "import seaborn as sn\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.patches as mpatches\n", + "\n", + "%matplotlib inline" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "miHfaVJMfbzo", + "colab_type": "text" + }, + "source": [ + "## Carregar o dataframe" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Fl6PmIMlaASk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 257 + }, + "outputId": "7a0dd619-8228-410e-b0a1-eeaecde06c69" + }, + "source": [ + "url= 'https://raw.githubusercontent.com/MathMachado/Python_RFB/DS_Python/Dataframes/FruitsDataWithColor.txt?token=AGDJQ66OKOVQC63ZVXGHMJ25P3M6G'\n", + "df_Fruits = pd.read_table(url)\n", + "df_Fruits.head()\n" + ], + "execution_count": 168, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: FutureWarning: read_table is deprecated, use read_csv instead, passing sep='\\t'.\n", + " \n" + ], + "name": "stderr" + }, + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
fruit_labelfruit_namefruit_subtypemasswidthheightcolor_score
01applegranny_smith1928.47.30.55
11applegranny_smith1808.06.80.59
21applegranny_smith1767.47.20.60
32mandarinmandarin866.24.70.80
42mandarinmandarin846.04.60.79
\n", + "
" + ], + "text/plain": [ + " fruit_label fruit_name fruit_subtype mass width height color_score\n", + "0 1 apple granny_smith 192 8.4 7.3 0.55\n", + "1 1 apple granny_smith 180 8.0 6.8 0.59\n", + "2 1 apple granny_smith 176 7.4 7.2 0.60\n", + "3 2 mandarin mandarin 86 6.2 4.7 0.80\n", + "4 2 mandarin mandarin 84 6.0 4.6 0.79" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 168 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oYRXDVehff1O", + "colab_type": "text" + }, + "source": [ + "## Definir as matrizes de dados X e y" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QbCz9aYpdlG4", + "colab_type": "code", + "colab": {} + }, + "source": [ + "l_feature_names_fruits = ['height', 'width', 'mass', 'color_score']\n", + "df_X_Fruits = df_Fruits[l_feature_names_fruits]\n", + "df_y_Fruits = df_Fruits['fruit_label']\n", + "l_target_names_fruits = ['apple', 'mandarin', 'orange', 'lemon']" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "2Y0e3pX4ewit", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 202 + }, + "outputId": "572a3b11-7577-4c68-931b-295e6e2e46a7" + }, + "source": [ + "df_X_Fruits.head()" + ], + "execution_count": 170, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
heightwidthmasscolor_score
07.38.41920.55
16.88.01800.59
27.27.41760.60
34.76.2860.80
44.66.0840.79
\n", + "
" + ], + "text/plain": [ + " height width mass color_score\n", + "0 7.3 8.4 192 0.55\n", + "1 6.8 8.0 180 0.59\n", + "2 7.2 7.4 176 0.60\n", + "3 4.7 6.2 86 0.80\n", + "4 4.6 6.0 84 0.79" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 170 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wvbx58GQfjh9", + "colab_type": "text" + }, + "source": [ + "## Definir as amostras de treinamento e validação/teste" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IprsJG4QeUgM", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.model_selection import train_test_split\n", + "from sklearn.neighbors import KNeighborsClassifier\n", + "X_train, X_test, y_train, y_test = train_test_split(df_X_Fruits, df_y_Fruits, random_state = 20111974)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2AKSGahrfw6E", + "colab_type": "text" + }, + "source": [ + "## Aplica a transformação para que as features/variáveis tenham a mesma escala\n", + "* Neste caso, vamos aplicar a transformação MinMaxScaler()" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "w5C-fUeLf_X-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.preprocessing import MinMaxScaler\n", + "scaler = MinMaxScaler()\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "\n", + "# Devemos aplicar a transformação scaler no dataframe de validação\n", + "X_test_scaled = scaler.fit_transform(X_test)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PDm64r2BgD9I", + "colab_type": "text" + }, + "source": [ + "## Treinar e testar o modelo" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WYYT-C9DKO0b", + "colab_type": "text" + }, + "source": [ + "### k-NN" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3WlJ5NL8ecJ8", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "40d19f0d-b95b-4881-8d5e-7a17d56a4529" + }, + "source": [ + "knn = KNeighborsClassifier(n_neighbors = 5)\n", + "knn.fit(X_train_scaled, y_train)\n", + "\n", + "print(f'Treinamento - Acurácia do Classificador K-NN: {round(knn.score(X_train_scaled, y_train),2)}')\n", + "print(f'Validação - Acurácia do Classificador K-NN: {round(knn.score(X_test_scaled, y_test),2)}')" + ], + "execution_count": 116, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Treinamento - Acurácia do Classificador K-NN: 0.95\n", + "Validação - Acurácia do Classificador K-NN: 0.67\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UrA6H1b5hYVj", + "colab_type": "text" + }, + "source": [ + "### Teste out-of-sample" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9qrGsN5qgvkP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "104df0d9-cd47-4bac-f1eb-5b42c3f2461c" + }, + "source": [ + "# Considere um novo registro, dado por:\n", + "l_New_Fruit = [[5.5, 2.2, 10, 0.70]]\n", + "l_New_Fruit" + ], + "execution_count": 117, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[5.5, 2.2, 10, 0.7]]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 117 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MxthIGgYhluB", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "3f2a00c5-c507-4ea4-ac3f-ff58910ff1ae" + }, + "source": [ + "# Aplicando a transformação ao novo registro:\n", + "l_New_Fruit_Scaled = scaler.transform(l_New_Fruit)\n", + "l_New_Fruit_Scaled" + ], + "execution_count": 118, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 0.25, -0.95, -0.23, 0.04]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 118 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hZ45ypKChu0X", + "colab_type": "text" + }, + "source": [ + "#### Predição" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ehyg048mh5u4", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "0fc37d26-6975-4d3d-9cbb-6fad52ed3c06" + }, + "source": [ + "new_pred= l_target_names_fruits[knn.predict(l_New_Fruit_Scaled)[0]-1]\n", + "new_pred" + ], + "execution_count": 119, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'lemon'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 119 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1hMG1mgGKck9", + "colab_type": "text" + }, + "source": [ + "### Logistic Regression" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D-iFOabyKe2Z", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "F6ytngwmKez-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "QmJovhegKew9", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZtWrzTVKin82", + "colab_type": "text" + }, + "source": [ + "# Exemplo 2 - Crime" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A4IfufijmkTs", + "colab_type": "text" + }, + "source": [ + "## Carregar o dataframe" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "outputId": "d55c48e9-f743-4a2f-863d-0d303d1029bc", + "id": "DsP9SNNirc2A", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "url= 'https://raw.githubusercontent.com/MathMachado/Python_RFB/DS_Python/Dataframes/Crime.txt?token=AGDJQ665WUIWIEKDPK6WO625P3QUQ'\n", + "df_Crime = pd.read_table(url, sep=',', na_values='?')\n", + "\n", + "# Remover features/variáveis de baixa relevância\n", + "l_Features_Keep = [5, 6] + list(range(11,26)) + list(range(32, 103)) + [145] \n", + "#l_Features_Keep" + ], + "execution_count": 120, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: FutureWarning: read_table is deprecated, use read_csv instead.\n", + " \n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EEQ-2Q9NnBny", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "56d328d0-7e21-4211-f323-8d9b0989d7cb" + }, + "source": [ + "df_Crime2 = df_Crime.iloc[l_Features_Keep].dropna()\n", + "df_Crime2.head()" + ], + "execution_count": 121, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
communitynamestatecountyCodecommunityCodefoldpopulationhouseholdsizeracepctblackracePctWhiteracePctAsianracePctHispagePct12t21agePct12t29agePct16t24agePct65upnumbUrbanpctUrbanmedIncomepctWWagepctWFarmSelfpctWInvIncpctWSocSecpctWPubAsstpctWRetiremedFamIncperCapIncwhitePerCapblackPerCapindianPerCapAsianPerCapOtherPerCapHispPerCapNumUnderPovPctPopUnderPovPctLess9thGradePctNotHSGradPctBSorMorePctUnemployedPctEmployPctEmplManu...LemasTotalReqLemasTotReqPerPopPolicReqPerOfficPolicPerPopRacialMatchCommPolPctPolicWhitePctPolicBlackPctPolicHispPctPolicAsianPctPolicMinorOfficAssgnDrugUnitsNumKindsDrugsSeizPolicAveOTWorkedLandAreaPopDensPctUsePubTransPolicCarsPolicOperBudgLemasPctPolicOnPatrLemasGangUnitDeployLemasPctOfficDrugUnPolicBudgPerPopmurdersmurdPerPoprapesrapesPerPoprobberiesrobbbPerPopassaultsassaultPerPopburglariesburglPerPoplarcenieslarcPerPopautoTheftautoTheftPerPoparsonsarsonsPerPopViolentCrimesPerPopnonViolPerPop
54ToledocityOH95.077000.013329432.5419.7076.961.053.9714.7828.6514.5212.25332943100.02481971.860.3636.3728.9312.2918.4430980118941307777229450124157272.078176242619.098.5726.8414.069.9255.5619.58...351211.0108407.80514.2210.897.9178.7716.984.100.021.0830.012.089.383.53988.53.00222.041348280.080.675.04.39127629.24513.89357.0110.191594.0492.021195.0368.865502.01698.2915251.04707.514517.01394.26357.0110.19984.967910.25
55PhiladelphiacityPA101.060000.0115855772.6339.8653.522.745.6313.9228.0214.1213.741585577100.02460370.120.3532.1531.6313.9818.2030140120911502790611014682855083.0605331337420.2711.2935.6915.229.6252.7713.58...5480855.0358261.41840.2426.479.5473.4823.182.840.025.95273.014.039.9140.011326.029.31822.0287578496.099.940.04.19187978.543928.70785.051.3111531.0753.746821.0445.8615117.0988.1439181.02561.1023785.01554.732282.0149.171279.605253.14
58LynchburgcityVA680.047672.01660492.6326.4172.450.760.7217.7929.8418.1914.3366049100.02372672.250.6339.0635.027.5417.2030141126571472172001283361398400.07231988916.4513.6130.5221.745.9756.2719.31...61164.091072.10443.2205.581.8790.589.420.000.09.428.08.038.751.21290.53.3362.05585480.089.130.05.8083166.834.4745.067.00107.0159.32351.0522.63560.0833.832316.03448.48184.0273.9750.074.45753.424630.73
99NewHaventownCT9.052070.011304742.6636.1453.852.4113.2217.0634.5519.1710.81130474100.02581173.040.4737.0726.2814.6112.7531163129681612796167750117397014.074202548121.2911.8628.9726.709.2556.7116.41...145000.0121233.40371.8326.174.6279.2317.443.330.020.7712.010.0167.419.56679.99.54100.017939552.089.745.03.08149991.22218.39130.0108.691238.01035.081154.0964.853417.02856.937719.06453.801873.01566.00134.0112.042127.0210988.76
\n", + "

4 rows × 147 columns

\n", + "
" + ], + "text/plain": [ + " communityname state ... ViolentCrimesPerPop nonViolPerPop\n", + "54 Toledocity OH ... 984.96 7910.25\n", + "55 Philadelphiacity PA ... 1279.60 5253.14\n", + "58 Lynchburgcity VA ... 753.42 4630.73\n", + "99 NewHaventown CT ... 2127.02 10988.76\n", + "\n", + "[4 rows x 147 columns]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 121 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bi1UQunrnvXY", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "a5fff1f4-6511-4843-cad6-f169a3663dff" + }, + "source": [ + "# Corrigir os nomes das features/variáveis\n", + "df_Crime2.columns= [cols.lower() for cols in df_Crime2.columns]\n", + "df_Crime2.head()" + ], + "execution_count": 122, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
communitynamestatecountycodecommunitycodefoldpopulationhouseholdsizeracepctblackracepctwhiteracepctasianracepcthispagepct12t21agepct12t29agepct16t24agepct65upnumburbanpcturbanmedincomepctwwagepctwfarmselfpctwinvincpctwsocsecpctwpubasstpctwretiremedfamincpercapincwhitepercapblackpercapindianpercapasianpercapotherpercaphisppercapnumunderpovpctpopunderpovpctless9thgradepctnothsgradpctbsormorepctunemployedpctemploypctemplmanu...lemastotalreqlemastotreqperpoppolicreqperofficpolicperpopracialmatchcommpolpctpolicwhitepctpolicblackpctpolichisppctpolicasianpctpolicminorofficassgndrugunitsnumkindsdrugsseizpolicaveotworkedlandareapopdenspctusepubtranspoliccarspolicoperbudglemaspctpoliconpatrlemasgangunitdeploylemaspctofficdrugunpolicbudgperpopmurdersmurdperpoprapesrapesperpoprobberiesrobbbperpopassaultsassaultperpopburglariesburglperpoplarcenieslarcperpopautotheftautotheftperpoparsonsarsonsperpopviolentcrimesperpopnonviolperpop
54ToledocityOH95.077000.013329432.5419.7076.961.053.9714.7828.6514.5212.25332943100.02481971.860.3636.3728.9312.2918.4430980118941307777229450124157272.078176242619.098.5726.8414.069.9255.5619.58...351211.0108407.80514.2210.897.9178.7716.984.100.021.0830.012.089.383.53988.53.00222.041348280.080.675.04.39127629.24513.89357.0110.191594.0492.021195.0368.865502.01698.2915251.04707.514517.01394.26357.0110.19984.967910.25
55PhiladelphiacityPA101.060000.0115855772.6339.8653.522.745.6313.9228.0214.1213.741585577100.02460370.120.3532.1531.6313.9818.2030140120911502790611014682855083.0605331337420.2711.2935.6915.229.6252.7713.58...5480855.0358261.41840.2426.479.5473.4823.182.840.025.95273.014.039.9140.011326.029.31822.0287578496.099.940.04.19187978.543928.70785.051.3111531.0753.746821.0445.8615117.0988.1439181.02561.1023785.01554.732282.0149.171279.605253.14
58LynchburgcityVA680.047672.01660492.6326.4172.450.760.7217.7929.8418.1914.3366049100.02372672.250.6339.0635.027.5417.2030141126571472172001283361398400.07231988916.4513.6130.5221.745.9756.2719.31...61164.091072.10443.2205.581.8790.589.420.000.09.428.08.038.751.21290.53.3362.05585480.089.130.05.8083166.834.4745.067.00107.0159.32351.0522.63560.0833.832316.03448.48184.0273.9750.074.45753.424630.73
99NewHaventownCT9.052070.011304742.6636.1453.852.4113.2217.0634.5519.1710.81130474100.02581173.040.4737.0726.2814.6112.7531163129681612796167750117397014.074202548121.2911.8628.9726.709.2556.7116.41...145000.0121233.40371.8326.174.6279.2317.443.330.020.7712.010.0167.419.56679.99.54100.017939552.089.745.03.08149991.22218.39130.0108.691238.01035.081154.0964.853417.02856.937719.06453.801873.01566.00134.0112.042127.0210988.76
\n", + "

4 rows × 147 columns

\n", + "
" + ], + "text/plain": [ + " communityname state ... violentcrimesperpop nonviolperpop\n", + "54 Toledocity OH ... 984.96 7910.25\n", + "55 Philadelphiacity PA ... 1279.60 5253.14\n", + "58 Lynchburgcity VA ... 753.42 4630.73\n", + "99 NewHaventown CT ... 2127.02 10988.76\n", + "\n", + "[4 rows x 147 columns]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 122 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NT6MLaTloaEk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "b9eb0264-d33a-4908-aff7-4157a04f4be3" + }, + "source": [ + "df_Crime2.shape" + ], + "execution_count": 123, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(4, 147)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 123 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7hbunSKYnpnc", + "colab_type": "code", + "colab": {} + }, + "source": [ + "df_X_Crime = df_Crime2\n", + "\n", + "# A variável-target neste exemplo se chama 'violentcrimesperpop'\n", + "df_X_Crime = df_X_Crime.drop(['violentcrimesperpop'], axis= 1)\n", + "\n", + "# A variável-target neste exemplo se chama 'violentcrimesperpop'\n", + "df_y_Crime = df_Crime2['violentcrimesperpop']" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "4nEo3K5Urg4q", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.linear_model import LinearRegression\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime,\n", + " random_state = 20111974)\n", + "linreg = LinearRegression().fit(X_train, y_train)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "NIUDqIraCEFr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "eef7f4d3-2e78-4ee6-d3ec-8b0a6ca9851d" + }, + "source": [ + "print(f'Modelo Linear com intercepto= {linreg.intercept_}')" + ], + "execution_count": 140, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Modelo Linear com intercepto= 1817.1396229192374\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aGsq5IpeCF5S", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 260 + }, + "outputId": "e579557d-7a3c-4bb8-c682-b8b4ccfe1472" + }, + "source": [ + "print(f'Coeficientes:\\n{linreg.coef_}')" + ], + "execution_count": 139, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Coeficientes:\n", + "[-1.90e-03 5.93e+01 -5.41e+00 -4.88e+01 3.54e+01 -1.45e+01 1.88e-03\n", + " 8.42e-01 -1.54e-02 -1.40e+01 2.07e+01 -4.27e+00 1.20e+00 4.46e+00\n", + " -1.14e+01 6.24e-03 4.02e-03 -2.01e-04 -1.39e+01 -1.88e+01 7.09e+00\n", + " 1.98e+00 2.47e+00 7.49e+00 -5.40e+00 -2.35e+00 3.30e+00 7.89e+00\n", + " 1.67e+02 1.05e+01 1.39e+02 -2.94e+02 -9.26e+01 1.30e+01 -2.85e+01\n", + " 2.21e+00 2.44e-01 4.54e+00 -1.21e+01 -3.52e-03 5.67e+01 1.86e-04\n", + " 1.68e+00 9.72e-01 -2.65e+00 3.05e+00 4.77e+01 -9.38e+01 5.02e+01\n", + " -2.65e+01 1.86e+00 -1.82e+01 1.83e+01 -4.15e+01 7.49e+01 5.96e+02\n", + " -3.14e+02 -5.76e+01 2.58e+01 4.30e+00 2.73e+01 1.36e-02 -1.95e+00\n", + " 5.39e+01 5.95e+00 -1.53e+00 5.05e-01 1.14e+01 3.32e+00 2.35e-04\n", + " -5.67e-04 -1.19e-04 -3.54e-04 -6.59e-01 2.02e-01 -6.12e-01 4.71e-02\n", + " 1.10e+00 -3.63e+00 7.91e+00 -3.36e+01 6.63e-02 -1.31e-01 1.64e+01\n", + " 3.52e-01 4.06e+00 5.63e-01 -7.90e-01]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Kr9kBtxSCHwJ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "4ef5a88f-7a7d-4a49-d532-fbfd91d1ecf1" + }, + "source": [ + "print(f'R-squared Score (treinamento): {linreg.score(X_train, y_train)}')" + ], + "execution_count": 141, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-squared Score (training): 0.6842481991498113\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "X9fdFMM4CJjo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8c68f4d1-123a-48f0-f9fd-3508580352d7" + }, + "source": [ + "print(f'R-squared Score (test): {linreg.score(X_test, y_test)}')" + ], + "execution_count": 142, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-squared Score (test): 0.5870108365195615\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gjk1eiVfCrx7", + "colab_type": "text" + }, + "source": [ + "## Regressão Ridge" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YoP5rjFzrhB_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "ad23ecbd-3fce-47c2-c71d-489253d0f4ba" + }, + "source": [ + "from sklearn.linear_model import Ridge\n", + "X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime, random_state = 20111974)\n", + "\n", + "linridge = Ridge(alpha=20.0).fit(X_train, y_train)" + ], + "execution_count": 132, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Regressão Linear Ridge com Intercepto= : 433.33621790849304\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ATj-k7uqCwq3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "b010f794-e8f6-489a-de5a-ff7052963d6a" + }, + "source": [ + "print(f'Regressão Linear Ridge com Intercepto= : {linridge.intercept_}')" + ], + "execution_count": 143, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Regressão Linear Ridge com Intercepto= : 433.33621790849304\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9j8r0xkcBmqr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 260 + }, + "outputId": "3060f298-1eef-4759-ea3a-df9a8e12f66c" + }, + "source": [ + "print(f'Regressão Linear Ridge com Coeficientes= :\\n{linridge.coef_}')" + ], + "execution_count": 133, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Regressão Linear Ridge com Coeficientes= :\n", + "[-1.63e-03 5.00e+01 -6.09e+00 -5.04e+01 3.78e+01 -1.89e+01 1.52e-03\n", + " 9.56e-01 -7.09e-03 -1.33e+01 1.64e+01 -4.16e+00 9.13e-01 5.24e+00\n", + " -1.23e+01 2.42e-03 4.74e-04 1.98e-04 -1.56e+01 -2.16e+01 8.00e+00\n", + " 1.22e+00 3.70e+00 6.97e+00 -5.27e+00 -2.24e+00 2.91e+00 6.69e+00\n", + " 4.10e+01 1.15e+01 3.39e+00 -3.75e+01 1.94e+01 1.32e+01 -2.65e+01\n", + " 1.87e+00 1.36e-01 4.01e+00 -1.21e+01 -3.64e-03 5.76e+01 1.98e-04\n", + " 2.47e+00 -7.33e-01 -1.66e+00 2.80e+00 1.64e+01 -4.02e+01 1.45e+01\n", + " -1.84e+01 1.52e+00 -1.96e+01 1.96e+01 -3.28e+01 4.53e+01 7.05e+01\n", + " -5.30e+01 -2.20e+01 2.64e+01 3.55e+00 1.43e+01 1.38e-02 -1.96e+00\n", + " 1.89e+01 6.20e+00 -1.58e+00 1.44e+00 1.43e+01 5.13e+00 -8.12e-05\n", + " 1.42e-05 -3.28e-04 -2.47e-04 -7.03e-01 1.69e-01 -6.11e-01 9.21e-02\n", + " 9.82e-01 -6.86e-01 9.25e+00 -3.54e+01 6.85e-02 -1.22e-01 1.68e+01\n", + " 2.39e-01 5.45e+00 5.79e-01 -1.77e+00]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TiajGq1mBx05", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "b0b8b85b-a110-4e05-a935-6cf8b7ce4ba7" + }, + "source": [ + "print(f'R-squared Score (treinamento): {linridge.score(X_train, y_train)}')" + ], + "execution_count": 134, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-squared Score (treinamento): 0.6816534123759052\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "u1IhSiv_B0aA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "3dd4474a-db23-45fa-be4d-7eac5480218d" + }, + "source": [ + "print(f'R-squared Score (test): {linridge.score(X_test, y_test)}')" + ], + "execution_count": 135, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-squared Score (test): 0.5912142914540512\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XKPrPRcQCzl7", + "colab_type": "text" + }, + "source": [ + "## Regressão Ridge com Normalização" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XTDMm2R8rhIt", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.preprocessing import MinMaxScaler\n", + "scaler = MinMaxScaler()\n", + "\n", + "from sklearn.linear_model import Ridge\n", + "X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime, random_state = 20111974)\n", + "\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "\n", + "linridge = Ridge(alpha=20.0).fit(X_train_scaled, y_train)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "uqqGPqYWC6pH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "5d088f76-6a86-4d57-872d-7d64220a140d" + }, + "source": [ + "print(f'Regressão Linear Ridge com intercepto= : {linridge.intercept_}')" + ], + "execution_count": 145, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Regressão Linear Ridge com intercepto= : 901.0084531793458\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DeATFR7NC6ut", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 228 + }, + "outputId": "b3991408-e925-4099-c348-e5b80b90ac7b" + }, + "source": [ + "print(f'Coeficientes:\\n{linridge.coef_}')" + ], + "execution_count": 146, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Coeficientes:\n", + "[ 71.63 55.03 -44.33 -86.32 -57.78 -4.94 71.74 167.39 21.74\n", + " -50.61 -23.88 -194.9 -17.29 112.39 -100.08 -4.14 25.69 70.53\n", + " 24.03 -75.35 89.45 19.21 23.75 -30.92 -136.9 -19.44 -9.85\n", + " 38.77 196.85 108.83 92.75 139.96 98.22 -317.23 -427.48 -258.71\n", + " -248.14 19.99 -98.61 74.46 549.32 43.07 13.5 -21.35 26.69\n", + " 113.37 5.28 10.85 38.61 59.92 66.85 -25.7 122.92 66.03\n", + " 55.02 10.86 53.42 -54.82 125.39 77.77 -8.33 127.34 -107.85\n", + " -4.57 206.43 27.51 44.97 155.85 18.81 -18.88 -6.17 21.99\n", + " 58.34 -72.72 13.49 29.62 111.9 70.42 37.43 58.39 -91.71\n", + " 66.29 43.23 61.56 -89.41 74.48 94.03 4.96]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BTPYP__tC6xz", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "71c98e1f-aa65-45cd-eb66-6e0507ee71ac" + }, + "source": [ + "print(f'R-Squared Score (treinamento): {linridge.score(X_train_scaled, y_train)}')" + ], + "execution_count": 148, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-Squared Score (treinamento): 0.6232854705911746\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e8cDp0hYC61I", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "2d2591ca-f55a-49d9-ad9c-b0df55a526cb" + }, + "source": [ + "print(f'R-Squared Score (test): {linridge.score(X_test_scaled, y_test)}')" + ], + "execution_count": 150, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-Squared Score (test): 0.5648178743415879\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kn7LtNt-D5NJ", + "colab_type": "text" + }, + "source": [ + "## Regressão LASSO" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "iNpIvAgirg_k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.linear_model import Lasso\n", + "from sklearn.preprocessing import MinMaxScaler\n", + "scaler = MinMaxScaler()\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime, random_state = 20111974)\n", + "\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "\n", + "linlasso = Lasso(alpha=2.0, max_iter = 10000).fit(X_train_scaled, y_train)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "TZ8lkKOkEGe8", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "34a2c60a-99c6-4c5e-9c41-cc2730f21680" + }, + "source": [ + "print(f'Regressão Linear LASSO com intercepto: {linlasso.intercept_}')" + ], + "execution_count": 152, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Regressão Linear LASSO com intercepto: 941.7332749983227\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Cvx6BavAEJFX", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 228 + }, + "outputId": "45669887-3a13-4dc7-faa8-f5bd8a2106ce" + }, + "source": [ + "print(f'Coeficientes:\\n{linlasso.coef_}')" + ], + "execution_count": 153, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Coeficientes:\n", + "[ 0. 0. -0. -78.34 -0. 0. 0. 137.07 0.\n", + " -0. 0. -140.39 -0. 0. -7.43 0. 0. 0.\n", + " -0. -0. 0. 0. 0. -0. -165.63 -0. -0.\n", + " 0. 430.93 -0. 0. 0. 0. -0. -910.2 -0.\n", + " -0. -0. -223.9 0. 1753.79 0. 0. -0. 0.\n", + " 49.13 0. 0. 0. 0. -0. 0. 102.78 0.\n", + " 0. 0. 0. 0. 302.52 0. -0. 0. -64.08\n", + " 0. 0. 0. 15.5 12.4 0. 0. 0. 20.82\n", + " 0. -0. 0. 0. 112.79 5.41 0. 0. -99.14\n", + " 0. 0. 112.6 -29.46 0. 0. 0. ]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "C4JqRgIzEJLm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "e498e25a-9eb2-46ca-e0a7-06d33fc42389" + }, + "source": [ + "print(f'R-Squared Score (treinamento): {linlasso.score(X_train_scaled, y_train)}')" + ], + "execution_count": 154, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-Squared Score (treinamento): 0.6370906974012746\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "H0AiU1zMEJPQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "0aa5ac89-cbd6-4c2f-cc1c-01c0e5bc81bc" + }, + "source": [ + "print(f'R-Squared Score (test): {linlasso.score(X_test_scaled, y_test)}')" + ], + "execution_count": 156, + "outputs": [ + { + "output_type": "stream", + "text": [ + "R-Squared Score (test): 0.590060887029305\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eAdGr2OkESGa", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 399 + }, + "outputId": "52043953-5e88-4f88-e406-c80405444062" + }, + "source": [ + "print('Features com não-zero weight (ordenado por magnitude absoluta):')\n", + "for e in sorted (list(zip(list(X_crime), linlasso.coef_)),\n", + " key = lambda e: -abs(e[1])):\n", + " if e[1] != 0:\n", + " print('\\t{}, {:.3f}'.format(e[0], e[1]))" + ], + "execution_count": 157, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Features com não-zero weight (ordenado por magnitude absoluta):\n", + "\tPctKidsBornNeverMar, 1753.791\n", + "\tPctKids2Par, -910.201\n", + "\tMalePctDivorce, 430.930\n", + "\tPctPersDenseHous, 302.521\n", + "\tPctWorkMom, -223.900\n", + "\tPctEmplManu, -165.628\n", + "\tpctWInvInc, -140.391\n", + "\tpctUrban, 137.073\n", + "\tRentQrange, 112.794\n", + "\tPctForeignBorn, 112.602\n", + "\tPctLargHouseFam, 102.775\n", + "\tMedOwnCostPctIncNoMtg, -99.139\n", + "\tagePct12t29, -78.344\n", + "\tPctHousOccup, -64.075\n", + "\tPctImmigRec10, 49.135\n", + "\tPctBornSameState, -29.462\n", + "\tOwnOccHiQuart, 20.824\n", + "\tMedYrHousBuilt, 15.500\n", + "\tPctHousNoPhone, 12.396\n", + "\tpctWRetire, -7.427\n", + "\tMedRent, 5.407\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0slj3K7T4eif", + "colab_type": "text" + }, + "source": [ + "# Exemplo 3 - Predict Breast Cancer" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FtxWXAZt4grz", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.linear_model import LogisticRegression\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 20111974)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i4i8PUZnGBu2", + "colab_type": "text" + }, + "source": [ + "## LogisticRegression" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TVgFwBx0GRDl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "9568ab3c-0756-461f-8591-5277dddf79b6" + }, + "source": [ + "clf = LogisticRegression().fit(X_train, y_train)" + ], + "execution_count": 177, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n", + " FutureWarning)\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "seTLgBANFLHc", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "0b54fcd0-ea64-4725-9256-2deb3a51de4b" + }, + "source": [ + "print(f'Acurácia do Classificador Regressão Logística')\n", + "print(f'Treinamento: {clf.score(X_train, y_train)}')\n", + "print(f'Validação..: {clf.score(X_test, y_test)}')" + ], + "execution_count": 178, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Acurácia do Classificador Regressão Logística\n", + "Treinamento: 0.9530516431924883\n", + "Validação..: 0.951048951048951\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1jibpf-qGYKx", + "colab_type": "text" + }, + "source": [ + "## LinearSVC" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "G6k4GwS0Gci9", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.svm import LinearSVC\n", + "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 20111974)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "K2Y71ytP4q8S", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 124 + }, + "outputId": "e244e2b8-f7e9-4652-e76a-3df6064d298a" + }, + "source": [ + "# Treinamento\n", + "clf = LinearSVC().fit(X_train, y_train)\n", + "\n", + "print(f'Acurácia do Classificador LinearSVC')\n", + "print(f'Treinamento: {clf.score(X_train, y_train)}')\n", + "print(f'Validação..: {clf.score(X_test, y_test)}')" + ], + "execution_count": 180, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Acurácia do Classificador LinearSVC\n", + "Treinamento: 0.8826291079812206\n", + "Validação..: 0.8951048951048951\n" + ], + "name": "stdout" + }, + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:929: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n", + " \"the number of iterations.\", ConvergenceWarning)\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Cs0sF19zG8Vw", + "colab_type": "text" + }, + "source": [ + "## SVM - Features não-normalizado" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "p9x4ZagD4wll", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sklearn.svm import SVC\n", + "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 20111974)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s0o9CBH_HKoe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "bf2996cf-bdeb-4c56-e937-69bd10dce5bd" + }, + "source": [ + "clf = SVC(C=10).fit(X_train, y_train)" + ], + "execution_count": 182, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", + " \"avoid this warning.\", FutureWarning)\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EqYOhGkU4rBm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "outputId": "0ac325b4-9714-4be2-c02c-88ba75ba1812" + }, + "source": [ + "print(f'Dataframe: Breast Cancer (Features não-normalizadas)')\n", + "print(f'Acurácia do RBF-kernel SVC')\n", + "print(f'Treinamento: {clf.score(X_train, y_train)}')\n", + "print(f'Treinamento: {clf.score(X_test, y_test)}')" + ], + "execution_count": 183, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Dataframe: Breast Cancer (Features não-normalizadas)\n", + "Acurácia do RBF-kernel SVC\n", + "Treinamento: 1.0\n", + "Treinamento: 0.6223776223776224\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "85HV2o8ZLfRl", + "colab_type": "text" + }, + "source": [ + "## SVM - Features Normalizadas" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HilWCqwdLmUC", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 245 + }, + "outputId": "97588ff0-2c40-4e27-c1c2-2ab5a042ead9" + }, + "source": [ + "from sklearn.preprocessing import MinMaxScaler\n", + "scaler = MinMaxScaler()\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "\n", + "# Treinamento do algoritmo\n", + "clf = SVC(C=10).fit(X_train_scaled, y_train)\n", + "\n", + "# Cross-Validation\n", + "cv_scores = cross_val_score(clf, X_train_scaled, y_train)\n", + "print(f'CV-Score (3-fold): {cv_scores}')\n", + "print(f'Média do CV-Score: {np.mean(cv_scores)}')" + ], + "execution_count": 190, + "outputs": [ + { + "output_type": "stream", + "text": [ + "CV-Score (3-fold): [0.98 0.96 0.96]\n", + "Média do CV-Score: 0.9670855066419946\n" + ], + "name": "stdout" + }, + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", + " \"avoid this warning.\", FutureWarning)\n", + "/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_split.py:1978: FutureWarning: The default value of cv will change from 3 to 5 in version 0.22. Specify it explicitly to silence this warning.\n", + " warnings.warn(CV_WARNING, FutureWarning)\n", + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", + " \"avoid this warning.\", FutureWarning)\n", + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", + " \"avoid this warning.\", FutureWarning)\n", + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", + " \"avoid this warning.\", FutureWarning)\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oCbD5FhPLyE-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "outputId": "d0d97665-16c1-42ac-97ff-04b12c33d51d" + }, + "source": [ + "print(f'Dataframe: Breast Cancer (Features Normalizadas)')\n", + "print(f'Acurácia do RBF-kernel SVC com MinMax scaling)')\n", + "print(f'Treinamento: {clf.score(X_train_scaled, y_train)}')\n", + "print(f'Treinamento: {clf.score(X_test_scaled, y_test)}')" + ], + "execution_count": 186, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Dataframe: Breast Cancer (Features Normalizadas)\n", + "Acurácia do RBF-kernel SVC com MinMax scaling)\n", + "Treinamento: 0.9765258215962441\n", + "Treinamento: 0.9790209790209791\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dgMLxkuUOWUr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 17 + }, + "outputId": "0894aaf4-3a90-403a-deba-429f10703868" + }, + "source": [ + "# This code based on scikit-learn validation_plot example\n", + "# See: http://scikit-learn.org/stable/auto_examples/model_selection/plot_validation_curve.html\n", + "plt.figure()\n", + "\n", + "train_scores_mean = np.mean(train_scores, axis=1)\n", + "train_scores_std = np.std(train_scores, axis=1)\n", + "test_scores_mean = np.mean(test_scores, axis=1)\n", + "test_scores_std = np.std(test_scores, axis=1)\n", + "\n", + "plt.title('Validation Curve with SVM')\n", + "plt.xlabel('$\\gamma$ (gamma)')\n", + "plt.ylabel('Score')\n", + "plt.ylim(0.0, 1.1)\n", + "lw = 2\n", + "\n", + "plt.semilogx(param_range, train_scores_mean, label='Training score',\n", + " color='darkorange', lw=lw)\n", + "\n", + "plt.fill_between(param_range, train_scores_mean - train_scores_std,\n", + " train_scores_mean + train_scores_std, alpha=0.2,\n", + " color='darkorange', lw=lw)\n", + "\n", + "plt.semilogx(param_range, test_scores_mean, label='Cross-validation score',\n", + " color='navy', lw=lw)\n", + "\n", + "plt.fill_between(param_range, test_scores_mean - test_scores_std,\n", + " test_scores_mean + test_scores_std, alpha=0.2,\n", + " color='navy', lw=lw)\n", + "\n", + "plt.legend(loc='best')\n", + "plt.show()" + ], + "execution_count": 200, + "outputs": [ + { + "output_type": "display_data", + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " fig.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('