outlier-toolkit 0.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of outlier-toolkit might be problematic. Click here for more details.
- outlier_toolkit-0.0.2/LICENSE +13 -0
- outlier_toolkit-0.0.2/PKG-INFO +141 -0
- outlier_toolkit-0.0.2/README.md +122 -0
- outlier_toolkit-0.0.2/outlier/__init__.py +1 -0
- outlier_toolkit-0.0.2/outlier/i_outlier/IQR.py +40 -0
- outlier_toolkit-0.0.2/outlier/i_outlier/Zscore.py +24 -0
- outlier_toolkit-0.0.2/outlier/i_outlier/__init__.py +1 -0
- outlier_toolkit-0.0.2/outlier/outlierGroup/__init__.py +1 -0
- outlier_toolkit-0.0.2/outlier/outlierGroup/binning.py +68 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/__init__.py +1 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/remove.py +55 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/replace.py +105 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/winsorization/__init__.py +1 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/winsorization/adaptive.py +72 -0
- outlier_toolkit-0.0.2/outlier/outlierTech/winsorization/standard.py +23 -0
- outlier_toolkit-0.0.2/outlier_toolkit.egg-info/PKG-INFO +141 -0
- outlier_toolkit-0.0.2/outlier_toolkit.egg-info/SOURCES.txt +20 -0
- outlier_toolkit-0.0.2/outlier_toolkit.egg-info/dependency_links.txt +1 -0
- outlier_toolkit-0.0.2/outlier_toolkit.egg-info/top_level.txt +1 -0
- outlier_toolkit-0.0.2/pyproject.toml +3 -0
- outlier_toolkit-0.0.2/setup.cfg +4 -0
- outlier_toolkit-0.0.2/setup.py +26 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 IRENE BETSY D
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: outlier_toolkit
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: A Python library for identifying and handling outliers
|
|
5
|
+
Home-page: https://github.com/irenebetsy/outlier_library
|
|
6
|
+
Author: Irene Betsy D
|
|
7
|
+
Author-email: betsydnicholraja@gmail.com
|
|
8
|
+
License: Apache License 2.0
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/irenebetsy/outlier_library/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/irenebetsy/outlier_library#readme
|
|
11
|
+
Project-URL: Source Code, https://github.com/irenebetsy/outlier_library
|
|
12
|
+
Keywords: outlier IQR ZScore Winsorization binning data preprocessing analytics
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.7
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
|
|
20
|
+
# 🧰 Outlier Toolkit 🛠️
|
|
21
|
+
|
|
22
|
+
A **standalone Python library** for detecting, handling, and transforming outliers in numeric and categorical data.
|
|
23
|
+
No external dependencies required.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 📜 License
|
|
28
|
+
|
|
29
|
+
This project is licensed under the **Apache License 2.0**. See the [LICENSE](./LICENSE) file for more details.
|
|
30
|
+
|
|
31
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## 📊 Features
|
|
37
|
+
|
|
38
|
+
### 1. Outlier Detection
|
|
39
|
+
- **Z-score Detection**: Identify extreme values based on standard deviation.
|
|
40
|
+
- **IQR Detection**: Detect outliers using the interquartile range (Q1, Q3).
|
|
41
|
+
|
|
42
|
+
### 2. Outlier Handling Techniques
|
|
43
|
+
- **Remove Outliers**: Drop outlier values from datasets.
|
|
44
|
+
- **Replace Outliers**: Replace outliers with mean, median, or most frequent values.
|
|
45
|
+
|
|
46
|
+
### 3. Winsorization
|
|
47
|
+
- **Standard Winsorization**: Cap extreme values at a fixed percentile.
|
|
48
|
+
- **Adaptive Quartiles**: Replace low/high outliers using Q1 and Q3.
|
|
49
|
+
- **Adaptive Inliers**: Replace low/high outliers using nearest inlier values (custom method).
|
|
50
|
+
|
|
51
|
+
### 4. Binning
|
|
52
|
+
- **Equal Width Binning**: Divide numeric range into equal-width intervals.
|
|
53
|
+
- **Equal Frequency Binning**: Divide data so each bin has approximately the same number of values.
|
|
54
|
+
- **Auto Binning (Outlier-based)**: Automatically separate low/high outliers and inliers using IQR.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 🔧 Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install outlier_library
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
No external libraries required. Compatible with Python 3.7+.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 🧮 Usage
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
from outlier.i_outlier.Zscore import detect_outliers_zscore
|
|
73
|
+
from outlier.i_outlier.IQR import detect_outliers_iqr
|
|
74
|
+
from outlier.outlierTech.remove import remove_outliers
|
|
75
|
+
from outlier.outlierTech.replace import replace_outliers
|
|
76
|
+
from outlier.outlierTech.winsorization.standard import winsorize_standard
|
|
77
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_quartiles
|
|
78
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_inliers
|
|
79
|
+
from outlier.outlierGroup.binning import eq_width_bin
|
|
80
|
+
from outlier.outlierGroup.binning import eq_freq_bin
|
|
81
|
+
from outlier.outlierGroup.binning import custom_binning
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# Sample Test data
|
|
86
|
+
numeric_data = [1, 2, 85, 95, 65, 75, 53, 67, 87, 89, 93, 1001, 1027, 3018]
|
|
87
|
+
categorical_data = ["Male", "Female", "Male", "Male", "Unknown", "Unknown", "Other"]
|
|
88
|
+
|
|
89
|
+
#Detection
|
|
90
|
+
print("=== Zscore Detection ===")
|
|
91
|
+
print(detect_outliers_zscore(numeric_data))
|
|
92
|
+
|
|
93
|
+
print("\n=== IQR Detection ===")
|
|
94
|
+
print(detect_outliers_iqr(numeric_data))
|
|
95
|
+
|
|
96
|
+
#Handling
|
|
97
|
+
print("\n=== Remove Outliers ===")
|
|
98
|
+
print(remove_outliers(numeric_data, method="IQR"))
|
|
99
|
+
|
|
100
|
+
print("\n=== Replace Outliers (auto-detect) ===")
|
|
101
|
+
print(replace_outliers(numeric_data, method="IQR"))
|
|
102
|
+
print(replace_outliers(categorical_data, method="IQR"))
|
|
103
|
+
|
|
104
|
+
#Winsorization
|
|
105
|
+
print("\n=== Winsorization (Standard 5%) ===")
|
|
106
|
+
print(winsorize_standard(numeric_data[:]))
|
|
107
|
+
|
|
108
|
+
print("\n=== Winsorization (Adaptive Quartiles) ===")
|
|
109
|
+
print(winsorize_quartiles(numeric_data[:]))
|
|
110
|
+
|
|
111
|
+
print("\n=== Winsorization (Adaptive Inliers) ===")
|
|
112
|
+
print(winsorize_inliers(numeric_data[:]))
|
|
113
|
+
|
|
114
|
+
#Binning
|
|
115
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
116
|
+
print(eq_width_bin(numeric_data[:]))
|
|
117
|
+
|
|
118
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
119
|
+
print(eq_freq_bin(numeric_data[:]))
|
|
120
|
+
|
|
121
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
122
|
+
print(custom_binning(numeric_data[:]))
|
|
123
|
+
```
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## 📝 Notes
|
|
127
|
+
|
|
128
|
+
- Works for numeric and categorical data.
|
|
129
|
+
- All functions are standalone and do not require external libraries.
|
|
130
|
+
- Custom winsorization allows mapping outliers to nearest inliers for more controlled transformations.
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## 👩💻 Author
|
|
135
|
+
**Irene Betsy D**
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# 🧰 Outlier Toolkit 🛠️
|
|
2
|
+
|
|
3
|
+
A **standalone Python library** for detecting, handling, and transforming outliers in numeric and categorical data.
|
|
4
|
+
No external dependencies required.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 📜 License
|
|
9
|
+
|
|
10
|
+
This project is licensed under the **Apache License 2.0**. See the [LICENSE](./LICENSE) file for more details.
|
|
11
|
+
|
|
12
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## 📊 Features
|
|
18
|
+
|
|
19
|
+
### 1. Outlier Detection
|
|
20
|
+
- **Z-score Detection**: Identify extreme values based on standard deviation.
|
|
21
|
+
- **IQR Detection**: Detect outliers using the interquartile range (Q1, Q3).
|
|
22
|
+
|
|
23
|
+
### 2. Outlier Handling Techniques
|
|
24
|
+
- **Remove Outliers**: Drop outlier values from datasets.
|
|
25
|
+
- **Replace Outliers**: Replace outliers with mean, median, or most frequent values.
|
|
26
|
+
|
|
27
|
+
### 3. Winsorization
|
|
28
|
+
- **Standard Winsorization**: Cap extreme values at a fixed percentile.
|
|
29
|
+
- **Adaptive Quartiles**: Replace low/high outliers using Q1 and Q3.
|
|
30
|
+
- **Adaptive Inliers**: Replace low/high outliers using nearest inlier values (custom method).
|
|
31
|
+
|
|
32
|
+
### 4. Binning
|
|
33
|
+
- **Equal Width Binning**: Divide numeric range into equal-width intervals.
|
|
34
|
+
- **Equal Frequency Binning**: Divide data so each bin has approximately the same number of values.
|
|
35
|
+
- **Auto Binning (Outlier-based)**: Automatically separate low/high outliers and inliers using IQR.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 🔧 Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install outlier_library
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
No external libraries required. Compatible with Python 3.7+.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## 🧮 Usage
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
from outlier.i_outlier.Zscore import detect_outliers_zscore
|
|
54
|
+
from outlier.i_outlier.IQR import detect_outliers_iqr
|
|
55
|
+
from outlier.outlierTech.remove import remove_outliers
|
|
56
|
+
from outlier.outlierTech.replace import replace_outliers
|
|
57
|
+
from outlier.outlierTech.winsorization.standard import winsorize_standard
|
|
58
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_quartiles
|
|
59
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_inliers
|
|
60
|
+
from outlier.outlierGroup.binning import eq_width_bin
|
|
61
|
+
from outlier.outlierGroup.binning import eq_freq_bin
|
|
62
|
+
from outlier.outlierGroup.binning import custom_binning
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# Sample Test data
|
|
67
|
+
numeric_data = [1, 2, 85, 95, 65, 75, 53, 67, 87, 89, 93, 1001, 1027, 3018]
|
|
68
|
+
categorical_data = ["Male", "Female", "Male", "Male", "Unknown", "Unknown", "Other"]
|
|
69
|
+
|
|
70
|
+
#Detection
|
|
71
|
+
print("=== Zscore Detection ===")
|
|
72
|
+
print(detect_outliers_zscore(numeric_data))
|
|
73
|
+
|
|
74
|
+
print("\n=== IQR Detection ===")
|
|
75
|
+
print(detect_outliers_iqr(numeric_data))
|
|
76
|
+
|
|
77
|
+
#Handling
|
|
78
|
+
print("\n=== Remove Outliers ===")
|
|
79
|
+
print(remove_outliers(numeric_data, method="IQR"))
|
|
80
|
+
|
|
81
|
+
print("\n=== Replace Outliers (auto-detect) ===")
|
|
82
|
+
print(replace_outliers(numeric_data, method="IQR"))
|
|
83
|
+
print(replace_outliers(categorical_data, method="IQR"))
|
|
84
|
+
|
|
85
|
+
#Winsorization
|
|
86
|
+
print("\n=== Winsorization (Standard 5%) ===")
|
|
87
|
+
print(winsorize_standard(numeric_data[:]))
|
|
88
|
+
|
|
89
|
+
print("\n=== Winsorization (Adaptive Quartiles) ===")
|
|
90
|
+
print(winsorize_quartiles(numeric_data[:]))
|
|
91
|
+
|
|
92
|
+
print("\n=== Winsorization (Adaptive Inliers) ===")
|
|
93
|
+
print(winsorize_inliers(numeric_data[:]))
|
|
94
|
+
|
|
95
|
+
#Binning
|
|
96
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
97
|
+
print(eq_width_bin(numeric_data[:]))
|
|
98
|
+
|
|
99
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
100
|
+
print(eq_freq_bin(numeric_data[:]))
|
|
101
|
+
|
|
102
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
103
|
+
print(custom_binning(numeric_data[:]))
|
|
104
|
+
```
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## 📝 Notes
|
|
108
|
+
|
|
109
|
+
- Works for numeric and categorical data.
|
|
110
|
+
- All functions are standalone and do not require external libraries.
|
|
111
|
+
- Custom winsorization allows mapping outliers to nearest inliers for more controlled transformations.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## 👩💻 Author
|
|
116
|
+
**Irene Betsy D**
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package init
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
def _quickselect(arr, k):
|
|
3
|
+
def select(left, right, k):
|
|
4
|
+
if left == right:
|
|
5
|
+
return arr[left]
|
|
6
|
+
# deterministic pivot = middle element
|
|
7
|
+
pivot_index = (left + right) // 2
|
|
8
|
+
pivot_value = arr[pivot_index]
|
|
9
|
+
arr[pivot_index], arr[right] = arr[right], arr[pivot_index]
|
|
10
|
+
store_index = left
|
|
11
|
+
for i in range(left, right):
|
|
12
|
+
if arr[i] < pivot_value:
|
|
13
|
+
arr[store_index], arr[i] = arr[i], arr[store_index]
|
|
14
|
+
store_index += 1
|
|
15
|
+
arr[right], arr[store_index] = arr[store_index], arr[right]
|
|
16
|
+
if k == store_index:
|
|
17
|
+
return arr[k]
|
|
18
|
+
elif k < store_index:
|
|
19
|
+
return select(left, store_index - 1, k)
|
|
20
|
+
else:
|
|
21
|
+
return select(store_index + 1, right, k)
|
|
22
|
+
return select(0, len(arr) - 1, k)
|
|
23
|
+
|
|
24
|
+
def detect_outliers_iqr(data):
|
|
25
|
+
n = len(data)
|
|
26
|
+
if n < 4:
|
|
27
|
+
return []
|
|
28
|
+
arr_copy = data[:]
|
|
29
|
+
q1 = _quickselect(arr_copy, n // 4)
|
|
30
|
+
arr_copy = data[:]
|
|
31
|
+
q3 = _quickselect(arr_copy, (3 * n) // 4)
|
|
32
|
+
IQR = q3 - q1
|
|
33
|
+
lower_bound = q1 - 1.5 * IQR
|
|
34
|
+
upper_bound = q3 + 1.5 * IQR
|
|
35
|
+
|
|
36
|
+
outliers = []
|
|
37
|
+
for i, val in enumerate(data):
|
|
38
|
+
if val < lower_bound or val > upper_bound:
|
|
39
|
+
outliers.append((i, val))
|
|
40
|
+
return outliers
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
def detect_outliers_zscore(data, threshold=3):
|
|
3
|
+
n = len(data)
|
|
4
|
+
if n == 0:
|
|
5
|
+
return []
|
|
6
|
+
# Welford's algorithm for mean and variance (no imports)
|
|
7
|
+
mean = 0
|
|
8
|
+
m2 = 0
|
|
9
|
+
count = 0
|
|
10
|
+
for x in data:
|
|
11
|
+
count += 1
|
|
12
|
+
delta = x - mean
|
|
13
|
+
mean += delta / count
|
|
14
|
+
delta2 = x - mean
|
|
15
|
+
m2 += delta * delta2
|
|
16
|
+
variance = m2 / n
|
|
17
|
+
std_dev = variance ** 0.5
|
|
18
|
+
|
|
19
|
+
outliers = []
|
|
20
|
+
for i in range(n):
|
|
21
|
+
z = (data[i] - mean) / std_dev
|
|
22
|
+
if z > threshold or z < -threshold:
|
|
23
|
+
outliers.append((i, data[i]))
|
|
24
|
+
return outliers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package init
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package init
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#Equal Width Binning
|
|
2
|
+
def eq_width_bin(data, bins=5):
|
|
3
|
+
if not data:
|
|
4
|
+
return {}
|
|
5
|
+
|
|
6
|
+
min_val, max_val = min(data), max(data)
|
|
7
|
+
bin_width = (max_val - min_val) / bins if bins > 0 else 1
|
|
8
|
+
|
|
9
|
+
bins_dict = {}
|
|
10
|
+
for value in data:
|
|
11
|
+
bin_index = int((value - min_val) // bin_width) if bin_width > 0 else 0
|
|
12
|
+
if bin_index == bins: # edge case for max value
|
|
13
|
+
bin_index -= 1
|
|
14
|
+
bin_range = (min_val + bin_index * bin_width, min_val + (bin_index + 1) * bin_width)
|
|
15
|
+
bins_dict.setdefault(bin_range, []).append(value)
|
|
16
|
+
|
|
17
|
+
return bins_dict
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
#Equal Frequency Binning
|
|
21
|
+
def eq_freq_bin(data, bins=5):
|
|
22
|
+
if not data:
|
|
23
|
+
return {}
|
|
24
|
+
|
|
25
|
+
sorted_data = sorted(data)
|
|
26
|
+
n = len(sorted_data)
|
|
27
|
+
bin_size = max(1, n // bins)
|
|
28
|
+
|
|
29
|
+
bins_dict = {}
|
|
30
|
+
for i in range(bins):
|
|
31
|
+
start = i * bin_size
|
|
32
|
+
end = (i + 1) * bin_size if i < bins - 1 else n
|
|
33
|
+
bins_dict[f"bin_{i+1}"] = sorted_data[start:end]
|
|
34
|
+
|
|
35
|
+
return bins_dict
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
#Custom_binning
|
|
39
|
+
def custom_binning(data):
|
|
40
|
+
if not data:
|
|
41
|
+
return {}
|
|
42
|
+
|
|
43
|
+
# Sort data
|
|
44
|
+
sorted_data = sorted(data)
|
|
45
|
+
n = len(sorted_data)
|
|
46
|
+
|
|
47
|
+
# Calculate Q1, Q3
|
|
48
|
+
q1_index = n // 4
|
|
49
|
+
q3_index = (3 * n) // 4
|
|
50
|
+
q1 = sorted_data[q1_index]
|
|
51
|
+
q3 = sorted_data[q3_index]
|
|
52
|
+
|
|
53
|
+
iqr = q3 - q1
|
|
54
|
+
lower_fence = q1 - 1.5 * iqr
|
|
55
|
+
upper_fence = q3 + 1.5 * iqr
|
|
56
|
+
|
|
57
|
+
# Split outliers and inliers
|
|
58
|
+
bins_dict = {"below": [], "above": [], "inliers": []}
|
|
59
|
+
for x in data:
|
|
60
|
+
if x < lower_fence:
|
|
61
|
+
bins_dict["below"].append(x)
|
|
62
|
+
elif x > upper_fence:
|
|
63
|
+
bins_dict["above"].append(x)
|
|
64
|
+
else:
|
|
65
|
+
bins_dict["inliers"].append(x)
|
|
66
|
+
|
|
67
|
+
return bins_dict
|
|
68
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package init
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
|
|
2
|
+
def remove_outliers(data, method="IQR"):
|
|
3
|
+
n = len(data)
|
|
4
|
+
if n == 0:
|
|
5
|
+
return []
|
|
6
|
+
|
|
7
|
+
cleaned_data = []
|
|
8
|
+
if method == "IQR":
|
|
9
|
+
q1 = _quickselect(data[:], n // 4)
|
|
10
|
+
q3 = _quickselect(data[:], (3 * n) // 4)
|
|
11
|
+
IQR = q3 - q1
|
|
12
|
+
lower = q1 - 1.5 * IQR
|
|
13
|
+
upper = q3 + 1.5 * IQR
|
|
14
|
+
for val in data:
|
|
15
|
+
if lower <= val <= upper:
|
|
16
|
+
cleaned_data.append(val)
|
|
17
|
+
|
|
18
|
+
elif method == "Zscore":
|
|
19
|
+
mean = 0
|
|
20
|
+
m2 = 0
|
|
21
|
+
count = 0
|
|
22
|
+
for x in data:
|
|
23
|
+
count += 1
|
|
24
|
+
delta = x - mean
|
|
25
|
+
mean += delta / count
|
|
26
|
+
delta2 = x - mean
|
|
27
|
+
m2 += delta * delta2
|
|
28
|
+
std_dev = (m2 / n) ** 0.5
|
|
29
|
+
for val in data:
|
|
30
|
+
z = (val - mean) / std_dev
|
|
31
|
+
if abs(z) <= 3:
|
|
32
|
+
cleaned_data.append(val)
|
|
33
|
+
|
|
34
|
+
return cleaned_data
|
|
35
|
+
|
|
36
|
+
def _quickselect(arr, k):
|
|
37
|
+
def select(left, right, k):
|
|
38
|
+
if left == right:
|
|
39
|
+
return arr[left]
|
|
40
|
+
pivot_index = (left + right) // 2
|
|
41
|
+
pivot_value = arr[pivot_index]
|
|
42
|
+
arr[pivot_index], arr[right] = arr[right], arr[pivot_index]
|
|
43
|
+
store_index = left
|
|
44
|
+
for i in range(left, right):
|
|
45
|
+
if arr[i] < pivot_value:
|
|
46
|
+
arr[store_index], arr[i] = arr[i], arr[store_index]
|
|
47
|
+
store_index += 1
|
|
48
|
+
arr[right], arr[store_index] = arr[store_index], arr[right]
|
|
49
|
+
if k == store_index:
|
|
50
|
+
return arr[k]
|
|
51
|
+
elif k < store_index:
|
|
52
|
+
return select(left, store_index - 1, k)
|
|
53
|
+
else:
|
|
54
|
+
return select(store_index + 1, right, k)
|
|
55
|
+
return select(0, len(arr) - 1, k)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
def replace_outliers(data, method="IQR"):
|
|
2
|
+
n = len(data)
|
|
3
|
+
if n == 0:
|
|
4
|
+
return []
|
|
5
|
+
new_data = data[:]
|
|
6
|
+
|
|
7
|
+
# Detect if numeric or categorical
|
|
8
|
+
is_numeric = True
|
|
9
|
+
for x in new_data:
|
|
10
|
+
if not isinstance(x, (int, float)):
|
|
11
|
+
is_numeric = False
|
|
12
|
+
break
|
|
13
|
+
|
|
14
|
+
# Compute replacement value
|
|
15
|
+
if not is_numeric:
|
|
16
|
+
# Categorical → use mode
|
|
17
|
+
freq = {}
|
|
18
|
+
for x in new_data:
|
|
19
|
+
freq[x] = freq.get(x, 0) + 1
|
|
20
|
+
max_count = 0
|
|
21
|
+
mode_val = None
|
|
22
|
+
for key, count in freq.items():
|
|
23
|
+
if count > max_count:
|
|
24
|
+
max_count = count
|
|
25
|
+
mode_val = key
|
|
26
|
+
rep_val = mode_val
|
|
27
|
+
else:
|
|
28
|
+
# Numeric → choose mean or median based on skewness
|
|
29
|
+
total = sum(new_data)
|
|
30
|
+
mean = total / n
|
|
31
|
+
sorted_data = sorted(new_data)
|
|
32
|
+
median = sorted_data[n // 2]
|
|
33
|
+
|
|
34
|
+
# Compute standard deviation
|
|
35
|
+
m2 = 0
|
|
36
|
+
for x in new_data:
|
|
37
|
+
m2 += (x - mean) ** 2
|
|
38
|
+
std_dev = (m2 / n) ** 0.5
|
|
39
|
+
|
|
40
|
+
if std_dev == 0:
|
|
41
|
+
skewness = 0
|
|
42
|
+
else:
|
|
43
|
+
skewness = 3 * (mean - median) / std_dev
|
|
44
|
+
|
|
45
|
+
if abs(skewness) < 0.5:
|
|
46
|
+
rep_val = mean
|
|
47
|
+
else:
|
|
48
|
+
rep_val = median
|
|
49
|
+
|
|
50
|
+
# Apply method
|
|
51
|
+
if method == "IQR":
|
|
52
|
+
if is_numeric:
|
|
53
|
+
q1 = _quickselect(new_data[:], n // 4)
|
|
54
|
+
q3 = _quickselect(new_data[:], (3 * n) // 4)
|
|
55
|
+
IQR = q3 - q1
|
|
56
|
+
lower = q1 - 1.5 * IQR
|
|
57
|
+
upper = q3 + 1.5 * IQR
|
|
58
|
+
for i in range(n):
|
|
59
|
+
if new_data[i] < lower or new_data[i] > upper:
|
|
60
|
+
new_data[i] = rep_val
|
|
61
|
+
else:
|
|
62
|
+
# For categorical: replace rare categories with mode
|
|
63
|
+
for i in range(n):
|
|
64
|
+
if freq[new_data[i]] < max_count:
|
|
65
|
+
new_data[i] = rep_val
|
|
66
|
+
|
|
67
|
+
elif method == "Zscore" and is_numeric:
|
|
68
|
+
m2 = 0
|
|
69
|
+
count = 0
|
|
70
|
+
mean = 0
|
|
71
|
+
for x in new_data:
|
|
72
|
+
count += 1
|
|
73
|
+
delta = x - mean
|
|
74
|
+
mean += delta / count
|
|
75
|
+
delta2 = x - mean
|
|
76
|
+
m2 += delta * delta2
|
|
77
|
+
std_dev = (m2 / n) ** 0.5
|
|
78
|
+
for i in range(n):
|
|
79
|
+
z = (new_data[i] - mean) / std_dev
|
|
80
|
+
if abs(z) > 3:
|
|
81
|
+
new_data[i] = rep_val
|
|
82
|
+
|
|
83
|
+
return new_data
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _quickselect(arr, k):
|
|
87
|
+
def select(left, right, k):
|
|
88
|
+
if left == right:
|
|
89
|
+
return arr[left]
|
|
90
|
+
pivot_index = (left + right) // 2
|
|
91
|
+
pivot_value = arr[pivot_index]
|
|
92
|
+
arr[pivot_index], arr[right] = arr[right], arr[pivot_index]
|
|
93
|
+
store_index = left
|
|
94
|
+
for i in range(left, right):
|
|
95
|
+
if arr[i] < pivot_value:
|
|
96
|
+
arr[store_index], arr[i] = arr[i], arr[store_index]
|
|
97
|
+
store_index += 1
|
|
98
|
+
arr[right], arr[store_index] = arr[store_index], arr[right]
|
|
99
|
+
if k == store_index:
|
|
100
|
+
return arr[k]
|
|
101
|
+
elif k < store_index:
|
|
102
|
+
return select(left, store_index - 1, k)
|
|
103
|
+
else:
|
|
104
|
+
return select(store_index + 1, right, k)
|
|
105
|
+
return select(0, len(arr) - 1, k)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Package init
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
def winsorize_quartiles(data):
|
|
2
|
+
print(">> Custom winsorize_adaptive running!")
|
|
3
|
+
if len(data) < 4:
|
|
4
|
+
return data[:]
|
|
5
|
+
|
|
6
|
+
sorted_data = sorted(data)
|
|
7
|
+
n = len(sorted_data)
|
|
8
|
+
|
|
9
|
+
# Q1 and Q3 indices
|
|
10
|
+
q1_index = n // 4
|
|
11
|
+
q3_index = (3 * n) // 4
|
|
12
|
+
q1 = sorted_data[q1_index]
|
|
13
|
+
q3 = sorted_data[q3_index]
|
|
14
|
+
|
|
15
|
+
# Nearest valid inliers
|
|
16
|
+
lower_replacement = next(x for x in sorted_data if x >= q1)
|
|
17
|
+
upper_replacement = max(x for x in sorted_data if x <= q3)
|
|
18
|
+
|
|
19
|
+
new_data = []
|
|
20
|
+
for x in data:
|
|
21
|
+
if x < q1:
|
|
22
|
+
new_data.append(lower_replacement) # Use nearest inlier above Q1 (85)
|
|
23
|
+
elif x > q3:
|
|
24
|
+
new_data.append(upper_replacement) # Use nearest inlier below Q3 (93)
|
|
25
|
+
else:
|
|
26
|
+
new_data.append(x)
|
|
27
|
+
return new_data
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def winsorize_inliers(data):
|
|
31
|
+
"""
|
|
32
|
+
Custom winsorization:
|
|
33
|
+
- Sorts the data
|
|
34
|
+
- Replaces low outliers with the smallest inlier
|
|
35
|
+
- Replaces high outliers with the largest inlier
|
|
36
|
+
- Keeps other values unchanged
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
if len(data) < 4:
|
|
40
|
+
return data[:]
|
|
41
|
+
|
|
42
|
+
# Sort the data
|
|
43
|
+
sorted_data = sorted(data)
|
|
44
|
+
|
|
45
|
+
# Use IQR to detect outliers
|
|
46
|
+
n = len(sorted_data)
|
|
47
|
+
q1_index = n // 4
|
|
48
|
+
q3_index = (3 * n) // 4
|
|
49
|
+
q1 = sorted_data[q1_index]
|
|
50
|
+
q3 = sorted_data[q3_index]
|
|
51
|
+
iqr = q3 - q1
|
|
52
|
+
lower_fence = q1 - 1.5 * iqr
|
|
53
|
+
upper_fence = q3 + 1.5 * iqr
|
|
54
|
+
|
|
55
|
+
# Identify inliers
|
|
56
|
+
inliers = [x for x in sorted_data if lower_fence <= x <= upper_fence]
|
|
57
|
+
|
|
58
|
+
# Determine nearest inlier values for replacement
|
|
59
|
+
min_inlier = min(inliers)
|
|
60
|
+
max_inlier = max(inliers)
|
|
61
|
+
|
|
62
|
+
# Replace outliers
|
|
63
|
+
new_data = []
|
|
64
|
+
for x in data:
|
|
65
|
+
if x < lower_fence:
|
|
66
|
+
new_data.append(min_inlier)
|
|
67
|
+
elif x > upper_fence:
|
|
68
|
+
new_data.append(max_inlier)
|
|
69
|
+
else:
|
|
70
|
+
new_data.append(x)
|
|
71
|
+
|
|
72
|
+
return new_data
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
def winsorize_standard(data, percentile=0.05):
|
|
2
|
+
if len(data) < 4:
|
|
3
|
+
return data[:]
|
|
4
|
+
|
|
5
|
+
sorted_data = sorted(data)
|
|
6
|
+
n = len(sorted_data)
|
|
7
|
+
|
|
8
|
+
# Calculate index positions for percentile
|
|
9
|
+
lower_idx = int(n * percentile)
|
|
10
|
+
upper_idx = int(n * (1 - percentile)) - 1
|
|
11
|
+
|
|
12
|
+
lower_limit = sorted_data[lower_idx]
|
|
13
|
+
upper_limit = sorted_data[upper_idx]
|
|
14
|
+
|
|
15
|
+
new_data = []
|
|
16
|
+
for x in data:
|
|
17
|
+
if x < lower_limit:
|
|
18
|
+
new_data.append(lower_limit)
|
|
19
|
+
elif x > upper_limit:
|
|
20
|
+
new_data.append(upper_limit)
|
|
21
|
+
else:
|
|
22
|
+
new_data.append(x)
|
|
23
|
+
return new_data
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: outlier-toolkit
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: A Python library for identifying and handling outliers
|
|
5
|
+
Home-page: https://github.com/irenebetsy/outlier_library
|
|
6
|
+
Author: Irene Betsy D
|
|
7
|
+
Author-email: betsydnicholraja@gmail.com
|
|
8
|
+
License: Apache License 2.0
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/irenebetsy/outlier_library/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/irenebetsy/outlier_library#readme
|
|
11
|
+
Project-URL: Source Code, https://github.com/irenebetsy/outlier_library
|
|
12
|
+
Keywords: outlier IQR ZScore Winsorization binning data preprocessing analytics
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.7
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
|
|
20
|
+
# 🧰 Outlier Toolkit 🛠️
|
|
21
|
+
|
|
22
|
+
A **standalone Python library** for detecting, handling, and transforming outliers in numeric and categorical data.
|
|
23
|
+
No external dependencies required.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 📜 License
|
|
28
|
+
|
|
29
|
+
This project is licensed under the **Apache License 2.0**. See the [LICENSE](./LICENSE) file for more details.
|
|
30
|
+
|
|
31
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## 📊 Features
|
|
37
|
+
|
|
38
|
+
### 1. Outlier Detection
|
|
39
|
+
- **Z-score Detection**: Identify extreme values based on standard deviation.
|
|
40
|
+
- **IQR Detection**: Detect outliers using the interquartile range (Q1, Q3).
|
|
41
|
+
|
|
42
|
+
### 2. Outlier Handling Techniques
|
|
43
|
+
- **Remove Outliers**: Drop outlier values from datasets.
|
|
44
|
+
- **Replace Outliers**: Replace outliers with mean, median, or most frequent values.
|
|
45
|
+
|
|
46
|
+
### 3. Winsorization
|
|
47
|
+
- **Standard Winsorization**: Cap extreme values at a fixed percentile.
|
|
48
|
+
- **Adaptive Quartiles**: Replace low/high outliers using Q1 and Q3.
|
|
49
|
+
- **Adaptive Inliers**: Replace low/high outliers using nearest inlier values (custom method).
|
|
50
|
+
|
|
51
|
+
### 4. Binning
|
|
52
|
+
- **Equal Width Binning**: Divide numeric range into equal-width intervals.
|
|
53
|
+
- **Equal Frequency Binning**: Divide data so each bin has approximately the same number of values.
|
|
54
|
+
- **Auto Binning (Outlier-based)**: Automatically separate low/high outliers and inliers using IQR.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 🔧 Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install outlier_library
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
No external libraries required. Compatible with Python 3.7+.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 🧮 Usage
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
from outlier.i_outlier.Zscore import detect_outliers_zscore
|
|
73
|
+
from outlier.i_outlier.IQR import detect_outliers_iqr
|
|
74
|
+
from outlier.outlierTech.remove import remove_outliers
|
|
75
|
+
from outlier.outlierTech.replace import replace_outliers
|
|
76
|
+
from outlier.outlierTech.winsorization.standard import winsorize_standard
|
|
77
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_quartiles
|
|
78
|
+
from outlier.outlierTech.winsorization.adaptive import winsorize_inliers
|
|
79
|
+
from outlier.outlierGroup.binning import eq_width_bin
|
|
80
|
+
from outlier.outlierGroup.binning import eq_freq_bin
|
|
81
|
+
from outlier.outlierGroup.binning import custom_binning
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# Sample Test data
|
|
86
|
+
numeric_data = [1, 2, 85, 95, 65, 75, 53, 67, 87, 89, 93, 1001, 1027, 3018]
|
|
87
|
+
categorical_data = ["Male", "Female", "Male", "Male", "Unknown", "Unknown", "Other"]
|
|
88
|
+
|
|
89
|
+
#Detection
|
|
90
|
+
print("=== Zscore Detection ===")
|
|
91
|
+
print(detect_outliers_zscore(numeric_data))
|
|
92
|
+
|
|
93
|
+
print("\n=== IQR Detection ===")
|
|
94
|
+
print(detect_outliers_iqr(numeric_data))
|
|
95
|
+
|
|
96
|
+
#Handling
|
|
97
|
+
print("\n=== Remove Outliers ===")
|
|
98
|
+
print(remove_outliers(numeric_data, method="IQR"))
|
|
99
|
+
|
|
100
|
+
print("\n=== Replace Outliers (auto-detect) ===")
|
|
101
|
+
print(replace_outliers(numeric_data, method="IQR"))
|
|
102
|
+
print(replace_outliers(categorical_data, method="IQR"))
|
|
103
|
+
|
|
104
|
+
#Winsorization
|
|
105
|
+
print("\n=== Winsorization (Standard 5%) ===")
|
|
106
|
+
print(winsorize_standard(numeric_data[:]))
|
|
107
|
+
|
|
108
|
+
print("\n=== Winsorization (Adaptive Quartiles) ===")
|
|
109
|
+
print(winsorize_quartiles(numeric_data[:]))
|
|
110
|
+
|
|
111
|
+
print("\n=== Winsorization (Adaptive Inliers) ===")
|
|
112
|
+
print(winsorize_inliers(numeric_data[:]))
|
|
113
|
+
|
|
114
|
+
#Binning
|
|
115
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
116
|
+
print(eq_width_bin(numeric_data[:]))
|
|
117
|
+
|
|
118
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
119
|
+
print(eq_freq_bin(numeric_data[:]))
|
|
120
|
+
|
|
121
|
+
print("\n=== Binning (Equal Width Binning) ===")
|
|
122
|
+
print(custom_binning(numeric_data[:]))
|
|
123
|
+
```
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## 📝 Notes
|
|
127
|
+
|
|
128
|
+
- Works for numeric and categorical data.
|
|
129
|
+
- All functions are standalone and do not require external libraries.
|
|
130
|
+
- Custom winsorization allows mapping outliers to nearest inliers for more controlled transformations.
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## 👩💻 Author
|
|
135
|
+
**Irene Betsy D**
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
outlier/__init__.py
|
|
6
|
+
outlier/i_outlier/IQR.py
|
|
7
|
+
outlier/i_outlier/Zscore.py
|
|
8
|
+
outlier/i_outlier/__init__.py
|
|
9
|
+
outlier/outlierGroup/__init__.py
|
|
10
|
+
outlier/outlierGroup/binning.py
|
|
11
|
+
outlier/outlierTech/__init__.py
|
|
12
|
+
outlier/outlierTech/remove.py
|
|
13
|
+
outlier/outlierTech/replace.py
|
|
14
|
+
outlier/outlierTech/winsorization/__init__.py
|
|
15
|
+
outlier/outlierTech/winsorization/adaptive.py
|
|
16
|
+
outlier/outlierTech/winsorization/standard.py
|
|
17
|
+
outlier_toolkit.egg-info/PKG-INFO
|
|
18
|
+
outlier_toolkit.egg-info/SOURCES.txt
|
|
19
|
+
outlier_toolkit.egg-info/dependency_links.txt
|
|
20
|
+
outlier_toolkit.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
outlier
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="outlier_toolkit",
|
|
5
|
+
version="0.0.2",
|
|
6
|
+
author="Irene Betsy D",
|
|
7
|
+
author_email="betsydnicholraja@gmail.com",
|
|
8
|
+
license="Apache License 2.0",
|
|
9
|
+
description="A Python library for identifying and handling outliers",
|
|
10
|
+
long_description=open("README.md", "r", encoding="utf-8").read(),
|
|
11
|
+
long_description_content_type="text/markdown",
|
|
12
|
+
url="https://github.com/irenebetsy/outlier_library",
|
|
13
|
+
packages=find_packages(),
|
|
14
|
+
classifiers=[
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: Apache Software License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
],
|
|
19
|
+
python_requires='>=3.7',
|
|
20
|
+
keywords="outlier IQR ZScore Winsorization binning data preprocessing analytics",
|
|
21
|
+
project_urls={
|
|
22
|
+
"Bug Tracker": "https://github.com/irenebetsy/outlier_library/issues",
|
|
23
|
+
"Documentation": "https://github.com/irenebetsy/outlier_library#readme",
|
|
24
|
+
"Source Code": "https://github.com/irenebetsy/outlier_library",
|
|
25
|
+
},
|
|
26
|
+
)
|