You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
52 KiB
Plaintext

6 months ago
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ae02d6c8",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn import linear_model"
]
},
{
"cell_type": "markdown",
"id": "18cfa351",
"metadata": {},
"source": [
"<img src='linear_regression.png' width=50%>"
]
},
{
"cell_type": "markdown",
"id": "e7ac8c6d",
"metadata": {},
"source": [
"$$最小化\\sum^n_{i=1}(d_i)^2$$"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6729b5ba",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"housing_price.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "717fda9f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>area</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>86.45</td>\n",
" <td>117.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>91.57</td>\n",
" <td>98.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>85.52</td>\n",
" <td>114.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>103.60</td>\n",
" <td>146.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>105.25</td>\n",
" <td>106.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>99.00</td>\n",
" <td>109.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>87.95</td>\n",
" <td>91.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" area price\n",
"0 86.45 117.0\n",
"1 91.57 98.0\n",
"2 85.52 114.0\n",
"3 103.60 146.0\n",
"4 105.25 106.0\n",
"5 99.00 109.0\n",
"6 87.95 91.5"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8bae038f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x129771424c8>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjsAAAGwCAYAAABPSaTdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAvn0lEQVR4nO3dfVjUdb7/8dcAiqQyBCYwgohpamlGuhEVpclVaGu26JbGZdaans7J0ujGPP3Ubo/dbanlydOte7WWbR2yPbbZmlbiSqQW282WR40UFHSNANFUbj6/P+YwOXEj4sDMfHw+rmsunc/3M595f/zyZV5+78ZhjDECAACwVIi/CwAAAGhPhB0AAGA1wg4AALAaYQcAAFiNsAMAAKxG2AEAAFYj7AAAAKuF+buAQFBfX689e/aoe/fucjgc/i4HAAC0gjFGBw4ckMvlUkhI8/tvCDuS9uzZo8TERH+XAQAA2qC4uFgJCQnNLifsSOrevbsk9z9WZGSkn6sBAACtUVVVpcTERM/neHMIO5Ln0FVkZCRhBwCAIHO8U1A4QRkAAFiNsAMAAKxG2AEAAFYj7AAAAKsRdgAAgNUIOwAAwGqEHQAAYDXCDgAAsB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5s9TgHDx7U0KFDtWTJkkbLDh06pM8++0xz587VZ599ptzcXG3dulVXX321V7/s7Gx9/fXXWrNmjVatWqX169dr+vTpJz4pAABgpbC2vCg5OVmFhYVKSkryal+9erUGDRr
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"plt.xlabel(\"area\")\n",
"plt.ylabel(\"price\")\n",
"plt.scatter(df.area, df.price, c='r')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1dbdd68d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 1)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[[\"area\"]].shape"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "c147b635",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression()"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = linear_model.LinearRegression()\n",
"model.fit(df[[\"area\"]].values, df.price.values)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "cd1a7008",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([0.83809367]), 32.70161672527138)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.coef_, model.intercept_"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "d7e74d63",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([115.93269948])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.predict(np.array([[99.31]]))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "4fffcc21",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([115.93269948])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price = model.coef_ * 99.31 + model.intercept_\n",
"price"
]
},
{
"cell_type": "markdown",
"id": "410fa831",
"metadata": {},
"source": [
"画出这条预测线"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "a5fe689e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\envs\\stark-lin\\lib\\site-packages\\numpy\\core\\shape_base.py:65: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
" ary = asanyarray(ary)\n"
]
},
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x12978a40ec8>]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjsAAAGwCAYAAABPSaTdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAABIoUlEQVR4nO3deViVdf7/8edhR2URlE1BUVEQLE3LTK1MJrdUtL6NDVPWNDYtlmYzpZNgblnN1KjV1GRN9pupnJlG0ax0HDWXMrfUBDfcFQVUhMMi2zn37w+KidRCBO5zDq/HdXEV9/05N+/zKTkv7+XzthiGYSAiIiLiotzMLkBERESkISnsiIiIiEtT2BERERGXprAjIiIiLk1hR0RERFyawo6IiIi4NIUdERERcWkeZhfgCOx2O6dOncLPzw+LxWJ2OSIiIlILhmFQWFhIREQEbm6XP3+jsAOcOnWKyMhIs8sQERGROjhx4gRt27a97H6FHcDPzw+omix/f3+TqxEREZHasFqtREZGVn+OX47CDlRfuvL391fYERERcTI/dQuKblAWERERl6awIyIiIi5NYUdERERcmsKOiIiIuDSFHREREXFpCjsiIiLi0hR2RERExKUp7IiIiIhLU9gRERERl6YVlEVERFydzQYbNsDp0xAeDv37g7u72VU1GoUdERERV7Z4MUyYACdP/m9b27Ywbx6MHm1eXY1Il7FERERc1eLFcN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Pxxx/j5ubGnXfeyfz582nRokWt69C
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"plt.xlabel(\"area\")\n",
"plt.ylabel(\"price\")\n",
"plt.scatter(df.area, df.price, c='r')\n",
"plt.plot([0, 111],[model.intercept_, model.predict(np.array([[111]]))])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "f7253975",
"metadata": {},
"outputs": [],
"source": [
"preds = pd.read_csv(\"predict.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "a27fe359",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>area</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>128</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>111</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>138</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" area\n",
"0 150\n",
"1 128\n",
"2 111\n",
"3 138"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"preds"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "a7ac643a",
"metadata": {},
"outputs": [],
"source": [
"preds[\"predicted\"] = model.predict(preds[[\"area\"]].values)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "bef47aea",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>area</th>\n",
" <th>predicted</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>150</td>\n",
" <td>158.415668</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>128</td>\n",
" <td>139.977607</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>111</td>\n",
" <td>125.730015</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>138</td>\n",
" <td>148.358544</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" area predicted\n",
"0 150 158.415668\n",
"1 128 139.977607\n",
"2 111 125.730015\n",
"3 138 148.358544"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"preds"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e29158e1",
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}