From 430684fe507ca1a263a6e48bb5ce8ed1bc6763f9 Mon Sep 17 00:00:00 2001 From: "yzhao062@gmail.com" <9io9mZ9K#nNL> Date: Fri, 23 Jun 2023 17:53:16 +0800 Subject: [PATCH] bug fix --- CHANGES.txt | 1 + examples/cd_example.py | 1 - pyod/models/cd.py | 22 ++++++++++++---------- pyod/test/test_cd.py | 1 - 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index b2ba2b591..c8fb2c9d6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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. \ No newline at end of file diff --git a/examples/cd_example.py b/examples/cd_example.py index 55db45b74..a8b91c7f3 100644 --- a/examples/cd_example.py +++ b/examples/cd_example.py @@ -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 diff --git a/pyod/models/cd.py b/pyod/models/cd.py index 0b5ff2042..c321d51a4 100644 --- a/pyod/models/cd.py +++ b/pyod/models/cd.py @@ -15,6 +15,7 @@ from .base import BaseDetector + def _Cooks_dist(X, y, model): """Calculated the Cook's distance @@ -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 @@ -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) @@ -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() diff --git a/pyod/test/test_cd.py b/pyod/test/test_cd.py index 590fe7081..6bf50d093 100644 --- a/pyod/test/test_cd.py +++ b/pyod/test/test_cd.py @@ -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