在对SynthText的gt.mat中的wordBB中的数据进行处理时,我遇到一个问题:确保每一个值都在[0.,1.]内。如果用循环,就没有体现出python的逼格。于是找了三种方法(仅展示使每一个元素都小于1的方法)
>>>import numpy as np
>>> a = np.random.randn(5,5)
>>>a
array([[-0.41332305, -0.89819726, 0.00230665, 1.22772589, 0.38366591],
[-0.90198011, 0.03564843, -1.28926568, 2.18393331, 0.10967645],
[ 0.45699427, -0.13016951, 0.90630688, 0.48602874, -1.10298832],
[ 0.36888316, -0.714038 , -0.66919675, -0.81525787, 1.41081888],
[ 1.95669035, -0.93295112, -0.24652865, 0.97873326, 0.24871858]])
#方法一
>>> np.minimum(a,1)
array([[-0.35197027, -0.42240549, 0.26679478, 0.21464243, 0.26239148],
[-0.07749789, 0.235409 , -1.46625603, -0.29844664, -1.05742573],
[-1.70085612, 1. , -0.4486504 , -0.34203943, -0.16684031],
[ 0.7492834 , 1. , 0.43738106, 1. , 0.57563926],
[ 0.41377862, 0.91504777, -0.46647957, 1. , -0.55535723]])
#方法二
>>> np.where(a<1,a,1)
array([[-0.35197027, -0.42240549, 0.26679478, 0.21464243, 0.26239148],
[-0.07749789, 0.235409 , -1.46625603, -0.29844664, -1.05742573],
[-1.70085612, 1. , -0.4486504 , -0.34203943, -0.16684031],
[ 0.7492834 , 1. , 0.43738106, 1. , 0.57563926],
[ 0.41377862, 0.91504777, -0.46647957, 1. , -0.55535723]])
#方法三
>>> a[a>1] = 1
>>> a
array([[-0.35197027, -0.42240549, 0.26679478, 0.21464243, 0.26239148],
[-0.07749789, 0.235409 , -1.46625603, -0.29844664, -1.05742573],
[-1.70085612, 1. , -0.4486504 , -0.34203943, -0.16684031],
[ 0.7492834 , 1. , 0.43738106, 1. , 0.57563926],
[ 0.41377862, 0.91504777, -0.46647957, 1. , -0.55535723]])
网友评论