Skip to content
Snippets Groups Projects
07_regression.ipynb 470 KiB
Newer Older
  • Learn to ignore specific revisions
  • schmittu's avatar
    schmittu committed
        "\n",
        "from sklearn.pipeline import make_pipeline\n",
        "from sklearn.preprocessing import StandardScaler, PolynomialFeatures\n",
        "from sklearn.kernel_ridge import KernelRidge\n",
        "from sklearn.linear_model import LinearRegression\n",
        "from sklearn.svm import SVR\n",
        "from sklearn.tree import DecisionTreeRegressor \n",
        "from sklearn.model_selection import cross_val_score\n",
        "from sklearn.decomposition import PCA\n",
        "\n",
        "\n",
        "sockey_indices = features[\"kind\"] == 1\n",
        "features_sockeye = features[sockey_indices]\n",
        "values_sockeye = values[sockey_indices]\n",
        "\n",
        "atlantic_indices = features[\"kind\"] == 0\n",
        "features_atlantic = features[atlantic_indices]\n",
        "values_atlantic = values[atlantic_indices]\n",
        "\n",
        "\n",
        "def eval_clf(clf):\n",
        "    print(clf)\n",
        "    p = make_pipeline(PolynomialFeatures(), PCA(), DecisionTreeRegressor())\n",
        "\n",
        "    param_grid = {'polynomialfeatures__degree': range(3, 12),\n",
        "                  'pca__n_components': range(1, 10),\n",
        "                 }\n",
        "\n",
        "    search = GridSearchCV(p, param_grid, scoring=\"neg_median_absolute_error\", cv=4, n_jobs=4)\n",
        "\n",
        "    search.fit(features, values)\n",
        "    print(\"! FULL DATASET:  best_score    = {:.2f}\".format(search.best_score_))\n",
        "    score_full = search.best_score_\n",
        "\n",
        "    search.fit(features_atlantic, values_atlantic)\n",
        "    print(\"  ATLANTIC    :  best_score    = {:.2f}\".format(search.best_score_))\n",
        "    score_atlantic = search.best_score_\n",
        "\n",
        "    search.fit(features_sockeye, values_sockeye)\n",
        "    print(\"  SOCKEYE     :  best_score    = {:.2f}\".format(search.best_score_))\n",
        "    score_sockeye = search.best_score_\n",
        "\n",
        "    print(\"! COMBINED    :  average_score = {:.2f}\".format((score_atlantic + score_sockeye) / 2.0))\n",
        "    \n",
        "    \n",
        "eval_clf(LinearRegression())\n",
        "print()\n",
        "\n",
        "eval_clf(DecisionTreeRegressor())\n",
        "print()\n",
        "\n",
        "eval_clf(KernelRidge())\n",
        "print()\n",
        "\n",
        "eval_clf(KernelRidge(kernel=\"rbf\"))\n",
        "print()\n",
        "\n",
        "eval_clf(SVR())"
       ]
    
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": []
    
      }
     ],
     "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.7.2"
      },
      "latex_envs": {
       "LaTeX_envs_menu_present": true,
       "autoclose": false,
       "autocomplete": true,
       "bibliofile": "biblio.bib",
       "cite_by": "apalike",
       "current_citInitial": 1,
       "eqLabelWithNumbers": true,
       "eqNumInitial": 1,
       "hotkeys": {
        "equation": "Ctrl-E",
        "itemize": "Ctrl-I"
       },
       "labels_anchors": false,
       "latex_user_defs": false,
       "report_style_numbering": false,
       "user_envs_cfg": false
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }