opengood.py-ml-plot 1.10.0__tar.gz → 2.0.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.
- opengood_py_ml_plot-2.0.0/PKG-INFO +328 -0
- opengood_py_ml_plot-2.0.0/README.md +287 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/pyproject.toml +1 -1
- opengood_py_ml_plot-2.0.0/src/opengood/py_ml_plot/__init__.py +5 -0
- opengood_py_ml_plot-2.0.0/src/opengood/py_ml_plot/classification_plot.py +74 -0
- opengood_py_ml_plot-2.0.0/src/opengood.py_ml_plot.egg-info/PKG-INFO +328 -0
- opengood_py_ml_plot-1.10.0/PKG-INFO +0 -143
- opengood_py_ml_plot-1.10.0/README.md +0 -102
- opengood_py_ml_plot-1.10.0/src/opengood/py_ml_plot/__init__.py +0 -5
- opengood_py_ml_plot-1.10.0/src/opengood/py_ml_plot/classification_plot.py +0 -43
- opengood_py_ml_plot-1.10.0/src/opengood.py_ml_plot.egg-info/PKG-INFO +0 -143
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/LICENSE +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/setup.cfg +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/src/opengood/__init__.py +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/src/opengood.py_ml_plot.egg-info/SOURCES.txt +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/src/opengood.py_ml_plot.egg-info/dependency_links.txt +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/src/opengood.py_ml_plot.egg-info/requires.txt +0 -0
- {opengood_py_ml_plot-1.10.0 → opengood_py_ml_plot-2.0.0}/src/opengood.py_ml_plot.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opengood.py_ml_plot
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Modules containing reusable functions for machine learning visualization plotting
|
|
5
|
+
Author-email: OpenGood <ai@opengood.ai>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 OpenGood
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/opengood-aio/py-ml-plot
|
|
29
|
+
Project-URL: Documentation, https://github.com/opengood-aio/py-ml-plot
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.13
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: matplotlib>=3.10.3
|
|
37
|
+
Requires-Dist: numpy>=2.3.0rc1
|
|
38
|
+
Requires-Dist: pandas>=2.2.3
|
|
39
|
+
Requires-Dist: scikit-learn>=1.7.0rc1
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# Python Machine Learning (ML) Plot
|
|
43
|
+
|
|
44
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions?query=workflow%3Abuild)
|
|
45
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions?query=workflow%3Arelease)
|
|
46
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions/workflows/codeql.yml)
|
|
47
|
+
[](https://codecov.io/gh/opengood-aio/py-ml-plot)
|
|
48
|
+
[](https://github.com/opengood-aio/py-ml-plot/releases/latest)
|
|
49
|
+
[](https://pypi.org/project/opengood.py-ml-plot/)
|
|
50
|
+

|
|
51
|
+
[](https://raw.githubusercontent.com/opengood-aio/py-ml-plot/master/LICENSE)
|
|
52
|
+
|
|
53
|
+
Modules containing reusable functions for machine learning visualization
|
|
54
|
+
plotting
|
|
55
|
+
|
|
56
|
+
## Compatibility
|
|
57
|
+
|
|
58
|
+
* Python 3.13 or later
|
|
59
|
+
|
|
60
|
+
## Setup
|
|
61
|
+
|
|
62
|
+
### Add Dependency
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
python3 -m pip install opengood.py-ml-plot
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Note:** See *Release* version badge above for the latest version.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Features
|
|
73
|
+
|
|
74
|
+
### Classification Model Plotting
|
|
75
|
+
|
|
76
|
+
#### Set Up and Display 2-D Classification Plot
|
|
77
|
+
|
|
78
|
+
Set up a 2-D classification model plot then display its result visualization.
|
|
79
|
+
|
|
80
|
+
**Notes:**
|
|
81
|
+
|
|
82
|
+
* The example below uses a dataset to train a logistic regression model then
|
|
83
|
+
display the plot for the training set
|
|
84
|
+
* For feature scaling, if required, implement the feature scaling logic in the
|
|
85
|
+
`feature_scaling` lambda
|
|
86
|
+
* For predictions, implement the prediction logic in the `predict` lambda
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import os
|
|
90
|
+
|
|
91
|
+
import numpy as np
|
|
92
|
+
import pandas as pd
|
|
93
|
+
from matplotlib import pyplot as plt
|
|
94
|
+
from matplotlib.colors import ListedColormap
|
|
95
|
+
from sklearn.linear_model import LogisticRegression
|
|
96
|
+
from sklearn.model_selection import train_test_split
|
|
97
|
+
from sklearn.preprocessing import StandardScaler
|
|
98
|
+
|
|
99
|
+
from src.opengood.py_ml_plot import setup_classification_plot
|
|
100
|
+
|
|
101
|
+
resource_path = os.path.join(os.path.dirname(__file__), "../resources", "data.csv")
|
|
102
|
+
dataset = pd.read_csv(resource_path)
|
|
103
|
+
|
|
104
|
+
x = dataset.iloc[:, :-1].values
|
|
105
|
+
y = dataset.iloc[:, -1].values
|
|
106
|
+
|
|
107
|
+
x_train, _, y_train, _ = train_test_split(x, y, test_size=0.2, random_state=0)
|
|
108
|
+
|
|
109
|
+
sc = StandardScaler()
|
|
110
|
+
x_train = sc.fit_transform(x_train)
|
|
111
|
+
|
|
112
|
+
classifier = LogisticRegression(random_state=0)
|
|
113
|
+
classifier.fit(x_train, y_train)
|
|
114
|
+
|
|
115
|
+
setup_classification_plot(
|
|
116
|
+
x=x_train,
|
|
117
|
+
y=y_train,
|
|
118
|
+
cmap=ListedColormap(("salmon", "dodgerblue")),
|
|
119
|
+
title="Logistic Regression",
|
|
120
|
+
x_label="Age",
|
|
121
|
+
y_label="Estimated Salary",
|
|
122
|
+
feature_scale=lambda x_set, y_set: (
|
|
123
|
+
sc.inverse_transform(x_set), y_set
|
|
124
|
+
),
|
|
125
|
+
predict=lambda x1, x2: (
|
|
126
|
+
classifier.predict(
|
|
127
|
+
sc.transform(
|
|
128
|
+
np.array([x1.ravel(), x2.ravel()]).T)
|
|
129
|
+
).reshape(x1.shape)
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
plt.show()
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
##### Output
|
|
137
|
+
|
|
138
|
+

|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
##### Visualization Logic
|
|
143
|
+
|
|
144
|
+
###### Feature Scale Lambda Logic
|
|
145
|
+
|
|
146
|
+
`feature_scale` lambda implementation logic for function
|
|
147
|
+
`setup_classification_plot` is as follows:
|
|
148
|
+
|
|
149
|
+
* Inverse feature scaling is invoked via a featuring scaling object, such as
|
|
150
|
+
the `StandardScalar` object `sc` created earlier for feature scaling
|
|
151
|
+
* `x_set` and `y_set` are assigned non-feature scaled values of the matrix of
|
|
152
|
+
features and the dependent variable
|
|
153
|
+
* `x_set` values are inverted from their feature-scaled values in `x`
|
|
154
|
+
* `y_set` values are not inverted and taken directly from `y`
|
|
155
|
+
|
|
156
|
+
###### Predict Lambda Logic
|
|
157
|
+
|
|
158
|
+
`predict` lambda implementation logic for function `setup_classification_plot`
|
|
159
|
+
is as follows:
|
|
160
|
+
|
|
161
|
+
* Classifier object `classifier` method `predict` is invoked
|
|
162
|
+
* Since the values of the reshaped 2D array are not feature scaled, the
|
|
163
|
+
values are feature scaled via the `transform` method on the `sc`
|
|
164
|
+
object
|
|
165
|
+
* This method call is not required for models that do not require feature
|
|
166
|
+
scaling
|
|
167
|
+
* `ravel` function from the NumPy library is used to flatten a
|
|
168
|
+
multidimensional array into a one-dimensional array
|
|
169
|
+
* `x1` and `x2` are flatten into a 1D array via the `ravel` function
|
|
170
|
+
* They are then combined via the `array` function from the NumPy
|
|
171
|
+
library into a 2D array
|
|
172
|
+
* The result is then reshaped via the `reshape` function to match
|
|
173
|
+
the shape of `x1`
|
|
174
|
+
|
|
175
|
+
###### Setup Plot Visualization Logic
|
|
176
|
+
|
|
177
|
+
Visualization implementation logic for function `setup_classification_plot` is
|
|
178
|
+
as follows:
|
|
179
|
+
|
|
180
|
+
* `ListedColormap` class from the Matplotlib library creates an object that
|
|
181
|
+
generates a colormap visual from a list of colors
|
|
182
|
+
* If the `feature_scale` lambda is defined, `x_set` and `y_set` are
|
|
183
|
+
assigned non-feature scaled values of the matrix of features and the dependent
|
|
184
|
+
variable from the sets using a feature scaling object, such as the
|
|
185
|
+
`StandardScalar` object created earlier for feature scaling
|
|
186
|
+
* `x_set` values are inverted from their feature-scaled values in `x`
|
|
187
|
+
* `y_set` values are not inverted and taken directly from `y`
|
|
188
|
+
* If the `feature_scale` lambda is not defined, `x_set` and `y_set` are
|
|
189
|
+
assigned the values of `x` and `y`, respectively
|
|
190
|
+
* `meshgrid` function from the NumPy library returns a tuple of coordinate
|
|
191
|
+
matrices from coordinate vectors
|
|
192
|
+
* Two sets of matrices (`x1` and `x2`) are returned with coordinate vectors
|
|
193
|
+
* `x1`
|
|
194
|
+
* `arange` function is called with a defined start and stop interval
|
|
195
|
+
* `x_set[:, 0]` returns all the rows for feature `x1`
|
|
196
|
+
* `start` parameter
|
|
197
|
+
* Start of an interval
|
|
198
|
+
* `x_set[:, 0].min()` returns the minimum value for feature `x1`
|
|
199
|
+
* Value of `10` is subtracted for padding
|
|
200
|
+
* `stop` parameter
|
|
201
|
+
* End of an interval
|
|
202
|
+
* `x_set[:, 0].max()` returns the maximum value for feature `x1`
|
|
203
|
+
* Value of `10` is added for padding
|
|
204
|
+
* `step` parameter
|
|
205
|
+
* Spacing between values
|
|
206
|
+
* Value of `0.25` is added for spacing
|
|
207
|
+
* `x2`
|
|
208
|
+
* `arange` function is called with a defined start and stop interval
|
|
209
|
+
* `x_set[:, 1]` returns all the rows for feature `x2`
|
|
210
|
+
* `start` parameter
|
|
211
|
+
* Start of an interval
|
|
212
|
+
* `x_set[:, 1].min()` returns the minimum value for feature `x2`
|
|
213
|
+
* Value of `1000` is subtracted for padding
|
|
214
|
+
* Value of `1000` is used instead of `10` due to the difference
|
|
215
|
+
in scaling for feature `x2` vs. feature `x1`
|
|
216
|
+
* `stop` parameter
|
|
217
|
+
* End of interval
|
|
218
|
+
* `x_set[:, 1].max()` returns the maximum value for feature `x2`
|
|
219
|
+
* Value of `1000` is added for padding
|
|
220
|
+
* `step` parameter
|
|
221
|
+
* Spacing between values
|
|
222
|
+
* Value of `0.25` is added for spacing
|
|
223
|
+
* The prediction logic implemented in the `preodict` lambda is executed, and
|
|
224
|
+
the result is assigned to `y_pred`, containing the predictions
|
|
225
|
+
* `contourf` function from the Matplotlib library is used for creating filled
|
|
226
|
+
contour plots
|
|
227
|
+
* It visualizes 3D data in 2D by drawing filled contours representing
|
|
228
|
+
constant z-values (heights) on an x-y plane
|
|
229
|
+
* These plots are useful for displaying data like temperature distributions,
|
|
230
|
+
terrain elevations, or any scalar field where the magnitude varies over 2
|
|
231
|
+
dimensions
|
|
232
|
+
* The most basic use case of `contourf` involves providing a 2D array
|
|
233
|
+
representing the z-values
|
|
234
|
+
* Matplotlib automatically determines the x and y coordinates based on the
|
|
235
|
+
array's indices
|
|
236
|
+
* `X` and `Y` parameters
|
|
237
|
+
* The coordinates of the values in `Z`
|
|
238
|
+
* `X` and `Y` must both be 2D arrays with the same shape as `Z`
|
|
239
|
+
* `x1` is used for `X` containing `x1` values
|
|
240
|
+
* `x2` is used for `Y` containing `x2` values
|
|
241
|
+
* `Z` parameter
|
|
242
|
+
* The height values over which the contour is drawn
|
|
243
|
+
* `ravel` function from the NumPy library is used to flatten a
|
|
244
|
+
multidimensional array into a one-dimensional array
|
|
245
|
+
* `x1` and `x2` are flatten into a 1D array via the `ravel` function
|
|
246
|
+
* They are then combined via the `array` function from the NumPy
|
|
247
|
+
library into a 2D array
|
|
248
|
+
* The result is then reshaped via the `reshape` function to match
|
|
249
|
+
the shape of `x1`
|
|
250
|
+
* Since the values of the reshaped 2D array are not feature scaled, the
|
|
251
|
+
values are feature scaled via the `transform` method on the `sc`
|
|
252
|
+
object
|
|
253
|
+
* `alpha` parameter
|
|
254
|
+
* The alpha blending value, between `0` (transparent) and `1` (opaque)
|
|
255
|
+
* Value of `0.75` is used to make the blending mostly opaque
|
|
256
|
+
* `cmap` parameter
|
|
257
|
+
* The `Colormap` object instance or registered colormap name used to map
|
|
258
|
+
scalar data to colors
|
|
259
|
+
* `salmon` and `dodgerblue` are used for a `ListedColormap` object
|
|
260
|
+
* `salmon` = 0 or negative classifier
|
|
261
|
+
* `dodgerblue` = 1 or positive classifier
|
|
262
|
+
* `xlim` function from the Matplotlib library is used to get or set the x-axis
|
|
263
|
+
limits of the current axes
|
|
264
|
+
* `min()` and `max()` for `x1` are used for the limits
|
|
265
|
+
* `ylim` function from the Matplotlib library is used to get or set the y-axis
|
|
266
|
+
limits of the current axes
|
|
267
|
+
* `min()` and `max()` for `x2` are used for the limits
|
|
268
|
+
* The values from `y_set` are iterated over in a for-in loop
|
|
269
|
+
* `unique` function from the NumPy library returns sorted, unique elements
|
|
270
|
+
of an array
|
|
271
|
+
* Values of `y_set` are made unique and sorted
|
|
272
|
+
* Iterator variable `i` represents the current row of iteration
|
|
273
|
+
* Iterator variable `j` represents the classification value for the
|
|
274
|
+
dependent variable
|
|
275
|
+
* `0` negative classifier
|
|
276
|
+
* `1` positive classifier
|
|
277
|
+
* `scatter` method from the Matplotlib library creates a scatter plot of data
|
|
278
|
+
points with the shaded contour showing the classification for the dependent
|
|
279
|
+
variable
|
|
280
|
+
* x-axis uses values from `x_set` where `y_set` value = 0 (negative
|
|
281
|
+
classifier)
|
|
282
|
+
* y-axis uses values from `x_set` where `y_set` value = 1 (positive
|
|
283
|
+
classifier)
|
|
284
|
+
* `c` parameter
|
|
285
|
+
* The marker colors
|
|
286
|
+
* Uses the `ListedColormap` with the classification colors for the
|
|
287
|
+
current row at index `i`
|
|
288
|
+
* `label` parameter
|
|
289
|
+
* Sets the label
|
|
290
|
+
* Values
|
|
291
|
+
* `0` negative classifier
|
|
292
|
+
* `1` positive classifier
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
# Development
|
|
297
|
+
|
|
298
|
+
## Python Virtual Environment
|
|
299
|
+
|
|
300
|
+
Create Python virtual environment:
|
|
301
|
+
|
|
302
|
+
```bash
|
|
303
|
+
cd ~/workspace/opengood-aio/py-ml-plot/.venv
|
|
304
|
+
python3 -m venv ~/workspace/opengood-aio/py-ml-plot/.venv
|
|
305
|
+
source .venv/bin/activate
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Install Packages
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
python3 -m pip install matplotlib
|
|
312
|
+
python3 -m pip install numpy
|
|
313
|
+
python3 -m pip install pandas
|
|
314
|
+
python3 -m pip install scikit-learn
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Create Requirements File
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
pip freeze > requirements.txt
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## Run Tests
|
|
324
|
+
|
|
325
|
+
```bash
|
|
326
|
+
python -m pytest tests/
|
|
327
|
+
```
|
|
328
|
+
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# Python Machine Learning (ML) Plot
|
|
2
|
+
|
|
3
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions?query=workflow%3Abuild)
|
|
4
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions?query=workflow%3Arelease)
|
|
5
|
+
[](https://github.com/opengood-aio/py-ml-plot/actions/workflows/codeql.yml)
|
|
6
|
+
[](https://codecov.io/gh/opengood-aio/py-ml-plot)
|
|
7
|
+
[](https://github.com/opengood-aio/py-ml-plot/releases/latest)
|
|
8
|
+
[](https://pypi.org/project/opengood.py-ml-plot/)
|
|
9
|
+

|
|
10
|
+
[](https://raw.githubusercontent.com/opengood-aio/py-ml-plot/master/LICENSE)
|
|
11
|
+
|
|
12
|
+
Modules containing reusable functions for machine learning visualization
|
|
13
|
+
plotting
|
|
14
|
+
|
|
15
|
+
## Compatibility
|
|
16
|
+
|
|
17
|
+
* Python 3.13 or later
|
|
18
|
+
|
|
19
|
+
## Setup
|
|
20
|
+
|
|
21
|
+
### Add Dependency
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
python3 -m pip install opengood.py-ml-plot
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Note:** See *Release* version badge above for the latest version.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
### Classification Model Plotting
|
|
34
|
+
|
|
35
|
+
#### Set Up and Display 2-D Classification Plot
|
|
36
|
+
|
|
37
|
+
Set up a 2-D classification model plot then display its result visualization.
|
|
38
|
+
|
|
39
|
+
**Notes:**
|
|
40
|
+
|
|
41
|
+
* The example below uses a dataset to train a logistic regression model then
|
|
42
|
+
display the plot for the training set
|
|
43
|
+
* For feature scaling, if required, implement the feature scaling logic in the
|
|
44
|
+
`feature_scaling` lambda
|
|
45
|
+
* For predictions, implement the prediction logic in the `predict` lambda
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import os
|
|
49
|
+
|
|
50
|
+
import numpy as np
|
|
51
|
+
import pandas as pd
|
|
52
|
+
from matplotlib import pyplot as plt
|
|
53
|
+
from matplotlib.colors import ListedColormap
|
|
54
|
+
from sklearn.linear_model import LogisticRegression
|
|
55
|
+
from sklearn.model_selection import train_test_split
|
|
56
|
+
from sklearn.preprocessing import StandardScaler
|
|
57
|
+
|
|
58
|
+
from src.opengood.py_ml_plot import setup_classification_plot
|
|
59
|
+
|
|
60
|
+
resource_path = os.path.join(os.path.dirname(__file__), "../resources", "data.csv")
|
|
61
|
+
dataset = pd.read_csv(resource_path)
|
|
62
|
+
|
|
63
|
+
x = dataset.iloc[:, :-1].values
|
|
64
|
+
y = dataset.iloc[:, -1].values
|
|
65
|
+
|
|
66
|
+
x_train, _, y_train, _ = train_test_split(x, y, test_size=0.2, random_state=0)
|
|
67
|
+
|
|
68
|
+
sc = StandardScaler()
|
|
69
|
+
x_train = sc.fit_transform(x_train)
|
|
70
|
+
|
|
71
|
+
classifier = LogisticRegression(random_state=0)
|
|
72
|
+
classifier.fit(x_train, y_train)
|
|
73
|
+
|
|
74
|
+
setup_classification_plot(
|
|
75
|
+
x=x_train,
|
|
76
|
+
y=y_train,
|
|
77
|
+
cmap=ListedColormap(("salmon", "dodgerblue")),
|
|
78
|
+
title="Logistic Regression",
|
|
79
|
+
x_label="Age",
|
|
80
|
+
y_label="Estimated Salary",
|
|
81
|
+
feature_scale=lambda x_set, y_set: (
|
|
82
|
+
sc.inverse_transform(x_set), y_set
|
|
83
|
+
),
|
|
84
|
+
predict=lambda x1, x2: (
|
|
85
|
+
classifier.predict(
|
|
86
|
+
sc.transform(
|
|
87
|
+
np.array([x1.ravel(), x2.ravel()]).T)
|
|
88
|
+
).reshape(x1.shape)
|
|
89
|
+
),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
plt.show()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
##### Output
|
|
96
|
+
|
|
97
|
+

|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
##### Visualization Logic
|
|
102
|
+
|
|
103
|
+
###### Feature Scale Lambda Logic
|
|
104
|
+
|
|
105
|
+
`feature_scale` lambda implementation logic for function
|
|
106
|
+
`setup_classification_plot` is as follows:
|
|
107
|
+
|
|
108
|
+
* Inverse feature scaling is invoked via a featuring scaling object, such as
|
|
109
|
+
the `StandardScalar` object `sc` created earlier for feature scaling
|
|
110
|
+
* `x_set` and `y_set` are assigned non-feature scaled values of the matrix of
|
|
111
|
+
features and the dependent variable
|
|
112
|
+
* `x_set` values are inverted from their feature-scaled values in `x`
|
|
113
|
+
* `y_set` values are not inverted and taken directly from `y`
|
|
114
|
+
|
|
115
|
+
###### Predict Lambda Logic
|
|
116
|
+
|
|
117
|
+
`predict` lambda implementation logic for function `setup_classification_plot`
|
|
118
|
+
is as follows:
|
|
119
|
+
|
|
120
|
+
* Classifier object `classifier` method `predict` is invoked
|
|
121
|
+
* Since the values of the reshaped 2D array are not feature scaled, the
|
|
122
|
+
values are feature scaled via the `transform` method on the `sc`
|
|
123
|
+
object
|
|
124
|
+
* This method call is not required for models that do not require feature
|
|
125
|
+
scaling
|
|
126
|
+
* `ravel` function from the NumPy library is used to flatten a
|
|
127
|
+
multidimensional array into a one-dimensional array
|
|
128
|
+
* `x1` and `x2` are flatten into a 1D array via the `ravel` function
|
|
129
|
+
* They are then combined via the `array` function from the NumPy
|
|
130
|
+
library into a 2D array
|
|
131
|
+
* The result is then reshaped via the `reshape` function to match
|
|
132
|
+
the shape of `x1`
|
|
133
|
+
|
|
134
|
+
###### Setup Plot Visualization Logic
|
|
135
|
+
|
|
136
|
+
Visualization implementation logic for function `setup_classification_plot` is
|
|
137
|
+
as follows:
|
|
138
|
+
|
|
139
|
+
* `ListedColormap` class from the Matplotlib library creates an object that
|
|
140
|
+
generates a colormap visual from a list of colors
|
|
141
|
+
* If the `feature_scale` lambda is defined, `x_set` and `y_set` are
|
|
142
|
+
assigned non-feature scaled values of the matrix of features and the dependent
|
|
143
|
+
variable from the sets using a feature scaling object, such as the
|
|
144
|
+
`StandardScalar` object created earlier for feature scaling
|
|
145
|
+
* `x_set` values are inverted from their feature-scaled values in `x`
|
|
146
|
+
* `y_set` values are not inverted and taken directly from `y`
|
|
147
|
+
* If the `feature_scale` lambda is not defined, `x_set` and `y_set` are
|
|
148
|
+
assigned the values of `x` and `y`, respectively
|
|
149
|
+
* `meshgrid` function from the NumPy library returns a tuple of coordinate
|
|
150
|
+
matrices from coordinate vectors
|
|
151
|
+
* Two sets of matrices (`x1` and `x2`) are returned with coordinate vectors
|
|
152
|
+
* `x1`
|
|
153
|
+
* `arange` function is called with a defined start and stop interval
|
|
154
|
+
* `x_set[:, 0]` returns all the rows for feature `x1`
|
|
155
|
+
* `start` parameter
|
|
156
|
+
* Start of an interval
|
|
157
|
+
* `x_set[:, 0].min()` returns the minimum value for feature `x1`
|
|
158
|
+
* Value of `10` is subtracted for padding
|
|
159
|
+
* `stop` parameter
|
|
160
|
+
* End of an interval
|
|
161
|
+
* `x_set[:, 0].max()` returns the maximum value for feature `x1`
|
|
162
|
+
* Value of `10` is added for padding
|
|
163
|
+
* `step` parameter
|
|
164
|
+
* Spacing between values
|
|
165
|
+
* Value of `0.25` is added for spacing
|
|
166
|
+
* `x2`
|
|
167
|
+
* `arange` function is called with a defined start and stop interval
|
|
168
|
+
* `x_set[:, 1]` returns all the rows for feature `x2`
|
|
169
|
+
* `start` parameter
|
|
170
|
+
* Start of an interval
|
|
171
|
+
* `x_set[:, 1].min()` returns the minimum value for feature `x2`
|
|
172
|
+
* Value of `1000` is subtracted for padding
|
|
173
|
+
* Value of `1000` is used instead of `10` due to the difference
|
|
174
|
+
in scaling for feature `x2` vs. feature `x1`
|
|
175
|
+
* `stop` parameter
|
|
176
|
+
* End of interval
|
|
177
|
+
* `x_set[:, 1].max()` returns the maximum value for feature `x2`
|
|
178
|
+
* Value of `1000` is added for padding
|
|
179
|
+
* `step` parameter
|
|
180
|
+
* Spacing between values
|
|
181
|
+
* Value of `0.25` is added for spacing
|
|
182
|
+
* The prediction logic implemented in the `preodict` lambda is executed, and
|
|
183
|
+
the result is assigned to `y_pred`, containing the predictions
|
|
184
|
+
* `contourf` function from the Matplotlib library is used for creating filled
|
|
185
|
+
contour plots
|
|
186
|
+
* It visualizes 3D data in 2D by drawing filled contours representing
|
|
187
|
+
constant z-values (heights) on an x-y plane
|
|
188
|
+
* These plots are useful for displaying data like temperature distributions,
|
|
189
|
+
terrain elevations, or any scalar field where the magnitude varies over 2
|
|
190
|
+
dimensions
|
|
191
|
+
* The most basic use case of `contourf` involves providing a 2D array
|
|
192
|
+
representing the z-values
|
|
193
|
+
* Matplotlib automatically determines the x and y coordinates based on the
|
|
194
|
+
array's indices
|
|
195
|
+
* `X` and `Y` parameters
|
|
196
|
+
* The coordinates of the values in `Z`
|
|
197
|
+
* `X` and `Y` must both be 2D arrays with the same shape as `Z`
|
|
198
|
+
* `x1` is used for `X` containing `x1` values
|
|
199
|
+
* `x2` is used for `Y` containing `x2` values
|
|
200
|
+
* `Z` parameter
|
|
201
|
+
* The height values over which the contour is drawn
|
|
202
|
+
* `ravel` function from the NumPy library is used to flatten a
|
|
203
|
+
multidimensional array into a one-dimensional array
|
|
204
|
+
* `x1` and `x2` are flatten into a 1D array via the `ravel` function
|
|
205
|
+
* They are then combined via the `array` function from the NumPy
|
|
206
|
+
library into a 2D array
|
|
207
|
+
* The result is then reshaped via the `reshape` function to match
|
|
208
|
+
the shape of `x1`
|
|
209
|
+
* Since the values of the reshaped 2D array are not feature scaled, the
|
|
210
|
+
values are feature scaled via the `transform` method on the `sc`
|
|
211
|
+
object
|
|
212
|
+
* `alpha` parameter
|
|
213
|
+
* The alpha blending value, between `0` (transparent) and `1` (opaque)
|
|
214
|
+
* Value of `0.75` is used to make the blending mostly opaque
|
|
215
|
+
* `cmap` parameter
|
|
216
|
+
* The `Colormap` object instance or registered colormap name used to map
|
|
217
|
+
scalar data to colors
|
|
218
|
+
* `salmon` and `dodgerblue` are used for a `ListedColormap` object
|
|
219
|
+
* `salmon` = 0 or negative classifier
|
|
220
|
+
* `dodgerblue` = 1 or positive classifier
|
|
221
|
+
* `xlim` function from the Matplotlib library is used to get or set the x-axis
|
|
222
|
+
limits of the current axes
|
|
223
|
+
* `min()` and `max()` for `x1` are used for the limits
|
|
224
|
+
* `ylim` function from the Matplotlib library is used to get or set the y-axis
|
|
225
|
+
limits of the current axes
|
|
226
|
+
* `min()` and `max()` for `x2` are used for the limits
|
|
227
|
+
* The values from `y_set` are iterated over in a for-in loop
|
|
228
|
+
* `unique` function from the NumPy library returns sorted, unique elements
|
|
229
|
+
of an array
|
|
230
|
+
* Values of `y_set` are made unique and sorted
|
|
231
|
+
* Iterator variable `i` represents the current row of iteration
|
|
232
|
+
* Iterator variable `j` represents the classification value for the
|
|
233
|
+
dependent variable
|
|
234
|
+
* `0` negative classifier
|
|
235
|
+
* `1` positive classifier
|
|
236
|
+
* `scatter` method from the Matplotlib library creates a scatter plot of data
|
|
237
|
+
points with the shaded contour showing the classification for the dependent
|
|
238
|
+
variable
|
|
239
|
+
* x-axis uses values from `x_set` where `y_set` value = 0 (negative
|
|
240
|
+
classifier)
|
|
241
|
+
* y-axis uses values from `x_set` where `y_set` value = 1 (positive
|
|
242
|
+
classifier)
|
|
243
|
+
* `c` parameter
|
|
244
|
+
* The marker colors
|
|
245
|
+
* Uses the `ListedColormap` with the classification colors for the
|
|
246
|
+
current row at index `i`
|
|
247
|
+
* `label` parameter
|
|
248
|
+
* Sets the label
|
|
249
|
+
* Values
|
|
250
|
+
* `0` negative classifier
|
|
251
|
+
* `1` positive classifier
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
# Development
|
|
256
|
+
|
|
257
|
+
## Python Virtual Environment
|
|
258
|
+
|
|
259
|
+
Create Python virtual environment:
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
cd ~/workspace/opengood-aio/py-ml-plot/.venv
|
|
263
|
+
python3 -m venv ~/workspace/opengood-aio/py-ml-plot/.venv
|
|
264
|
+
source .venv/bin/activate
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Install Packages
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
python3 -m pip install matplotlib
|
|
271
|
+
python3 -m pip install numpy
|
|
272
|
+
python3 -m pip install pandas
|
|
273
|
+
python3 -m pip install scikit-learn
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Create Requirements File
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
pip freeze > requirements.txt
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Run Tests
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
python -m pytest tests/
|
|
286
|
+
```
|
|
287
|
+
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "opengood.py_ml_plot"
|
|
7
|
-
version = "
|
|
7
|
+
version = "2.0.0"
|
|
8
8
|
description = "Modules containing reusable functions for machine learning visualization plotting"
|
|
9
9
|
authors = [
|
|
10
10
|
{ name = "OpenGood", email = "ai@opengood.ai" }
|