小编Cur*_*tLH的帖子

使用 Bokeh 按 Networkx 节点属性对节点进行着色

我正在尝试使用散景来创建交互式网络可视化。我了解如何将属性数据添加到散景图,但我不确定如何根据节点属性分配填充颜色。

我一直在关注我能找到的所有散景示例,但我似乎无法弄清楚。

如何调整下面的代码以按 NetworkX 节点属性对节点进行着色?

import networkx as nx
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
from bokeh.models import Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx

output_notebook()

# create a sample graph
G = nx.karate_club_graph()

# create the plot
plot = figure(x_range=(-1.1, 1.1), y_range=(-1.1, 1.1))

# add tools to the plot
plot.add_tools(HoverTool(tooltips=[("Name", "@name"), 
                                   ("Club", "@club")]), 
               TapTool(), 
               BoxSelectTool())

# create bokeh graph
graph = from_networkx(G, nx.spring_layout, iterations=1000, scale=1, center=(0,0))

# add name to node data
graph.node_renderer.data_source.data['name'] …
Run Code Online (Sandbox Code Playgroud)

python networkx bokeh

4
推荐指数
1
解决办法
3509
查看次数

将带有日期值的列表加载到pandas数据帧中,并绘制一段时间内的活动

我有一些Twitter数据,我想根据推文的类型(推文/提及/转推)加班加点活动.

数据当前被加载到包含 date 的元组列表中type:

time = [('2014-04-13', 'tweet'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'retweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'retweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'retweet'),
        ('2014-04-13', 'retweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'tweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'retweet'),
        ('2014-04-13', 'mention'),
        ('2014-04-13', 'tweet')]
Run Code Online (Sandbox Code Playgroud)

我已将数据加载到pandas DataFrame中:

time_df = pd.DataFrame(time, columns=['date','time'])
Run Code Online (Sandbox Code Playgroud)

现在数据看起来像这样:

         date     time
0  2014-04-13    tweet
1  2014-04-13    tweet
2  2014-04-13  mention
3  2014-04-13  retweet
4  2014-04-13  mention
...
...
...
Run Code Online (Sandbox Code Playgroud)

但是,现在我在绘制这些数据时会迷失方向.另外,我想将每种类型(推文/提及/转推)分解为不同的颜色线.我还应该注意,有时我可能需要按日/周/月汇总数据.

理想情况下,我希望我的情节看起来类似于下面的情节,除了Tweet,Mention,Retweet:

http://pandas.pydata.org/pandas-docs/stable/visualization.html

python time-series pandas

3
推荐指数
1
解决办法
1251
查看次数

在networkx中循环连接的组件并提取包含某些节点的组件

我有一个非常大的无向网络加载到graph()由许多断开连接的组件组成的NetworkX 中。我还将一组感兴趣的节点加载到一组中。我想浏览所有提取的所有组件,其中至少包含感兴趣的节点之一。

# create empty graph
g = nx.Graph()

# add edges to the graph
g.add_edges_from([['a','b'],['a','c'],['b','c'],['d','e'],['e','f'],['d','f'],['g','h'],['g','i'],['h','i']])

# load nodes of interest into a set
interest_nodes = set(['a', 'b', 'f'])

# number of connected components
nx.number_connected_components(g)

# loop through each connected component and add all of the edges for that component to a list if a node in that component is found in the interest_nodes
interest_edges = []
for i in nx.connected_component_subgraph(g):
    for u in i.edges():
        if u in …
Run Code Online (Sandbox Code Playgroud)

python for-loop networkx

3
推荐指数
1
解决办法
1937
查看次数

将数据列表(表示边)加载到python中的igraph图中

我有一些关系数据,其格式为我想要导入的列表列表iGraph.Graph().列表列表包含重复的边,最终,我想为重复边添加边权重.然而,目前,当我试图将图形边缘添加到减去权重因子时,我无法弄清楚我做错了什么.

我认为问题是我必须首先将所有顶点导入到图形中,然后我可以在顶点之间添加边缘,但似乎并非如此.

将这些边加载到图中我做错了什么?

*如何修改边缘加载过程以首先查找图形中的边缘,如果未找到,则添加边缘,如果找到,则将该边缘的权重增加1,*

数据

In [60]:    edges

Out[60]:    [['a', 'b'],
             ['a', 'b'],
             ['a', 'b'],
             ['b', 'a'],
             ['a', 'c'],
             ['c', 'a'],
             ['c', 'd'],
             ['c', 'd'],
             ['d', 'c'],
             ['d', 'c']]
Run Code Online (Sandbox Code Playgroud)

IGRAPH代码将图形加载到图形和错误中

