-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Module for Principal Feature Analysis | ||
PFA method | ||
""" | ||
from nptdms import TdmsFile | ||
from sklearn import preprocessing | ||
import pandas as pd | ||
import numpy as np | ||
#from sklearn.decomposition import PCA | ||
from sklearn.cluster import KMeans | ||
from collections import defaultdict | ||
#from sklearn.preprocessing import StandardScaler | ||
import matplotlib.pyplot as plt | ||
import seglearn as sgl | ||
from matplotlib.mlab import PCA | ||
import math | ||
|
||
class PFA(object): | ||
def __init__(self, n_features, q=None): | ||
self.q = q | ||
self.n_features = n_features | ||
|
||
def fit(self, X): | ||
if not self.q: | ||
self.q = X.shape[1] | ||
|
||
sc = StandardScaler() | ||
X = sc.fit_transform(X) | ||
|
||
pca = PCA(n_components=self.q).fit(X) | ||
A_q = pca.components_.T | ||
|
||
kmeans = KMeans(n_clusters=self.n_features).fit(A_q) | ||
clusters = kmeans.predict(A_q) | ||
cluster_centers = kmeans.cluster_centers_ | ||
|
||
dists = defaultdict(list) | ||
for i, c in enumerate(clusters): | ||
dist = euclidean_distances([A_q[i, :]], [cluster_centers[c, :]])[0][0] | ||
dists[c].append((i, dist)) | ||
|
||
self.indices_ = [sorted(f, key=lambda x: x[1])[0][0] for f in dists.values()] | ||
self.features_ = X[:, self.indices_] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
""" | ||
The __init__.py file is usually empty, but if you remove the __init__.py file, Python will no longer look for submodules inside that directory. | ||
""" | ||
# Export public objects |