Skip to content

Created LGBM classifier @ lgbm_classifier.py #11759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
73 changes: 73 additions & 0 deletions machine_learning/lgbm_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# LGBM Classifier Example
import numpy as np
from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier


def data_handling(data: dict) -> tuple:

Check failure on line 10 in machine_learning/lgbm_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/lgbm_classifier.py:2:1: I001 Import block is un-sorted or un-formatted

Check failure on line 10 in machine_learning/lgbm_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/lgbm_classifier.py:2:1: I001 Import block is un-sorted or un-formatted
"""
Splits dataset into features and target labels.

>>> data_handling({'data': '[5.1, 3.5, 1.4, 0.2]', 'target': [0]})
('[5.1, 3.5, 1.4, 0.2]', [0])
>>> data_handling({'data': '[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', 'target': [0, 0]})

Check failure on line 16 in machine_learning/lgbm_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/lgbm_classifier.py:16:89: E501 Line too long (95 > 88)

Check failure on line 16 in machine_learning/lgbm_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/lgbm_classifier.py:16:89: E501 Line too long (95 > 88)
('[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', [0, 0])
"""
return data["data"], data["target"]


def lgbm_classifier(features: np.ndarray, target: np.ndarray) -> LGBMClassifier:
"""
Trains an LGBM Classifier on the given features and target labels.

>>> lgbm_classifier(np.array([[5.1, 3.6, 1.4, 0.2]]), np.array([0]))
LGBMClassifier()
"""
classifier = LGBMClassifier()
classifier.fit(features, target)
return classifier


def main() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_classifier.py, please provide doctest for the function main

"""
Main function to demonstrate LGBM classification on the Iris dataset.

URL for LightGBM documentation:
https://lightgbm.readthedocs.io/en/latest/
"""
# Load the Iris dataset
iris = load_iris()
features, targets = data_handling(iris)

# Split the dataset into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(
features, targets, test_size=0.25, random_state=42
)

# Class names for display
names = iris["target_names"]

# Train the LGBM classifier
lgbm_clf = lgbm_classifier(x_train, y_train)

# Display the confusion matrix for the classifier
ConfusionMatrixDisplay.from_estimator(
lgbm_clf,
x_test,
y_test,
display_labels=names,
cmap="Blues",
normalize="true",
)
plt.title("Normalized Confusion Matrix - IRIS Dataset")
plt.show()


if __name__ == "__main__":
import doctest

doctest.testmod(verbose=True)
main()
62 changes: 62 additions & 0 deletions machine_learning/lgbm_regressor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# LGBM Regressor Example using Bank Marketing Dataset
import numpy as np
from sklearn.datasets import fetch_openml
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor


def data_handling(data: dict) -> tuple:

Check failure on line 9 in machine_learning/lgbm_regressor.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/lgbm_regressor.py:2:1: I001 Import block is un-sorted or un-formatted

Check failure on line 9 in machine_learning/lgbm_regressor.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/lgbm_regressor.py:2:1: I001 Import block is un-sorted or un-formatted
# Split dataset into features and target. Data is features.
"""
>>> data_handling((
... {'data':'[0.12, 0.02, 0.01, 0.25, 0.09]',
... 'target':([1])}))
('[0.12, 0.02, 0.01, 0.25, 0.09]', [1])
"""
return (data["data"], data["target"])


def lgbm_regressor(
features: np.ndarray, target: np.ndarray, test_features: np.ndarray
) -> np.ndarray:
"""
>>> lgbm_regressor(np.array([[ 0.12, 0.02, 0.01, 0.25, 0.09]]), np.array([1]),
... np.array([[0.11, 0.03, 0.02, 0.28, 0.08]]))
array([[0.98]], dtype=float32)
"""
lgbm = LGBMRegressor(verbosity=0, random_state=42)
lgbm.fit(features, target)
# Predict target for test data
predictions = lgbm.predict(test_features)
predictions = predictions.reshape(len(predictions), 1)
return predictions


def main() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/lgbm_regressor.py, please provide doctest for the function main

"""
The URL for this algorithm:
https://lightgbm.readthedocs.io/en/latest/
Bank Marketing Dataset is used to demonstrate the algorithm.

Expected error values:
Mean Absolute Error: 0.2 (approx.)
Mean Square Error: 0.15 (approx.)
"""
# Load Bank Marketing dataset
bank_data = fetch_openml(name="bank-marketing", version=1, as_frame=False)
data, target = data_handling(bank_data)
x_train, x_test, y_train, y_test = train_test_split(
data, target, test_size=0.25, random_state=1
)
predictions = lgbm_regressor(x_train, y_train, x_test)
# Error printing
print(f"Mean Absolute Error: {mean_absolute_error(y_test, predictions)}")
print(f"Mean Square Error: {mean_squared_error(y_test, predictions)}")


if __name__ == "__main__":
import doctest

doctest.testmod(verbose=True)
main()
Loading