欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

忘了这啥是啥

程序员文章站 2022-07-14 12:44:48
...
//app
from flask import Flask,render_template
from demo import loadData
app=Flask(__name__)

@app.route('/')
def hello_world():
    expenses,count=loadData()
    # print(expenses,count)
    expenses=list(map(lambda x:"%.2f元"%x,expenses))
    return render_template("index.html",x=expenses,y=count)

if __name__ == '__main__':
    app.run(debug=True)

// demo
from sklearn.cluster import KMeans
import numpy as np
def loadData():
    #city.txt处理成2=>城市列表,消费数据列表
    file=open("city.txt",encoding="utf-8")
    cityName=[]
    cityData=[]
    for line in file:
        items=line.split(",")
        cityName.append(items[0])
        cityData.append([items[i] for i in range(1,len(items))])
    # print(cityData)
    # print(cityName)

    #实例化kmeans算法类的对象
    kmeans=KMeans(n_clusters=5)
    #调用fit_predict()
    labels=kmeans.fit_predict(cityData)
    #将城市名称按簇的编号分类
    cityCluster=[[0],[1],[2],[3],[4]]
    print(cityName)
    print(labels)
    for i in range(len(cityName)):#利用cityname的长度产生下标范围
        cityCluster[labels[i]].append(cityName[i])

    expenses=np.sum(kmeans.cluster_centers_,axis=1)
    count=[]
    for i in range(len(cityCluster)):
        print("各消费水平:",expenses[i])
        print("各城市名称:",cityCluster[i])
        count.append(len(cityCluster[i]))
    return expenses,count

if __name__ == '__main__':
    loadData()
// index
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="box" style="width: 550px;height: 400px;margin: 0 auto"></div>
    <script src="/static/echarts.min.js"></script>
    <script>
        var myecharts=echarts.init(document.getElementById("box"),"dark")
        var option = {
    title : {
        text: '31省消费水平统计'
    },
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['城市数量']
    },
    xAxis : [
        {
            type : 'category',
            data : {{ x | safe }}
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'城市数量',
            type:'bar',
            data:{{ y | safe }}
        },

    ]
};

        myecharts.setOption(option);
    </script>
</body>
</html>
相关标签: python python