基于Flask+Bootstrap+机器学习的南昌市租房价格预测系统(下)

简介: 基于Flask+Bootstrap+机器学习的南昌市租房价格预测系统

基于Flask+Bootstrap+机器学习的南昌市租房价格预测系统(上):https://developer.aliyun.com/article/1434867


2.3.4数据可视化


manager.py

@app.route('/display_data')
def display_data():  
    return render_template('display_data.html')


display_data.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据可视化</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xl-2">
        <div class="top" style="margin-top: 40px;margin-left: 40px;">
          <ul class="nav flex-column">
              <li class="nav-item" >
                <a class="nav-link" href="" >
                  <button type="button" class="btn btn-outline-primary" disabled>1.导入数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>2.查看数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>3.数据预处理</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>4.数据可视化</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="{{url_for('feature_engineering')}}">
                  <button type="button" class="btn btn-outline-primary">5.特征工程</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>6.构建模型</button>  
                </a>
              </li>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="">
                <button type="button" class="btn btn-outline-primary" disabled>7.保存模型</button>  
              </a>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="{{url_for('index')}}">
                <button type="button" class="btn btn-outline-primary" disabled>返回首页</button>  
              </a>
            </li>
            </ul>
      </div>
      </div>
      <div class="col-xl-10" style="margin-top: 10px;">
        <div class="card" >
          <div class="card-body">
            <img src="../static/img/display_data4.png">
            <img src="../static/img/display_data3.png">
            <img src="../static/img/display_data2.png"  width="1160px">
            <img src="../static/img/display_data1.png"  width="1160px">
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


2.3.5特征工程


manager.py

@app.route('/feature_engineering')
def feature_engineering():  
    return render_template('feature_engineering.html')


feature_engineering.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>特征工程</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xl-2">
        <div class="top" style="margin-top: 40px;margin-left: 40px;">
          <ul class="nav flex-column">
              <li class="nav-item" >
                <a class="nav-link" href="" >
                  <button type="button" class="btn btn-outline-primary" disabled>1.导入数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>2.查看数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>3.数据预处理</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>4.数据可视化</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>5.特征工程</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="{{url_for('build_model')}}">
                  <button type="button" class="btn btn-outline-primary" >6.构建模型</button>  
                </a>
              </li>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="">
                <button type="button" class="btn btn-outline-primary" disabled>7.保存模型</button>  
              </a>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="{{url_for('index')}}">
                <button type="button" class="btn btn-outline-primary" disabled>返回首页</button>  
              </a>
            </li>
            </ul>
      </div>
      </div>
      <div class="col-xl-10">
        <div class="card" style="margin-top: 20px;">
          <div class="card-body">
            <img src="../static/img/feature_engineering.png" >
              <br>
              <br>
              <p>特征工程完毕!!!</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


2.3.6构建模型


manager.py

@app.route('/build_model')
def build_model():  
    return render_template('build_model.html')


build_model.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>构建模型</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xl-2">
        <div class="top" style="margin-top: 40px;margin-left: 40px;">
          <ul class="nav flex-column">
              <li class="nav-item" >
                <a class="nav-link" href="{{url_for('load_data')}}" >
                  <button type="button" class="btn btn-outline-primary">1.导入数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>2.查看数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>3.数据预处理</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>4.数据可视化</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>5.特征工程</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>6.构建模型</button>  
                </a>
              </li>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="{{url_for('save_model')}}">
                <button type="button" class="btn btn-outline-primary">7.保存模型</button>  
              </a>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="{{url_for('index')}}">
                <button type="button" class="btn btn-outline-primary" disabled>返回首页</button>  
              </a>
            </li>
            </ul>
      </div>
      </div>
      <div class="col-xl-10">
        <div class="card" style="margin-top: 20px;">
          <div class="card-body">
            <p>Model is:  RandomForestRegressor()</p>
            <p>Training score:  0.9049714978258847</p>
            <p>r2 score is:  0.5589430756516771</p>
            <p>MAE: 451.4159751799188</p>
            <p>MSE: 450572.2871418395</p>
            <p>RMSE: 671.2468153681174</p>
            <br>
            <p>真实值和预测值的差值图如下:</p>
            <img src="../static/img/build_model1.png" alt="">
            <img src="../static/img/build_model2.png" alt="">
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


2.3.7保存模型


manager.py

@app.route('/save_model')
def save_model():
    return render_template('save_model.html')


