{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "from sklearn import metrics\n", "from sklearn import datasets\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn import metrics\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from matplotlib.pylab import rcParams\n", "rcParams['figure.figsize'] = 8, 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def linear_prediction(plot_dict):\n", " for noise in plot_dict:\n", " X, y = datasets.make_regression(n_features=1, random_state = 42, noise = noise)\n", " model = LinearRegression()\n", " model.fit(X, y)\n", " prediction = model.predict(X)\n", " mae = metrics.mean_absolute_error(prediction, y)\n", " mse = metrics.mean_squared_error(prediction, y)\n", " r2 = metrics.r2_score(prediction, y)\n", " \n", " plt.subplot(plot_dict[noise])\n", " plt.xlabel('prediction')\n", " plt.ylabel('actual')\n", " plt.tight_layout()\n", " plt.plot(prediction, y,'.')\n", " plt.title('Plot for noise: %d'%noise + '\\n' + 'mae:%.2f'%mae\n", " + '\\n' + 'mse:%.2f'%mse\n", " + '\\n' + 'r2:%.2f'%r2)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 觀察不同scatter plot和evaluation metrics的關係\n", "plot_dict = {1:141, 9:142, 18:143, 1000:144}\n", "linear_prediction(plot_dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }