我想做线性缩放,例如:有一个序列A=1,2,3,4,所以A的长度是4,我想做一个线性缩放来生成一个新的序列B(与A相比,B的长度可能是0.5倍,1.1倍,1.5倍,等等……)例如:假设缩放率为7/4,使用线性缩放时B的长度为4*( 7/4) =7,则B序列的结果必须为B=1,1.5,2,2.5,3,3.5 ,4如何使用scipy.interpolate.interp1d来实现?或者有没有其他函数可以做到这一点?输入A以获得输出B(具有任意缩放速率)谢谢!
发布于 2020-05-26 00:03:27
由于我的语言能力,很抱歉我不能准确地描述我想要的东西。好了,我已经完成了这个函数:
enter code here
#vec[0]&vec[-1] will not change
#num is the numbers you want to add to vec
#num cant > vec
#It's Linear zoom function
#just change length=len(vec)-num can do Linear reduction!
def linscale(vec,num):
ans=[ ]
length=len(vec)+num
VL=len(vec)-1
AL=length-1
count=0
for i in range(length-2):
count+= VL/AL
if int(count)/count==1:
value=vec[int(count)]
ans.append(value)
elif int(count)/count!=1:
new=int(count)
if new==0:
value=(VL*vec[new+1]+(AL-VL)*vec[new])/AL
ans.append(value)
elif new!=0:
value=((count-new)*vec[new+1]+((new+1)-count)*vec[new])
ans.append(value)
ans.insert(0,vec[0])
ans.insert(AL,vec[-1])
return ans
-------------------------------------------------
test=[1,2,3,4]
print(linscale(test,3))
print(linscale(test,2))
[1, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
[1, 1.6, 2.2, 2.8, 3.4, 4.0]https://stackoverflow.com/questions/61901175
复制相似问题