-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale.py
191 lines (162 loc) · 7 KB
/
scale.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
from sklearn.preprocessing import StandardScaler
class scaler:
def __init__(self, input_array, samples, features,do_weights, w_mag, weight_array):
#initialize variables
self.feature_index = 1
self.sample_index = 0
self.features = features
self.samples = samples
self.rates = 0
# Copy data and correct array shape
if input_array.shape[self.feature_index] == features:
self.data = input_array
self.original_data = input_array
else:
self.data = input_array.transpose
self.original_data = input_array.transpose
# Scale Matrix
self.D = np.zeros((features,features))
self.feature_mean = 0
# Weight matrix
self.do_weights = do_weights
self.weight_array = weight_array
self.W = np.identity(self.features)
for i in range(self.features):
if self.weight_array[i] == 1:
self.W[i,i] *= w_mag
self.W = np.linalg.inv(self.W)
#print(self.W)
############################################
############################################ Construction of scaling array
############################################
def scale_scipy_std(self):
# Standard Deviation Scaling (scipy)
self.sc = StandardScaler(with_mean=False) #,with_std=False)
self.data = self.sc.fit_transform(self.data)
def scale_std(self):
# Standard Deviation Scaling
for i in range(self.D.shape[1]):
self.D[i,i] = np.std(self.data[:,i])
if self.do_weights == 1:
self.D = self.D.dot(self.W)
def scale_pareto(self):
# Pareto Scaling
for i in range(self.D.shape[self.feature_index]):
self.D[i,i] = np.sqrt(np.std(self.data[i]))
if self.do_weights == 1:
self.D = self.D.dot(self.W)
def scale_vast(self):
# Vast Scaling
for i in range(self.D.shape[self.feature_index]):
self.mean_of_feature = np.mean(self.data[self.feature_index])
self.D[i,i] = np.power( np.std(self.data[i]),2.0)/self.mean_of_feature
if self.do_weights == 1:
self.D = self.D.dot(self.W)
def scale_range(self):
# Range scaling
for i in range(self.D.shape[self.feature_index]):
self.max_range = np.max( self.data[i] - np.mean(self.data[i]) )
self.min_range = np.min( self.data[i] - np.mean(self.data[i]) )
self.D[i,i] = self.max_range - self.min_range
if self.do_weights == 1:
self.D = self.D.dot(self.W)
def scale_level(self):
# Level scaling
for i in range(self.D.shape[self.feature_index]):
self.D[i,i] = np.mean(self.data[i])
if self.do_weights == 1:
self.D = self.D.dot(self.W)
############################################
############################################ Log Scaling methods
############################################
def scale_bisymlog(self):
# Bisymmetric Log Transfer Scaling
C = 1.0 #Shaping constant
for i in range(self.data.shape[0]):
for j in range(self.data.shape[1]):
self.data[i,j] = np.sign(self.data[i,j])* \
np.log10(1.+np.abs(self.data[i,j]/C))
def unscale_bisymlog(self):
# Bisymmetric Log Transfer Unscaling
for i in range(self.data.shape[0]):
for j in range(self.data.shape[1]):
self.data[i,j] = np.sign(self.data[i,j])* \
(-1.0+np.power(10.,np.abs(self.data[i,j])))
def scale_log(self):
# apply logarithmic scaling
self.data = np.log(self.data)
def unscale_log(self):
# unapply logarithmic scaling
self.data = np.exp(self.data)
############################################
############################################ Apply/Unapply Scaling
############################################
def apply_scaling(self):
# applies scaling matrix D
if np.sum(self.D)==0:
print("ERROR: EMPTY SCALING MATRIX")
# if self.do_weights == 1:
# self.set_wght_mgntd_mag()
# temp = np.linalg.inv(self.D).dot(self.W)
# else:
temp = np.linalg.inv(self.D)
self.data = np.dot(self.data, temp)
def unapply_scaling(self):
#print(self.data.shape, self.D.shape)
# if self.do_weights == 1:
# self.set_wght_mgntd_mag()
# temp = self.D.dot(np.linalg.inverse(self.W))
# else:
temp = self.D
self.data = np.dot(self.data, temp)
############################################
############################################ WEIGHTS
############################################
def set_wght_mgntd_mag(self):
# set weight magnitudes by multiplying target species
for i in range(self.features):
if self.weight_array == 1:
self.W[i,i] *= self.multiplier
# def weight_features(self):
# # weight certain features by preferrentially scaling magnitudes
# # modifies scale matrix
# self.D = self.D.dot(self.W)
# def unweight_features(self):
# # unweight certain features; not sure if needed
# self.D = self.D.dot(np.linalg.inverse(self.W))
############################################
############################################ MISC
############################################
def load_data(self, newdata):
# loads new data to be scaled or unscaled;
# USE WITH CAUTION
self.data = newdata
def reset_data(self):
# resets data to pre-scaling state; mainly for testing
self.data = self.original_data
def find_mean(self):
# finds the mean of a matrix dimension
for i in range(self.data.shape[1]):
self.feature_mean[i] = np.mean(self.data[:,i])
self.feature_mean.reshape(1,-1)
def center(self):
# subtracts the mean from the matrix
if type(self.feature_mean)==type(0):
self.feature_mean = np.zeros(self.features+1)
print("Finding Mean (should only happen once)")
self.find_mean()
#print(self.data.shape, self.feature_mean.shape)
for i in range(self.data.shape[1]):
self.data[:,i] = np.subtract(self.data[:,i],self.feature_mean[i])
def center_rates(self,rates):
# subtracts species mean from rate expression
self.rates = rates
for i in range(self.rates.shape[1]):
self.rates[:,i] = np.subtract(self.rates[:,i],self.feature_mean[i])
def uncenter(self):
for i in range(self.data.shape[1]):
self.data[:,i] = np.add(self.data[:,i],self.feature_mean[i])
# def center_easy(self):
# self.mean_ = np.mean(self.data, axis=0)
# self.data -= self.mean_