save_model.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>构建模型</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xl-2">
        <div class="top" style="margin-top: 40px;margin-left: 40px;">
          <ul class="nav flex-column">
              <li class="nav-item" >
                <a class="nav-link" href="{{url_for('load_data')}}" >
                  <button type="button" class="btn btn-outline-primary">1.导入数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>2.查看数据</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>3.数据预处理</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>4.数据可视化</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>5.特征工程</button>  
                </a>
              </li>
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
                <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
              </svg>
              <li class="nav-item" >
                <a class="nav-link" href="">
                  <button type="button" class="btn btn-outline-primary" disabled>6.构建模型</button>  
                </a>
              </li>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="">
                <button type="button" class="btn btn-outline-primary" disabled>7.保存模型</button>  
              </a>
            </li>
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down" viewBox="0 0 16 16" style="margin-left: 60px;">
              <path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/>
            </svg>
            <li class="nav-item" >
              <a class="nav-link" href="{{url_for('main')}}">
                <button type="button" class="btn btn-outline-primary">返回首页</button>  
              </a>
            </li>
            </ul>
      </div>
      </div>
      <div class="col-xl-10">
        <div class="card" style="margin-top: 20px;">
          <div class="card-body">
            <p>模型保存成功!!!</p>
            <br>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


2.4预测房价模块


manager.py

class PredictView(views.MethodView):    
    def __jump(self,result=None,error=None):      
        return render_template('predict_price.html',result=result,error=error)    
    def get(self, result=None,error=None):        
        return self.__jump()    
    def post(self):
        try:        
            address = request.form['address']      
            derection = request.form['derection']        
            floor = request.form['floor']        
            elevator = request.form['elevator']        
            area = float(request.form['area'])      
            new_data = pd.DataFrame(data=[[address,derection,area,floor,elevator]],columns=['address','derection','area','floor','elevator'])
            new_data['address'].replace(to_replace={'南昌县':0,'红谷滩':1,'新建区':2,'高新区':3,'东湖区':4,'西湖区':5,'经开区':6,'青山湖区':7,'青云谱区':8,'湾里区':9,'进贤县':10},inplace=True)
            new_data['derection'].replace(to_replace={'精装修':1,'简装修':0},inplace=True)
            new_data['floor'].replace(to_replace={'高':0,'中':1,'低':2},inplace=True)
            new_data['elevator'].replace(to_replace={'有':1,'无':0},inplace=True)
            predict = model.predict(new_data)[0]
            return self.__jump(result=predict) 
        except:
            return self.__jump(error='输入数据格式不对,请重新输入!')   
app.add_url_rule('/predict_price/',view_func=PredictView.as_view('my_predict'))


predict_price.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>预测租房价格</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="{{url_for('main')}}">首页</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
              <div class="navbar-nav">
                <a class="nav-link" href="{{url_for('train_model')}}">训练模型<span class="sr-only">(current)</span></a>
                <a class="nav-link active" href="{{url_for('predict_price')}}">预测租房价格</a>
                <a class="nav-link" href="{{url_for('my_look')}}">查看租房价格信息</a>
              </div>
            </div>
          </nav>
          <div class="container-fluid">
            <div class="row">
              <div class="col col-lg-2">
              </div>
              <div class="col-md-auto">
                  <form action="/predict_price/" method="post">
                    <div class="form-group row">
                        <label for="inputPassword" class="col-sm-4 col-form-label">请选择地区:</label>
                        <div class="col-sm-8">
                          <input type="text" placeholder="南昌县/红谷滩/新建区/..." name="address" class="form-control">
                        </div>
                      </div>
                    <div class="form-group row">
                        <label for="inputPassword" class="col-sm-4 col-form-label">装修类型:</label>
                        <div class="col-sm-8">
                          <input type="text" placeholder="简装修/精装修" name="derection" class="form-control">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="inputPassword" class="col-sm-4 col-form-label">楼层情况:</label>
                        <div class="col-sm-8">
                          <input type="text" placeholder="高/中/低" name="floor" class="form-control">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="inputPassword" class="col-sm-4 col-form-label">电梯情况:</label>
                        <div class="col-sm-8">
                          <input type="text" placeholder="有/无" name="elevator" class="form-control">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="inputPassword" class="col-sm-4 col-form-label">房屋面积:</label>
                        <div class="col-sm-8">
                          <input type="text" placeholder="㎡" name="area" class="form-control" id="inputPassword">
                        </div>
                    <button style="margin-left: 10px;" type="submit" class="btn btn-primary">预测</button>
                  </form>
              </div>
              {% if error %}
              <font color="red">{{ error }}</font>
              {% endif %}
              {% if result %}
              <font color="red">预测的房价为:{{ result }}</font>
              {% endif %}
              <div class="col col-lg-2">
              </div>
            </div>
          </div>
    </div>
</body>
</html>


2.4查看房价信息模块


manager.py

class LookPriceView(views.MethodView):    
    def __jump(self,houses=None,error=None):      
        return render_template('look_house_price.html',houses=houses,error=error)    
    def get(self, houses=None,error=None): 
        data = pd.read_csv('lianjia.csv')
        houses = []
        for item in data.values:
            house = {}
            house['address'] = item[1]
            house['price'] = item[2]
            house['lease method'] = item[3]
            house['layout'] = item[4]
            house['derection'] = item[5]
            house['area'] = item[6]
            house['orientation'] = item[7]
            house['floor'] = item[8]
            house['elevator'] = item[9]
            house['water'] = item[10]
            house['power'] = item[11]
            house['gas'] = item[12]
            houses.append(house)
        return self.__jump(houses=houses)    
    def post(self):
        try:        
            min_price = float(request.form['min'])   
            max_price = float(request.form['max'])
            data = pd.read_csv('lianjia.csv')
            data = data[(data['price']>min_price) & (data['price']<max_price)]  
            houses = []
            for item in data.values:
                house = {}
                house['address'] = item[1]
                house['price'] = item[2]
                house['lease method'] = item[3]
                house['layout'] = item[4]
                house['derection'] = item[5]
                house['area'] = item[6]
                house['orientation'] = item[7]
                house['floor'] = item[8]
                house['elevator'] = item[9]
                house['water'] = item[10]
                house['power'] = item[11]
                house['gas'] = item[12]
                houses.append(house)
            return self.__jump(houses=houses) 
        except:
            return self.__jump(error='输入数据格式不对,请重新输入!')
app.add_url_rule('/look_price/',view_func=LookPriceView.as_view('my_look'))


look_house_price.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查看租房价格信息</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css">
    <link rel="stylesheet" href="../static/main.css">
</head>
<body>
    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="{{url_for('main')}}">首页</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
              <div class="navbar-nav">
                <a class="nav-link" href="{{url_for('train_model')}}">训练模型<span class="sr-only">(current)</span></a>
                <a class="nav-link" href="{{url_for('predict_price')}}">预测租房价格</a>
                <a class="nav-link active" href="{{url_for('my_look')}}">查看租房价格信息</a>
              </div>
            </div>
          </nav>
          <div>
            <form action="/look_price/" method="post">
              <div class="form-group row">
                  <label for="inputPassword" class="col-sm-2 col-form-label">请输入价格区间:</label>
                  <div class="col-sm-3">
                    <input type="text" placeholder="1000" name="min" class="form-control">
                  </div>
                  <div class="col-sm-1">
                    ——
                  </div>
                  <div class="col-sm-3">
                    <input type="text" placeholder="2000" name="max" class="form-control">
                  </div>
                  <div class="col-sm-3">
                    <input type="submit" value="搜索" class="btn btn-primary">
                  </div>
                </div>
            </form>
            <section class="counts section-bg">
              <div class="container">
                <table class="table text-nowrap">
                  <tr class="text-center">
                    <td>address</td>
                    <td>lease method</td>
                    <td>layout</td>
                    <td>derection</td>
                    <td>area</td>
                    <td>orientation</td>
                    <td>floor</td>
                    <td>elevator</td>
                    <td>water</td>
                    <td>power</td>
                    <td>gas</td>
                    <td>price</td>
                  </tr>
                  {% for house in houses %}
                  <tr class="text-center">
                    <td>{{ house.address }}</td>
                    <td>{{ house['lease method'] }}</td>
                    <td>{{ house.layout }}</td>
                    <td>{{ house.derection }}</td>
                    <td>{{ house.area }}</td>
                    <td>{{ house.orientation }}</td>
                    <td>{{ house.floor }}</td>
                    <td>{{ house.elevator }}</td>
                    <td>{{ house.water }}</td>
                    <td>{{ house.power }}</td>
                    <td>{{ house.gas }}</td>
                    <td>{{ house.price }}</td>
                  </tr>
                  {% endfor %}
                </table>
              </div>
            </section><!-- End Counts Section -->
          </div>
    </div>
</body>
</html>


