Spaces:
Sleeping
Sleeping
Download networksecurity/utils/main_utils/utils.py from Divb30/network-intrusion-detection: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/Divb30/network-intrusion-detection/resolve/main/networksecurity/utils/main_utils/utils.py
- Command line
-
hf download hf://spaces/Divb30/network-intrusion-detection/networksecurity/utils/main_utils/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Divb30/network-intrusion-detection/resolve/main/networksecurity/utils/main_utils/utils.py
3.28 kB
| import yaml | |
| from networksecurity.exception.exception import NetworkSecurityException | |
| from networksecurity.logging.logger import logging | |
| import os,sys | |
| import numpy as np | |
| #import dill | |
| import pickle | |
| from sklearn.metrics import r2_score | |
| from sklearn.model_selection import GridSearchCV | |
| def read_yaml_file(file_path: str) -> dict: | |
| try: | |
| with open(file_path, "rb") as yaml_file: | |
| return yaml.safe_load(yaml_file) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) from e | |
| def write_yaml_file(file_path: str, content: object, replace: bool = False) -> None: | |
| try: | |
| if replace: | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| os.makedirs(os.path.dirname(file_path), exist_ok=True) | |
| with open(file_path, "w") as file: | |
| yaml.dump(content, file) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) | |
| def save_numpy_array_data(file_path: str, array: np.ndarray): | |
| """ | |
| Save numpy array data to file | |
| file_path: str location of file to save | |
| array: np.array data to save | |
| """ | |
| try: | |
| dir_path = os.path.dirname(file_path) | |
| os.makedirs(dir_path, exist_ok=True) | |
| with open(file_path, "wb") as file_obj: | |
| np.save(file_obj, array) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) from e | |
| def save_object(file_path: str, obj: object) -> None: | |
| try: | |
| logging.info("Entered the save_object method of MainUtils class") | |
| os.makedirs(os.path.dirname(file_path), exist_ok=True) | |
| with open(file_path, "wb") as file_obj: | |
| pickle.dump(obj, file_obj) | |
| logging.info("Exited the save_object method of MainUtils class") | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) from e | |
| def load_object(file_path: str) -> object: | |
| try: | |
| if not os.path.exists(file_path): | |
| raise Exception(f"The file {file_path} does not exist") | |
| with open(file_path, "rb") as file_obj: | |
| return pickle.load(file_obj) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) from e | |
| def load_numpy_array_data(file_path: str) -> np.ndarray: | |
| """ | |
| load numpy array data from file | |
| file_path: str location of file to load | |
| return: np.array data loaded | |
| """ | |
| try: | |
| with open(file_path, "rb") as file_obj: | |
| return np.load(file_obj) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) from e | |
| def evaluate_models(X_train, y_train, X_test, y_test, models, param): | |
| try: | |
| from sklearn.metrics import f1_score | |
| report = {} | |
| for i in range(len(list(models))): | |
| model = list(models.values())[i] | |
| para = param[list(models.keys())[i]] | |
| gs = GridSearchCV(model, para, cv=3, scoring='f1') | |
| gs.fit(X_train, y_train) | |
| model.set_params(**gs.best_params_) | |
| model.fit(X_train, y_train) | |
| y_test_pred = model.predict(X_test) | |
| test_model_score = f1_score(y_test, y_test_pred) | |
| report[list(models.keys())[i]] = test_model_score | |
| return report | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) |