當前位置: 首頁>>代碼示例>>Python>>正文


Python covariance.MinCovDet方法代碼示例

本文整理匯總了Python中sklearn.covariance.MinCovDet方法的典型用法代碼示例。如果您正苦於以下問題:Python covariance.MinCovDet方法的具體用法?Python covariance.MinCovDet怎麽用?Python covariance.MinCovDet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sklearn.covariance的用法示例。


在下文中一共展示了covariance.MinCovDet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _h_getMahalanobisRobust

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def _h_getMahalanobisRobust(dat, critical_alpha=0.01, good_rows=np.zeros(0)):
    '''Calculate the Mahalanobis distance from the sample vector.'''
    if good_rows.size == 0:
        good_rows = np.any(~np.isnan(dat), axis=1)

    try:
        dat2fit = dat[good_rows]
        assert not np.any(np.isnan(dat2fit))

        robust_cov = MinCovDet().fit(dat2fit)
        mahalanobis_dist = np.sqrt(robust_cov.mahalanobis(dat))
    except ValueError:
        # this step will fail if the covariance matrix is not singular. This happens if the data is not
        # a unimodal symetric distribution. For example there is too many small noisy particles. Therefore
        # I will take a safe option and return zeros in the mahalanobis
        # distance if this is the case.
        mahalanobis_dist = np.zeros(dat.shape[0])

    # critial distance of the maholanobis distance using the chi-square distirbution
    # https://en.wikiversity.org/wiki/Mahalanobis%27_distance
    # http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2.html
    maha_lim = chi2.ppf(1 - critical_alpha, dat.shape[1])
    outliers = mahalanobis_dist > maha_lim

    return mahalanobis_dist, outliers, maha_lim 
開發者ID:ver228,項目名稱:tierpsy-tracker,代碼行數:27,代碼來源:getFilteredSkels.py

示例2: fit

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def fit(self, X, y=None):
        """Fit detector. y is ignored in unsupervised methods.

        Parameters
        ----------
        X : numpy array of shape (n_samples, n_features)
            The input samples.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        self : object
            Fitted estimator.
        """
        # Validate inputs X and y (optional)
        X = check_array(X)
        self._set_n_classes(y)

        self.detector_ = MinCovDet(store_precision=self.store_precision,
                                   assume_centered=self.assume_centered,
                                   support_fraction=self.support_fraction,
                                   random_state=self.random_state)
        self.detector_.fit(X=X, y=y)

        # Use mahalanabis distance as the outlier score
        self.decision_scores_ = self.detector_.dist_
        self._process_decision_scores()
        return self 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:32,代碼來源:mcd.py

示例3: raw_location_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def raw_location_(self):
        """The raw robust estimated location before correction and
        re-weighting.

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.raw_location_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:9,代碼來源:mcd.py

示例4: raw_covariance_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def raw_covariance_(self):
        """The raw robust estimated location before correction and
        re-weighting.

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.raw_covariance_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:9,代碼來源:mcd.py

示例5: raw_support_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def raw_support_(self):
        """A mask of the observations that have been used to compute
        the raw robust estimates of location and shape, before correction
        and re-weighting.

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.raw_support_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:10,代碼來源:mcd.py

示例6: location_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def location_(self):
        """Estimated robust location.

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.location_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:8,代碼來源:mcd.py

示例7: precision_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def precision_(self):
        """ Estimated pseudo inverse matrix.
        (stored only if store_precision is True)

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.precision_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:9,代碼來源:mcd.py

示例8: support_

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def support_(self):
        """A mask of the observations that have been used to compute
        the robust estimates of location and shape.

        Decorator for scikit-learn MinCovDet attributes.
        """
        return self.detector_.support_ 
開發者ID:yzhao062,項目名稱:pyod,代碼行數:9,代碼來源:mcd.py

示例9: test_mcd_class_on_invalid_input

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def test_mcd_class_on_invalid_input():
    X = np.arange(100)
    mcd = MinCovDet()
    assert_raise_message(ValueError, 'Expected 2D array, got 1D array instead',
                         mcd.fit, X) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:7,代碼來源:test_robust_covariance.py

示例10: launch_mcd_on_dataset

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def launch_mcd_on_dataset(n_samples, n_features, n_outliers, tol_loc, tol_cov,
                          tol_support):

    rand_gen = np.random.RandomState(0)
    data = rand_gen.randn(n_samples, n_features)
    # add some outliers
    outliers_index = rand_gen.permutation(n_samples)[:n_outliers]
    outliers_offset = 10. * \
        (rand_gen.randint(2, size=(n_outliers, n_features)) - 0.5)
    data[outliers_index] += outliers_offset
    inliers_mask = np.ones(n_samples).astype(bool)
    inliers_mask[outliers_index] = False

    pure_data = data[inliers_mask]
    # compute MCD by fitting an object
    mcd_fit = MinCovDet(random_state=rand_gen).fit(data)
    T = mcd_fit.location_
    S = mcd_fit.covariance_
    H = mcd_fit.support_
    # compare with the estimates learnt from the inliers
    error_location = np.mean((pure_data.mean(0) - T) ** 2)
    assert(error_location < tol_loc)
    error_cov = np.mean((empirical_covariance(pure_data) - S) ** 2)
    assert(error_cov < tol_cov)
    assert(np.sum(H) >= tol_support)
    assert_array_almost_equal(mcd_fit.mahalanobis(data), mcd_fit.dist_) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:28,代碼來源:test_robust_covariance.py

示例11: test_mcd_issue1127

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def test_mcd_issue1127():
    # Check that the code does not break with X.shape = (3, 1)
    # (i.e. n_support = n_samples)
    rnd = np.random.RandomState(0)
    X = rnd.normal(size=(3, 1))
    mcd = MinCovDet()
    mcd.fit(X) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:9,代碼來源:test_robust_covariance.py

示例12: test_mcd_issue3367

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def test_mcd_issue3367():
    # Check that MCD completes when the covariance matrix is singular
    # i.e. one of the rows and columns are all zeros
    rand_gen = np.random.RandomState(0)

    # Think of these as the values for X and Y -> 10 values between -5 and 5
    data_values = np.linspace(-5, 5, 10).tolist()
    # Get the cartesian product of all possible coordinate pairs from above set
    data = np.array(list(itertools.product(data_values, data_values)))

    # Add a third column that's all zeros to make our data a set of point
    # within a plane, which means that the covariance matrix will be singular
    data = np.hstack((data, np.zeros((data.shape[0], 1))))

    # The below line of code should raise an exception if the covariance matrix
    # is singular. As a further test, since we have points in XYZ, the
    # principle components (Eigenvectors) of these directly relate to the
    # geometry of the points. Since it's a plane, we should be able to test
    # that the Eigenvector that corresponds to the smallest Eigenvalue is the
    # plane normal, specifically [0, 0, 1], since everything is in the XY plane
    # (as I've set it up above). To do this one would start by:
    #
    #     evals, evecs = np.linalg.eigh(mcd_fit.covariance_)
    #     normal = evecs[:, np.argmin(evals)]
    #
    # After which we need to assert that our `normal` is equal to [0, 0, 1].
    # Do note that there is floating point error associated with this, so it's
    # best to subtract the two and then compare some small tolerance (e.g.
    # 1e-12).
    MinCovDet(random_state=rand_gen).fit(data) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:32,代碼來源:test_robust_covariance.py

示例13: test_mcd_support_covariance_is_zero

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def test_mcd_support_covariance_is_zero():
    # Check that MCD returns a ValueError with informative message when the
    # covariance of the support data is equal to 0.
    X_1 = np.array([0.5, 0.1, 0.1, 0.1, 0.957, 0.1, 0.1, 0.1, 0.4285, 0.1])
    X_1 = X_1.reshape(-1, 1)
    X_2 = np.array([0.5, 0.3, 0.3, 0.3, 0.957, 0.3, 0.3, 0.3, 0.4285, 0.3])
    X_2 = X_2.reshape(-1, 1)
    msg = ('The covariance matrix of the support data is equal to 0, try to '
           'increase support_fraction')
    for X in [X_1, X_2]:
        assert_raise_message(ValueError, msg, MinCovDet().fit, X) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:13,代碼來源:test_robust_covariance.py

示例14: compute_MCD_weft

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def compute_MCD_weft(weftsPickled, target_path):

    weft_points_list = floatPointList()
    for pickled_path in weftsPickled:
        weft_points_list.extend(pickle.load(open(pickled_path, "rb" )))

    x_vals = [fp.x for fp in weft_points_list]
    y_vals = [fp.y for fp in weft_points_list]

    mean_hor_dist = weft_points_list.getMedianWeftDist()

    min_x = min(x_vals) + 1.5 * mean_hor_dist
    max_x = max(x_vals) - 1.5 * mean_hor_dist
    min_y = min(y_vals) + 1.5 * mean_hor_dist
    max_y = max(y_vals) - 1.5 * mean_hor_dist

    inner_points = floatPointList()
    for pt in weft_points_list:
        if min_x < pt.x < max_x and min_y < pt.y < max_y:
            inner_points.append(pt)

    X = np.zeros([len(inner_points), 3])

    for idx, pt in enumerate(inner_points):
        X[idx,0] = pt.area
        X[idx,1] = pt.right_dist
        X[idx,2] = pt.left_dist

    Y = X[~(X<=0).any(axis=1)]

    robust_cov = MinCovDet(support_fraction=0.8).fit(Y)
    pickle.dump(robust_cov, open(target_path, "wb")) 
開發者ID:weningerleon,項目名稱:TextileDefectDetection,代碼行數:34,代碼來源:stats_of_cloth.py

示例15: compute_MCD_warp

# 需要導入模塊: from sklearn import covariance [as 別名]
# 或者: from sklearn.covariance import MinCovDet [as 別名]
def compute_MCD_warp(warpsPickled, target_path):

    warp_points_list = floatPointList()
    for pickled_path in warpsPickled:
        warp_points_list.extend(pickle.load(open(pickled_path, "rb" )))

    x_vals = [fp.x for fp in warp_points_list]
    y_vals = [fp.y for fp in warp_points_list]

    mean_ver_dist = warp_points_list.getMedianWarpDist()

    min_x = min(x_vals) + 1.5 * mean_ver_dist
    max_x = max(x_vals) - 1.5 * mean_ver_dist
    min_y = min(y_vals) + 1.5 * mean_ver_dist
    max_y = max(y_vals) - 1.5 * mean_ver_dist

    inner_points = floatPointList()
    for pt in warp_points_list:
        if min_x < pt.x < max_x and min_y < pt.y < max_y:
            inner_points.append(pt)


    #####CHANGED
    #print("attention, only 2D!!!!!")
    X = np.zeros([len(inner_points), 3])

    for idx, pt in enumerate(inner_points):
        X[idx,0] = pt.area
        X[idx,1] = pt.lower_dist
        X[idx,2] = pt.upper_dist


    Y = X[~(X<=0).any(axis=1)]

    robust_cov = MinCovDet(support_fraction=0.8).fit(Y)
    pickle.dump(robust_cov, open(target_path, "wb")) 
開發者ID:weningerleon,項目名稱:TextileDefectDetection,代碼行數:38,代碼來源:stats_of_cloth.py


注:本文中的sklearn.covariance.MinCovDet方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。