forked from abeljoseph/clusters_classifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_class.py
More file actions
38 lines (28 loc) · 1.26 KB
/
data_class.py
File metadata and controls
38 lines (28 loc) · 1.26 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
import numpy as np
from math import pi, sqrt
class data_class:
def __init__(self, n, mean, covariance):
self.covariance = covariance
self.mean = mean
self.n = n
self.cluster = self.create_normal_distribution()
self.eigenvals, self.eigenvecs = np.linalg.eig(self.covariance)
self.testing_cluster = self.create_normal_distribution()
def create_normal_distribution(self):
return np.random.multivariate_normal(self.mean, self.covariance, size=self.n)
def plot(self, ax):
max_index = np.where(self.eigenvals == max(self.eigenvals))[0][0]
min_index = np.where(self.eigenvals == min(self.eigenvals))[0][0]
largest_eigval = self.eigenvals[max_index]
smallest_eigval = self.eigenvals[min_index]
largest_eigvec = self.eigenvecs[:, max_index]
theta = np.arctan2(*largest_eigvec[::-1])
theta_grid = np.linspace(0, 2 * pi)
dim_a = sqrt(largest_eigval)
dim_b = sqrt(smallest_eigval)
axes_x = dim_a * np.cos(theta_grid)
axes_y = dim_b * np.sin(theta_grid)
rtn = [[np.cos(theta), np.sin(theta)], [-1 * np.sin(theta), np.cos(theta)]]
ellipse = np.matmul(np.array([axes_x, axes_y]).T, rtn)
ax.plot([x[0] + self.mean[0] for x in ellipse], [x[1] + self.mean[1] for x in ellipse])
ax.scatter([x[0] for x in self.cluster], [x[1] for x in self.cluster])