forked from sachinthakur9614/bin_by_median
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMax.py
92 lines (72 loc) · 3.07 KB
/
MinMax.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
#....An Open source Library for Minmax transformation
""" Created on 8/12/2019
@author:Jayas p Jacob
"""
import numpy as np
from scipy import sparse
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_is_fitted, check_array, warn_if_not_float
class MinMax(BaseEstimator, TransformerMixin):
def __init__(self, feature_range=(0, 1), copy=True):
self.feature_range = feature_range
self.copy = copy
def fit(self, X, y=None):
X = check_array(X, copy=self.copy, ensure_2d=True,
accept_sparse="csc", dtype=np.float32,
ensure_min_samples=2)
if warn_if_not_float(X, estimator=self):
X = X.astype(np.float)
feature_range = self.feature_range
if feature_range[0] >= feature_range[1]:
raise ValueError("Minimum of desired feature range must be smaller"
" than maximum. Got %s." % str(feature_range))
if sparse.issparse(X):
data_min = []
data_max = []
data_range = []
for i in range(X.shape[1]):
if X.indptr[i] == X.indptr[i+1]:
data_min.append(0)
data_max.append(0)
data_range.append(0)
else:
data_min.append(X.data[X.indptr[i]:X.indptr[i + 1]].min())
data_max.append(X.data[X.indptr[i]:X.indptr[i + 1]].max())
data_min = np.array(data_min, dtype=np.float32)
data_max = np.array(data_max, dtype=np.float32)
data_range = data_max - data_min
else:
data_min = np.min(X, axis=0)
data_range = np.max(X, axis=0) - data_min
# Do not scale constant features
if isinstance(data_range, np.ndarray):
# For a sparse matrix, constant features will be set to one!
if sparse.issparse(X):
for i in range(len(data_min)):
if data_range[i] == 0.0:
data_min[i] = data_min[i] - 1
data_range[data_range == 0.0] = 1.0
elif data_range == 0.:
data_range = 1.
self.scale_ = (feature_range[1] - feature_range[0]) / data_range
self.min_ = feature_range[0] - data_min * self.scale_
self.data_range = data_range
self.data_min = data_min
return self
def transform(self, X):
check_is_fitted(self, 'scale_')
X = check_array(X, accept_sparse="csc", copy=self.copy)
if sparse.issparse(X):
for i in range(X.shape[1]):
X.data[X.indptr[i]:X.indptr[i + 1]] *= self.scale_[i]
X.data[X.indptr[i]:X.indptr[i + 1]] += self.min_[i]
else:
X *= self.scale_
X += self.min_
return X
def inverse_transform(self, X):
check_is_fitted(self, 'scale_')
X = check_array(X, copy=self.copy, accept_sparse="csc", ensure_2d=False)
X -= self.min_
X /= self.scale_
return X