-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussianMixtureModel.py
More file actions
95 lines (84 loc) · 4.01 KB
/
GaussianMixtureModel.py
File metadata and controls
95 lines (84 loc) · 4.01 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn import decomposition
from sklearn.mixture import GaussianMixture
from sklearn import preprocessing
class GaussianMixtureModel():
def __init__(self, data, labels):
self.data = data
self.processed_data = data
self.labels = labels
self.operationsOrder = []
self.score = 0
def reduce(self, n_components = 2):
self.operationsOrder.append("reduce")
pca = decomposition.PCA(n_components = n_components)
pca.fit(self.processed_data)
self.processed_data = pca.transform(self.processed_data)
self.pca = pca
def scale(self, scaling_type):
self.operationsOrder.append("scale")
if(scaling_type == "standard"):
self.scaler = preprocessing.StandardScaler().fit(self.processed_data)
elif(scaling_type == "normalizer"):
self.scaler = preprocessing.Normalizer().fit(self.processed_data)
elif(scaling_type == "minmax"):
self.scaler = preprocessing.MinMaxScaler().fit(self.processed_data)
elif(scaling_type == "robust"):
self.scaler = preprocessing.RobustScaler().fit(self.processed_data)
else:
print("Illegal Argument given for scaling.")
self.processed_data = self.scaler.transform(self.processed_data)
def fit(self, n_components=3 , covariance_type = "full"):
self.model = GaussianMixture(n_components=n_components, covariance_type=covariance_type)
self.model.fit(self.processed_data)
def plot(self, colors = ['cyan', 'green', 'red'], subplot_ = plt.subplot(111)):
for n, color in enumerate(colors):
if self.model.covariance_type == 'full':
covariances = self.model.covariances_[n][:2, :2]
elif self.model.covariance_type == 'tied':
covariances = self.model.covariances_[:2, :2]
elif self.model.covariance_type == 'diag':
covariances = np.diag(self.model.covariances_[n][:2])
elif self.model.covariance_type == 'spherical':
covariances = np.eye(self.model.means_.shape[1]) * self.model.covariances_[n]
v, w = np.linalg.eigh(covariances)
u = w[0] / np.linalg.norm(w[0])
angle = np.arctan2(u[1], u[0])
angle = 180 * angle / np.pi # convert to degrees
v = 2. * np.sqrt(2.) * np.sqrt(v)
ellipse = mpl.patches.Ellipse(self.model.means_[n, :2], v[0], v[1], 180 + angle, color=color)
ellipse.set_clip_box(subplot_.bbox)
ellipse.set_alpha(0.3)
subplot_.add_artist(ellipse)
## ------------------------------
if self.operationsOrder[0] == "reduce":
temp_data = self.pca.transform(self.data[self.labels==n+1])
temp_data = self.scaler.transform(temp_data)
else:
temp_data = self.scaler.transform(self.data[self.labels==n+1])
temp_data = self.pca.transform(temp_data)
plt.scatter(temp_data[:, 0], temp_data[:, 1], color=color)
def run(self, plot = True, subplot_ = plt.subplot(111), test = True, reduce_first = True, reduce_components = 2, scaling_type="standard", model_components = 3, covariance_type="full"):
if(reduce_first):
self.reduce(reduce_components)
self.scale(scaling_type)
else:
self.scale(scaling_type)
self.reduce(reduce_components)
self.fit(model_components, covariance_type = covariance_type)
if plot: self.plot(subplot_ = subplot_)
if test: self.test()
def test(self):
labels = []
for l in self.labels:
labels.append(l-1)
pred_labels = self.model.predict(self.processed_data).ravel()
score = np.mean(pred_labels == labels) * 100
self.score = score
print("Score: {}%".format(score))
return score
def reset(self):
self.processed_data = self.data
self.score = 0