-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.py
131 lines (107 loc) · 5.52 KB
/
manifold.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Manifold learning of non-linear data relationships
This file contains functions to manifold learn the non-linear relationships in
patient data.
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with this
work for additional information regarding copyright ownership. The ASF
licenses this file to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License."""
# --------------------------------------------------------------------------- #
# MODULE HISTORY #
# --------------------------------------------------------------------------- #
# Version 1
# Date 2018-04-11
# Author LH John
# Note Original version
#
# --------------------------------------------------------------------------- #
# SYSTEM IMPORTS #
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# OTHER IMPORTS #
# --------------------------------------------------------------------------- #
import pandas as pd
from sklearn.manifold import Isomap
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.manifold import TSNE
from sklearn.manifold import MDS
# --------------------------------------------------------------------------- #
# OWN IMPORTS #
# --------------------------------------------------------------------------- #
import load
# --------------------------------------------------------------------------- #
# META DATA #
# --------------------------------------------------------------------------- #
__author__ = 'LH John'
__copyright__ = 'Copyright 2018 The Apache Software Foundation'
__credits__ = ['LH John']
__license__ = 'Apache License, Version 2.0'
__version__ = '1'
__maintainer__ = 'LH John'
__email__ = '[email protected]'
__status__ = 'Development'
# --------------------------------------------------------------------------- #
# CONSTANTS #
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# GLOBAL VARIABLES #
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# EXPORTED FUNCTIONS #
# --------------------------------------------------------------------------- #
def fit_manifold(pc, outcome, technique="isomap", dim=2):
df = []
if technique == "isomap":
df = __manifold_isomap(pc, outcome, dim)
elif technique == "tsne":
df = __manifold_tsne(pc, outcome, dim=2, n_iterations=4000)
elif technique == "lle":
df = __manifold_lle(pc, outcome, dim)
elif technique == "mds":
df = __manifold_mds(pc, outcome, dim)
df = pd.concat([df, outcome], axis=1)
return df
# --------------------------------------------------------------------------- #
# LOCAL FUNCTIONS #
# --------------------------------------------------------------------------- #
def __manifold_isomap(pc, outcome, dim=3):
"""Fit Isomap.
:return: DataFrame of covariates"""
isomap = Isomap(n_components=dim, n_jobs=-1)
iso_out = isomap.fit_transform(pc)
df_iso_out = pd.DataFrame(iso_out, columns=["D1", "D2", "D3"])
return df_iso_out
def __manifold_lle(pc, outcome, dim=2):
"""Fit Locally Linear Embedding.
:return: DataFrame of covariates"""
lle = LocallyLinearEmbedding(n_components=dim, n_jobs=-1,
method='standard')
lle_out = lle.fit_transform(pc)
df_lle_out = pd.DataFrame(lle_out, columns=["D1", "D2"])
return df_lle_out
def __manifold_tsne(pc, outcome, dim=2, n_iterations=4000):
"""Fit t-distributed Stochastic Neighbor Embedding.
:return: DataFrame of covariates"""
tsne = TSNE(n_components=dim, verbose=2, n_iter=n_iterations)
tsne_out = tsne.fit_transform(pc)
df_tsne_out = pd.DataFrame(tsne_out, columns=["D1", "D2"])
return df_tsne_out
def __manifold_mds(pc, outcome, dim=3):
"""Fit Multidimensional Scaling.
:return: DataFrame of covariates"""
mds = MDS(n_components=dim, n_jobs=-1, verbose=2)
mds_out = mds.fit_transform(pc)
df_mds = pd.DataFrame(mds_out, columns=["D1", "D2", "D3"])
return df_mds
# --------------------------------------------------------------------------- #
# END OF FILE #
# --------------------------------------------------------------------------- #