{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F23_linear_model_coefficients.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/23_linear_model_coefficients.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #23 ([video](https://www.youtube.com/watch?v=JmYR283vCdw&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=23))\n", "\n", "Q: How do you display the intercept & coefficients for a linear model?\n", "\n", "A: They are stored as attributes of the model: `intercept_` and `coef_`\n", "\n", "Attributes end with `_` if they are set during \"fit\"\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_diabetes\n", "dataset = load_diabetes()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "X, y = dataset.data, dataset.target\n", "features = dataset.feature_names" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LinearRegression\n", "model = LinearRegression()\n", "model.fit(X, y);" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "152.1334841628965" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.intercept_" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ -10.01219782, -239.81908937, 519.83978679, 324.39042769,\n", " -792.18416163, 476.74583782, 101.04457032, 177.06417623,\n", " 751.27932109, 67.62538639])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.coef_" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('age', -10.012197817470962),\n", " ('sex', -239.81908936565566),\n", " ('bmi', 519.8397867901349),\n", " ('bp', 324.39042768937657),\n", " ('s1', -792.1841616283053),\n", " ('s2', 476.7458378236622),\n", " ('s3', 101.04457032134493),\n", " ('s4', 177.06417623225025),\n", " ('s5', 751.2793210873947),\n", " ('s6', 67.62538639104369)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# display the feature names with the coefficients\n", "list(zip(features, model.coef_))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }