-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoNN.py
More file actions
45 lines (33 loc) · 1.24 KB
/
twoNN.py
File metadata and controls
45 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
from sklearn.linear_model import LinearRegression
import random
import dataset_generator
def euclidean_distance(a, b):
return np.sqrt(np.sum((a - b) ** 2))
def two_nn(dataset : np.ndarray, trim : float = 0.05):
"""
twoNN dimension estimation method.
:param dataset: The dataset to compute the ID of.
:param trim: The percentage of "high distances" discarded. Default at 5%.
"""
n_points = dataset.shape[0]
mu_values = np.zeros(n_points)
N = int(len(dataset)*(1.0-trim/2))
for i in range(n_points):
distances = []
for j in range(n_points):
if i != j:
dist = euclidean_distance(dataset[i], dataset[j])
distances.append((dist, j))
distances.sort(key=lambda x: x[0])
r1 = distances[0][0]
r2 = distances[1][0]
mu = r2 / r1
mu_values[i] = mu
mu_values = mu_values[np.argsort(mu_values)]
mu_values = mu_values[len(mu_values)-N:N]
Femp = np.arange(2*N-len(dataset)) / (2*N-len(dataset))
Ir = LinearRegression(fit_intercept=False)
Ir.fit(np.log(mu_values).reshape(-1,1), -np.log(1 - Femp).reshape(-1,1))
d = Ir.coef_[0][0]
return d, mu_values