mathcoreml 0.1.0__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.
- mathcoreml-0.1.0/LICENSE +21 -0
- mathcoreml-0.1.0/PKG-INFO +106 -0
- mathcoreml-0.1.0/README.md +92 -0
- mathcoreml-0.1.0/pyproject.toml +20 -0
- mathcoreml-0.1.0/setup.cfg +4 -0
- mathcoreml-0.1.0/src/MathCoreML/Models/__init__.py +0 -0
- mathcoreml-0.1.0/src/MathCoreML/Models/linearRegression.py +95 -0
- mathcoreml-0.1.0/src/MathCoreML/Models/logisticRegression.py +100 -0
- mathcoreml-0.1.0/src/MathCoreML/Models/template_models/modelImplementationTemplate.py +13 -0
- mathcoreml-0.1.0/src/MathCoreML/utils/__init__.py +0 -0
- mathcoreml-0.1.0/src/MathCoreML/utils/cleandata.py +122 -0
- mathcoreml-0.1.0/src/MathCoreML/utils/csvstore.py +478 -0
- mathcoreml-0.1.0/src/MathCoreML/utils/modelevalutaor.py +472 -0
- mathcoreml-0.1.0/src/MathCoreML.egg-info/PKG-INFO +106 -0
- mathcoreml-0.1.0/src/MathCoreML.egg-info/SOURCES.txt +28 -0
- mathcoreml-0.1.0/src/MathCoreML.egg-info/dependency_links.txt +1 -0
- mathcoreml-0.1.0/src/MathCoreML.egg-info/requires.txt +4 -0
- mathcoreml-0.1.0/src/MathCoreML.egg-info/top_level.txt +1 -0
mathcoreml-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tigran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mathcoreml
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Mathematical Implementation of ML algorithms from scratch
|
|
5
|
+
Author-email: Tigran <tigran.harutunyan.02@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: numpy
|
|
10
|
+
Requires-Dist: pandas
|
|
11
|
+
Requires-Dist: matplotlib
|
|
12
|
+
Requires-Dist: scikit-learn
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# MathCoreML
|
|
16
|
+
|
|
17
|
+
Lightweight utilities and example models for small-scale machine learning experiments.
|
|
18
|
+
|
|
19
|
+
## Overview
|
|
20
|
+
|
|
21
|
+
**MathCoreML** is a compact Python library designed for learning, teaching, and rapid prototyping of small machine learning workflows.
|
|
22
|
+
The project emphasizes **clarity, simplicity, and minimal dependencies**, making it suitable for educational use and lightweight experiments.
|
|
23
|
+
|
|
24
|
+
It provides:
|
|
25
|
+
|
|
26
|
+
- Simple CSV data exploration utilities
|
|
27
|
+
- Basic data cleaning helpers
|
|
28
|
+
- Minimal reference implementations of classic ML models
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
### 📊 Data Utilities
|
|
35
|
+
|
|
36
|
+
- **CSVStore**
|
|
37
|
+
- Easy CSV loading
|
|
38
|
+
- Basic statistics (min, max, mean, counts)
|
|
39
|
+
- Quick summaries and simple visualizations
|
|
40
|
+
- **CleanData**
|
|
41
|
+
- IQR-based outlier detection
|
|
42
|
+
- Lightweight data-cleaning helpers
|
|
43
|
+
|
|
44
|
+
### 🤖 Models
|
|
45
|
+
|
|
46
|
+
- **Linear Regression**
|
|
47
|
+
- **Logistic Regression**
|
|
48
|
+
|
|
49
|
+
These implementations are intentionally minimal and readable, aimed at education and experimentation rather than production-scale systems.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Install from PyPI (once published):
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install mathcoreml
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
For local development:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
python -m venv .venv
|
|
65
|
+
source .venv/bin/activate
|
|
66
|
+
pip install -e .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Quick Start
|
|
70
|
+
|
|
71
|
+
Example usage with CSVStore (provide your own CSV file):
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from MathCoreML.utils.CSVStore import CSVStore
|
|
75
|
+
from MathCoreML.utils.CleanData import CleanData
|
|
76
|
+
|
|
77
|
+
store = CSVStore('/absolute/path/to/your_dataset.csv')
|
|
78
|
+
print('Rows:', len(store))
|
|
79
|
+
print('Max Age:', store.max_of('Age'))
|
|
80
|
+
|
|
81
|
+
store.quick_summary()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Package Structure
|
|
85
|
+
```text
|
|
86
|
+
MathCoreML/
|
|
87
|
+
├── src/
|
|
88
|
+
│ └── mathcoreml/
|
|
89
|
+
│ ├── Models/
|
|
90
|
+
│ │ ├── linearRegression.py
|
|
91
|
+
│ │ └── logisticRegression.py
|
|
92
|
+
│ └── utils/
|
|
93
|
+
│ ├── csvstore.py
|
|
94
|
+
│ ├── cleandata.py
|
|
95
|
+
│ └── modelevaluator.py
|
|
96
|
+
└── pyproject.toml
|
|
97
|
+
```
|
|
98
|
+
## Notes
|
|
99
|
+
|
|
100
|
+
### Designed for clarity and learning, not as a replacement for full ML frameworks.
|
|
101
|
+
|
|
102
|
+
#### Public API is limited to modules inside MathCoreML.
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
This project is licensed under the MIT License. See the `LICENSE` file for details.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# MathCoreML
|
|
2
|
+
|
|
3
|
+
Lightweight utilities and example models for small-scale machine learning experiments.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
**MathCoreML** is a compact Python library designed for learning, teaching, and rapid prototyping of small machine learning workflows.
|
|
8
|
+
The project emphasizes **clarity, simplicity, and minimal dependencies**, making it suitable for educational use and lightweight experiments.
|
|
9
|
+
|
|
10
|
+
It provides:
|
|
11
|
+
|
|
12
|
+
- Simple CSV data exploration utilities
|
|
13
|
+
- Basic data cleaning helpers
|
|
14
|
+
- Minimal reference implementations of classic ML models
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
### 📊 Data Utilities
|
|
21
|
+
|
|
22
|
+
- **CSVStore**
|
|
23
|
+
- Easy CSV loading
|
|
24
|
+
- Basic statistics (min, max, mean, counts)
|
|
25
|
+
- Quick summaries and simple visualizations
|
|
26
|
+
- **CleanData**
|
|
27
|
+
- IQR-based outlier detection
|
|
28
|
+
- Lightweight data-cleaning helpers
|
|
29
|
+
|
|
30
|
+
### 🤖 Models
|
|
31
|
+
|
|
32
|
+
- **Linear Regression**
|
|
33
|
+
- **Logistic Regression**
|
|
34
|
+
|
|
35
|
+
These implementations are intentionally minimal and readable, aimed at education and experimentation rather than production-scale systems.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install from PyPI (once published):
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install mathcoreml
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
For local development:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
python -m venv .venv
|
|
51
|
+
source .venv/bin/activate
|
|
52
|
+
pip install -e .
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick Start
|
|
56
|
+
|
|
57
|
+
Example usage with CSVStore (provide your own CSV file):
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from MathCoreML.utils.CSVStore import CSVStore
|
|
61
|
+
from MathCoreML.utils.CleanData import CleanData
|
|
62
|
+
|
|
63
|
+
store = CSVStore('/absolute/path/to/your_dataset.csv')
|
|
64
|
+
print('Rows:', len(store))
|
|
65
|
+
print('Max Age:', store.max_of('Age'))
|
|
66
|
+
|
|
67
|
+
store.quick_summary()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Package Structure
|
|
71
|
+
```text
|
|
72
|
+
MathCoreML/
|
|
73
|
+
├── src/
|
|
74
|
+
│ └── mathcoreml/
|
|
75
|
+
│ ├── Models/
|
|
76
|
+
│ │ ├── linearRegression.py
|
|
77
|
+
│ │ └── logisticRegression.py
|
|
78
|
+
│ └── utils/
|
|
79
|
+
│ ├── csvstore.py
|
|
80
|
+
│ ├── cleandata.py
|
|
81
|
+
│ └── modelevaluator.py
|
|
82
|
+
└── pyproject.toml
|
|
83
|
+
```
|
|
84
|
+
## Notes
|
|
85
|
+
|
|
86
|
+
### Designed for clarity and learning, not as a replacement for full ML frameworks.
|
|
87
|
+
|
|
88
|
+
#### Public API is limited to modules inside MathCoreML.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
This project is licensed under the MIT License. See the `LICENSE` file for details.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mathcoreml"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
authors = [{name = "Tigran", email = "tigran.harutunyan.02@gmail.com"}]
|
|
10
|
+
description = "Mathematical Implementation of ML algorithms from scratch"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
dependencies = [
|
|
13
|
+
"numpy",
|
|
14
|
+
"pandas",
|
|
15
|
+
"matplotlib",
|
|
16
|
+
"scikit-learn"
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[tool.setuptools.packages.find]
|
|
20
|
+
where = ["src"]
|
|
File without changes
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from sklearn.model_selection import train_test_split
|
|
2
|
+
from sklearn.linear_model import LinearRegression
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import mathcoreml.utils.csvstore as csvstore
|
|
5
|
+
from mathcoreml.Models.template_models.modelImplementationTemplate import Models
|
|
6
|
+
from mathcoreml.utils.cleandata import cleandata
|
|
7
|
+
from mathcoreml.utils.modelevalutaor import RegressionEvaluator
|
|
8
|
+
class linear_regression(Models):
|
|
9
|
+
def __init__(self,object:csvstore):
|
|
10
|
+
self.model = LinearRegression()
|
|
11
|
+
self._df = object.frame
|
|
12
|
+
self.is_trained = False
|
|
13
|
+
self.is_preaper = False
|
|
14
|
+
def preaper_data(self,x_column,y_column):
|
|
15
|
+
"""
|
|
16
|
+
Prepare data for model training by separating features and target.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
x_column: Column name to use as target variable (X)
|
|
20
|
+
y_column: Column name to use as target variable (Y)
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
None (prepares self.X and self.Y)
|
|
24
|
+
"""
|
|
25
|
+
if self.is_preaper:
|
|
26
|
+
print("Your Data is preapered")
|
|
27
|
+
return
|
|
28
|
+
q1_x = self._df[x_column].quantile(0.25)
|
|
29
|
+
q3_x = self._df[x_column].quantile(0.75)
|
|
30
|
+
q1_y = self._df[y_column].quantile(0.25)
|
|
31
|
+
q3_y = self._df[y_column].quantile(0.75)
|
|
32
|
+
|
|
33
|
+
self.__clean_data = cleandata(q1_x, q3_x, q1_y, q3_y)
|
|
34
|
+
mask_x = self._df[x_column].apply(lambda x: not self.__clean_data.x_is_outlier(x))
|
|
35
|
+
mask_y = self._df[y_column].apply(lambda y: not self.__clean_data.y_is_outlier(y))
|
|
36
|
+
self._df = self._df[mask_x & mask_y].copy()
|
|
37
|
+
self._df = self._df.dropna(subset=[x_column, y_column])
|
|
38
|
+
self._df = self._df.reset_index(drop=True)
|
|
39
|
+
self.X = self._df[[x_column]]
|
|
40
|
+
self.Y = self._df[y_column]
|
|
41
|
+
self.is_preaper = True
|
|
42
|
+
def train_model(self):
|
|
43
|
+
"""
|
|
44
|
+
Train the Lineary regression model using prepared data.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
None
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
float: Model accuracy score on test set
|
|
51
|
+
"""
|
|
52
|
+
if self.is_trained:
|
|
53
|
+
print("Your Model is trained")
|
|
54
|
+
return
|
|
55
|
+
X_train,X_test,Y_train,Y_test = train_test_split(self.X,self.Y,test_size=0.2,train_size=0.8)
|
|
56
|
+
self.model.fit(X_train,Y_train)
|
|
57
|
+
self.is_trained = True
|
|
58
|
+
self.y_hat = self.model.predict(self.X)
|
|
59
|
+
return self.model.score(X_test,Y_test)
|
|
60
|
+
def predict_single(self, index):
|
|
61
|
+
"""
|
|
62
|
+
Predict the probability for a single sample at given index.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
index: Row index in the dataset (0-based)
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
float: Predicted probability of the positive class (0-1)
|
|
69
|
+
"""
|
|
70
|
+
if index < 0 or index > len(self._df):
|
|
71
|
+
print("Invalid Index")
|
|
72
|
+
return 0
|
|
73
|
+
return self.model.predict(self.X.iloc[[index]])[0]
|
|
74
|
+
def plot_regression(self):
|
|
75
|
+
plt.figure()
|
|
76
|
+
plt.scatter(self.X,self.Y,color='black')
|
|
77
|
+
self._slope = self.model.coef_[0]
|
|
78
|
+
self.intercept = self.model.intercept_
|
|
79
|
+
y_line = self._slope * self.X + self.intercept
|
|
80
|
+
plt.plot(self.X,y_line,color='red',label="Regression line")
|
|
81
|
+
plt.xlabel(self.X.columns[0])
|
|
82
|
+
plt.ylabel(self.Y.name)
|
|
83
|
+
plt.title(f"{self.X.columns[0]} and {self.Y.name} regressoin graph")
|
|
84
|
+
plt.tight_layout()
|
|
85
|
+
plt.show()
|
|
86
|
+
def compare_dataset(self):
|
|
87
|
+
evaluator = RegressionEvaluator(self.X,self.y_hat)
|
|
88
|
+
evaluator.get_all_metrics()
|
|
89
|
+
evaluator.print_metrics()
|
|
90
|
+
ess ,rss,tss = evaluator.calculate_sum_of_squares()
|
|
91
|
+
print(f"ESS:{ess}")
|
|
92
|
+
print(f"RSS:{rss}")
|
|
93
|
+
print(f"TSS:{tss}")
|
|
94
|
+
|
|
95
|
+
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from sklearn.linear_model import LogisticRegression
|
|
2
|
+
from sklearn.model_selection import train_test_split
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import numpy as np
|
|
5
|
+
import mathcoreml.utils.csvstore as csvstore
|
|
6
|
+
from mathcoreml.Models.template_models.modelImplementationTemplate import Models
|
|
7
|
+
class logistic_regression(Models):
|
|
8
|
+
def __init__(self,dataset_object:csvstore):
|
|
9
|
+
"""
|
|
10
|
+
Initialize logistic regression model with a dataset.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
dataset_object: csvstore object containing the dataset
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
None
|
|
17
|
+
"""
|
|
18
|
+
self.model = LogisticRegression(max_iter=1000)
|
|
19
|
+
self._df = dataset_object.frame
|
|
20
|
+
self.is_trained = False
|
|
21
|
+
self.is_preaper = False
|
|
22
|
+
def preaper_data(self,x_column):
|
|
23
|
+
"""
|
|
24
|
+
Prepare data for model training by separating features and target.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
x_column: Column name to use as target variable (Y)
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
None (prepares self.X and self.Y)
|
|
31
|
+
"""
|
|
32
|
+
if self.is_preaper:
|
|
33
|
+
print("Your Data is preapered")
|
|
34
|
+
return
|
|
35
|
+
self.X = self._df.drop(x_column,axis=1)
|
|
36
|
+
self.Y = self._df[x_column]
|
|
37
|
+
self.is_preaper = True
|
|
38
|
+
def train_model(self):
|
|
39
|
+
"""
|
|
40
|
+
Train the logistic regression model using prepared data.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
None
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
float: Model accuracy score on test set
|
|
47
|
+
"""
|
|
48
|
+
if self.is_trained:
|
|
49
|
+
print("Your Model is tarined")
|
|
50
|
+
return
|
|
51
|
+
X_train , X_test , Y_train , Y_test = train_test_split(self.X,self.Y,test_size=0.2,train_size=0.7)
|
|
52
|
+
self.model.fit(X_train,Y_train)
|
|
53
|
+
self.is_trained = True
|
|
54
|
+
return self.model.score(X_test,Y_test)
|
|
55
|
+
def predict_single(self,index:int):
|
|
56
|
+
"""
|
|
57
|
+
Predict the probability for a single sample at given index.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
index: Row index in the dataset (0-based)
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
float: Predicted probability of the positive class (0-1)
|
|
64
|
+
"""
|
|
65
|
+
if index < 0 or index > len(self._df):
|
|
66
|
+
print("Invalid Index")
|
|
67
|
+
return 0
|
|
68
|
+
row_df = self.X.iloc[[index]]
|
|
69
|
+
print(row_df)
|
|
70
|
+
return self.model.predict_proba(row_df)[0][1]
|
|
71
|
+
def plot_my_model_sigmoid(self):
|
|
72
|
+
"""
|
|
73
|
+
Visualize the logistic regression sigmoid curve with predictions.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
None
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
None (displays sigmoid curve plot)
|
|
80
|
+
"""
|
|
81
|
+
z_values = self.model.decision_function(self.X)
|
|
82
|
+
probs = self.model.predict_proba(self.X)[:, 1]
|
|
83
|
+
|
|
84
|
+
z_range = np.linspace(z_values.min() - 1, z_values.max() + 1, 100)
|
|
85
|
+
sigmoid_curve = 1 / (1 + np.exp(-z_range))
|
|
86
|
+
|
|
87
|
+
plt.figure(figsize=(10, 6))
|
|
88
|
+
plt.plot(z_range, sigmoid_curve, color='blue', alpha=0.5, label='Sigmoid Curve')
|
|
89
|
+
|
|
90
|
+
plt.scatter(z_values, probs, c=probs, cmap='RdYlGn_r', edgecolors='black', s=40)
|
|
91
|
+
|
|
92
|
+
plt.axhline(0.5, color='red', linestyle='--', label='Threshold (0.5)')
|
|
93
|
+
plt.axvline(0, color='black', alpha=0.3)
|
|
94
|
+
|
|
95
|
+
plt.title('My Logistic Regression Sigmoid')
|
|
96
|
+
plt.xlabel('z')
|
|
97
|
+
plt.ylabel('Probabilty')
|
|
98
|
+
plt.legend()
|
|
99
|
+
plt.grid(True, alpha=0.2)
|
|
100
|
+
plt.show()
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
class cleandata:
|
|
2
|
+
def __init__(self, x_q1, x_q3, y_q1, y_q3):
|
|
3
|
+
"""
|
|
4
|
+
Initialize CleanData with quartile values for outlier detection.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
x_q1: First quartile (25th percentile) value for X
|
|
8
|
+
x_q3: Third quartile (75th percentile) value for X
|
|
9
|
+
y_q1: First quartile (25th percentile) value for Y
|
|
10
|
+
y_q3: Third quartile (75th percentile) value for Y
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
None
|
|
14
|
+
"""
|
|
15
|
+
# Store X quartile values for X dimension outlier detection
|
|
16
|
+
self._x_q1 = x_q1
|
|
17
|
+
self._x_q3 = x_q3
|
|
18
|
+
# Store Y quartile values for Y dimension outlier detection
|
|
19
|
+
self._y_q1 = y_q1
|
|
20
|
+
self._y_q3 = y_q3
|
|
21
|
+
@property
|
|
22
|
+
def x_iqr(self) -> float:
|
|
23
|
+
"""
|
|
24
|
+
Get the Interquartile Range (IQR) of X.
|
|
25
|
+
|
|
26
|
+
IQR = Q3 - Q1, represents the middle 50% of data spread.
|
|
27
|
+
Used in the IQR method for outlier detection.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
float: The IQR value (Q3 - Q1)
|
|
31
|
+
"""
|
|
32
|
+
return self._x_q3 - self._x_q1
|
|
33
|
+
@property
|
|
34
|
+
def y_iqr(self) -> float:
|
|
35
|
+
"""
|
|
36
|
+
Get the Interquartile Range (IQR) of Y.
|
|
37
|
+
|
|
38
|
+
IQR = Q3 - Q1, represents the middle 50% of data spread.
|
|
39
|
+
Used in the IQR method for outlier detection.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
float: The IQR value (Q3 - Q1)
|
|
43
|
+
"""
|
|
44
|
+
return self._y_q3 - self._y_q1
|
|
45
|
+
@property
|
|
46
|
+
def x_lower_bound(self) -> float:
|
|
47
|
+
"""
|
|
48
|
+
Get the lower bound for X outlier detection.
|
|
49
|
+
|
|
50
|
+
Uses IQR method: Lower Bound = Q1 - 1.5 * IQR
|
|
51
|
+
Values below this bound are considered outliers.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
float: Lower bound calculated as Q1 - 1.5 * IQR
|
|
55
|
+
"""
|
|
56
|
+
return self._x_q1 - 1.5 * self.x_iqr
|
|
57
|
+
@property
|
|
58
|
+
def x_upper_bound(self) -> float:
|
|
59
|
+
"""
|
|
60
|
+
Get the upper bound for X outlier detection.
|
|
61
|
+
|
|
62
|
+
Uses IQR method: Upper Bound = Q3 + 1.5 * IQR
|
|
63
|
+
Values above this bound are considered outliers.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
float: Upper bound calculated as Q3 + 1.5 * IQR
|
|
67
|
+
"""
|
|
68
|
+
return self._x_q3 + 1.5 * self.x_iqr
|
|
69
|
+
@property
|
|
70
|
+
def y_lower_bound(self) -> float:
|
|
71
|
+
"""
|
|
72
|
+
Get the lower bound for Y outlier detection.
|
|
73
|
+
|
|
74
|
+
Uses IQR method: Lower Bound = Q1 - 1.5 * IQR
|
|
75
|
+
Values below this bound are considered outliers.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
float: Lower bound calculated as Q1 - 1.5 * IQR
|
|
79
|
+
"""
|
|
80
|
+
return self._y_q1 - 1.5 * self.y_iqr
|
|
81
|
+
@property
|
|
82
|
+
def y_upper_bound(self) -> float:
|
|
83
|
+
"""
|
|
84
|
+
Get the upper bound for Y outlier detection.
|
|
85
|
+
|
|
86
|
+
Uses IQR method: Upper Bound = Q3 + 1.5 * IQR
|
|
87
|
+
Values above this bound are considered outliers.
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
float: Upper bound calculated as Q3 + 1.5 * IQR
|
|
91
|
+
"""
|
|
92
|
+
return self._y_q3 + 1.5 * self.y_iqr
|
|
93
|
+
def y_is_outlier(self, value) -> bool:
|
|
94
|
+
"""
|
|
95
|
+
Check if a Y value is an outlier using the IQR method.
|
|
96
|
+
|
|
97
|
+
An outlier is any value that falls outside the bounds:
|
|
98
|
+
(value < lower_bound) or (value > upper_bound)
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
value: The Y value to check
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
bool: True if value is outside the bounds (outlier), False otherwise
|
|
105
|
+
"""
|
|
106
|
+
return (value < self.y_lower_bound) or (value > self.y_upper_bound)
|
|
107
|
+
def x_is_outlier(self, value) -> bool:
|
|
108
|
+
"""
|
|
109
|
+
Check if an X value is an outlier using the IQR method.
|
|
110
|
+
|
|
111
|
+
An outlier is any value that falls outside the bounds:
|
|
112
|
+
(value < lower_bound) or (value > upper_bound)
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
value: The X value to check
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
bool: True if value is outside the bounds (outlier), False otherwise
|
|
119
|
+
"""
|
|
120
|
+
return (value < self.x_lower_bound) or (value > self.x_upper_bound)
|
|
121
|
+
|
|
122
|
+
|