Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jun 23, 2023
1 parent 4cfa581 commit 430684f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ v<1.0.8>, <03/08/2023> -- Improve clone compatibility (#471).
v<1.0.8>, <03/08/2023> -- Add QMCD detector (#452).
v<1.0.8>, <03/08/2023> -- Optimized ECDF and drop Statsmodels dependency (#467).
v<1.0.9>, <03/19/2023> -- Hot fix for errors in ECOD and COPOD due to the issue of scipy.
v<1.0.9>, <06/19/2023> -- Further integration of PyThresh.
1 change: 0 additions & 1 deletion examples/cd_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))

import numpy as np
from pyod.models.cd import CD
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print
Expand Down
22 changes: 12 additions & 10 deletions pyod/models/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .base import BaseDetector


def _Cooks_dist(X, y, model):
"""Calculated the Cook's distance
Expand Down Expand Up @@ -45,20 +46,22 @@ def _Cooks_dist(X, y, model):
# Compute the MSE from the residuals
residuals = y - model.predict(X)
mse = np.dot(residuals, residuals) / df

# Compute Cook's distance
if (mse!=0) or (mse!=np.nan):
residuals_studentized = residuals / np.sqrt(mse) / np.sqrt(1 - leverage)
if (mse != 0) or (mse != np.nan):
residuals_studentized = residuals / np.sqrt(mse) / np.sqrt(
1 - leverage)
distance_ = residuals_studentized ** 2 / X.shape[1]
distance_ *= leverage / (1 - leverage)
distance_ = ((distance_ - distance_.min())
/ (distance_.max() - distance_.min()))
/ (distance_.max() - distance_.min()))

else:
distance_ = np.ones(len(y))*np.nan
distance_ = np.ones(len(y)) * np.nan

return distance_


def _process_distances(X, model):
"""Calculated the mean Cook's distances for
each feature
Expand All @@ -79,15 +82,14 @@ def _process_distances(X, model):

distances_ = []
for i in range(X.shape[1]):

mod = model

# Extract new X and y inputs
exp = np.delete(X.copy(), i, axis=1)
resp = X[:,i]
resp = X[:, i]

exp = exp.reshape(-1, 1) if exp.ndim == 1 else exp

exp = exp.reshape(-1,1) if exp.ndim == 1 else exp

# Fit the model
mod.fit(exp, resp)

Expand Down Expand Up @@ -168,7 +170,7 @@ def fit(self, X, y=None):

# Get Cook's distance
distances_ = _process_distances(X, self.model)

self.decision_scores_ = distances_

self._process_decision_scores()
Expand Down
1 change: 0 additions & 1 deletion pyod/test/test_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
import unittest

import numpy as np
# noinspection PyProtectedMember
from numpy.testing import assert_equal
from numpy.testing import assert_raises
Expand Down

0 comments on commit 430684f

Please sign in to comment.