Pages

Control Chart with python

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:
  1. Datas from samples
  2. Average of the samples ofeach lot
  3. Finding range of the sample means
  4. Calculate average value of the sample mean(it would be the center line)
  5. 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]:
xbar R Rbar UCL_R LCL_R
sample number
1 29.0 11 4.8 10.1472 0
2 25.0 8 4.8 10.1472 0
3 26.0 5 4.8 10.1472 0
4 25.2 5 4.8 10.1472 0
5 25.4 3 4.8 10.1472 0
6 28.0 4 4.8 10.1472 0
7 26.0 5 4.8 10.1472 0
8 27.0 4 4.8 10.1472 0
9 24.8 7 4.8 10.1472 0
10 21.4 4 4.8 10.1472 0
11 23.9 3 4.8 10.1472 0
12 24.1 5 4.8 10.1472 0
13 27.0 4 4.8 10.1472 0
14 26.8 6 4.8 10.1472 0
15 26.4 2 4.8 10.1472 0
16 23.8 4 4.8 10.1472 0
17 27.0 5 4.8 10.1472 0
18 28.4 3 4.8 10.1472 0
19 25.4 7 4.8 10.1472 0
20 26.2 6 4.8 10.1472 0
21 27.0 5 4.8 10.1472 0
22 26.0 5 4.8 10.1472 0
23 28.0 3 4.8 10.1472 0
24 26.4 6 4.8 10.1472 0
25 27.3 4 4.8 10.1472 0
26 24.0 4 4.8 10.1472 0
27 22.6 2 4.8 10.1472 0
28 28.0 6 4.8 10.1472 0
29 24.4 5 4.8 10.1472 0
30 26.4 3 4.8 10.1472 0
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]:
<matplotlib.axes._subplots.AxesSubplot at 0x4cc23d4470>
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]:
1
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]:
xbar R Rbar UCL_R LCL_R
sample number
2 25.0 8 4.586207 9.695241 0
3 26.0 5 4.586207 9.695241 0
4 25.2 5 4.586207 9.695241 0
5 25.4 3 4.586207 9.695241 0
6 28.0 4 4.586207 9.695241 0
7 26.0 5 4.586207 9.695241 0
8 27.0 4 4.586207 9.695241 0
9 24.8 7 4.586207 9.695241 0
10 21.4 4 4.586207 9.695241 0
11 23.9 3 4.586207 9.695241 0
12 24.1 5 4.586207 9.695241 0
13 27.0 4 4.586207 9.695241 0
14 26.8 6 4.586207 9.695241 0
15 26.4 2 4.586207 9.695241 0
16 23.8 4 4.586207 9.695241 0
17 27.0 5 4.586207 9.695241 0
18 28.4 3 4.586207 9.695241 0
19 25.4 7 4.586207 9.695241 0
20 26.2 6 4.586207 9.695241 0
21 27.0 5 4.586207 9.695241 0
22 26.0 5 4.586207 9.695241 0
23 28.0 3 4.586207 9.695241 0
24 26.4 6 4.586207 9.695241 0
25 27.3 4 4.586207 9.695241 0
26 24.0 4 4.586207 9.695241 0
27 22.6 2 4.586207 9.695241 0
28 28.0 6 4.586207 9.695241 0
29 24.4 5 4.586207 9.695241 0
30 26.4 3 4.586207 9.695241 0
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]:
<matplotlib.axes._subplots.AxesSubplot at 0x4cc235fe48>
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]:
0
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

Calculation of $\bar X$:

