Skip to content

Commit

Permalink
removed np.NINF
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiguender committed Oct 8, 2024
1 parent 61a495a commit dd515ef
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 176 deletions.
Binary file modified dddex/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/_modidx.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/baseClasses.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/crossValidation.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/levelSetKDEx_multivariate.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/levelSetKDEx_univariate.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/loadData.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/utils.cpython-38.pyc
Binary file not shown.
Binary file modified dddex/__pycache__/wSAA.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion dddex/levelSetKDEx_univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def generateBins(binSize: int, # Size of the bins of values of `yPred` being gro
for i in range(len(yPred)):

if i == 0:
lowerBoundPerBin[binIndex] = np.NINF
lowerBoundPerBin[binIndex] = -np.inf

currentBinSize += 1
trainIndicesLeft -= 1
Expand Down
212 changes: 106 additions & 106 deletions nbs/.ipynb_checkpoints/01_levelSetKDEx_univariate-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@
" for i in range(len(yPred)):\n",
" \n",
" if i == 0:\n",
" lowerBoundPerBin[binIndex] = np.NINF\n",
" lowerBoundPerBin[binIndex] = -np.inf\n",
" \n",
" currentBinSize += 1\n",
" trainIndicesLeft -= 1\n",
Expand Down Expand Up @@ -735,128 +735,128 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"\n",
"from drf import drf \n",
"\n",
"class LevelSetKDEx_DRF(BaseWeightsBasedEstimator, BaseLSx):\n",
" \"\"\"\n",
" `LevelSetKDEx` turns any point forecasting model into an estimator of the underlying conditional density.\n",
" The name 'LevelSet' stems from the fact that this approach interprets the values of the point forecasts\n",
" as a similarity measure between samples. The point forecasts of the training samples are sorted and \n",
" recursively assigned to a bin until the size of the current bin reaches `binSize` many samples. Then\n",
" a new bin is created and so on. For a new test sample we check into which bin its point prediction\n",
" would have fallen and interpret the training samples of that bin as the empirical distribution function\n",
" of this test sample. \n",
" \"\"\"\n",
" \n",
" def __init__(self, \n",
" estimator, # Model with a .fit and .predict-method (implementing the scikit-learn estimator interface).\n",
" binSize: int=100, # Size of the bins created while running fit.\n",
" ):\n",
" \n",
" super(BaseEstimator, self).__init__(estimator = estimator)\n",
"\n",
" # Check if binSize is integer\n",
" if not isinstance(binSize, (int, np.int32, np.int64)):\n",
" raise ValueError(\"'binSize' must be an integer!\")\n",
"\n",
" self.binSize = binSize\n",
" \n",
" self.yTrain = None\n",
" self.yPredTrain = None\n",
" self.drf = None\n",
" self.fitted = False\n",
" \n",
" #---\n",
" \n",
" def fit(self: LevelSetKDEx_DRF, \n",
" X: np.ndarray, # Feature matrix used by `estimator` to predict `y`.\n",
" y: np.ndarray, # 1-dimensional target variable corresponding to the feature matrix `X`.\n",
" ):\n",
" \"\"\"\n",
" Fit `LevelSetKDEx` model by grouping the point predictions of the samples specified via `X`\n",
" according to their value. Samples are recursively sorted into bins until each bin contains\n",
" `binSize` many samples. For details, checkout the function `generateBins` which does the\n",
" heavy lifting.\n",
" \"\"\"\n",
" \n",
" # Checks\n",
" if not isinstance(self.binSize, (int, np.int32, np.int64)):\n",
" raise ValueError(\"'binSize' must be an integer!\")\n",
"# #| export\n",
"\n",
"# from drf import drf \n",
"\n",
"# class LevelSetKDEx_DRF(BaseWeightsBasedEstimator, BaseLSx):\n",
"# \"\"\"\n",
"# `LevelSetKDEx` turns any point forecasting model into an estimator of the underlying conditional density.\n",
"# The name 'LevelSet' stems from the fact that this approach interprets the values of the point forecasts\n",
"# as a similarity measure between samples. The point forecasts of the training samples are sorted and \n",
"# recursively assigned to a bin until the size of the current bin reaches `binSize` many samples. Then\n",
"# a new bin is created and so on. For a new test sample we check into which bin its point prediction\n",
"# would have fallen and interpret the training samples of that bin as the empirical distribution function\n",
"# of this test sample. \n",
"# \"\"\"\n",
" \n",
"# def __init__(self, \n",
"# estimator, # Model with a .fit and .predict-method (implementing the scikit-learn estimator interface).\n",
"# binSize: int=100, # Size of the bins created while running fit.\n",
"# ):\n",
" \n",
"# super(BaseEstimator, self).__init__(estimator = estimator)\n",
"\n",
"# # Check if binSize is integer\n",
"# if not isinstance(binSize, (int, np.int32, np.int64)):\n",
"# raise ValueError(\"'binSize' must be an integer!\")\n",
"\n",
"# self.binSize = binSize\n",
" \n",
"# self.yTrain = None\n",
"# self.yPredTrain = None\n",
"# self.drf = None\n",
"# self.fitted = False\n",
" \n",
"# #---\n",
" \n",
"# def fit(self: LevelSetKDEx_DRF, \n",
"# X: np.ndarray, # Feature matrix used by `estimator` to predict `y`.\n",
"# y: np.ndarray, # 1-dimensional target variable corresponding to the feature matrix `X`.\n",
"# ):\n",
"# \"\"\"\n",
"# Fit `LevelSetKDEx` model by grouping the point predictions of the samples specified via `X`\n",
"# according to their value. Samples are recursively sorted into bins until each bin contains\n",
"# `binSize` many samples. For details, checkout the function `generateBins` which does the\n",
"# heavy lifting.\n",
"# \"\"\"\n",
" \n",
"# # Checks\n",
"# if not isinstance(self.binSize, (int, np.int32, np.int64)):\n",
"# raise ValueError(\"'binSize' must be an integer!\")\n",
" \n",
" if self.binSize > y.shape[0]:\n",
" raise ValueError(\"'binSize' mustn't be bigger than the size of 'y'!\")\n",
"# if self.binSize > y.shape[0]:\n",
"# raise ValueError(\"'binSize' mustn't be bigger than the size of 'y'!\")\n",
" \n",
" if X.shape[0] != y.shape[0]:\n",
" raise ValueError(\"'X' and 'y' must contain the same number of samples!\")\n",
"# if X.shape[0] != y.shape[0]:\n",
"# raise ValueError(\"'X' and 'y' must contain the same number of samples!\")\n",
" \n",
" #---\n",
"# #---\n",
" \n",
" try:\n",
" yPred = self.estimator.predict(X)\n",
"# try:\n",
"# yPred = self.estimator.predict(X)\n",
" \n",
" except NotFittedError:\n",
" try:\n",
" self.estimator.fit(X = X, y = y) \n",
" except:\n",
" raise ValueError(\"Couldn't fit 'estimator' with user specified 'X' and 'y'!\")\n",
" else:\n",
" yPred = self.estimator.predict(X)\n",
"# except NotFittedError:\n",
"# try:\n",
"# self.estimator.fit(X = X, y = y) \n",
"# except:\n",
"# raise ValueError(\"Couldn't fit 'estimator' with user specified 'X' and 'y'!\")\n",
"# else:\n",
"# yPred = self.estimator.predict(X)\n",
" \n",
" #---\n",
"# #---\n",
" \n",
" yPred = pd.DataFrame(yPred)\n",
" y = pd.Series(y)\n",
"# yPred = pd.DataFrame(yPred)\n",
"# y = pd.Series(y)\n",
"\n",
" DRF = drf(min_node_size = self.binSize, num_trees = 100, num_features = 1, honesty = False, sample_fraction = 0.5, response_scaling = False, mtry = 1, num_threads = 16)\n",
" DRF.fit(X = yPred, Y = y)\n",
"# DRF = drf(min_node_size = self.binSize, num_trees = 100, num_features = 1, honesty = False, sample_fraction = 0.5, response_scaling = False, mtry = 1, num_threads = 16)\n",
"# DRF.fit(X = yPred, Y = y)\n",
" \n",
" #---\n",
"# #---\n",
" \n",
" # IMPORTANT: In case 'y' is given as a pandas.Series, we can potentially run into indexing \n",
" # problems later on.\n",
" self.yTrain = y.ravel()\n",
"# # IMPORTANT: In case 'y' is given as a pandas.Series, we can potentially run into indexing \n",
"# # problems later on.\n",
"# self.yTrain = y.ravel()\n",
" \n",
" self.yPredTrain = yPred\n",
" self.drf = DRF\n",
" self.fitted = True\n",
"# self.yPredTrain = yPred\n",
"# self.drf = DRF\n",
"# self.fitted = True\n",
" \n",
" #---\n",
"# #---\n",
" \n",
" def getWeights(self: LevelSetKDEx_DRF, \n",
" X: np.ndarray, # Feature matrix for which conditional density estimates are computed.\n",
" # Specifies structure of the returned density estimates. One of: \n",
" # 'all', 'onlyPositiveWeights', 'summarized', 'cumDistribution', 'cumDistributionSummarized'\n",
" outputType: str='onlyPositiveWeights', \n",
" # Optional. List with length X.shape[0]. Values are multiplied to the estimated \n",
" # density of each sample for scaling purposes.\n",
" scalingList: list=None, \n",
" ) -> list: # List whose elements are the conditional density estimates for the samples specified by `X`.\n",
"# def getWeights(self: LevelSetKDEx_DRF, \n",
"# X: np.ndarray, # Feature matrix for which conditional density estimates are computed.\n",
"# # Specifies structure of the returned density estimates. One of: \n",
"# # 'all', 'onlyPositiveWeights', 'summarized', 'cumDistribution', 'cumDistributionSummarized'\n",
"# outputType: str='onlyPositiveWeights', \n",
"# # Optional. List with length X.shape[0]. Values are multiplied to the estimated \n",
"# # density of each sample for scaling purposes.\n",
"# scalingList: list=None, \n",
"# ) -> list: # List whose elements are the conditional density estimates for the samples specified by `X`.\n",
" \n",
" # __annotations__ = BaseWeightsBasedEstimator.getWeights.__annotations__\n",
" __doc__ = BaseWeightsBasedEstimator.getWeights.__doc__\n",
"# # __annotations__ = BaseWeightsBasedEstimator.getWeights.__annotations__\n",
"# __doc__ = BaseWeightsBasedEstimator.getWeights.__doc__\n",
" \n",
" if not self.fitted:\n",
" raise NotFittedError(\"This LevelSetKDEx instance is not fitted yet. Call 'fit' with \"\n",
" \"appropriate arguments before trying to compute weights.\")\n",
"# if not self.fitted:\n",
"# raise NotFittedError(\"This LevelSetKDEx instance is not fitted yet. Call 'fit' with \"\n",
"# \"appropriate arguments before trying to compute weights.\")\n",
" \n",
" #---\n",
"# #---\n",
" \n",
" yPred = self.estimator.predict(X)\n",
" yPred = pd.DataFrame(yPred)\n",
"# yPred = self.estimator.predict(X)\n",
"# yPred = pd.DataFrame(yPred)\n",
" \n",
" weightsArray = self.drf.predict(yPred).weights\n",
" weightsList = list(weightsArray)\n",
" weightsDataList = [(weights[weights > 0], np.where(weights > 0)[0]) for weights in weightsList]\n",
"# weightsArray = self.drf.predict(yPred).weights\n",
"# weightsList = list(weightsArray)\n",
"# weightsDataList = [(weights[weights > 0], np.where(weights > 0)[0]) for weights in weightsList]\n",
"\n",
" weightsDataList = restructureWeightsDataList(weightsDataList = weightsDataList, \n",
" outputType = outputType, \n",
" y = self.yTrain,\n",
" scalingList = scalingList,\n",
" equalWeights = True)\n",
"# weightsDataList = restructureWeightsDataList(weightsDataList = weightsDataList, \n",
"# outputType = outputType, \n",
"# y = self.yTrain,\n",
"# scalingList = scalingList,\n",
"# equalWeights = True)\n",
" \n",
" return weightsDataList\n",
"# return weightsDataList\n",
" \n",
" "
]
Expand Down Expand Up @@ -2842,9 +2842,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "dddex39",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "dddex39"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -2856,7 +2856,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit dd515ef

Please sign in to comment.