首页 \ 问答 \ 为什么在图形交叉之后缺少节点 - NetworkX,igraph,python和r(Why there are missing nodes after graph intersection - NetworkX, igraph, python and r)

为什么在图形交叉之后缺少节点 - NetworkX,igraph,python和r(Why there are missing nodes after graph intersection - NetworkX, igraph, python and r)

在尝试获取两个网络/图形之间的交集时,我遇到了一些奇怪的事情。 当我检查结果交叉点时,我发现缺少节点,我希望了解为什么会发生这种情况。

最初我正在使用python 3.5.2 / pandas 0.17.1。 在Linux Mint 18上,重现问题的数据集和代码在链接上: 数据集和代码

两个表(链接中附带的Test_01.ncol和Test_02.ncol)都是边缘列表。

首先,我尝试使用合并功能获得两个带有pandas的图表的交集:

import pandas as pd

# Load graphs
test_01 = pd.read_csv("Test_01.ncol",sep=" ") # Load Net 1
test_02 = pd.read_csv("Test_02.ncol",sep=" ") # Load Net 2
pandas_intersect = pd.merge(test_01, test_02, how='inner', on=['i1', 'i2']) # Intersection by column

pandas_nodes = len(set(pandas_intersect['i1'].tolist() + pandas_intersect['i2'].tolist())) # Store the number of nodes

然后检查合并是否没有问题我将结果的节点数与NetworkX交集的结果节点进行了比较,如下所示:

# Now test with NetworkX
import networkx as nx
n1 = nx.from_pandas_dataframe(test_01, source="i1", target="i2") # Transform net 1 in NetworkX Graph
n2 = nx.from_pandas_dataframe(test_02, source="i1", target="i2") # Transform net 2 in NetworkX Graph
fn = nx.intersection(n1,n2)  # NetworkX Intersection

networkx_nodes = len(fn.nodes()) # Store the number of nodes

# The number of nodes are different!!!
pandas_nodes == networkx_nodes

我认为它可能是节点顺序的东西,这在附加的表中不是规范的,但即使我按规范顺序放置两个数据集,也会丢失节点。

我的下一个假设是它可能是Pandas或NetworkX中的一个错误,所以我在R(版本3.3.2)和igraph(版本1.0.1)中尝试它:

library("igraph")

# Read Tables
g1 <- read.table("Test_01.ncol",header=TRUE)
g2 <- read.table("Test_02.ncol",header=TRUE)

# Transform Tables in Graphs
g1 <- graph_from_data_frame(g1, directed=FALSE)
g2 <- graph_from_data_frame(g2, directed=FALSE)

# Create igraph interssection
gi <- graph.intersection(g1,g2)

# Save graph intersection
write.graph(gi,"Test_igraph_intersection.ncol", format="ncol")

# Reload graph intersection
gi_r <- read.graph("Test_igraph_intersection.ncol",format="ncol")

# Prepare result summary
Methods <- c("igraph_intersection","pandas_table_intersection")
Vertex_counts <- c(vcount(gi),vcount(gi_r))
Edge_counts <- c(ecount(gi),ecount(gi_r))

# Create Summary Table
info_data = data.frame(Methods, Vertex_counts, Edge_counts)
colnames(info_data) <- c("Method","Vertices","Edges")

# Check info_data
info_data

但是当我看一下info_data时,结果是一样的。

我知道节点的数量可能会因为交叉过程而减少,但为什么会在我在python上再次将其转换为表格式并保存文件然后再次使用igraph加载之后发生这种情况? 或者我做错了什么?

如果有人可以解释在python中发生的事情或RI欣赏。 我真的需要理解为什么会这样,如果我能相信这些交叉点继续我的工作。


I'm experiencing something strange while trying to obtain the intersection between two networks/graphs. I found missing nodes when I check the resulting intersection and I wish to understand why this is happening.

Originally I'm working with python 3.5.2 / pandas 0.17.1. on Linux Mint 18, and the dataset and code to reproduce the problem is on the link: Dataset and code

Both tables (Test_01.ncol and Test_02.ncol attached in the link) are edge lists.

First I try to get the intersection of two graph tables with pandas, with merge function:

import pandas as pd

# Load graphs
test_01 = pd.read_csv("Test_01.ncol",sep=" ") # Load Net 1
test_02 = pd.read_csv("Test_02.ncol",sep=" ") # Load Net 2
pandas_intersect = pd.merge(test_01, test_02, how='inner', on=['i1', 'i2']) # Intersection by column

pandas_nodes = len(set(pandas_intersect['i1'].tolist() + pandas_intersect['i2'].tolist())) # Store the number of nodes

