在python中实现matlab的mapminmax方法:
import numpy as np
class MapMinMaxApplier(object):
def __init__(self, slope, intercept):
self.slope = slope
self.intercept = intercept
def __call__(self, x):
return x * self.slope + self.intercept
def reverse(self, y):
return (y-self.intercept) / self.slope
def mapminmax(x, ymin=-1, ymax=+1):
x = np.asanyarray(x)
xmax = x.max(axis=-1)
xmin = x.min(axis=-1)
if (xmax==xmin).any():
raise ValueError("some rows have no variation")
slope = ((ymax-ymin) / (xmax - xmin))[:,np.newaxis]
intercept = (-xmin*(ymax-ymin)/(xmax-xmin))[:,np.newaxis] + ymin
ps = MapMinMaxApplier(slope, intercept)
return ps(x), ps
调用方法:
#在matlab中:
[ab,ps1]=mapminmax(test_x,0,1);
[ab,ps1]=mapminmax(test_x); #默认为-1,1
mapminmax('apply',test_x,ps1)
mapminmax('reverse',test_x,ps1)
#在python中相似的:
ab,ps1=mapminmax(test_x,0,1);
ab,ps1=mapminmax(test_x); #默认为-1,1
ps1(test_x)
ps1. reverse(test_x)


