为什么在图形交叉之后缺少节点 - 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
最满意答案
您不必一遍又一遍地包括
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);
相关问答
更多-
WebSocket连接正在关闭(WebSocket connection is closing)[2023-03-04]
在任何规范下,这都不是有效的Web套接字响应。 最初的Web套接字响应总是要求您将一些数字作为响应头的一部分来处理,以证明您是一个Web套接字服务器。 要读取和写入哪些标头以及要执行哪些操作取决于web-socket的版本(hibi / hixie 76 / rfc)。 它实际上看起来像您的服务器正在使用客户端的标头。 例如,RFC6455(13)响应将开始: HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Se ... -
您应该真的使用Amazon的Redshift特定JDBC驱动程序: http : //docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html 如果要使用通用Postgres驱动程序,则需要在DataGrip连接的高级选项中设置tcpKeepAlive=true 。 You should really use Amazon's Redshift specific JDBC driver: http://docs.aws.a ...
-
laravel mongodb关闭客户端连接剩余数据太小(laravel mongodb closing client connection remaining data too small)[2023-05-23]
出现此问题( Client Error: Remaining data too small for BSON object )至少有两个原因: 1. PHP MongoDB驱动程序与机器上安装的MongoDB不兼容。 (最初在第一个答案中提到)。 检查作为提示,这相当于找到数组中增长最长的子序列(你知道为什么吗?)因为这是一个已知O(n log n)解决方案的标准算法,你应该能够通过稍加修改来解决问题LIS。 希望这可以帮助! As a hint, this is equivalent to finding the longest increasing subsequence of the array (do you see why?) Since that's a standard algorithm with known O(n log n) sol ...您不必一遍又一遍地包括connect.php文件。 假设您已经将它包含在顶部的某个位置,因为您运行$AddBook->execute() 看到这个更新的代码: $AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())"); $AddBook->bind_param('iiis',$PerfID, $NumAdults, $ ...猫鼬连接保持关闭(Mongoose Connection Keeps Closing)[2022-06-27]
检查MongoDB的日志文件,看它是否遇到任何导致它终止连接的问题。 正如您所提到的,如果日志中没有任何内容,重新启动MongoDB将是一件好事。 Check MongoDB's log file to see if it's hitting any issues that are making it kill your connection. As you mention, restarting MongoDB would be a good thing to try if there's nothing ...在C中关闭连接后,从函数中的MySQL查询返回结果(Return a result from a MySQL query in a function after closing connection in C)[2021-09-07]
cast retvalue = (int)row[0]错误。 它将结果的地址(转换为int)放在retvalue 。 row[0]是一个char* ,所以你应该使用retvalue=atoi(row[0]) 。 The cast retvalue = (int)row[0] is wrong. It puts the address of the result (converted to int) in retvalue. row[0] is a char*, so you should use retva ...我相信Oracle的Connection类方法是同步的。 看到这个API描述 。 这将是使用相同连接的工件,而不是可配置属性。 如果您需要解决此限制,您可以使用2个连接或查看连接池,如果您需要更灵活的解决方案。 I believe that Oracle's Connection class methods are synchronized. see this api description. This would then be an artifact of using the same connecti ...包装器是一个好主意,但我不知道任何API可靠地检查连接是否关闭。 所以解决方案将沿着这些方向发展: for retry in range(3): try: ... execute query ... return # Stop on success except e: if is_connection_broken_error(e): reconnect() continue ...与Firebase服务器的连接将在第一次建立连接时保持打开状态,直到应用程序(或浏览器窗口/选项卡)关闭或显式调用goOffline() 。 我检查了AngularFire代码 ,它从不调用goOffline() 。 当你调用$destroy()它会调用off() 。 这将取消listen / subscription,因此您不会再接收更新的数据。 但这种联系仍然是开放的。 The connection to Firebase servers will remain open from the first ...相关文章
更多- Python资源索引 【转载】
- 【转帖】Python 资源索引
- 在Hadoop集群上运行R程序--安装RHadoop
- python top project of 2013
- Python:渗透测试开源项目【源码值得精读】
- python2和python3的区别
- python下载pps视频
- Hadoop删除节点(Decommissioning Nodes)
- R语言实战视频教程-尚学堂视频教程
- R简单数据分析
最新问答
更多- 您如何使用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)