In [61]:    # extract vertices for edges list
            vertices = []
            for line in edges:
                nodes.append(line[0])
                nodes.append(line[1])

            # find unique vertices
            uniq_vertices = set(sorted(nodes))

In [62]:    # create an empty graph
            g = igraph.Graph()

In [63]:    # add vertices to the graph
            g.add_vertices(uniq_vertices)

In [64]:    # for each …
Run Code Online (Sandbox Code Playgroud)

python igraph

3
推荐指数
1
解决办法
2120
查看次数

将 pytest 固定装置放在一个位置并跨子模块使用它们

我正在构建一个 python 包,pytest用于所有单元测试。我的包由几个模块组成,每个模块下都有各种子模块。单元测试位于test每个模块的文件夹中(例如,./tumble/tumble/math/test/test_multiply.py./tumble/tumble/science/test/test_divide.py

我有一些装置想要在所有模块和子模块中共享。因此,./tumble/tumble/test在本例中,我希望将它们放置在一个中心位置,并且每个子模块(math/testscience/test)中都没有重复的装置。

如果我放置在每个子模块conftest.pytest文件夹中,一切都会按预期工作。但是,我在两个位置使用相同的灯具,这并不理想。

当我将我的装置放在一个中心位置时,我可以在使用命令时看到它们pytest --fixtures,但是,当我运行时pytest,它告诉我fixture not found并且该装置没有在available fixtures.

我是否需要将所有单元测试移动到test文件夹下,或者我可以做些什么来将单元测试保留在子模块中,但固定装置位于中心位置?

tumble
+-- setup.py
+-- README.md
+-- tumble
|   +-- math
|   |   +-- __init__.py
|   |   +-- multiply.py
|   |   +-- test
|   |   +-- __init__.py
|   |   |   +-- test
|   |   |   |   +-- __init__.py
|   |   |   | …
Run Code Online (Sandbox Code Playgroud)

python unit-testing fixtures pytest

3
推荐指数
1
解决办法
2744
查看次数

NetworkX DiGraph() 到 Graph() 的边权重未求和,如何求和?

我有一些关系数据想要加载到 NetworkX 中,并最终将其转换为加权图。

本质上,关系边是有向和加权的,我想在转换图形时保留权重属性。使用以下代码,我已经能够将关系边从字典加载到MultiDiGraph()

MG = nx.MultiDiGraph([(i['source'],i['target']) for i in edges ])
Run Code Online (Sandbox Code Playgroud)

然后,我将 转换MultiDiGraph()为 a DiGraph(),并将重复的边压缩为一条,并更新每条边的边权重:

G = nx.DiGraph()
for (u,v) in MG.edges():
    G.add_edge(u, v, weight=len(MG[u][v]))
Run Code Online (Sandbox Code Playgroud)

从这里,我想将 转换DiGraph()为 a Graph(),并再次保留并压缩边权重:

g = G.to_undirected()
Run Code Online (Sandbox Code Playgroud)

但我遇到的问题是,它似乎保留了为'a' -> 'b'or找到的第一个边权重'b' -> 'a'

我想要的是在到达无向边缘时将这些边缘的总和保持为权重。

下面是一个示例,展示了我正在使用的内容:

# relational directed edge data containing duplicate edges 
edges = [{'source': 'a', 'target': 'b'},
         {'source': 'a', 'target': 'b'},
         {'source': 'a', 'target': 'b'},
         {'source': 'b', 'target': 'a'},
         {'source': 'a', …
Run Code Online (Sandbox Code Playgroud)

python networkx

2
推荐指数
1
解决办法
3799
查看次数

按日期时间间隔计算记录数

我将一些数据加载到Pandas DataFrame我想要聚合到日期时间间隔的数据中,并计算每个间隔内的记录数.问题是我发现聚合到日期时间间隔并计算每个间隔内的记录数的方法看起来相当笨重,可能不是最有效的.更改我想要分组的间隔以计算推文的数量也是一种痛苦.

data = [[Timestamp('2016-10-26 18:47:53'), 'mention'],
        [Timestamp('2016-10-26 20:28:35'), 'retweet'],
        [Timestamp('2016-10-26 20:57:38'), 'tweet'],
        [Timestamp('2016-10-26 21:36:37'), 'mention'],
        [Timestamp('2016-10-26 22:49:08'), 'tweet'],
        [Timestamp('2016-10-27 00:10:19'), 'tweet'],
        [Timestamp('2016-10-27 01:14:46'), 'tweet'],
        [Timestamp('2016-10-27 01:45:03'), 'retweet'],
        [Timestamp('2016-10-27 02:33:03'), 'tweet'],
        [Timestamp('2016-10-27 05:55:52'), 'retweet'],
        [Timestamp('2016-10-27 14:26:57'), 'mention'],
        [Timestamp('2016-10-27 17:46:42'), 'tweet'],
        [Timestamp('2016-10-27 17:53:33'), 'retweet'],
        [Timestamp('2016-10-27 18:53:38'), 'tweet'],
        [Timestamp('2016-10-27 21:02:00'), 'retweet'],
        [Timestamp('2016-10-27 21:23:50'), 'retweet'],
        [Timestamp('2016-10-27 22:21:01'), 'retweet'],
        [Timestamp('2016-10-28 05:30:02'), 'retweet'],
        [Timestamp('2016-10-28 13:11:01'), 'retweet'],
        [Timestamp('2016-10-28 16:55:13'), 'retweet'],
        [Timestamp('2016-10-28 18:25:02'), 'retweet'],
        [Timestamp('2016-10-28 18:54:44'), 'retweet'],
        [Timestamp('2016-10-28 19:22:14'), 'tweet'],
        [Timestamp('2016-10-28 19:23:20'), 'tweet'],
        [Timestamp('2016-10-28 22:33:03'), …
Run Code Online (Sandbox Code Playgroud)

python group-by pandas pandas-groupby

2
推荐指数
1
解决办法
557
查看次数

Travis-CI 仅在所有构建完成后部署一次

我正在尝试编写一个基本的 Travis-CI 脚本来测试构建我的 Python 包并为 Python 3.5 到 3.8 版运行 pytest。一旦这一切成功通过,我就希望 Travis-CI 构建文档并更新 GitHub 页面。我已经能够成功地测试构建包并按预期运行测试,我什至可以构建文档,但文档被构建了 4 次。我只希望在其他一切成功后构建和更新文档一次。我读过关于Jobs,但没有成功地让它与deploy关键字一起工作。

这是我目前的回购链接:https : //github.com/CurtLH/my_pkg

这是我现有的 Travis-CI 脚本,它可以工作但部署到 GitHub 页面 4 次。如何调整脚本以仅构建和部署文档一次?

language: python
python:
  - 3.8
  - 3.7
  - 3.6
  - 3.5
install:
  - pip install -e .[dev]
script:
  - pytest
  - sphinx-build -n -b html -d docs/build/doctrees docs/source docs/build/html
  - touch docs/build/html/.nojekyll

deploy:
  provider: pages
  skip_cleanup: true
  github_token: $GITHUB_TOKEN
  keep-history: true
  on:
    branch: master
  local_dir: docs/build/html
Run Code Online (Sandbox Code Playgroud)

travis-ci

2
推荐指数
1
解决办法
507
查看次数

ggplot2 圆形堆积条形图重复没有任何条形的标签

我正在尝试使用 ggplot重新创建此示例以创建圆形条形图。除了,而不是标准条形图,我想创建一个堆积条形图。我已经能够非常接近,但出于某种原因,在这个圆形条形图中重复了标签。我认为问题id在于我正在创建以匹配示例,但我不确定如何解决它。

df <- structure(list(team = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, NA), .Label = c("Team1", 
"Team2", "Team3", "Team4", "Team5", "Team6", "Team7", "Team8", "Team9", "Team10", 
"Team11", "Team12", "Team13", "Team14", "Team15"), class = "factor"), 
    variable = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

1
推荐指数
1
解决办法
326
查看次数

使用 Python Elastic Client 插入新文档引发非法参数异常

我在 AWS 上设置了一个 Elasticsearch 服务,其中包含一个现有索引,我正在尝试向该索引添加更多文档。我想使用 Python Elasticsearch 客户端与此服务交互。我能够成功连接服务并按预期查询它。但是,当我向 Elasticsearch 添加新文档时,收到以下错误:

RequestError: RequestError(400, 'illegal_argument_exception', 'mapper [city] cannot be changed from type [keyword] to [text]')
Run Code Online (Sandbox Code Playgroud)

我是否需要以某种方式为添加 Elasticsearch 的每个文档指定映射?我已经搜索过文档,但没有看到任何这样的例子。我想将城市字段映射为关键字,但我不知道在上传新文档时如何指定。

这是我当前的流程:

# create auth for AWS version 4
awsauth = AWS4Auth(access_key, secret_key, "us-east-2", "es")

# instantiate the elastic search client
es = Elasticsearch(
    hosts = [{'host': host, 'port': 443}],
    http_auth = awsauth,
    use_ssl = True,
    verify_certs = True,
    connection_class = RequestsHttpConnection
)

# create a document to upload
data = {'ad_id': 1053674,
 'city': 'Houston', …
Run Code Online (Sandbox Code Playgroud)

python elasticsearch

1
推荐指数
1
解决办法
1万
查看次数