Clip
Clip¶
You can use clip to take values and set either a minimum or a maximum. Let’s start with a basic sample and see what it looks like at first look.
In [4]:
#Create some test data, ignore for now
np.random.seed(1)
sample = np.random.normal(10, 5, 2000)
#Plot the test data
plt.hist(sample, bins=40)
plt.show()
We call clip with a first argument as the minimum value and the second argument as the maximum value. So the code below will get us to a sample where values can only be between 0 and 20.
In [5]:
#Clip the sample to be between 0 and 20, then plot a histogram of it
plt.hist(sample.clip(0, 20))
plt.show()
We call clip with a first argument as the minimum value and the second argument as the maximum value. So the code below will get us to a sample where values can only be between 5 and 15.
In [6]:
#Clip between 5 and 15
plt.hist(sample.clip(5, 15))
plt.show()
If you want only a maximum or a minimum set you can use None instead of setting a number. For example, below is setting just a maximum of 15.
In [7]:
#Clip to have a maximum of 15
plt.hist(sample.clip(None, 15))
plt.show()
Set a minimum of 5.
In [8]:
#Clip to have a minimum of 5
plt.hist(sample.clip(5, None))
plt.show()