Stay Hungry, Stay Foolish

python 实现matlab的mapminmax方法

Python 18663℃ 0评论

在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)
喜欢 (10)
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in C:\inetpub\wordpress\wp-content\themes\XHBlog\comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址