将百分比字符转换为数字时遇到问题.例如,我想将"10%"转换为10%,但是
as.numeric("10%")
Run Code Online (Sandbox Code Playgroud)
回报NA.你有什么想法?
在题为" 度量相关性的缩放"及其对无标度网络中的扩散的影响的论文中,作者定义了$ E_b(k)$的数量来衡量度相关程度.
LK Gallos,C.Song和HA Makse,度量相关性的比例及其对无标度网络扩散的影响,物理学.莱特牧师.100,248701(2008).
我的问题是如何使用Python计算网络的Eb(k)?我的问题是我无法重现作者的结果.我使用Condense Matter数据进行测试.Eb(k)的结果如上图所示.您可以看到我的图中的一个问题是Eb(k)远大于1!!我也尝试了互联网(作为级别数据)和WWW数据,问题仍然存在.毫无疑问,我的算法或代码存在严重问题.您可以重现我的结果,并将其与作者进行比较.您的解决方案或建议非常感谢.我将在下面介绍我的算法和python脚本.
python脚本如下:
%matplotlib inline
import networkx as nx
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from collections import defaultdict
def ebks(g, b):
edge_dict = defaultdict(lambda: defaultdict(int))
degree_dict = defaultdict(int)
edge_degree = [sorted(g.degree(e).values()) for e in g.edges()]
for e in edge_degree:
edge_dict[e[0]][e[-1]] +=1
for i in g.degree().values(): …Run Code Online (Sandbox Code Playgroud) 数据集中有两列,分别是user_id和site_name.它记录每个用户浏览的每个站点名称.
toy_dict = {'site_name': {0: u'\u4eac\u4e1c\u7f51\u4e0a\u5546\u57ce',
1: u'\u963f\u91cc\u4e91',
2: u'\u6dd8\u5b9d\u7f51',
3: u'\u624b\u673a\u6dd8\u5b9d\u7f51',
4: u'\u6211\u4eec\u7684\u70b9\u5fc3\u7f51',
5: u'\u8c46\u74e3\u7f51',
6: u'\u9ad8\u5fb7\u5730\u56fe',
7: u'\u817e\u8baf\u7f51',
8: u'\u70b9\u5fc3',
9: u'\u767e\u5ea6',
10: u'\u641c\u72d7',
11: u'\u8c37\u6b4c',
12: u'AccuWeather\u6c14\u8c61\u9884\u62a5',
13: u'\u79fb\u52a8\u68a6\u7f51',
14: u'\u817e\u8baf\u7f51',
15: u'\u641c\u72d7\u7f51',
16: u'360\u624b\u673a\u52a9\u624b',
17: u'\u641c\u72d0',
18: u'\u767e\u5ea6'},
'user_id': {0: 37924550,
1: 37924550,
2: 37924550,
3: 37924550,
4: 37924550,
5: 37924550,
6: 37924550,
7: 37924550,
8: 37924551,
9: 37924551,
10: 37924551,
11: 37924551,
12: 37924551,
13: 37924552,
14: 45285152,
15: 45285153,
16: 45285153,
17: 45285153,
18: …Run Code Online (Sandbox Code Playgroud) 给出一个字符列表,例如:
L <- list("a", "b", "c", "d")
Run Code Online (Sandbox Code Playgroud)
注意,长度L不固定.
如何获得相邻的组合对,例如:
[,1] [,2]
[1,] "a" "b"
[2,] "b" "c"
[3,] "c" "d"
Run Code Online (Sandbox Code Playgroud)
实际上,我这样做是为了获得有针对性的矩阵以进行进一步的网络分析.你知道,在一个特定的计算机媒介沟通中,人们一个接一个地讨论,有一个序列,新来者只回复最新的帖子.
在大多数情况下,我们使用R将数字时间转换为posixct格式.但是,有时,我们想比较哪个时间点更早,然后我们更喜欢数字时间格式.因此,将时间格式转换为数字是非常实际的问题.例如,我的数据格式如"2001-03-13 10:31:00",
begin <- "2001-03-13 10:31:00"
Run Code Online (Sandbox Code Playgroud)
使用R,我想弄清楚如何将其转换为数字,例如Julian时间,类似于1970-01-01 00:00:00和2001-03-13 10:31:00之间的秒数.
你有什么建议吗?
朱利安历法开始于公元前45年(70年代AUC),作为朱利叶斯·凯撒对罗马历法的改革.它是在与亚历山大的天文学家Sosigenes协商后选择的,可能是为了接近热带年份(至少从Hipparchus开始就已知).见http://en.wikipedia.org/wiki/Julian_calendar
我想用频率数据拟合离散广义β分布(DGBD).
数据如下所示:
freq = c(1116, 2067, 137 , 124, 643, 2042, 55 ,47186, 7504, 1488, 211, 1608,
3517 , 7 , 896 , 378, 17 ,3098, 164977 , 601 , 196, 637, 149 , 44,2 , 1801, 882 , 636,5184, 1851, 776 , 343 , 851, 33 ,4011, 209, 715 ,
937 , 20, 6922, 2028 , 23, 3045 , 16 , 334, 31 , 2)
Rank = rank(-freq, ties.method = c("first") )
p = freq/sum(freq)
Run Code Online (Sandbox Code Playgroud)
log.f …Run Code Online (Sandbox Code Playgroud) 我想用R定义一个分段函数,但是,我的R代码出错了.任何建议都是受欢迎的.
x<-seq(-5, 5, by=0.01)
for (x in -5:5){
if (-0.326 < x < 0.652) fx<- 0.632
else if (-1.793<x<-1.304) fx<- 0.454
else if (1.630<x<2.119) fx<-0.227
else fx<- 0 }
Run Code Online (Sandbox Code Playgroud) 我有一个大型网络要分析。例如:
import networkx as nx
import random
BA = nx.random_graphs.barabasi_albert_graph(1000000, 3)
nx.info(BA)
Run Code Online (Sandbox Code Playgroud)
我必须在保持度数分布不变的情况下洗牌边缘。基本思想是由马斯洛夫提出的。因此,我和我的同事编写了一个 shuffleNetwork 函数,在该函数中,我们在网络对象 G 上工作了 num 次。edge 是一个列表对象。
问题是此功能对于大型网络运行速度太慢。我尝试使用set或dict而不是list作为边缘对象(set 和 dict 是哈希表)。但是,由于我们还需要对其进行删除和添加元素,因此时间复杂度变得更大。
您对进一步优化此功能有什么建议吗?
def shuffleNetwork(G,Num):
edges=G.edges()
l=range(len(edges))
for n in range(Num):
i,j = random.sample(l, 2)
a,b=edges[i]
c,d=edges[j]
if a != d and c!= b:
if not (a,d) in edges or (d, a) in edges or (c,b) in edges or (b, c) in edges:
edges[i]=(a,d)
edges[j]=(c,b)
K=nx.from_edgelist(edges)
return K …Run Code Online (Sandbox Code Playgroud) 我想通过R中的名称和行名称来加入矩阵:
m1 = matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE,
dimnames = list(c("r1", "r2"),
c("a", "b", "c")))
m2 = matrix(c(4, 5, 0, 2,3,4), nrow = 2, ncol = 3, byrow = TRUE,
dimnames = list(c("r2", "r3"),
c("d", "b", "c")))
Run Code Online (Sandbox Code Playgroud)
检查m1:
> m1
a b c
r1 1 2 3
r2 11 12 13
Run Code Online (Sandbox Code Playgroud)
检查平方米:
> m2
d b c
r2 4 5 0
r3 2 3 4
Run Code Online (Sandbox Code Playgroud)
我想得到看起来像这样的m3:
> m3
a b c d
r1 1 …Run Code Online (Sandbox Code Playgroud) 我想按照正态分布使用python从列表中选择一个元素。我有一个清单,例如
alist = ['an', 'am', 'apple', 'cool', 'why']
Run Code Online (Sandbox Code Playgroud)
例如,根据正态分布的概率密度函数(PDF),给定列表中的第3个元素应该被选择为最大概率。
r ×6
python ×3
list ×2
networkx ×2
numeric ×2
algorithm ×1
character ×1
combinations ×1
correlation ×1
curve ×1
distribution ×1
function ×1
hash ×1
join ×1
lines ×1
matrix ×1
merge ×1
montecarlo ×1
networking ×1
pdf ×1
piecewise ×1
plot ×1
points ×1
posixct ×1
pymc ×1
python-3.x ×1
random ×1
select ×1
shuffle ×1
strptime ×1
time ×1