谨慎设计的质量

2024-09-22 16:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个不同类型的容器表(df_1)。我有另一张表,上面列出了它们所包含的内容(df_2)。我想评估df_1的哪些行更有可能被归类为其真实类型,这取决于它们所包含的内容是否是该类型容器的典型内容

df_1 = pd.DataFrame({'Container' : [1,2,3,4,5,6,7,8],
                          'Type' : ['Box','Bag','Bin','Bag','Bin','Box','Bag','Bin']})

df_2 = pd.DataFrame({'Container' : [1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,7,7,7,8],
                          'Item' : ['Ball','Ball','Brain','Ball','Ball','Baloon','Brain','Ball','Ball','Baloon','Brain','Ball','Ball','Baloon','Brain','Ball','Ball','Baloon','Bomb','Ball','Ball','Baloon','Brain','Ball','Ball','Bomb']})

Tags: box类型内容dataframedfbincontainer容器
1条回答
网友
1楼 · 发布于 2024-09-22 16:27:02

以下方法考虑每个容器的内容是否为该类型的典型内容。它对在其他容器中发现的物品(阳性)和在其他容器中未发现的物品(阴性)给予同等的重量。它忽略在其他容器中找到项目的频率。它还忽略了内容物是否是另一种容器的典型。 我认为这种方法会扩大规模

# List of how typical the contents of each container are given the type of container
x = []

# Join
df_J = df_1 .set_index('Container').join(df_2 .set_index('Container'))
df_J['Container'] = df_J.index
df_J.index = range(len(df_J.index))
df_J ['Thing'] = 1

# Type of each container
Q_B = pd.DataFrame(df_1.Container).set_index('Container')
Q_B['Type'] = df_1.set_index('Container').Type
Di_Q_B = dict(zip(Q_B.index, Q_B.Type))

# Compare each container against all of the other containers
for Container in df_1.Container:

    # Test data: Everything in the container
    Te_C = df_2[df_2['Container'] == Container]
    del Te_C['Container']

    # Everything in all of the other containers
    Tr_C = df_J[df_J['Container'] != Container]

    # Training data: Everything in all of the other containers of that type
    Tr_E = Tr_C[Tr_C['Type'] == Di_Q_B[Container]]

    # Table of how many of each item is in each container
    S_Tr = pd.pivot_table(Tr_E, values='Thing', index=Tr_E.Container, columns='Item', aggfunc=np.sum).fillna(0)

    # Table of whether each item is in each container
    Q_Tr = S_Tr.apply(np.sign)

    # Table of how many containers in the training data contain each item
    X_Tr = Q_Tr.sum(axis=0)
    Y_Tr = pd.DataFrame(X_Tr)

    # Table of whether any containers in the training data contain each item
    Z_Tr = Y_Tr.apply(np .sign)

    # List of which items are in the training data
    Train = list(Z_Tr.index)

    # Identify which of the items in the container are typical
    Te_C['Typical'] = Te_C['Item'].map(lambda a: a in Train)

    # Count how many typical items are in the container
    u = Te_C['Typical'].sum()

    # Count how many atypical items items are in the container
    v = len(Te_C.index) - u

    # Gauge how typical the contents of the container are (giving equal weight to typical and atypical items)
    w = u - v
    x.append(w)

# How typical the contents of each container are given the type of container
df_1['Pa_Ty'] = x

相关问题 更多 >