You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据指定数据框的值调整加权邻接矩阵的权重?

How to Update Weighted Adjacency Matrix Using Values from a Data Frame

Got it, let's work through this step by step. The main challenges here are aligning the node name formats between your data frame and adjacency matrix, then correctly mapping the weight values to the right positions in the matrix. Here's a feasible, reproducible solution:

Step 1: Fix Node Name Format Mismatch

First, notice your data frame uses reversed node names (like fgh_a instead of the adjacency matrix's a_fgh). We'll write a helper function to flip these names to match the matrix's convention:

# Helper function to reverse underscore-separated node names
reverse_node_name <- function(node_str) {
  parts <- strsplit(node_str, "_")[[1]]
  paste(rev(parts), collapse = "_")
}

# Apply the function to fix name1 and name2 in your data frame
table$name1_fixed <- sapply(table$name1, reverse_node_name)
table$name2_fixed <- sapply(table$name2, reverse_node_name)

Step 2: Choose Which Weight Column to Use

Your data frame has columns for different value ranges (e.g., (3,4.5), (6,7.5)). Pick which column's values you want to use for updating the matrix. For this example, we'll use (6,7.5)—replace this with your desired column name later.

Step 3: Update the Adjacency Matrix

Loop through each row of the data frame, check if the fixed node pair exists in the adjacency matrix, and update the corresponding weights. Since your original matrix is symmetric (undirected graph), we'll update both (node1, node2) and (node2, node1) positions:

# Define the column with weights you want to apply
target_weight_col <- "(6,7.5)"

# Iterate over each row to update the matrix
for (i in 1:nrow(table)) {
  node1 <- table$name1_fixed[i]
  node2 <- table$name2_fixed[i]
  
  # Check if both nodes exist in the adjacency matrix
  if (node1 %in% rownames(adjMat) && node2 %in% colnames(adjMat)) {
    # Update symmetric positions for undirected graph
    adjMat[node1, node2] <- table[[target_weight_col]][i]
    adjMat[node2, node1] <- table[[target_weight_col]][i]
    cat(paste("Updated weight for", node1, "-", node2, "to", table[[target_weight_col]][i], "\n"))
  } else {
    # Warn if nodes don't exist (e.g., opu_p isn't in your adjacency matrix)
    warning(paste("Skipping row", i, ": Node pair", node1, "-", node2, "not found in adjacency matrix"))
  }
}

Step 4: Verify the Updated Matrix

After running the code, check the result to confirm changes:

print(adjMat)

Key Notes

  • Directed vs Undirected: If your graph is directed (weights differ by direction), remove the line that updates adjMat[node2, node1].
  • Extra Rows/Columns: Your original data frame has a row (dfg_c opu_p) with an extra value—this will be ignored automatically since opu_p isn't in your adjacency matrix.
  • Negative Weights: Your data frame includes negative values—this is totally fine if your use case allows negative weights in the adjacency matrix.

内容的提问来源于stack exchange,提问作者minoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:30:20