-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonar.py
84 lines (57 loc) · 1.61 KB
/
sonar.py
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
import sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from sklearn.mixture import GaussianMixture
from sklearn.decomposition import FastICA
# np.set_printoptions(threshold=sys.maxsize)
filename = 'runningexample.txt'
with open(filename) as f:
data = f.readlines()
data = [x.strip() for x in data] # split rows
data = [x.split() for x in data] # split elemnets
# delete name of pint
y = []
for row in data:
y.append(row[2])
del row[2]
data = np.array(data)
y = np.array(y)
data = data.astype('float64')
y = y.astype('float64')
df = pd.DataFrame(data, columns=["x", "y"])
df["label"] = y
sns.pairplot(data=df, x_vars=["x"], y_vars=["y"], hue="label")
plt.show()
# Part 1: Apply EM algorithm
# print("True labels: ", y)
for k in range(1, 11):
modelEM = GaussianMixture(n_components=k, max_iter=3, n_init=100, tol=0.00001, covariance_type='full')
modelEM.fit(data)
print("N components: ", k)
print("Model score: ", modelEM.score(data, y))
# print("Predicted labels:", modelEM.predict(data))
# Part 2 independent analysis and whilening
# covariance matrix
def calculateCovariance(X):
meanX = np.mean(X, axis = 0)
lenX = X.shape[0]
X = X - meanX
covariance = X.T.dot(X)/lenX
return covariance
P = calculateCovariance(data)
print(P)
print(P.shape)
# eigenvalue decomposition
W, V = np.linalg.eig(P)
D = np.diag(W)
print(V@D@V.T - P)
# apply whitening ???
A = data.dot(V[1])
X = A/np.sqrt(W[1]+1e-5)
print(X)
X = X.reshape(-1, 1)
transformer = FastICA()
X_transformed = transformer.fit_transform(X)
print(X_transformed.shape)