Search code examples
pythonnumpyk-meansnumpy-ndarray

Numpy finds most common item per value from another column


I am working on a kMeans AI to determine the season of any given day For this, I have an array with data with 4 columns This is what it looks like (tho its longer):

['0.2742330338168506' '0' '1.3694492732480696' 'winter']
['0.28529288153011745' '0' '1.3805091209613365' 'lente']
['0.28595917620794253' '1' '1.3811754156391616' 'winter']
['0.2874392369724381' '2' '1.3826554764036572' 'lente']
['0.316557712713994' '2' '1.411773952145213' 'herfst']
['0.32113534393276466' '3 '1.4163515833639837' 'lente']
['0.3231108855082745' '3' '1.4220488660040091' 'lente']
['0.3163219663513872' '3' '1.4288377851608964' 'winter']
['0.31201423701381703' '4' '1.4331455144984666' 'lente']
['0.3081781460867783' '4' '1.4369816054255053' 'lente']
['0.29534720251567403' '4' '1.4498125489966096' 'winter']

Now I know how to find the most common item in the entire array, like so

Counter(array.flat).most_common()

But for this one I need the most common item in the 4th column per cluster, which is the value in the second column, is there an easier way to do this besides making a long for loop and counting them all?


Solution

  • For some reason the solution suggested in the comments throws a ValueError. So here's an alternate solution using pandas:

    import pandas as pd
    
    data = [] #A nested list for data shown in your question
    df = pd.DataFrame(data,  columns = ['val1','cluster', 'val2','season']) #read your data into a dataframe
    def print_mode(group):
        print("{} - {}".format(group['cluster'].values[0], group['season'].mode().values))
        
    df.groupby('cluster').apply(print_mode)
    

    Sample output for your example data would be :

    0 - ['lente' 'winter']
    1 - ['winter']
    2 - ['herfst' 'lente']
    3 - ['lente']
    4 - ['lente']
    

    Instead of printing it, you can use it however you wish depending on your use-case.