Skip to content

Commit 0376a80

Browse files
committed
Skip PCA when exploring anomaly detection solely on node embeddings
1 parent baff7f0 commit 0376a80

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

domains/anomaly-detection/explore/AnomalyDetectionIsolationForestExploration.ipynb

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,14 @@
279279
" 'clusterNoise', # highly correlated with \"clusterApproximateOutlierScore\". doesn't improve F1 score of proxy model.\n",
280280
" 'embeddingVisualizationX',\n",
281281
" 'embeddingVisualizationY',\n",
282-
"]"
282+
"]\n",
283+
"\n",
284+
"features_for_visualization_and_training: typing.List[str] = [\n",
285+
" 'pageRank', \n",
286+
" 'articleRank'\n",
287+
"]\n",
288+
"\n",
289+
"features_for_visualization: typing.List[str] = features_for_visualization_excluded_from_training + features_for_visualization_and_training"
283290
]
284291
},
285292
{
@@ -748,7 +755,9 @@
748755
"id": "b2cfcc56",
749756
"metadata": {},
750757
"source": [
751-
"#### 1.3b List the top 10 anomalies solely based on embeddings"
758+
"#### 1.3b List the top 10 anomalies solely based on embeddings\n",
759+
"\n",
760+
"By leaving out all other features, we can see if the embeddings alone are sufficient to detect anomalies. Anomalies detected solely based on embeddings could indicate structural outliers in the graph representation of the codebase. In most cases however, combining embeddings with other features yields better results."
752761
]
753762
},
754763
{
@@ -758,10 +767,18 @@
758767
"metadata": {},
759768
"outputs": [],
760769
"source": [
761-
"java_package_embedding_anomaly_detection_features = java_package_anomaly_detection_features[features_for_visualization_excluded_from_training + ['embedding', 'pageRank', 'articleRank']].copy()\n",
762-
"java_package_embedding_anomaly_detection_input = reduce_dimensionality_of_node_embeddings(java_package_embedding_anomaly_detection_features, max_dimensions=60, target_variance=0.95)\n",
763-
"java_package_embedding_anomaly_detection_feature_names = embedding_feature_names = [f'nodeEmbeddingPCA_{i}' for i in range(java_package_embedding_anomaly_detection_input.shape[1])]\n",
770+
"# Create a copy of the java_package features, selecting only visualization and embedding features\n",
771+
"java_package_embedding_anomaly_detection_features = java_package_anomaly_detection_features[features_for_visualization + ['embedding']].copy()\n",
772+
"\n",
773+
"# Skip PCA and keep the original dimensionality of the node embeddings. When only considering embeddings, there are no features that could get outperformed.\n",
774+
"# java_package_embedding_anomaly_detection_input = reduce_dimensionality_of_node_embeddings(java_package_embedding_anomaly_detection_features, max_dimensions=60, target_variance=0.95)\n",
775+
"java_package_embedding_anomaly_detection_input = np.stack(java_package_embedding_anomaly_detection_features['embedding'].apply(np.array).tolist())\n",
776+
"java_package_embedding_anomaly_detection_feature_names = [f'nodeEmbedding_{i}' for i in range(java_package_embedding_anomaly_detection_input.shape[1])]\n",
777+
"\n",
778+
"# Tune anomaly detection models using only the reduced embedding features, with automatic contamination threshold\n",
764779
"java_package_embedding_anomaly_detection_result = tune_anomaly_detection_models(java_package_embedding_anomaly_detection_input, contamination=\"auto\")\n",
780+
"\n",
781+
"# Add the anomaly detection results (labels and scores) to the features dataframe with custom column names for embedding-based anomalies\n",
765782
"java_package_embedding_anomaly_detection_features = add_anomaly_detection_results_to_features(java_package_embedding_anomaly_detection_features, java_package_embedding_anomaly_detection_result, anomaly_label_column='anomalyOfEmbeddingLabel', anomaly_score_column='anomalyOfEmbeddingScore')\n",
766783
"\n",
767784
"display(get_top_10_anomalies(java_package_embedding_anomaly_detection_features, anomaly_label_column='anomalyOfEmbeddingLabel', anomaly_score_column='anomalyOfEmbeddingScore').reset_index(drop=True))"
@@ -2000,7 +2017,9 @@
20002017
"id": "c314821d",
20012018
"metadata": {},
20022019
"source": [
2003-
"#### 2.3b List the top 10 anomalies solely based on embeddings"
2020+
"#### 2.3b List the top 10 anomalies solely based on embeddings\n",
2021+
"\n",
2022+
"By leaving out all other features, we can see if the embeddings alone are sufficient to detect anomalies. Anomalies detected solely based on embeddings could indicate structural outliers in the graph representation of the codebase. In most cases however, combining embeddings with other features yields better results."
20042023
]
20052024
},
20062025
{
@@ -2010,12 +2029,20 @@
20102029
"metadata": {},
20112030
"outputs": [],
20122031
"source": [
2013-
"java_type_embedding_anomaly_detection_features = java_type_anomaly_detection_features[features_for_visualization_excluded_from_training + ['embedding', 'pageRank', 'articleRank']].copy()\n",
2014-
"java_type_embedding_anomaly_detection_input = reduce_dimensionality_of_node_embeddings(java_type_embedding_anomaly_detection_features, max_dimensions=60, target_variance=0.95)\n",
2015-
"java_type_embedding_anomaly_detection_feature_names = embedding_feature_names = [f'nodeEmbeddingPCA_{i}' for i in range(java_type_embedding_anomaly_detection_input.shape[1])]\n",
2032+
"# Create a copy of the java_type features, selecting only embeddings and everything needed for visualization\n",
2033+
"java_type_embedding_anomaly_detection_features = java_type_anomaly_detection_features[features_for_visualization + ['embedding']].copy()\n",
2034+
"\n",
2035+
"# Skip PCA and keep the original dimensionality of the node embeddings. When only considering embeddings, there are no features that could get outperformed.\n",
2036+
"# java_type_embedding_anomaly_detection_input = reduce_dimensionality_of_node_embeddings(java_type_embedding_anomaly_detection_features, max_dimensions=60, target_variance=0.95)\n",
2037+
"java_type_embedding_anomaly_detection_input = np.stack(java_type_embedding_anomaly_detection_features['embedding'].apply(np.array).tolist())\n",
2038+
"java_type_embedding_anomaly_detection_feature_names = [f'nodeEmbedding_{i}' for i in range(java_type_embedding_anomaly_detection_input.shape[1])]\n",
2039+
"\n",
20162040
"java_type_embedding_anomaly_detection_result = tune_anomaly_detection_models(java_type_embedding_anomaly_detection_input, contamination=\"auto\")\n",
2041+
"\n",
2042+
"# Add the anomaly detection results (labels and scores) to the features dataframe with custom column names for embedding-based anomalies\n",
20172043
"java_type_embedding_anomaly_detection_features = add_anomaly_detection_results_to_features(java_type_embedding_anomaly_detection_features, java_type_embedding_anomaly_detection_result, anomaly_label_column='anomalyOfEmbeddingLabel', anomaly_score_column='anomalyOfEmbeddingScore')\n",
20182044
"\n",
2045+
"# Display the top 10 anomalies detected based on embeddings, sorted by anomaly score in descending order, with index reset for cleaner output\n",
20192046
"display(get_top_10_anomalies(java_type_embedding_anomaly_detection_features, anomaly_label_column='anomalyOfEmbeddingLabel', anomaly_score_column='anomalyOfEmbeddingScore').reset_index(drop=True))"
20202047
]
20212048
},

0 commit comments

Comments
 (0)