|
|
1. 安装Python以及IDE,如Anaconda、PyCharm或Jupyter Noteboook,网上教程很多,选择一个
2. 安装必要的库,网络教程或者AI教程都有
3. 打开你的IDE,开始写代码
第一步:导包(导入常用的库)
- # import libraries
- import pandas as pd
- pd.set_option('display.unicode.ambiguous_as_wide', True)
- pd.set_option('display.unicode.east_asian_width', True)
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy import stats
- import statsmodels.api as sm
- from statsmodels.formula.api import ols
- import pylab
- import seaborn as sns
- import openpyxl
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.rcParams['axes.unicode_minus'] = False
复制代码 第二步:准备数据
我是将基本的数据放到了EXCEL文件中
| Part | Appraiser | Trial | Measurement | | 1 | A | 1 | 0.29 | | 1 | A | 2 | 0.41 | | 1 | A | 3 | 0.64 | | 2 | A | 1 | -0.56 | | 2 | A | 2 | -0.68 | | 2 | A | 3 | -0.58 | | 3 | A | 1 | 1.34 | | 3 | A | 2 | 1.17 | | 3 | A | 3 | 1.27 | | 4 | A | 1 | 0.47 | | 4 | A | 2 | 0.50 | | 4 | A | 3 | 0.64 | | 5 | A | 1 | -0.80 | | 5 | A | 2 | -0.92 | | 5 | A | 3 | -0.84 | | 6 | A | 1 | 0.02 | | 6 | A | 2 | -0.11 | | 6 | A | 3 | -0.21 | | 7 | A | 1 | 0.59 | | 7 | A | 2 | 0.75 | | 7 | A | 3 | 0.68 | | 8 | A | 1 | -0.31 | | 8 | A | 2 | -0.20 | | 8 | A | 3 | -0.17 | | 9 | A | 1 | 2.26 | | 9 | A | 2 | 1.99 | | 9 | A | 3 | 2.01 | | 10 | A | 1 | -1.36 | | 10 | A | 2 | -1.25 | | 10 | A | 3 | -1.31 | | 1 | B | 1 | 0.08 | | 1 | B | 2 | 0.25 | | 1 | B | 3 | 0.07 | | 2 | B | 1 | -0.47 | | 2 | B | 2 | -1.22 | | 2 | B | 3 | -0.68 | | 3 | B | 1 | 1.19 | | 3 | B | 2 | 0.94 | | 3 | B | 3 | 1.34 | | 4 | B | 1 | 0.01 | | 4 | B | 2 | 1.03 | | 4 | B | 3 | 0.20 | | 5 | B | 1 | -0.56 | | 5 | B | 2 | -1.20 | | 5 | B | 3 | -1.28 | | 6 | B | 1 | -0.20 | | 6 | B | 2 | 0.22 | | 6 | B | 3 | 0.06 | | 7 | B | 1 | 0.47 | | 7 | B | 2 | 0.55 | | 7 | B | 3 | 0.83 | | 8 | B | 1 | -0.63 | | 8 | B | 2 | -0.08 | | 8 | B | 3 | -0.34 | | 9 | B | 1 | 1.80 | | 9 | B | 2 | 2.12 | | 9 | B | 3 | 2.19 | | 10 | B | 1 | -1.68 | | 10 | B | 2 | -1.82 | | 10 | B | 3 | -1.50 | | 1 | C | 1 | 0.04 | | 1 | C | 2 | -0.11 | | 1 | C | 3 | -0.15 | | 2 | C | 1 | -1.38 | | 2 | C | 2 | -1.13 | | 2 | C | 3 | -0.96 | | 3 | C | 1 | 0.88 | | 3 | C | 2 | 1.09 | | 3 | C | 3 | 0.67 | | 4 | C | 1 | 0.14 | | 4 | C | 2 | 0.20 | | 4 | C | 3 | 0.11 | | 5 | C | 1 | -1.46 | | 5 | C | 2 | -1.07 | | 5 | C | 3 | -1.45 | | 6 | C | 1 | -0.29 | | 6 | C | 2 | -0.67 | | 6 | C | 3 | -0.49 | | 7 | C | 1 | 0.02 | | 7 | C | 2 | 0.01 | | 7 | C | 3 | 0.21 | | 8 | C | 1 | -0.46 | | 8 | C | 2 | -0.66 | | 8 | C | 3 | -0.49 | | 9 | C | 1 | 1.77 | | 9 | C | 2 | 1.45 | | 9 | C | 3 | 1.87 | | 10 | C | 1 | -1.49 | | 10 | C | 2 | -1.77 | | 10 | C | 3 | -2.16 |
第三步:写代码
- # 均值极差法
- # 重复性和再现性,不能分析交互作用
- # 3人10件各3次测量
- # 样件能代表过程实际变差或预期范围
- study_name = 'Repeatability and Reproducibility Study(Xbar-R)' # 研究名称
- df_GRR = pd.read_excel("MSA1_GRR.xlsx", sheet_name = "GRR2") # 读取存储的EXCEL数据
- # print(df_GRR)
- Xbarbar = df_GRR["Measurement"].mean() # 计算测量值的平均值
- Xmax = df_GRR["Measurement"].max() # 计算测量值的最大值
- Xmin = df_GRR["Measurement"].min() # 计算测量值的最小值
- Xbar_ij = df_GRR.groupby(["Appraiser","Part"]).agg(Xbar_ij=("Measurement","mean")).reset_index() # 按照检验员、零件计算测量值的平均值
- # print(Xbar_ij)
- R_ij = df_GRR.groupby(["Appraiser","Part"]).agg(R_ij=("Measurement", lambda x: x.max()-x.min())).reset_index() # 按照检验员、零件计算测量值的极差
- Rmax = R_ij["R_ij"].max() # 计算极差的最大值
- # print(R_ij)
- fig, ax = plt.subplots(nrows=2, ncols=2, figsize= [14,6], layout = "tight") # 创建画布,为绘图做准备
- ax = ax.ravel()
- Xbar_Aj = Xbar_ij.query("Appraiser=='A'")["Xbar_ij"] # 从Xbar_ij中提取检验员A的平均值
- Xbar_Bj = Xbar_ij.query("Appraiser=='B'")["Xbar_ij"] # 从Xbar_ij中提取检验员B的平均值
- Xbar_Cj = Xbar_ij.query("Appraiser=='C'")["Xbar_ij"] # 从Xbar_ij中提取检验员C的平均值
- std_X = 0.05 # 计算标准差,为了控制图上控制线计算做准备,我这里直接假设标准差为0.05
- UCL_Xbar = Xbarbar + 3 * std_X # 上控制线,这里直接采用的3倍标准差,实际按照控制图去计算
- LCL_Xbar = Xbarbar - 3 * std_X # 下控制线,这里直接采用的3倍标准差,实际按照控制图去计算
- R_Aj = R_ij.query("Appraiser=='A'")["R_ij"] # 从R_ij中提取检验员A的极差
- R_Bj = R_ij.query("Appraiser=='B'")["R_ij"] # 从R_ij中提取检验员B的极差
- R_Cj = R_ij.query("Appraiser=='C'")["R_ij"] # 从R_ij中提取检验员C的极差
- R_Abar = R_ij.query("Appraiser=='A'")["R_ij"].mean() # 计算检验员A的极差平均值
- R_Bbar = R_ij.query("Appraiser=='B'")["R_ij"].mean() # 计算检验员B的极差平均值
- R_Cbar = R_ij.query("Appraiser=='C'")["R_ij"].mean() # 计算检验员C的极差平均值
- Rbarbar = (R_Abar + R_Bbar + R_Cbar) / 3 # 计算整体极差的平均值
- D4 = 2.58 #
- UCL_R = Rbarbar * D4 # 计算极差控制图的控制上线
- LCL_R = 0
- # 下面是绘图代码
- # 均值图(重叠)
- ax[0].plot(range(1,11), Xbar_Aj, marker = "o", linestyle = "--", label ="Appraiser A") # 绘制检验员A的均值图
- ax[0].plot(range(1,11), Xbar_Bj, marker = "s", linestyle = "-.", label ="Appraiser B") # 绘制检验员B的均值图
- ax[0].plot(range(1,11), Xbar_Cj, marker = "d", linestyle = ":", label ="Appraiser C") # 绘制检验员C的均值图
- ax[0].axhline(Xbarbar, color="g", linestyle = "-") # 添加平均值的参考线
- ax[0].axhline(UCL_Xbar, color="r", linestyle = "-.") # 添上控制限的参考线
- ax[0].axhline(LCL_Xbar, color="r", linestyle = "-.") # 添加下控制限的参考线
- ax[0].text(10.5, UCL_Xbar, "UCL", va="bottom") # 添加控制限文字
- ax[0].text(10.5, LCL_Xbar, "LCL", va="top") # 添加控制限文字
- ax[0].legend(loc="best") # 显示图例
- ax[0].set_xticks(range(1,11)) # 设置X轴刻度
- ax[0].set_xlabel("Part") # 设置X轴标签
- ax[0].set_ylabel("Average") # 设置Y轴标签
- ax[0].set_title("Average Chart") # 设置图表名称
- # 均值图(展开)
- ax[1].plot(range(1,11), Xbar_Aj, marker = "o", linestyle = "--", label ="Appraiser A")
- ax[1].plot(range(11,21), Xbar_Bj, marker = "s", linestyle = "--", label ="Appraiser B")
- ax[1].plot(range(21,31), Xbar_Cj, marker = "d", linestyle = "--", label ="Appraiser C")
- ax[1].text(1, Xmax,"Appraiser A", va="bottom")
- ax[1].text(11, Xmax,"Appraiser B", va="bottom")
- ax[1].text(21, Xmax,"Appraiser C", va="bottom")
- ax[1].axhline(Xbarbar, color="g", linestyle = "-")
- ax[1].axhline(UCL_Xbar, color="r", linestyle = "-.")
- ax[1].axhline(LCL_Xbar, color="r", linestyle = "-.")
- ax[1].axvline(10.5, color = "b", linestyle = "--")
- ax[1].axvline(20.5, color = "b", linestyle = "--")
- ax[1].text(31.5, UCL_Xbar, "UCL", va="bottom")
- ax[1].text(31.5, LCL_Xbar, "LCL", va="top")
- ax[1].set_xticks(ticks=np.arange(1,31),labels=[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10])
- ax[1].set_xlabel("Appraiser - Part")
- ax[1].set_ylabel("Average")
- ax[1].set_title("Average Chart")
- # 极差图(重叠)
- ax[2].plot(range(1,11), R_Aj, marker = "o", linestyle = "--", label ="Appraiser A")
- ax[2].plot(range(1,11), R_Bj, marker = "s", linestyle = "-.", label ="Appraiser B")
- ax[2].plot(range(1,11), R_Cj, marker = "d", linestyle = ":", label ="Appraiser C")
- ax[2].axhline(Rbarbar, color="g", linestyle = "-")
- ax[2].axhline(UCL_R, color="r", linestyle = "-.")
- ax[2].axhline(LCL_R, color="r", linestyle = "-.")
- ax[2].text(10.5, UCL_R, "UCL", va="bottom")
- ax[2].text(10.5, LCL_R, "LCL", va="top")
- ax[2].legend(loc="best")
- ax[2].set_xticks(range(1,11))
- ax[2].set_xlabel("Part")
- ax[2].set_ylabel("Range")
- ax[2].set_title("Range Chart")
- # 极差图(展开)
- ax[3].plot(range(1,11), R_Aj, marker = "o", linestyle = "--", label ="Appraiser A")
- ax[3].plot(range(11,21), R_Bj, marker = "s", linestyle = "--", label ="Appraiser B")
- ax[3].plot(range(21,31), R_Cj, marker = "d", linestyle = "--", label ="Appraiser C")
- ax[3].text(1, Rmax,"Appraiser A", va="bottom")
- ax[3].text(11, Rmax,"Appraiser B", va="bottom")
- ax[3].text(21, Rmax,"Appraiser C", va="bottom")
- ax[3].axhline(Rbarbar, color="g", linestyle = "-")
- ax[3].axhline(UCL_R, color="r", linestyle = "-.")
- ax[3].axhline(LCL_R, color="r", linestyle = "-.")
- ax[3].axvline(10.5, color = "b", linestyle = "--")
- ax[3].axvline(20.5, color = "b", linestyle = "--")
- ax[3].text(31.5, UCL_R, "UCL", va="bottom")
- ax[3].text(31.5, LCL_R, "LCL", va="top")
- ax[3].set_xticks(ticks=np.arange(1,31),labels=[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10])
- ax[3].set_xlabel("Appraiser - Part")
- ax[3].set_ylabel("Range")
- ax[3].set_title("Range Chart")
- plt.show()
复制代码 我这里写的代码是最简单的入门代码,没有使用循环去绘图,使用循环绘图代码会简短一些,等后面再去优化更新。
其实现在Python的教程资料很多,AI也能帮忙写代码。
希望上面的代码可以帮到你,有问题再联系。
|
|