Error in add_edges(g, from = X3_data$user_id_str[i], to = j) : argument "edges" is missing, with no default
时间: 2024-02-20 07:59:55 浏览: 172
这个错误通常是因为`add_edges()`函数需要一个`edges`参数,即要添加的边的列表。在你的代码中,你没有给`add_edges()`函数传递这个参数,导致出现了错误。
在这种情况下,你需要将边的信息存储在一个列表中,并将该列表作为`add_edges()`函数的`edges`参数传递给它。你可以使用下面的代码来构建这个列表:
```r
edges <- list()
for (i in 1:nrow(x3)) {
for (j in strsplit(x3$followingList[i], ";")[[1]]) {
edges <- c(edges, list(c(x3$user_id_str[i], j)))
}
}
```
然后,你可以将这个列表作为`add_edges()`函数的`edges`参数传递给它,如下所示:
```r
g <- add_edges(g, edges = edges)
```
这样就能够避免上述错误,并成功添加边到有向图中了。
相关问题
def minimum_cost_network(n,roads): G = nx.Graph() G.add_nodes_from(range(1,n+1)) G.add_weighted_edges_from(roads) min_tree = nx.minimum_spanning_tree(G) min_cost = sum(data["weight"] for u, v, data in min_tree.edges(data = True)) return min_cost
这是一个 Python 函数,它的作用是计算一个无向连通图的最小生成树的边权和。具体来说,它接受两个参数:一个整数 n 表示图中节点的数量,和一个包含 n-1 个元素的列表 roads,其中每个元素是一个三元组 (u, v, w),表示一条从节点 u 到节点 v 的有向边,边权为 w。函数使用 NetworkX 库构建了一个图对象 G,然后调用 NetworkX 库提供的 minimum_spanning_tree 函数计算最小生成树 min_tree,最后将 min_tree 的所有边的权值相加得到最小生成树的边权和 min_cost,然后将其返回。
import networkx as nxfrom sklearn.model_selection import train_test_splitfrom networkx.algorithms.link_prediction import katz_similarityfrom sklearn.metrics import roc_auc_score, average_precision_score# 加载数据集edges = []with open("email-Eu-core.txt") as f: for line in f: if line.startswith("#"): continue edge = line.strip().split() edges.append((int(edge[0]), int(edge[1])))# 划分训练集和测试集train_edges, test_edges = train_test_split(edges, test_size=0.1, random_state=42)# 构建无向图graph = nx.Graph()graph.add_edges_from(train_edges)# 计算相似度katz_scores = katz_similarity(graph, max_l=200, beta=0.01)# 计算AUC值y_true = [1 if edge in test_edges else 0 for edge in graph.edges()]y_scores = [katz_scores[edge] for edge in graph.edges()]auc = roc_auc_score(y_true, y_scores)# 计算Precisionk = 100top_k_edges = sorted(graph.edges(), key=lambda x: katz_scores[x], reverse=True)[:k]y_true = [1 if edge in test_edges else 0 for edge in top_k_edges]y_scores = [katz_scores[edge] for edge in top_k_edges]precision = average_precision_score(y_true, y_scores)print("Katz Algorithm")print("AUC: {:.4f}".format(auc))print("Precision@{}: {:.4f}".format(k, precision))有什么问题
这段代码中有一个语法错误,即第一行的 import 语句中没有正确地使用换行符。应该在 `import networkx as nx` 后添加一个换行符。
另外,代码中的 katz_similarity() 函数调用中使用了一个 max_l 参数,这个参数是不必要的,可以直接去掉,因为该函数不支持传递该参数。
最后,代码未定义变量 k,所以在计算 Precision 时会出错。您应该在代码开头定义一个 k 变量,或者将 k 的值硬编码到代码中。
下面是修改后的代码:
```python
import networkx as nx
from sklearn.model_selection import train_test_split
from networkx.algorithms.link_prediction import katz_similarity
from sklearn.metrics import roc_auc_score, average_precision_score
# 加载数据集
edges = []
with open("email-Eu-core.txt") as f:
for line in f:
if line.startswith("#"):
continue
edge = line.strip().split()
edges.append((int(edge[0]), int(edge[1])))
# 划分训练集和测试集
train_edges, test_edges = train_test_split(edges, test_size=0.1, random_state=42)
# 构建无向图
graph = nx.Graph()
graph.add_edges_from(train_edges)
# 计算相似度
katz_scores = katz_similarity(graph, beta=0.01)
# 计算AUC值
y_true = [1 if edge in test_edges else 0 for edge in graph.edges()]
y_scores = [katz_scores[edge] for edge in graph.edges()]
auc = roc_auc_score(y_true, y_scores)
# 计算Precision
k = 100
top_k_edges = sorted(graph.edges(), key=lambda x: katz_scores[x], reverse=True)[:k]
y_true = [1 if edge in test_edges else 0 for edge in top_k_edges]
y_scores = [katz_scores[edge] for edge in top_k_edges]
precision = average_precision_score(y_true, y_scores)
print("Katz Algorithm")
print("AUC: {:.4f}".format(auc))
print("Precision@{}: {:.4f}".format(k, precision))
```
请注意,这段代码依赖于 `email-Eu-core.txt` 数据集文件,您需要确保该文件存在并且与代码在同一目录下。
阅读全文