Fast, python-ish way of ranking chunks of 1's in numpy array?

后端 未结 2 1780
忘了有多久
忘了有多久 2021-01-05 03:29

I have a numpy array consisting of 0\'s and 1\'s. Each sequence of 1\'s within the array stands for occurrence of one event. I want to

2条回答
  •  臣服心动
    2021-01-05 03:34

    A really cool way to do this is using the DBSCAN clustering algorithm. It might not be the most efficient for this specific task, BUT it is resilient in case you want to give a minimum number of 1's per event, or allow a gap of a certain number of zeroes within an event.

    from sklearn.cluster import DBSCAN
    import numpy as np
    
    max_gap = 1
    min_samples = 1
    
    # Get indices of every element that belongs to a certain event
    input_values = np.array([0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1])  
    positives_indices = np.where(input_values > 0)[0]
    
    # Turn the indices into a 2D array of so called 'examples'
    X = positives_indices.reshape(-1, 1)
    
    # Train a model and transform the data in one
    clustering = DBSCAN(eps=max_gap, min_samples=min_samples) \
        .fit_predict(X)
    
    # Get results, yields (index, event_id)
    zip(X, clustering)
    

提交回复
热议问题