arundhaj

regression towards the datascience

Split array based on difference with NumPy

 

I had a NumPy array of numbers, which I had to split based on the change of value.

For example, consider an array as shown below.

values = [112.0, 111.0, 113.0, 111.0, 112.0, 112.0, 112.0, 113.0, 113.0,
       113.0, 114.0, 114.0, 115.0, 117.0, 102.0, 102.0, 102.0, 103.0,
       103.0, 103.0, 103.0, 104.0, 104.0, 104.0, 103.0, 103.0, 104.0]

In this array, I had to split at the point where the value falls more than 10 counts.

The python snippet which uses numpy to do this

# find the difference between each numbers
diff_values = np.diff(values)
# find the index whose difference is 10
split_index = np.where(diff_values < -10)[0]
# add 1 to index values
split_index += 1
# result
split_values = np.split(values, split_index)

The resulting split_values would contain two arrays with values

split_values[0] = [112.0, 111.0, 113.0, 111.0, 112.0, 112.0, 112.0, 113.0, 113.0,
       113.0, 114.0, 114.0, 115.0, 117.0]

split_values[1] = [102.0, 102.0, 102.0, 103.0,
       103.0, 103.0, 103.0, 104.0, 104.0, 104.0, 103.0, 103.0, 104.0]

Hope this helps.

Comments