Apply Nelson rules
Project description
nelson_rules
Table of Contents
Background
The 8 Nelson rules can be used in process control to detect potential anomalies during production. Those rules were defined by Lloyd S. Nelson in the 1980s. The rules and their exemplary visualizations are available on wikipedia.
About the package
The package was built with Numpy and tested with pytest.
By the use of this package, your data is checked for all 8 rules. As input data, a 1D NumPy Array is preferred, but a list of numeric values is also possible. As result, a dictionary is provided, that contains numpy arrays as values for each rule. It furthermore contains the input data and the evaluated zscores.
Results on each Nelson rule are available as 1D arrays with binary values (0, 1). Those results could e.g. be displayed in visuals with potential anomalies (1) being colored in red.
How to install
pip install nelson-rules
How to import
from nelson_rules import NelsonRules
How to run with default settings
Default settings refer to the values that are stated in the rules above. E.g. for rule 1, we'd check if at least one point is more than 3 standard deviations from the mean. The default values are encoded as follows:
d_rule_settings = {
"rule1": {"f_std": 3.0},
"rule2": {"i_points": 9},
"rule3": {"i_points": 6},
"rule4": {"i_points": 14},
"rule5": {"i_points": 3, "i_points_window": 2, "f_std": 2.0},
"rule6": {"i_points": 5, "i_points_window": 4, "f_std": 1.0},
"rule7": {"i_points": 15, "f_std": 1.0},
"rule8": {"i_points": 8, "f_std": 1.0},
}
To check for data points following Nelson rules, the rule setting dictionary is not needed as a parameter, because it is already set as default. Therefore, simply run the following on your data:
nr = NelsonRules(<your_data_in_1D_numpy_array>)
d_results = nr.apply_rules()
How to run with other settings
In case you'd like to run the code with other settings, adapt d_rule_settings to your needs and run:
nr = NelsonRules(<your_data_in_1D_numpy_array>, d_rule_settings)
d_results = nr.apply_rules()
How to access results
The results are available in a dictionary with the following structure:
d_results = {
"input_data": [...],
"zscore": [...],
"rule1": [...],
"rule2": [...],
"rule3": [...],
"rule4": [...],
"rule5_points": [...],
"rule5_windows": [...],
"rule6_points": [...],
"rule6_windows": [...],
"rule7": [...],
"rule8": [...]
}
You could load the dictionary into a pandas DataFrame (as mentioned above).
Or you could access the data of interest like this:
d_results["rule4"]
For rules 5 and 6, points and windows are available. By this, you could visualize both: the n data points out of m points (window) that fulfill the rule. You can access this data as follows:
d_results["rule5_points"]
d_results["rule5_windows"]
The program is set up with NumPy. However, if you'd like to get the results in a pandas DataFrame, run:
df = pd.DataFrame(data=d_results)
How to access further information
Zscores are available as numpy array and can be accessed as follows:
d_results["zscore"]
Mean and x standard deviations are available as single values each. They can be accessed as follows:
## mean
nr_mean = nr.f_mean
## 1 standard deviation
nr_1std = nr.f_std
## 2 standard deviations
nr_2std = 2 * nr.f_std
## 3 standard deviations
nr_3std = 3 * nr.f_std
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nelson_rules-0.1.0.tar.gz.
File metadata
- Download URL: nelson_rules-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5aad5f136e8e41a748c64dd813e68e0a569a2be75c98dd2bda6e1215b2fc3875
|
|
| MD5 |
4ffe8339f5e567eaac363072bf752b24
|
|
| BLAKE2b-256 |
e3a91b2ca11c755ce018c288ca9ac3b4be2ddb8af4b89f6c3b9ce16147996961
|
File details
Details for the file nelson_rules-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nelson_rules-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c0fa483825debfd722e324342c4f9d512b26780384fe08eaff6e078001ff68c
|
|
| MD5 |
ebf1a6d84a33321488c9ab0f9546cdfd
|
|
| BLAKE2b-256 |
36c87a38273d27fdd3dd84e33aad238817aca1f367fd522a33e50cce2b2b342b
|