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
最满意答案
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.
相关问答
更多-
安装RMagick Gem(Installing RMagick Gem)[2023-09-04]
使用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 ... -
在Ubuntu上安装rmagick(Installing rmagick on Ubuntu)[2023-12-22]
安装软件包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.
-
PIL vs RMagick / ruby-gd(PIL vs RMagick/ruby-gd)[2023-01-04]
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 ...
-
理解ImageMagick的转换并转换为Ruby RMagick(Understanding ImageMagick's convert and translating to Ruby RMagick)[2022-04-02]
代码检查特定图像是否包含透明度。 -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 ...