三、项目总结


       本次我们使用了Flask框架结合了基于机器学习的房价预测模型,构建了一个简易版基于机器学习的南昌市租房价格预测系统,整个项目还有很多地方可以优化,比如页面美化、模块添加等等,这些就留给学习的小伙伴根据自身需求进行创新升级!喜欢本项目的话就三连支持一下啦。

目录
相关文章
|
4月前
|
机器学习/深度学习 数据采集 数据可视化
基于爬虫和机器学习的招聘数据分析与可视化系统,python django框架,前端bootstrap,机器学习有八种带有可视化大屏和后台
本文介绍了一个基于Python Django框架和Bootstrap前端技术,集成了机器学习算法和数据可视化的招聘数据分析与可视化系统,该系统通过爬虫技术获取职位信息,并使用多种机器学习模型进行薪资预测、职位匹配和趋势分析,提供了一个直观的可视化大屏和后台管理系统,以优化招聘策略并提升决策质量。
237 4
|
5月前
|
监控 前端开发 API
实战指南:使用Python Flask与WebSocket实现高效的前后端分离实时系统
【7月更文挑战第18天】构建实时Web应用,如聊天室,可借助Python的Flask和WebSocket。安装Flask及Flask-SocketIO库,创建Flask应用,处理WebSocket事件。前端模板通过Socket.IO库连接服务器,发送和接收消息。运行应用,实现实时通信。此示例展现了Flask结合WebSocket实现前后端实时交互的能力。
599 3
|
3月前
|
安全 数据安全/隐私保护 Python
基于Flask框架实现一个简易后台用户登录系统
基于Flask框架实现一个简易后台用户登录系统
103 1
|
4月前
|
数据采集 自然语言处理 数据可视化
优秀python系统案例】基于python Flask的电影票房数据爬取与可视化系统的设计与实现
本文介绍了一个基于Python Flask框架开发的电影票房数据爬取与可视化系统,该系统利用网络爬虫技术从豆瓣电影网站抓取数据,通过Python进行数据处理和分析,并采用ECharts等库实现数据的可视化展示,为电影行业从业者提供决策支持。
358 2
优秀python系统案例】基于python Flask的电影票房数据爬取与可视化系统的设计与实现
|
3月前
|
人工智能 安全 数据安全/隐私保护
基于Flask框架实现一个简易后台用户登录系统
基于Flask框架实现一个简易后台用户登录系统
68 0
|
4月前
|
数据可视化 前端开发 数据挖掘
Python +flask+echart实现高校数据分析可视化系统
该博客文章介绍了如何使用Python、Flask框架和ECharts库实现高校数据分析可视化系统,包括工程创建、数据可视化和页面展示等步骤。
139 2
Python +flask+echart实现高校数据分析可视化系统
|
4月前
|
数据采集 数据可视化 关系型数据库
【优秀python web设计】基于Python flask的猫眼电影可视化系统,可视化用echart,前端Layui,数据库用MySQL,包括爬虫
本文介绍了一个基于Python Flask框架、MySQL数据库和Layui前端框架的猫眼电影数据采集分析与可视化系统,该系统通过爬虫技术采集电影数据,利用数据分析库进行处理,并使用Echart进行数据的可视化展示,以提供全面、准确的电影市场分析结果。
159 4
|
4月前
|
机器学习/深度学习 数据采集 数据可视化
【优秀python系统毕设】基于Python flask的气象数据可视化系统设计与实现,有LSTM算法预测气温
本文介绍了一个基于Python Flask框架开发的气象数据可视化系统,该系统集成了数据获取、处理、存储、LSTM算法气温预测以及多种数据可视化功能,旨在提高气象数据的利用价值并推动气象领域的发展。
277 1
|
4月前
|
存储 数据采集 数据可视化
基于Python flask+MySQL+echart的电影数据分析可视化系统
该博客文章介绍了一个基于Python Flask框架、MySQL数据库和ECharts库构建的电影数据分析可视化系统,系统功能包括猫眼电影数据的爬取、存储、展示以及电影评价词云图的生成。
214 1
|
4月前
|
数据采集 存储 数据可视化
基于Python flask的猫眼电影票房数据分析可视化系统,可以定制可视化
本文介绍了一个基于Python Flask框架开发的猫眼电影票房数据分析可视化系统,该系统集成了数据爬取、存储处理、可视化展示和用户交互功能,使用户能够直观地分析和展示电影票房数据,具有高度定制性。
175 0
基于Python flask的猫眼电影票房数据分析可视化系统,可以定制可视化