And then to check if the merge was done without problems I compared the resulting number of nodes with the resulting nodes of NetworkX intersection as follows:

# Now test with NetworkX
import networkx as nx
n1 = nx.from_pandas_dataframe(test_01, source="i1", target="i2") # Transform net 1 in NetworkX Graph
n2 = nx.from_pandas_dataframe(test_02, source="i1", target="i2") # Transform net 2 in NetworkX Graph
fn = nx.intersection(n1,n2)  # NetworkX Intersection

networkx_nodes = len(fn.nodes()) # Store the number of nodes

# The number of nodes are different!!!
pandas_nodes == networkx_nodes

I thought it might be something with the order of nodes, which is not canonical in the tables attached, but even when I put the two dataset on canonical order there are missing nodes.

My next hypothesis is that it might a bug in Pandas or NetworkX, so I try it in R (version 3.3.2) and igraph (version 1.0.1):

library("igraph")

# Read Tables
g1 <- read.table("Test_01.ncol",header=TRUE)
g2 <- read.table("Test_02.ncol",header=TRUE)

# Transform Tables in Graphs
g1 <- graph_from_data_frame(g1, directed=FALSE)
g2 <- graph_from_data_frame(g2, directed=FALSE)

# Create igraph interssection
gi <- graph.intersection(g1,g2)

# Save graph intersection
write.graph(gi,"Test_igraph_intersection.ncol", format="ncol")

# Reload graph intersection
gi_r <- read.graph("Test_igraph_intersection.ncol",format="ncol")

# Prepare result summary
Methods <- c("igraph_intersection","pandas_table_intersection")
Vertex_counts <- c(vcount(gi),vcount(gi_r))
Edge_counts <- c(ecount(gi),ecount(gi_r))

# Create Summary Table
info_data = data.frame(Methods, Vertex_counts, Edge_counts)
colnames(info_data) <- c("Method","Vertices","Edges")

# Check info_data
info_data

But when I take a look into info_data the result was the same.

I understand that the number of nodes may decrease because of intersection procedure, but why this is happening just after I convert it to table format again on python and after save the file and then loading it again with igraph? Or I'm doing something wrong?

If someone can explain whats happening in python or R I appreciate. I really need to understand why this is happening and if I can trust in these intersections to continue my work.


原文:https://stackoverflow.com/questions/42516341
更新时间:2022-10-13 12:10

最满意答案

您不必一遍又一遍地包括connect.php文件。 假设您已经将它包含在顶部的某个位置,因为您运行$AddBook->execute()

看到这个更新的代码:

$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
    //----------------DEDUCT SEATS--------------//
    $SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
    $SeatDeduction->bind_param('i',$PerfID);
    $SeatDeduction->execute();

    $SeatDeduction->close();
    $conn->next_result();

    // mysqli_close($conn); // remove this
    // require ('connect.php'); // remove this
    //-----------------GET BOOK ID--------------//
    $getBookID = "CALL getBookByUserName('$UserName')";
    $result2 = mysqli_query($conn, $getBookID);
    $Output2 = mysqli_fetch_assoc($result2);
    $BookID = $Output2['BookID'];
    // mysqli_close($conn); remove this
    // require ('connect.php');  remove this
    //-------------------SET COST---------------//
    // ...
    //-------------------GET COST---------------//
    // ...
    //---------------BOOKING CONFIRM------------//
    // ...
}
else
{
    die(mysqli_error($conn));
}

mysqli_close($conn);

You don't have to keep including the connect.php file over and over. Assuming you already included it somewhere at the top since you run $AddBook->execute()

See this updated code:

$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
    //----------------DEDUCT SEATS--------------//
    $SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
    $SeatDeduction->bind_param('i',$PerfID);
    $SeatDeduction->execute();

    $SeatDeduction->close();
    $conn->next_result();

    // mysqli_close($conn); // remove this
    // require ('connect.php'); // remove this
    //-----------------GET BOOK ID--------------//
    $getBookID = "CALL getBookByUserName('$UserName')";
    $result2 = mysqli_query($conn, $getBookID);
    $Output2 = mysqli_fetch_assoc($result2);
    $BookID = $Output2['BookID'];
    // mysqli_close($conn); remove this
    // require ('connect.php');  remove this
    //-------------------SET COST---------------//
    // ...
    //-------------------GET COST---------------//
    // ...
    //---------------BOOKING CONFIRM------------//
    // ...
}
else
{
    die(mysqli_error($conn));
}

mysqli_close($conn);

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)