From ef7014c806f32aa0c2e583b8f7d6b6e256123149 Mon Sep 17 00:00:00 2001 From: yashksaini-coder Date: Fri, 8 Nov 2024 04:17:02 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Enhance=20Models=20code?= =?UTF-8?q?=20snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #361 --- pysnippets/models/CV_means.py | 14 +++++- pysnippets/models/Catboost.py | 40 ++++++++++++--- pysnippets/models/Ensemble.py | 61 ++++++++++++++++++++--- pysnippets/models/Grid_search.py | 42 ++++++++++++++-- pysnippets/models/KMeans.py | 47 ++++++++++++++++-- pysnippets/models/LightGBM.py | 32 +++++++++--- pysnippets/models/Linear_regression.py | 61 ++++++++++++++++++++--- pysnippets/models/Logistic_regression.py | 62 +++++++++++++++++++++--- pysnippets/models/XGB.py | 33 ++++++++++--- pysnippets/models/__init__.py | 9 ++++ 10 files changed, 352 insertions(+), 49 deletions(-) diff --git a/pysnippets/models/CV_means.py b/pysnippets/models/CV_means.py index 9a41169..1be20fd 100644 --- a/pysnippets/models/CV_means.py +++ b/pysnippets/models/CV_means.py @@ -1,10 +1,20 @@ import numpy as np from sklearn.model_selection import cross_val_score from sklearn.linear_model import LinearRegression +import logging class CVMeansModel: + """Model to compute cross-validation mean score for Linear Regression.""" + def __init__(self): self.model = LinearRegression() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized LinearRegression model.") - def cross_val_mean(self, X, y, cv=5): - return np.mean(cross_val_score(self.model, X, y, cv=cv)) \ No newline at end of file + def cross_val_mean(self, X: np.ndarray, y: np.ndarray, cv: int = 5) -> float: + """Compute the mean cross-validation score.""" + try: + return np.mean(cross_val_score(self.model, X, y, cv=cv)) + except Exception as e: + self.logger.error(f"Error during cross-validation: {e}") + raise diff --git a/pysnippets/models/Catboost.py b/pysnippets/models/Catboost.py index 9237083..7179005 100644 --- a/pysnippets/models/Catboost.py +++ b/pysnippets/models/Catboost.py @@ -1,15 +1,43 @@ import numpy as np from catboost import CatBoostRegressor +import logging class CatBoostModel: + """ + CatBoost regression model. + """ def __init__(self): self.model = CatBoostRegressor(silent=True) + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized CatBoostRegressor.") - def fit(self, X, y): - self.model.fit(X, y) + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """ + Fit the CatBoost model to the data. + """ + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise - def predict(self, X): - return self.model.predict(X) + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict using the CatBoost model. + """ + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """ + Score the CatBoost model. + """ + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/Ensemble.py b/pysnippets/models/Ensemble.py index 94edb36..8b89836 100644 --- a/pysnippets/models/Ensemble.py +++ b/pysnippets/models/Ensemble.py @@ -1,15 +1,64 @@ import numpy as np from sklearn.ensemble import RandomForestClassifier +import logging class EnsembleModel: + """ + Ensemble model using RandomForestClassifier. + """ + def __init__(self): + """ + Initialize the EnsembleModel with a RandomForestClassifier. + """ self.model = RandomForestClassifier() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized RandomForestClassifier.") + + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """ + Fit the RandomForestClassifier model to the data. + + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. + """ + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise + + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict using the RandomForestClassifier model. + + Parameters: + X (np.ndarray): The feature matrix. + + Returns: + np.ndarray: The predicted values. + """ + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def fit(self, X, y): - self.model.fit(X, y) + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """ + Score the RandomForestClassifier model. - def predict(self, X): - return self.model.predict(X) + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + Returns: + float: The model's score. + """ + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/Grid_search.py b/pysnippets/models/Grid_search.py index c647197..226b6df 100644 --- a/pysnippets/models/Grid_search.py +++ b/pysnippets/models/Grid_search.py @@ -1,15 +1,47 @@ import numpy as np -from sklearn.model_selection import GridSearchCV from sklearn.linear_model import LogisticRegression +from sklearn.model_selection import GridSearchCV +import logging class GridSearchModel: + """ + Grid search model for Logistic Regression. + """ + def __init__(self): + """ + Initialize the GridSearchModel with a LogisticRegression model and a parameter grid. + """ self.model = LogisticRegression() self.param_grid = {'C': [0.1, 1, 10]} self.grid_search = GridSearchCV(self.model, self.param_grid) + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized LogisticRegression model with GridSearchCV.") + + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """ + Fit the GridSearchCV to the data. + + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. + """ + try: + self.grid_search.fit(X, y) + self.logger.info("Grid search fitting successful.") + except Exception as e: + self.logger.error(f"Error during grid search fitting: {e}") + raise - def fit(self, X, y): - self.grid_search.fit(X, y) + def best_params(self) -> dict: + """ + Retrieve the best parameters found by grid search. - def best_params(self): - return self.grid_search.best_params_ \ No newline at end of file + Returns: + dict: A dictionary containing the best parameters. + """ + try: + return self.grid_search.best_params_ + except Exception as e: + self.logger.error(f"Error retrieving best parameters: {e}") + raise diff --git a/pysnippets/models/KMeans.py b/pysnippets/models/KMeans.py index a3d924e..387c87e 100644 --- a/pysnippets/models/KMeans.py +++ b/pysnippets/models/KMeans.py @@ -1,12 +1,49 @@ import numpy as np from sklearn.cluster import KMeans +import logging class KMeansModel: - def __init__(self, n_clusters=3): + """ + KMeans clustering model. + """ + + def __init__(self, n_clusters: int = 3): + """ + Initialize the KMeansModel with a specified number of clusters. + + Parameters: + n_clusters (int): The number of clusters to form and the number of centroids to generate. + """ self.model = KMeans(n_clusters=n_clusters) + self.logger = logging.getLogger(__name__) + self.logger.info(f"Initialized KMeans with n_clusters={n_clusters}.") + + def fit(self, X: np.ndarray) -> None: + """ + Fit the KMeans model to the data. + + Parameters: + X (np.ndarray): The feature matrix. + """ + try: + self.model.fit(X) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise + + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict cluster indices for samples. - def fit(self, X): - self.model.fit(X) + Parameters: + X (np.ndarray): The feature matrix. - def predict(self, X): - return self.model.predict(X) \ No newline at end of file + Returns: + np.ndarray: The predicted cluster indices. + """ + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise diff --git a/pysnippets/models/LightGBM.py b/pysnippets/models/LightGBM.py index a5260b4..6bccabe 100644 --- a/pysnippets/models/LightGBM.py +++ b/pysnippets/models/LightGBM.py @@ -1,15 +1,35 @@ import numpy as np import lightgbm as lgb +import logging class LightGBMModel: + """LightGBM regression model.""" def __init__(self): self.model = lgb.LGBMRegressor() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized LGBMRegressor.") - def fit(self, X, y): - self.model.fit(X, y) + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """Fit the LightGBM model to the data.""" + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise - def predict(self, X): - return self.model.predict(X) + def predict(self, X: np.ndarray) -> np.ndarray: + """Predict using the LightGBM model.""" + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """Score the LightGBM model.""" + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/Linear_regression.py b/pysnippets/models/Linear_regression.py index 1332869..0a47e8e 100644 --- a/pysnippets/models/Linear_regression.py +++ b/pysnippets/models/Linear_regression.py @@ -1,15 +1,64 @@ import numpy as np from sklearn.linear_model import LinearRegression +import logging class LinearModel: + """ + Linear Regression model. + """ + def __init__(self): + """ + Initialize the LinearModel with a LinearRegression model. + """ self.model = LinearRegression() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized LinearRegression model.") + + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """ + Fit the Linear Regression model to the data. + + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. + """ + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise + + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict using the Linear Regression model. + + Parameters: + X (np.ndarray): The feature matrix. + + Returns: + np.ndarray: The predicted values. + """ + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def fit(self, X, y): - self.model.fit(X, y) + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """ + Score the Linear Regression model. - def predict(self, X): - return self.model.predict(X) + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + Returns: + float: The model's score. + """ + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/Logistic_regression.py b/pysnippets/models/Logistic_regression.py index 467b744..b06c82b 100644 --- a/pysnippets/models/Logistic_regression.py +++ b/pysnippets/models/Logistic_regression.py @@ -1,16 +1,64 @@ import numpy as np from sklearn.linear_model import LogisticRegression -from sklearn.model_selection import train_test_split +import logging class LogisticModel: + """ + Logistic Regression model. + """ + def __init__(self): + """ + Initialize the LogisticModel with a LogisticRegression model. + """ self.model = LogisticRegression() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized LogisticRegression model.") + + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """ + Fit the Logistic Regression model to the data. + + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. + """ + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise + + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict using the Logistic Regression model. + + Parameters: + X (np.ndarray): The feature matrix. + + Returns: + np.ndarray: The predicted values. + """ + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def fit(self, X, y): - self.model.fit(X, y) + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """ + Score the Logistic Regression model. - def predict(self, X): - return self.model.predict(X) + Parameters: + X (np.ndarray): The feature matrix. + y (np.ndarray): The target vector. - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + Returns: + float: The model's score. + """ + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/XGB.py b/pysnippets/models/XGB.py index 6e6ea9a..f3b4c9f 100644 --- a/pysnippets/models/XGB.py +++ b/pysnippets/models/XGB.py @@ -1,15 +1,36 @@ import numpy as np import xgboost as xgb +import logging class XGBModel: + """XGBoost regression model.""" + def __init__(self): self.model = xgb.XGBRegressor() + self.logger = logging.getLogger(__name__) + self.logger.info("Initialized XGBRegressor.") - def fit(self, X, y): - self.model.fit(X, y) + def fit(self, X: np.ndarray, y: np.ndarray) -> None: + """Fit the XGBoost model to the data.""" + try: + self.model.fit(X, y) + self.logger.info("Model fitting successful.") + except Exception as e: + self.logger.error(f"Error during model fitting: {e}") + raise - def predict(self, X): - return self.model.predict(X) + def predict(self, X: np.ndarray) -> np.ndarray: + """Predict using the XGBoost model.""" + try: + return self.model.predict(X) + except Exception as e: + self.logger.error(f"Error during prediction: {e}") + raise - def score(self, X, y): - return self.model.score(X, y) \ No newline at end of file + def score(self, X: np.ndarray, y: np.ndarray) -> float: + """Score the XGBoost model.""" + try: + return self.model.score(X, y) + except Exception as e: + self.logger.error(f"Error during scoring: {e}") + raise diff --git a/pysnippets/models/__init__.py b/pysnippets/models/__init__.py index e69de29..fc838a6 100644 --- a/pysnippets/models/__init__.py +++ b/pysnippets/models/__init__.py @@ -0,0 +1,9 @@ +from .Catboost import CatBoostModel +from .CV_means import CVMeansModel +from .Ensemble import EnsembleModel +from .Grid_search import GridSearchModel +from .KMeans import KMeansModel +from .LightGBM import LightGBMModel +from .Linear_regression import LinearModel +from .Logistic_regression import LogisticModel +from .XGB import XGBModel \ No newline at end of file