Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lucas Miranda
deepOF
Commits
94b4e521
Commit
94b4e521
authored
Jun 11, 2021
by
lucas_miranda
Browse files
Improved table_dict projections
parent
fedf75a5
Changes
1
Hide whitespace changes
Inline
Side-by-side
deepof/data.py
View file @
94b4e521
...
...
@@ -23,6 +23,7 @@ from typing import Dict, List, Tuple, Union
import
matplotlib.pyplot
as
plt
import
numpy
as
np
import
pandas
as
pd
import
seaborn
as
sns
import
tensorflow
as
tf
from
joblib
import
delayed
,
Parallel
,
parallel_backend
from
pkg_resources
import
resource_filename
...
...
@@ -1326,39 +1327,58 @@ class table_dict(dict):
return
X
,
labels
def
random_
projection
(
self
,
n_components
:
int
=
2
,
sample
:
int
=
1000
def
projection
(
self
,
proj
,
n_components
:
int
=
2
,
sample
:
int
=
1000
,
kernel
:
str
=
None
,
perplexity
:
int
=
None
,
)
->
deepof
.
utils
.
Tuple
[
deepof
.
utils
.
Any
,
deepof
.
utils
.
Any
]:
"""Returns a training set generated from the 2D original data (time x features) and a
random
projection
"""Returns a training set generated from the 2D original data (time x features) and a
specified
projection
to a n_components space. The sample parameter allows the user to randomly pick a subset of the data for
performance or visualization reasons"""
X
,
labels
=
self
.
prepare_projection
(
sample
=
sample
)
rproj
=
random_projection
.
GaussianRandomProjection
(
n_components
=
n_components
)
X
=
rproj
.
fit_transform
(
X
)
if
proj
==
"random"
:
proj
=
random_projection
.
GaussianRandomProjection
(
n_components
=
n_components
)
elif
proj
==
"pca"
:
proj
=
KernelPCA
(
n_components
=
n_components
,
kernel
=
kernel
)
elif
proj
==
"tsne"
:
proj
=
TSNE
(
n_components
=
n_components
,
perplexity
=
perplexity
)
X
=
proj
.
fit_transform
(
X
)
if
labels
is
not
None
:
X
=
np
.
concatenate
([
X
,
labels
[:,
np
.
newaxis
]],
axis
=
1
)
return
X
,
r
proj
return
X
,
proj
def
pca
(
# def plot_projection(self, projection, name):
# """Plots a given projection in a 2-dimensional space. If labels are provided,
# these are incorporated into the graph as different colours"""
#
# proj_df = pd.DataFrame(projection)
# sns.scatterplot(data=proj_df, x=0, y=1, hue=2)
# plt.xlabel(name + " 1")
# plt.ylabel(name + " 2")
#
# plt.legend()
# plt.show()
def
random_projection
(
self
,
n_components
:
int
=
2
,
sample
:
int
=
1000
,
kernel
:
str
=
"linear"
)
->
deepof
.
utils
.
Tuple
[
deepof
.
utils
.
Any
,
deepof
.
utils
.
Any
]:
"""Returns a training set generated from the 2D original data (time x features) and a
PCA
projection
"""Returns a training set generated from the 2D original data (time x features) and a
random
projection
to a n_components space. The sample parameter allows the user to randomly pick a subset of the data for
performance or visualization reasons"""
X
,
labels
=
self
.
prepare_projection
(
sample
=
sample
)
pca
=
KernelPCA
(
n_components
=
n_components
,
kernel
=
kernel
)
X
=
pca
.
fit_transform
(
X
)
return
self
.
projection
(
"random"
,
n_components
=
n_components
,
sample
=
sample
,
kernel
=
kernel
)
if
labels
is
not
None
:
X
=
np
.
concatenate
([
X
,
labels
[:,
np
.
newaxis
]],
axis
=
1
)
def
pca
(
self
,
n_components
:
int
=
2
,
sample
:
int
=
1000
,
kernel
:
str
=
"linear"
)
->
deepof
.
utils
.
Tuple
[
deepof
.
utils
.
Any
,
deepof
.
utils
.
Any
]:
"""Returns a training set generated from the 2D original data (time x features) and a PCA projection
to a n_components space. The sample parameter allows the user to randomly pick a subset of the data for
performance or visualization reasons"""
return
X
,
pca
return
self
.
projection
(
"pca"
,
n_components
=
n_components
,
sample
=
sample
,
kernel
=
kernel
)
def
tsne
(
self
,
n_components
:
int
=
2
,
sample
:
int
=
1000
,
perplexity
:
int
=
30
...
...
@@ -1367,15 +1387,7 @@ class table_dict(dict):
to a n_components space. The sample parameter allows the user to randomly pick a subset of the data for
performance or visualization reasons"""
X
,
labels
=
self
.
prepare_projection
(
sample
=
sample
)
tsne
=
TSNE
(
n_components
=
n_components
,
perplexity
=
perplexity
)
X
=
tsne
.
fit_transform
(
X
)
if
labels
is
not
None
:
X
=
np
.
concatenate
([
X
,
labels
[:,
np
.
newaxis
]],
axis
=
1
)
return
X
,
tsne
return
self
.
projection
(
"tsne"
,
n_components
=
n_components
,
sample
=
sample
,
perplexity
=
perplexity
)
def
merge_tables
(
*
args
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment