Search code examples
pythonarrayspandasmachine-learningcategorical-data

From regression to classification : transform floats to categories


I have an array with the age of a list of people : [41, 50, 12, ... , 94]

I would like to map those onto 20 categories. Is there a way to automatically detects the best categories from the list ( [15,20,22,24,28,.., 90] for instance ?) and then map the existing array on it.


Solution

  • You can use pandas cut function and it will map the ages to the right bin:

    import pandas as pd
    ages = [41, 50, 12, 78,43, 94]
    bin_edges = [0,10,15,20,30,50,70,100]
    pd.cut(ages,bin_edges)