Iteraiton 1

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]:
xbar R Xdoublebar UCLxbar LCLxbar
sample number
2 25.0 8 25.789655 28.435897 23.143414
3 26.0 5 25.789655 28.435897 23.143414
4 25.2 5 25.789655 28.435897 23.143414
5 25.4 3 25.789655 28.435897 23.143414
6 28.0 4 25.789655 28.435897 23.143414
7 26.0 5 25.789655 28.435897 23.143414
8 27.0 4 25.789655 28.435897 23.143414
9 24.8 7 25.789655 28.435897 23.143414
10 21.4 4 25.789655 28.435897 23.143414
11 23.9 3 25.789655 28.435897 23.143414
12 24.1 5 25.789655 28.435897 23.143414
13 27.0 4 25.789655 28.435897 23.143414
14 26.8 6 25.789655 28.435897 23.143414
15 26.4 2 25.789655 28.435897 23.143414
16 23.8 4 25.789655 28.435897 23.143414
17 27.0 5 25.789655 28.435897 23.143414
18 28.4 3 25.789655 28.435897 23.143414
19 25.4 7 25.789655 28.435897 23.143414
20 26.2 6 25.789655 28.435897 23.143414
21 27.0 5 25.789655 28.435897 23.143414
22 26.0 5 25.789655 28.435897 23.143414
23 28.0 3 25.789655 28.435897 23.143414
24 26.4 6 25.789655 28.435897 23.143414
25 27.3 4 25.789655 28.435897 23.143414
26 24.0 4 25.789655 28.435897 23.143414
27 22.6 2 25.789655 28.435897 23.143414
28 28.0 6 25.789655 28.435897 23.143414
29 24.4 5 25.789655 28.435897 23.143414
30 26.4 3 25.789655 28.435897 23.143414
In [142]:
B["Xdoublebar"].plot()
B["xbar"].plot(marker="o")
B["UCLxbar"].plot()
B["LCLxbar"].plot()
Out[142]:
<matplotlib.axes._subplots.AxesSubplot at 0x4cc2263f60>
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]:
2
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]:
xbar R Xdoublebar UCLxbar LCLxbar
sample number
2 25.0 8 26.07037 28.716612 23.424129
3 26.0 5 26.07037 28.716612 23.424129
4 25.2 5 26.07037 28.716612 23.424129
5 25.4 3 26.07037 28.716612 23.424129
6 28.0 4 26.07037 28.716612 23.424129
7 26.0 5 26.07037 28.716612 23.424129
8 27.0 4 26.07037 28.716612 23.424129
9 24.8 7 26.07037 28.716612 23.424129
11 23.9 3 26.07037 28.716612 23.424129
12 24.1 5 26.07037 28.716612 23.424129
13 27.0 4 26.07037 28.716612 23.424129
14 26.8 6 26.07037 28.716612 23.424129
15 26.4 2 26.07037 28.716612 23.424129
16 23.8 4 26.07037 28.716612 23.424129
17 27.0 5 26.07037 28.716612 23.424129
18 28.4 3 26.07037 28.716612 23.424129
19 25.4 7 26.07037 28.716612 23.424129
20 26.2 6 26.07037 28.716612 23.424129
21 27.0 5 26.07037 28.716612 23.424129
22 26.0 5 26.07037 28.716612 23.424129
23 28.0 3 26.07037 28.716612 23.424129
24 26.4 6 26.07037 28.716612 23.424129
25 27.3 4 26.07037 28.716612 23.424129
26 24.0 4 26.07037 28.716612 23.424129
28 28.0 6 26.07037 28.716612 23.424129
29 24.4 5 26.07037 28.716612 23.424129
30 26.4 3 26.07037 28.716612 23.424129
In [153]:
B["Xdoublebar"].plot()
B["xbar"].plot(marker="o")
B["UCLxbar"].plot()
B["LCLxbar"].plot()
Out[153]:
<matplotlib.axes._subplots.AxesSubplot at 0x4cc25912e8>
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]:
0
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]:
xbar R Xdoublebar UCLxbar LCLxbar Standard deviation $\sigma$
sample number
2 25.0 8 26.07037 28.716612 23.424129 0.205993
3 26.0 5 26.07037 28.716612 23.424129 0.013543
4 25.2 5 26.07037 28.716612 23.424129 0.167503
5 25.4 3 26.07037 28.716612 23.424129 0.129013
6 28.0 4 26.07037 28.716612 23.424129 0.371357
7 26.0 5 26.07037 28.716612 23.424129 0.013543
8 27.0 4 26.07037 28.716612 23.424129 0.178907
9 24.8 7 26.07037 28.716612 23.424129 0.244483
11 23.9 3 26.07037 28.716612 23.424129 0.417688
12 24.1 5 26.07037 28.716612 23.424129 0.379198
13 27.0 4 26.07037 28.716612 23.424129 0.178907
14 26.8 6 26.07037 28.716612 23.424129 0.140417
15 26.4 2 26.07037 28.716612 23.424129 0.063437
16 23.8 4 26.07037 28.716612 23.424129 0.436933
17 27.0 5 26.07037 28.716612 23.424129 0.178907
18 28.4 3 26.07037 28.716612 23.424129 0.448337
19 25.4 7 26.07037 28.716612 23.424129 0.129013
20 26.2 6 26.07037 28.716612 23.424129 0.024947
21 27.0 5 26.07037 28.716612 23.424129 0.178907
22 26.0 5 26.07037 28.716612 23.424129 0.013543
23 28.0 3 26.07037 28.716612 23.424129 0.371357
24 26.4 6 26.07037 28.716612 23.424129 0.063437
25 27.3 4 26.07037 28.716612 23.424129 0.236642
26 24.0 4 26.07037 28.716612 23.424129 0.398443
28 28.0 6 26.07037 28.716612 23.424129 0.371357
29 24.4 5 26.07037 28.716612 23.424129 0.321463
30 26.4 3 26.07037 28.716612 23.424129 0.063437
In [ ]: