Control chart with python(JUPYTER notebook)
A1 final
Control charts, also known as Shewhart charts (after Walter A. Shewhart) or process-behavior charts, in statistical process control are tools used to determine if a manufacturing or business process is in a state of statistical control.
The requirements and steps in a control chart are:
- Datas from samples
- Average of the samples ofeach lot
- Finding range of the sample means
- Calculate average value of the sample mean(it would be the center line)
- Define upper and lower control limit for range and sample means.
Problem:
A soft drink is sold in bottles marked 225ml.An automatic machine fills the bottles.Samples of 5 bottles taken every 15 minutes of production during first shift produced,the result is shown in the first chart below(Only sample number and sample mean($\bar x$) is given).Now calculate the samples that are out of range and show final chart with standard deviation.
Iteration 1 for calculating $R $:
In [112]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [113]:
A=pd.read_excel("A1.xlsx")
In [114]:
B=A.groupby(["sample number"]).sum()
In [115]:
Rbar=B["R"].sum()/30 ## mean value of range of each lot
UCL_R=2.114*Rbar ## upper control limit of range
LCL_R=0*Rbar ## lower control limit of range
In [116]:
B.insert(2,'Rbar',Rbar)
B.insert(3,'UCL_R',UCL_R)
B.insert(4,'LCL_R',LCL_R)
chart for iteration 1 :
In [117]:
B
Out[117]:
In [118]:
B["R"].plot(marker="o")
B["UCL_R"].plot(color='b')
B["LCL_R"].plot(color='k')
B["Rbar"].plot(color='r')
Out[118]:
In [119]:
count=0
for i in B["R"]:
if i>UCL_R:
count=count+1
B=B.drop(B[B.R>UCL_R].index)
In [120]:
count
Out[120]:
for iteration 1:
- center line,$\bar R$ = 4.8
- upper control limit, UCL$\bar x$ = 10.1472
- lower control limit,LCL$\bar x$ = 0.0
- out of range sample = 1
Iteration 2 for calculating $ R $:
In [121]:
del B["Rbar"]
del B["UCL_R"]
del B["LCL_R"]
In [122]:
Rbar=B["R"].sum()/B["R"].count()
UCL_R=2.114*Rbar
LCL_R=0*Rbar
In [123]:
B.insert(2,'Rbar',Rbar)
B.insert(3,'UCL_R',UCL_R)
B.insert(4,'LCL_R',LCL_R)
chart for iteration 2
In [124]:
B
Out[124]:
In [125]:
B["R"].plot(marker="o")
B["UCL_R"].plot(color='b')
B["LCL_R"].plot(color='k')
B["Rbar"].plot(color='r')
Out[125]:
In [126]:
count=0
for i in B["R"]:
if i>UCL_R:
count=count+1
B=B.drop(B[B.R>UCL_R].index)
In [127]:
count
Out[127]:
for iteration 2:
- center line,$\bar R$ = 4.586207
- upper control limit, UCL$\bar x$ = 9.695241
- lower control limit,LCL$\bar x$ = 0.0
- out of range sample = 0
In [130]:
del B["Rbar"]
del B["UCL_R"]
del B["LCL_R"]
In [134]:
Xdoublebar=B["xbar"].sum()/B["xbar"].count()
UCLxbar=Xdoublebar+.577*Rbar
LCLxbar=Xdoublebar-.577*Rbar
In [136]:
B.insert(2,"Xdoublebar",Xdoublebar)
In [140]:
B.insert(3,"UCLxbar",UCLxbar)
B.insert(4,"LCLxbar",LCLxbar)
chart for iteration 1:
In [141]:
B
Out[141]:
In [142]:
B["Xdoublebar"].plot()
B["xbar"].plot(marker="o")
B["UCLxbar"].plot()
B["LCLxbar"].plot()
Out[142]:
In [144]:
nw_count=0
for i in B["xbar"]:
if i<LCLxbar:
nw_count=nw_count+1
if i>UCLxbar:
nw_count=nw_count+1
B=B.drop(B[B.xbar>UCLxbar].index)
B=B.drop(B[B.xbar<LCLxbar].index)
In [145]:
nw_count
Out[145]:
for iteration 1:
- center line,$\bar{\bar x}$(mean of $\bar x$) = 25.789655
- upper control limit, UCL$\bar x$ = 28.435897
- lower control limit,LCL$\bar x$ = 23.143414
- out of range sample = 2
Iteration 2¶
In [148]:
del B["Xdoublebar"]
del B["UCLxbar"]
del B["LCLxbar"]
In [149]:
Xdoublebar=B["xbar"].sum()/B["xbar"].count()
UCLxbar=Xdoublebar+.577*Rbar
LCLxbar=Xdoublebar-.577*Rbar
In [150]:
B.insert(2,"Xdoublebar",Xdoublebar)
In [151]:
B.insert(3,"UCLxbar",UCLxbar)
B.insert(4,"LCLxbar",LCLxbar)
chart for iteration 2
In [152]:
B
Out[152]:
In [153]:
B["Xdoublebar"].plot()
B["xbar"].plot(marker="o")
B["UCLxbar"].plot()
B["LCLxbar"].plot()
Out[153]:
In [154]:
nw_count=0
for i in B["xbar"]:
if i<LCLxbar:
nw_count=nw_count+1
if i>UCLxbar:
nw_count=nw_count+1
B=B.drop(B[B.xbar>UCLxbar].index)
B=B.drop(B[B.xbar<LCLxbar].index)
In [155]:
nw_count
Out[155]:
for iteration 1:
- center line,$\bar{\bar x}$(mean of $\bar x$) = 26.07037
- upper control limit, UCL$\bar x$ = 28.71662
- lower control limit,LCL$\bar x$ = 23.424129
- out of range sample = 0
Calculation of standard deviation $\sigma$ : ¶
In [158]:
import numpy as np
dif=abs(B["xbar"]-B["Xdoublebar"])
standard_deviation=(dif/ np.sqrt(B["xbar"].count()))
In [159]:
B.insert(5,"Standard deviation $\sigma$",standard_deviation)
The final chart:
In [160]:
B
Out[160]:
In [ ]: