异常检测算法:Elliptic Envelope算法的python代码实现

Elliptic Envelope

算法思想

Elliptic Envelope算法的思路是,假设常规数据隐含这一个已知的概率分布。基于这个假设,我们尝试确定数据的形状(边界),也可以将远离边界的样本点定义为异常点。SKlearn提供了一个covariance.EllipticEnvelope类,它可以根据数据做一个鲁棒的协方差估计,然后学习到一个包围中心样本点并忽视离群点的椭圆

代码实现

使用sklearn中的相关包来实现Elliptic Envelope算法,举一个很简单的小demo:

from sklearn.covariance import EllipticEnvelope
X = [[0], [0.44], [0.45], [0.46], [1]]
clf = EllipticEnvelope(random_state=0)
clf = clf.fit(X)
# score越小,代表越有可能是离群点
scores = clf.score_samples(X)
"""
输出的结果是:
[-3.0375e+03 -1.5000e+00 -0.0000e+00 -1.5000e+00 -4.5375e+03]
"""
print(scores)

其他的内置函数以及介绍在:scikit-learn

可视化

sklearn上的可视化案例,链接为:scikit-learn

import numpy as np
from sklearn.covariance import EllipticEnvelope
from sklearn.svm import OneClassSVM
import matplotlib.pyplot as plt
import matplotlib.font_manager
from sklearn.datasets import load_wine

# Define "classifiers" to be used
classifiers = {
    "Empirical Covariance": EllipticEnvelope(support_fraction=1.,
                                             contamination=0.25),
    "Robust Covariance (Minimum Covariance Determinant)":
    EllipticEnvelope(contamination=0.25),
    "OCSVM": OneClassSVM(nu=0.25, gamma=0.35)}
colors = ['m', 'g', 'b']
legend1 = {}
legend2 = {}

# Get data
X1 = load_wine()['data'][:, [1, 2]]  # two clusters

# Learn a frontier for outlier detection with several classifiers
xx1, yy1 = np.meshgrid(np.linspace(0, 6, 500), np.linspace(1, 4.5, 500))
for i, (clf_name, clf) in enumerate(classifiers.items()):
    plt.figure(1)
    clf.fit(X1)
    Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()])
    Z1 = Z1.reshape(xx1.shape)
    legend1[clf_name] = plt.contour(
        xx1, yy1, Z1, levels=[0], linewidths=2, colors=colors[i])

legend1_values_list = list(legend1.values())
legend1_keys_list = list(legend1.keys())

# Plot the results (= shape of the data points cloud)
plt.figure(1)  # two clusters
plt.title("Outlier detection on a real data set (wine recognition)")
plt.scatter(X1[:, 0], X1[:, 1], color='black')
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->")
plt.annotate("outlying points", xy=(4, 2),
             xycoords="data", textcoords="data",
             xytext=(3, 1.25), bbox=bbox_args, arrowprops=arrow_args)
plt.xlim((xx1.min(), xx1.max()))
plt.ylim((yy1.min(), yy1.max()))
plt.legend((legend1_values_list[0].collections[0],
            legend1_values_list[1].collections[0],
            legend1_values_list[2].collections[0]),
           (legend1_keys_list[0], legend1_keys_list[1], legend1_keys_list[2]),
           loc="upper center",
           prop=matplotlib.font_manager.FontProperties(size=11))
plt.ylabel("ash")
plt.xlabel("malic_acid")

plt.show()

最终的结果是:

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECDHE(Elliptic Curve Diffie-Hellman Ephemeral)是一种密钥交换协议,它基于椭圆曲线密码学,可以在不安全的通信信道上安全地交换密钥。下面是用Python实现ECDHE算法的示例代码: ``` from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import utils # 生成密钥对 private_key = ec.generate_private_key(ec.SECP256R1()) public_key = private_key.public_key() # 将公钥序列化为字节串 serialized_public_key = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # 接收方使用接收到的公钥计算共享密钥 received_public_key = serialization.load_pem_public_key( serialized_public_key, backend=default_backend() ) shared_key = private_key.exchange( ec.ECDH(), received_public_key ) # 将共享密钥转换为字节串 shared_key_bytes = shared_key.to_bytes( utils.algorithms.ec.compute_max_size_in_bytes(ec.SECP256R1()), 'big' ) print("共享密钥:", shared_key_bytes) ``` 在这个示例中,我们使用Python的cryptography库实现ECDHE算法。首先,我们使用`ec.generate_private_key`方法生成一对公钥和私钥。然后,我们将公钥序列化为字节串,并将其发送给接收方。接收方使用`serialization.load_pem_public_key`方法将接收到的公钥反序列化为Python对象,并使用自己的私钥与之计算共享密钥。最后,我们将共享密钥转换为字节串并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值