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.

309 lines
22 KiB
Plaintext

6 months ago
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"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": "code",
"execution_count": 3,
"id": "6729b5ba",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"housing_price.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8bae038f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x193b20e02c8>"
]
},
"execution_count": 5,
"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/neHMIO5Ln0FVkZCRhBwCAIHO8U1A4QRkAAFiNsAMAAKxG2AEAAFYj7AAAAKsRdgAAgNUIOwAAwGqEHQAAYDXCDgAAsBphBwAAWI07KAMAYIO6OikvTyotleLjpfR0KTTU31UFBMIOAADBLjdXmjlTKin5uS0hQVq0SMrK8l9dAYLDWAAABLPcXGnCBO+gI0m7d7vbc3P9U1cAIewAABCs6urce3SMabysoW3WLHe/UxhhBwC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scMDw9XZGSk1wMAANgp4MPOL9XX1+vIkSOSpLS0NFVUVGjLli2e5evWrVN9fb1SU1P9VSIAAAggYf588+rqam3fvt3zvKioSIWFhYqOjlZMTIweeeQRXX311YqPj9f+/fu1ZMkS7d69W7/97W8lSYMGDVJmZqamTZumpUuXqqamRjNmzNDEiRNbfSUWAACwm1/DzubNmzVy5EjP85ycHEnSlClTtHTpUn377bf6wx/+oP379ysmJka/+tWvlJeXp3POOcfzmuXLl2vGjBkaNWqUQkJCNH78eC1evLjD5wIAAAJTwNxnx5+4zw4AAMHHuvvsAAAAtAVhBwAAWI2wAwAArEbYAQAAViPsAAAAqxF2AACA1Qg7AADAaoQdAABgNcIOAACwGmEHAABYjbADAACsRtgBAABWI+wAAACrEXYAAIDVCDsAAMBqhB0AAGA1wg4AALAaYQcAAFiNsAMAAKxG2AEAAFYj7AAAAKsRdgAAgNUIOwAAwGqEHQAAYDXCDgAAsBphBwAAWI2wAwAArEbYAQAAViPsAAAAqxF2AACA1Qg7AADAaoQdAABgNcIOAACwGmEHAABYjbADAACsRtgBAABWI+wAAACrEXYAAIDVCDsAAMBqhB0AAGA1wg4AALAaYQcAAFiNsAMAAKxG2AEAAFYj7AAAAKsRdgAAgNUIOwAAwGqEHQAAYDXCDgAAsFqbw86rr76qiy++WC6XSzt37pQkLVy4UO+8847PigMAADhZbQo7zz33nHJycjRmzBhVVFSorq5OkhQVFaWFCxf6sj4AAICT0qaw88wzz+iFF17Qfffdp9DQUE/78OHD9eWXX7Z6nPXr12vs2LFyuVxyOBxauXKlZ1lNTY1mz56tIUOGqGvXrnK5XLrhhhu0Z88erzHKy8uVnZ2tyMhIRUVFaerUqaqurm7LtAAAgIXaFHaKioqUkpLSqD08PFwHDx5s9TgHDx7U0KFDtWTJkkbLDh06pM8++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": 6,
"id": "1dbdd68d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 1)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[[\"area\"]].shape"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c147b635",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression()"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = linear_model.LinearRegression()\n",
"model.fit(df[[\"area\"]].values, df.price.values)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "cd1a7008",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([0.83809367]), 32.70161672527138)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.coef_, model.intercept_"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d7e74d63",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([115.93269948])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.predict(np.array([[99.31]]))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4fffcc21",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([115.93269948])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price = model.coef_ * 99.31 + model.intercept_\n",
"price"
]
},
{
"cell_type": "markdown",
"id": "1859e8a2",
"metadata": {},
"source": [
"## 保存模型"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e29158e1",
"metadata": {},
"outputs": [],
"source": [
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "757f7233",
"metadata": {},
"outputs": [],
"source": [
"with open(\"linear_model.pkl\")"
]
}
],
"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
}