首页 \ 问答 \ Python中单词列表中的同现矩阵(Co-occurrence Matrix from list of words in Python)

Python中单词列表中的同现矩阵(Co-occurrence Matrix from list of words in Python)

我有一个名称列表,如:

names = ['A', 'B', 'C', 'D']

和一份文件清单,在每份文件中都提到了其中一些名称。

document =[['A', 'B'], ['C', 'B', 'K'],['A', 'B', 'C', 'D', 'Z']]

我想获得一个输出作为共现的矩阵,如:

  A  B  C  D
A 0  2  1  1
B 2  0  2  1
C 1  2  0  1
D 1  1  1  0

在R中有一个针对这个问题的解决方案( 创建共现矩阵 ),但是我不能用Python来完成。 我正在考虑在熊猫中这样做,但还没有进展!


I have a list of names like:

names = ['A', 'B', 'C', 'D']

and a list of documents, that in each documents some of these names are mentioned.

document =[['A', 'B'], ['C', 'B', 'K'],['A', 'B', 'C', 'D', 'Z']]

I would like to get an output as a matrix of co-occurrences like:

  A  B  C  D
A 0  2  1  1
B 2  0  2  1
C 1  2  0  1
D 1  1  1  0

There is a solution (Creating co-occurrence matrix) for this problem in R, but I couldn't do it in Python. I am thinking of doing it in Pandas, but yet no progress!


原文:https://stackoverflow.com/questions/42814452
更新时间:2022-05-28 16:05

最满意答案

ImageMagic是一个庞大的图书馆,它将在阳光下做所有事情,但许多人报告RMagick版本的内存问题,我个人认为这对我的需求是一种矫枉过正。

正如你所说的,当涉及到英文文档时,ruby-gd在地面上是有点薄的......但是GD是在post平台上安装的一个小软件,并且有一些包含一些有用的例子,叫做gruby,值得一看。 (如果你在alpha透明之后,请确保你安装了最新的GD lib)

对于整体社区博客帮助,PIL就是这样。


ImageMagic is a huge library and will do everything under the sun, but many report memory issues with the RMagick variant and I have personally found it to be an overkill for my needs.

As you say ruby-gd is a little thin on the ground when it comes to English documentation.... but GD is a doddle to install on post platforms and there is a little wrapper with some helpful examples called gruby thats worth a look. (If you're after alpha transparency make sure you install the latest GD lib)

For overall community blogy help, PIL's the way.

相关问答

更多
  • 使用rvm来安装ruby(首选)或使用apt-get install ruby-ruby-dev aptitude install build-essential imagemagick libmagickcore-dev libmagickwand-dev gem install rmagick - 编辑1- 您还需要ruby-dev(或ruby-full)才能在gem安装之前成功编译RMagick。 use rvm to install ruby (preferred) or use apt-get ...
  • 安装软件包imagemagick和libmagickwand-dev (或libmagick9-dev )。 您应该可以安装Rmagick宝石。 sudo apt-get install imagemagick libmagickwand-dev 如果没有,你会丢失ruby相关的开发包,如build-essential和ruby1.8-dev 。 如果是这样,谷歌中的通用“ubuntu安装ruby 1.8”查询应该排除你。 Install the packages imagemagick and libma ...
  • magick-installer脚本( https://github.com/maddox/magick-installer )在Mac上安装ImageMagick方面做得非常出色。 当我不使用此脚本进行安装时,安装似乎会成功,但该gem永远无法找到安装。 使用这个脚本,我成功地安装了以前多次失败的安装。 祝你好运 :) The magick-installer script ( https://github.com/maddox/magick-installer ) does a fantastic jo ...
  • 我在Ruby 1.8.7中遇到了和1.9.1一样的问题。 这是我解决它的一种方式: 卸载rmagick gem(如果有的话)(gem uninstall rmagick) 下载http://rubyforge.org/frs/download.php/64917/RMagick-2.12.0-ImageMagick-6.5.6-8-Q8.zip 解压zip文件夹(例如C:\ temp \ rmagick) 安装ImageMagick-6.5.6-8-Q8-windows-dll.exe 解压缩rmagick ...
  • 我不得不使用32位安装而不是64位安装。 感谢Google Groups的Rubyinstaller的优秀人员指出我正确的方向。 I had to use the 32bit installation instead of the 64bit. Thanks to the good folks of Rubyinstaller at Google Groups to point me in the right direction.
  • ImageMagic是一个庞大的图书馆,它将在阳光下做所有事情,但许多人报告RMagick版本的内存问题,我个人认为这对我的需求是一种矫枉过正。 正如你所说的,当涉及到英文文档时,ruby-gd在地面上是有点薄的......但是GD是在post平台上安装的一个小软件,并且有一些包含一些有用的例子,叫做gruby,值得一看。 (如果你在alpha透明之后,请确保你安装了最新的GD lib) 对于整体社区博客帮助,PIL就是这样。 ImageMagic is a huge library and will do ...
  • 在旋转之前将背景设置为无: overlay.background_color = "none" 轮换后使用的其他可能方法: img.transparent_chroma(low, high, opacity=TransparentOpacity, invert=false) img.transparent(color, opacity=TransparentOpacity) 在你的情况下: overlay.transparent!("white") Before rotating set your b ...
  • 代码检查特定图像是否包含透明度。 -format '%[fx:ua]' info: 这指示图像magick检查第一个图像u ,a的alpha通道并在其上输出信息,如果左上角像素是透明的,它将返回0,如果不是,我将返回非0。 这就是为什么图像被调整为1x1的原因,因此只需要咨询一个像素。 -channel o是不透明度通道。 因此,英文代码将读取,遍历所有PNG文件,仅查看alpha通道(不透明度),调整大小为单个像素并查看它是否透明。 因此回声消息。 不幸的是我不知道Ruby或RMagick,但是快速查看A ...
  • 我想你缺少libmagickcore-dev,这将允许你成功编译rmagick。 请尝试以下方法: RUN apt-get install imagemagick libmagickcore-dev libmagickwand-dev I think you are missing libmagickcore-dev which will allow you to successfully compile rmagick. Try the following: RUN apt-get install im ...
  • 我无法让Helvetica看起来像Rmagick一样大胆。 您最好的选择可能是下载更大胆的字体 ,将其复制到您的Rails项目并告诉RMagick使用它: txt.font = "#{Rails.root}/lib/fonts/[font filename].ttf" 如果你非常小心,你可以使文字更大胆(但可能更丑): txt.stroke_width = 1 # <-- adjust this parameter txt.stroke = "#ffffff" txt.stroke_antialias t ...

相关文章

更多

最新问答

更多
  • 您如何使用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)