seher_2 0 Newbie Poster

i want to normalize these ingredients values to range of 0,1 my question is how to pass how dictionary to function? and recieving as a arguments

sample code

def normalize(d, target=1.0):
   raw = sum(d.values())
   factor = target/raw
   return {key:value*factor for key,value in d.iteritems()}
Use it like this:

>>> data = {'a': 0.2, 'b': 0.3, 'c': 1.5}
>>> normalize(data)
{'b': 0.15, 'c': 0.75, 'a': 0.1}


"""ho wto aplly sample code on my data, i want to normalize these ingredients values to range of 0,1 my question is how to pass how dictionary to function?

Ingredients={}
Ingredients={'Beef':{'Achari Qeema':1000,'Afghani Kebab':1000,'Aloo Gosht':0,'Beef Chilli':500,'Beef Fried Rice':340,'Beef Patties':1000,'Beef Qorma':1000,'Behari Boti':500},
             'Rice':{'Beef Fried Rice':200},
             'Salt':{'Achari Qeema':10,'Afghani Kebab':17,'Aloo Gosht':17,'Beef Chilli':0,'Beef Fried Rice':0,'Beef Patties':17,'Beef Qorma':17,'Behari Boti':4},
             'Oil':{'Afghani Kebab':200,'Aloo Gosht':74,'Beef Chilli':111,'Beef Fried Rice':41,'Beef Patties':41,'Beef Qorma':41,'Behari Boti':41},
             'Ghee':{'Achari Qeema':110},
             'Garlic':{'Behari Boti':15},
             'Onion':{'Achari Qeema':115,'Afghani Kebab':130,'Aloo Gosht':130,'Beef Patties':65,'Beef Qorma':260,'Behari Boti':65},
             'eggs':{'Beef Chilli':30,'Beef Fried Rice':50,'Beef Patties':100},
             'yogurt':{'Achari Qeema':245,'Beef Qorma':368,'Behari Boti':245},
             'ginger':{'Achari Qeema':15,'Aloo Gosht':15,'Beef Chilli':15,'Beef Qorma':15,'Behari Boti':15},
             'spring onions':{'Beef Chilli':45,'Beef Fried Rice':30},
             'Tomatoes':{'Afghani Kebab':1000},
             'Potatoes':{'Aloo Gosht':480,'Beef Patties':1000},
             'Green chillies':{'Afghani Kebab':40,'Aloo Gosht':12,'Beef Chilli':80,'Beef Patties':4},
             'chicken stock':{'Behari Boti':450}}

Dani AI

Generated

Two different things are commonly meant by "normalize to 0..1", and that choice determines how to pass and process 's nested Ingredients dict.

One option is proportion scaling (divide by the sum so values become fractions that add to 1). The sample in the thread does that for a flat dict. The other common option is min‑max scaling (map the smallest value to 0 and the largest to 1) — useful when you want each set of numbers to occupy the full 0..1 range. Because 's data is a nested dict (ingredient -> {recipe: amount}) decide whether to normalize each inner dict (per ingredient across recipes) or to pivot to a recipe-centric view (per recipe across ingredients) and normalize that.

Example: min‑max scale each inner dict (Python 3 style):

def minmax_per_ingredient(ingredients):
    out = {}
    for ing, recs in ingredients.items():
        vals = [float(v) for v in recs.values()]
        lo, hi = min(vals), max(vals)
        if hi == lo:
            out[ing] = {k: 0.0 for k in recs}
        else:
            out[ing] = {k: (v - lo) / (hi - lo) for k, v in recs.items()}
    return out

If the goal is to normalize each recipe's ingredient vector instead, first build a recipe->ingredient mapping and then apply the same per-dict scaler:

from collections import defaultdict

def build_recipes(ingredients):
    recipes = defaultdict(dict)
    for ing, recs in ingredients.items():
        for recipe, amt in recs.items():
            recipes[recipe][ing] = amt
    return dict(recipes)

Notes and troubleshooting:

  • Use items() in Python 3 (the thread's sample used iteritems() which is Python 2).
  • Convert values to float to avoid integer division.
  • Guard against constant vectors (hi == lo) to prevent division by zero.
  • Verify results with a quick assertion that all values lie in [0,1].
  • For larger datasets, pivoting to a pandas DataFrame and using sklearn.preprocessing.MinMaxScaler is often faster and clearer.

These patterns make it clear how to "pass a dictionary to a function" — call the scaler with the top-level Ingredients variable and choose the orientation (per ingredient or per recipe) that matches the analysis goal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.