精華區beta NTUCH-HW 關於我們 聯絡資訊
import numpy as np from numpy import array from numpy.random import normal, randint from numpy import array from numpy.random import normal import time import random #執行以下要這樣執行https://imgur.com/SMKEirj 反白一起按F9 不然會 #SyntaxError: unexpected EOF while parsing def gen_data(): heights = [] weights = [] grades = [] N = 10000 for i in range(N): while True: #身高服从均值172,标准差为6的正态分布 height = normal(172, 6) if 0 < height: break while True: #体重由身高作为自变量的线性回归模型产生,误差服从标准正态分布 weight = (height - 80) * 0.7 + normal(0, 1) if 0 < weight: break while True: #分数服从均值为70,标准差为15的正态分布 score = normal(70, 15) if 0 <= score and score <= 100: grade = 'E' if score < 60 else ('D' if score < 70 else ('C' if score < 80 else ('B' if score < 90 else 'A'))) break heights.append(height) weights.append(weight) grades.append(grade) return array(heights), array(weights), array(grades) heights, weights, grades = gen_data() #https://imgur.com/8Vr1vUL 得到這個 #---定性分析 #绘制柱状图 def draw_bar(grades): xticks = ['A', 'B', 'C', 'D', 'E'] gradeGroup = {} #对每一类成绩进行频数统计 for grade in grades: gradeGroup[grade] = gradeGroup.get(grade, 0) + 1 #创建柱状图 #第一个参数为柱的横坐标 #第二个参数为柱的高度 #参数align为柱的对齐方式,以第一个参数为参考标准 plt.bar(range(5), [gradeGroup.get(xtick, 0) for xtick in xticks], align='center') #设置柱的文字说明 #第一个参数为文字说明的横坐标 #第二个参数为文字说明的内容 plt.xticks(range(5), xticks) #设置横坐标的文字说明 plt.xlabel('Grade') #设置纵坐标的文字说明 plt.ylabel('Frequency') #设置标题 plt.title('Grades Of Male Students') #绘图 plt.show() draw_bar(grades) https://imgur.com/Tw8F1xl #绘制饼形图 def draw_pie(grades): labels = ['A', 'B', 'C', 'D', 'E'] gradeGroup = {} for grade in grades: gradeGroup[grade] = gradeGroup.get(grade, 0) + 1 #创建饼形图 #第一个参数为扇形的面积 #labels参数为扇形的说明文字 #autopct参数为扇形占比的显示格式 plt.pie([gradeGroup.get(label, 0) for label in labels], labels=labels, autopct='%1.1f%%') plt.title('Grades Of Male Students') plt.show() draw_pie(grades) #---定量分析 #绘制直方图 def draw_hist(heights): #创建直方图 #第一个参数为待绘制的定量数据,不同于定性数据,这里并没有事先进行频数统计 #第二个参数为划分的区间个数 plt.hist(heights, 100) plt.xlabel('Heights') plt.ylabel('Frequency') plt.title('Heights Of Male Students') plt.show() draw_hist(heights) #绘制累积曲线 def draw_cumulative_hist(heights): #创建累积曲线 #第一个参数为待绘制的定量数据 #第二个参数为划分的区间个数 #normed参数为是否无量纲化 #histtype参数为'step',绘制阶梯状的曲线 #cumulative参数为是否累积 plt.hist(heights, 20, normed=True, histtype='step', cumulative=True) plt.xlabel('Heights') plt.ylabel('Frequency') plt.title('Heights Of Male Students') plt.show() draw_cumulativeHist(heights) #绘制散点图 def draw_scatter(heights, weights): #创建散点图 #第一个参数为点的横坐标 #第二个参数为点的纵坐标 plt.scatter(heights, weights) plt.xlabel('Heights') plt.ylabel('Weights') plt.title('Heights & Weights Of Male Students') plt.show() draw_scatter(heights, weights) #---探索分析 #绘制箱形图 def draw_box(heights): #创建箱形图 #第一个参数为待绘制的定量数据 #第二个参数为数据的文字说明 plt.boxplot([heights], labels=['Heights']) plt.title('Heights Of Male Students') plt.show() draw_box(heights) https://github.com/yymm/flask-vuejs