zoukankan      html  css  js  c++  java
  • 将西瓜书中的表格数字化与可视化

    实验的题目

    数据处理:将以下数据数值化及可视化

    编号 色泽 根蒂 敲声 好瓜
    1 青绿 蜷缩 浊响
    2 乌黑 蜷缩 沉闷
    3 青绿 硬挺 清脆
    4 乌黑 稍蜷 沉闷
    • 可手动或者编程
    • 数据自动生成脚本 + 自动数值化脚本:照片,声音

    实验的目的

    简单了解数据数值化以及可视化的方法

    源程序及运行结果

    from prettytable import PrettyTable
    import matplotlib.pyplot as plt
    import matplotlib
    import numpy as np
    
    colors=["青绿", "乌黑", "青绿", "乌黑"]
    roots=["蜷缩", "蜷缩", "硬挺", "稍蜷"]
    voices=["浊响", "沉闷", "清脆", "沉闷"]
    isGood=["是","否","否","否"]
    x= PrettyTable()
    x.add_column("色泽", colors)
    x.add_column("根蒂", roots)
    x.add_column("敲声", voices)
    x.add_column("好瓜",isGood)
    print(x)
    
    #对表格进行数字化处理
    colorMap={"青绿":5,"乌黑":10 ,"浅白":15}
    rootMap={"蜷缩":10, "蜷缩":20 ,"硬挺":30,"稍蜷":40}
    voiceMap={"浊响":20,"沉闷":40,"清脆":60}
    isGoodMap={"是":0,"否":70}
    print("数字化后的表格为:
    ")
    digitalTable=PrettyTable()
    colorDigital=[]
    rootDigital=[]
    isGoodDigital=[]
    voiceDigital=[]
    for c in colors:
        colorDigital.append(colorMap[c])
    for r in roots:
        rootDigital.append(rootMap[r])
    for v in voices:
        voiceDigital.append(voiceMap[v])
    for i in isGood:
        isGoodDigital.append(isGoodMap[i])
    digitalTable.add_column("色泽",colorDigital)
    digitalTable.add_column("根蒂",rootDigital)
    digitalTable.add_column("敲声",voiceDigital)
    digitalTable.add_column("好瓜",isGoodDigital)
    print(digitalTable)
    
    #对数据进行可视化处理,画出 
    myfont = matplotlib.font_manager.FontProperties(fname=r'C:/Windows/Fonts/msyh.ttf') 
    
    N = 4
    menMeans = (20, 35, 30, 35)
    womenMeans = (25, 32, 34, 20)
    ind = np.arange(N)  
    width = 0.35    
    
    colorBar = plt.bar(ind,colorDigital, width)
    rootBar = plt.bar(ind,rootDigital, width, bottom=colorDigital)
    voiceBar = plt.bar(ind,voiceDigital, width, bottom=rootDigital)
    isGoodBar = plt.bar(ind,isGoodDigital, width, bottom=voiceDigital)
    
    plt.ylabel('各个属性的情况',fontproperties=myfont)
    plt.title('不同西瓜样本的可视化图',fontproperties=myfont)
    plt.xticks(ind, ('1', '2', '3', '4'))
    plt.yticks(np.arange(0, 81, 10))
    plt.legend((colorBar[0], rootBar[0],voiceBar[0],isGoodBar[0]), ('color', 'root','voice','isGood'))
    
    plt.show()
    

    运行结果为:

    +------+------+------+------+
    | 色泽 | 根蒂 | 敲声 | 好瓜 |
    +------+------+------+------+
    | 青绿 | 蜷缩 | 浊响 | 是 |
    | 乌黑 | 蜷缩 | 沉闷 | 否 |
    | 青绿 | 硬挺 | 清脆 | 否 |
    | 乌黑 | 稍蜷 | 沉闷 | 否 |
    +------+------+------+------+
    数字化后的表格为:

    +------+------+------+------+
    | 色泽 | 根蒂 | 敲声 | 好瓜 |
    +------+------+------+------+
    | 5 | 20 | 20 | 0 |
    | 10 | 20 | 40 | 70 |
    | 5 | 30 | 60 | 70 |
    | 10 | 40 | 40 | 70 |
    +------+------+------+------+

    可视化运行结果为:

    `myplot

  • 相关阅读:
    android 75 新闻列表页面
    android 74 下载文本
    android 73 下载图片
    android 72 确定取消对话框,单选对话框,多选对话框
    android 71 ArrayAdapter和SimpleAdapter
    android 70 使用ListView把数据显示至屏幕
    maven如何将本地jar安装到本地仓库
    Centos6.7搭建ISCSI存储服务器
    解决maven打包编译出现File encoding has not been set问题
    MySQL 解决 emoji表情 的方法,使用utf8mb4 字符集(4字节 UTF-8 Unicode 编码)
  • 原文地址:https://www.cnblogs.com/goWithHappy/p/watermelon_to_digital.html
Copyright © 2011-2022 走看看