Repository navigation
Expand file tree
/
Copy pathdimanalysis_lib.py
More file actions
112 lines (95 loc) · 4.06 KB
/
Copy pathdimanalysis_lib.py
File metadata and controls
112 lines (95 loc) · 4.06 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# coding: utf-8
# Author: Mandis Beigi
# Copyright (c) 2022 Medidata Solutions, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import copy
from random import shuffle
from datetime import datetime
import pandas as pd
import numpy as np
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn.decomposition import FastICA
from sklearn.preprocessing import StandardScaler
from sklearn.cross_decomposition import CCA
import gower
def reduce_cca(data_df, n_components=2):
"""
Uses CCA to reduce dimension.
Parameters:
data_df (DataFrame): The input data in DataFrame format
n_components (int): The number of components to reduce to. Default is 2.
returns:
DataFrame: returns the data in the reduced dimension
"""
new_df = data_df.reset_index(drop=True)
embedded = CCA(n_components=2).fit_transform(new_df.to_numpy())
return(pd.DataFrame(embedded, index=data_df.index))
def reduce_ica(data_df, n_components=None):
"""
Uses ICA to reduce dimension.
Parameters:
data_df (DataFrame): The input data in DataFrame format
n_components (int): The number of components to reduce to. Default is all components.
returns:
DataFrame: returns the data in the reduced dimension
"""
new_df = data_df.reset_index(drop=True)
embedded = FastICA(n_components=2).fit_transform(new_df)
return(pd.DataFrame(embedded, index=data_df.index))
def reduce_tsne(data_df, n_components=2, init='pca', metric='euclidean'):
"""
Uses tSNE to reduce dimension.
Parameters:
data_df (DataFrame): The input data in DataFrame format
n_components (int): The number of components to reduce to. Default is 2.
returns:
DataFrame: returns the data in the reduced dimension
"""
new_df = data_df.reset_index(drop=True)
if metric == 'gower':
#tsne = TSNE(n_components=n_components, metric='precomputed', square_distances=True)
tsne = TSNE(n_components=n_components)
df_gower = gower.gower_matrix(new_df)
embedded = tsne.fit_transform(df_gower)
else:
#tsne = TSNE(n_components, square_distances=True)
tsne = TSNE(n_components)
embedded = tsne.fit_transform(new_df)
return(pd.DataFrame(embedded, index=data_df.index))
def reduce_pca(data_df, n_components=None):
"""
Uses PCA to reduce dimension.
Parameters:
data_df (DataFrame): The input data in DataFrame format
n_components (float): The number of components or to reduce to. If the number if between 0 and 1, n_components is the % of
the principal components will be kept. Default is all components.
returns:
DataFrame: returns the data in the reduced dimension
"""
new_df = data_df.reset_index(drop=True)
data_np = new_df.to_numpy()
#Standardize the data by removing the mean and scaling to unit variance
pca_np = StandardScaler().fit_transform(data_np)
pca = PCA(n_components)
embedded = pca.fit_transform(pca_np)
return(pd.DataFrame(embedded, index=data_df.index))