【问题标题】:How can a histogram be transformed into a variety of predefined constituent histograms?如何将直方图转换为各种预定义的组成直方图?
【发布时间】:2019-12-30 20:03:06
【问题描述】:

假设我想用多种食材制作具有特定营养成分的餐点:

meal = a * ingredient_1 + b * ingredient_2 + c * ingredient_3

有点像傅里叶变换如何将波形转换为正弦波的组合,如何将一顿饭(营养的直方图)转换为成分的组合(其他营养的直方图),使得一组参数是否可以提取描述每种成分的数量以用于获得最佳“适合”膳食?

因此,例如,在 Pandas DataFrame 中,我们有一顿饭和各种食材:

import pandas as pd

df = pd.DataFrame(
         [
                    [        'meal',    1625.14,    90.9,   47.214,  49.962,    138.16,  6.726,           6.606,  28.858],
                    ['ingredient_1',        109,    13.4,        4,     0.9,       4.4,   1.75,             0.3,     134],
                    ['ingredient_2',        126,     0.8,      3.1,      19,       0.5,    0.4,             0.4,     0.4],
                    ['ingredient_3',         35,       8,      0.1,     6.6,         1,    0.1,             0.1,     6.2],
         ],
         columns = [
                             'name', 'calories', 'carbs',    'fat', 'fiber', 'protein', 'salt', 'saturated-fat', 'sugar'
         ]
)

【问题讨论】:

    标签: pandas dataframe histogram fft


    【解决方案1】:

    我猜你想要这样的东西:

    #A
    a1=df.loc[df.index.values.tolist()[1:],'calories'].tolist()
    a2=df.loc[df.index.values.tolist()[1:],'carbs'].tolist()
    a3=df.loc[df.index.values.tolist()[1:],'fat'].tolist()
    A=np.array([a1,a2,a3])
    #B
    b1=df.loc[df.index.values.tolist()[0],'calories'].tolist()
    b2=df.loc[df.index.values.tolist()[0],'carbs'].tolist()
    b3=df.loc[df.index.values.tolist()[0],'fat'].tolist()
    B=np.array([b1,b2,b3])
    #Solve
    X = np.linalg.solve(A,B)
    df['Quantity']=[X.sum()]+X.tolist()
    df_prop=df.loc[1:,['name','Quantity']].reset_index(drop=True).set_index('name')
    #Libraries
    import matplotlib.pyplot as plt
    #for use Jupyter Notebook
    %matplotlib inline 
    #Draw
    ax=df_prop.plot(kind='bar',figsize=(15,15),fontsize=20)
    ax.legend(fontsize=20)
    

    输出:

    X 包含每种成分的数量。如果您有任何问题,请不要犹豫!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2017-09-16
      • 2014-04-05
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多