Skip to content

Commit

Permalink
relocate _.pyx and setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Mai Rajborirug authored and Mai Rajborirug committed May 14, 2020
1 parent 15c5893 commit 71a912e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 263 deletions.
237 changes: 0 additions & 237 deletions Python/rerf/cy_usporf.pyx

This file was deleted.

49 changes: 30 additions & 19 deletions Python/rerf/pycy_usporf.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
"""
Usporf structure in python
Goal: apply USPORF's fast-BIC split, project-A matrix
apply EIF path length
"""
import numpy as np
import random as rn
import cy_usporf # Utilities functions


class UForest(object):
"""
""" UForest: USPORF forest
Creates an iForest object. This object holds the data as well as
the trained trees (iTree objects).
Note: download `cy_usporf.pyx` and `setup.py` into your local
computer and compile them to generate and generate `.C` and `.so`
before using this module
Attributes
Parameters
----------
n_samples: int
Size of the dataset.
Expand All @@ -25,6 +23,14 @@ class UForest(object):
Maximum depth a tree can have.
d: int
number of features in limit sparse matrix `A`
References
----------
.. [#Meghana] Meghana et al (2019).
https://arxiv.org/abs/1907.02844
.. [#Fei] Fei Tony et al. (2009).
https://arxiv.org/abs/1506.03410
Methods
-------
Expand Down Expand Up @@ -61,18 +67,20 @@ def fit(self, X, y=None):
return self

def compute_paths(self, X_in=None):
"""
compute_paths(X_in = None)
""" Compute paths(X_in = None)
Compute anomaly scores for all data points in a dataset X_in
Parameters
----------
X_in : list of list of floats
Data to be scored. iForest.Trees are used for computing
the depth reached in each tree by each data point.
Returns
-------
float
Anomaly score for a given data point.
Anomaly score for a given data point. The higher score,
the more anomaly.
"""
if X_in is None:
X_in = self.X_
Expand All @@ -88,10 +96,11 @@ def compute_paths(self, X_in=None):


class Node(object):
"""
"""Elements stored in tree node
A single node from each tree (each iTree object). Nodes containe
information on hyperplanes used for data division, date to be passed
to left and right nodes, whether they are external or internal nodes.
Attributes
----------
e: int
Expand Down Expand Up @@ -126,8 +135,9 @@ def __init__(self, X, d, Aj, splitPoint, e, left, right, node_type=''):


class iTree(object): # USPORF's algo 1
"""
"""iTree: a single USPORF tree
A single tree in the forest that is build using a unique subsample.
Attributes
----------
X: list
Expand Down Expand Up @@ -160,9 +170,9 @@ def __init__(self, X, e, max_depth, d, Lambda):
self.root = self.make_tree(X, e) # Create a tree with root node.

def make_tree(self, X, e):
"""
make_tree(X, e, l)
"""make_tree(X, e, l)
Builds the tree recursively from a given node. Returns a Node object.
Parameters
----------
X: list of list of floats
Expand Down Expand Up @@ -203,10 +213,11 @@ def make_tree(self, X, e):


class PathFactor(object):
"""
""" Path length finder
Given a single tree (iTree objext) and a data point x = [x1,x2,...,xn],
compute the legth of the path traversed by the point on the tree when
it reaches an external node.
it reaches an external node. See Fei Tony et al. (2009) [#Fei]_ for
further details
Attributes
----------
Expand All @@ -232,13 +243,13 @@ def __init__(self, x, itree):
self.path = self.find_path(itree.root)

def find_path(self, T):
"""
find_path(T)
"""find_path(T)
Given a tree, find the path for a single data point based
on the splitting criteria stored at each node.
Parameters
----------
T : iTree object (iTree.root object)
Returns
-------
int
Expand Down
7 changes: 0 additions & 7 deletions Python/rerf/setup.py

This file was deleted.

3 changes: 3 additions & 0 deletions docs/demos/URerF/3d_usporf_outlier_detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"- Open `SPORF/Python/rerf` and download the following\n",
"files into your local computer\n",
" - `pycy_usporf.py`\n",
"\n",
"- Open `SPORF/Python/src` and download the following\n",
"files into your local computer\n",
" - `cy_usporf.pyx` \n",
" - `setup.py`\n",
"- Open terminal, `cd` to the files location and run\n",
Expand Down

0 comments on commit 71a912e

Please sign in to comment.