1

我正在开发一个异常检测系统,我想将一个 matplotlib 图表迁移到一个 hvplot 图表,以允许我悬停 itens 并获取标签名称。

有人可以帮助编写等效的 hvplot 代码吗?

在我想在 hvplot 上显示的图表下方。

在此处输入图像描述

以及创建它的代码:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

# pca-one - inlier feature 1,  pca-two - inlier feature 2
inliers_pca_one = np.array(X['pca-one'][X['outlier'] == 0]).reshape(-1,1)
inliers_pca_two = np.array(X['pca-two'][X['outlier'] == 0]).reshape(-1,1)
    
# pca-one - outlier feature 1, pca-two - outlier feature 2
outliers_pca_one = X['pca-one'][X['outlier'] == 1].values.reshape(-1,1)
outliers_pca_two = X['pca-two'][X['outlier'] == 1].values.reshape(-1,1)

plt.figure(figsize=(8, 8))

xx , yy = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100))

# Use threshold value to consider a datapoint inlier or outlier
# threshold = stats.scoreatpercentile(scores_pred,100 * outliers_fraction)
threshold = percentile(scores_pred, 100 * outliers_fraction)
        
# decision function calculates the raw anomaly score for every point
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) * -1
Z = Z.reshape(xx.shape)

# fill blue map colormap from minimum anomaly score to threshold value
plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7),cmap=plt.cm.Blues_r)
        
# draw red contour line where anomaly score is equal to thresold
a = plt.contour(xx, yy, Z, levels=[threshold],linewidths=2, colors='red')
        
# fill orange contour lines where range of anomaly score is from threshold to maximum anomaly score
plt.contourf(xx, yy, Z, levels=[threshold, Z.max()],colors='orange')
b = plt.scatter(inliers_pca_one, inliers_pca_two, c='white',s=20, edgecolor='k')
    
c = plt.scatter(outliers_pca_one, outliers_pca_two, c='black',s=20, edgecolor='k')
       
plt.axis('tight')   
plt.legend([a.collections[0], b,c], ['learned decision function', 'inliers','outliers'],
           prop=matplotlib.font_manager.FontProperties(size=20),loc='lower right')
      
plt.xlim((0, 1))
plt.ylim((0, 1))
plt.title('Cluster-based Local Outlier Factor (CBLOF)')
plt.show();

4

0 回答 0