This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Data wrangling | |
| import pandas as pd | |
| # Infinity constant | |
| from math import inf | |
| class Tree(): | |
| """ | |
| Class to fit a regression tree to the given data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The base class with the weak learner | |
| from regression.tree import Tree | |
| # Data wrangling | |
| import pandas as pd | |
| # Python infinity | |
| from math import inf | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Dataset loading | |
| from sklearn.datasets import fetch_california_housing | |
| # Loading the data | |
| _cali_data = fetch_california_housing(as_frame=True) | |
| # Features and target | |
| X, y = _cali_data.data, _cali_data.target | |
| # Droping the geo coordinate featuress |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Train test spliting | |
| from sklearn.model_selection import train_test_split | |
| # Importing the sklearn implementation | |
| from sklearn.tree import DecisionTreeRegressor | |
| # Precision metrics | |
| from sklearn.metrics import mean_absolute_error | |
| # Spliting the data into training and testing sets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Entries in the nodes | |
| n_entries = { | |
| "node 1": 15480, | |
| "node 2": 12163, | |
| "node 3": 3317, | |
| "node 4": 5869, | |
| "node 5": 6294, | |
| "node 6": 2317, | |
| "node 7": 1000, | |
| "node 8": 2454, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def node_importance( | |
| node_main: str, | |
| node_left: str, | |
| node_right: str, | |
| n_entries_weight: dict, | |
| i_sq: dict | |
| ) -> float: | |
| """ | |
| Calculated the importance of the node_main | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Calculating the importance of each node | |
| importance = {} | |
| for node in node_pairs: | |
| importance[node] = round(node_importance(node, *node_pairs[node], n_entries_weight, i_sq), 3) | |
| print(f"Node importance: {importance}") | |
| # Going from node importance to feature importance | |
| feature_importance = {} | |
| for node in node_pairs: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Loading the data | |
| _cali_data = fetch_california_housing(as_frame=True) | |
| X, y = _cali_data.data, _cali_data.target | |
| # Droping the geo coordinate featuress | |
| X = X.drop(columns=['Latitude', 'Longitude']) | |
| # Droping the population feature; In real life modeling, this could be used as weight. | |
| # For educational and inference purposes, we drop it. | |
| X = X.drop(columns=['Population']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Importing the package | |
| import tensorflow as tf | |
| class NMSE(tf.keras.losses.Loss): | |
| def __init__(self): | |
| super().__init__() | |
| def call(self, y_true, y_pred): | |
| # Calculating the mse; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def elastic_net_to_keras(alpha: float, l1_ratio: float): | |
| """ | |
| Converts ElasticNet parameters from sklearn to Keras regularizers. | |
| Arguments | |
| --------- | |
| alpha: float | |
| The regularization strength of the model. | |
| l1_ratio: float | |
| The l1 regularization ratio of the model. |