python数据常用命令_数据_常用命令_Python

打印计数器

def flushPrint(variable):

sys.stdout.write('\r')

sys.stdout.write('%s' % variable)

sys.stdout.flush()

单变量最小二乘(OLS)回归拟合

def OLSRegressFit(x,y):

xx = sm.add_constant(x, prepend=True)

res = sm.OLS(y,xx).fit()

constant, beta = res.params

r2 = res.rsquared

return [constant, beta, r2]

双对数坐标系下线性回归拟合画图(幂律函数)

def alloPlot(x,y,col,lab):

lx = np.log(x+1)

ly = np.log(y+1)

xx = sm.add_constant(lx, prepend=True)

res = sm.OLS(ly,xx).fit()

constant, beta = res.params

plt.plot(x,y, "o",color=col)

plt.plot(x,np.exp(constant)*x**beta,color=col,label=lab+' '+str(np.round(beta,2)))

纵轴对数坐标下线性回归拟合(指数函数)

def semilogPlot(x,y,col,lab):

ly = np.log(y)

xx = sm.add_constant(x, prepend=True)

res = sm.OLS(ly,xx).fit()

constant, beta = res.params

plt.plot(x,y, "o",color=col)

plt.plot(x,np.exp(constant+x*beta),color=col,label=lab+' '+str(np.round(beta,2)))

计算一个平衡过的流网络从源到各节点的流距离

def flowDistanceFromSource(G): #input a balanced nx graph

R = G.reverse()

mapping = {'source':'sink','sink':'source'}

H = nx.relabel_nodes(R,mapping)

#---------initialize flow distance dict------

L = dict((i,1) for i in G.nodes())

#---------prepare weighted out-degree dict------

T = G.out_degree(weight='weight')

#---------iterate until converge------------

ls = np.array(L.values())

delta = len(L)*0.001 + 1

k=0

while delta > len(L)*0.001:

k+=1

if k>20:

break

for i in L:

l=1

for m,n in H.edges(i):

l+=L[n]*H[m][n].values()[0]/float(T[m])

L[i]=l

delta = sum(np.abs(np.array(L.values()) - ls))

ls = np.array(L.values())

#---------clean the result-------

del L['sink']

for i in L:

L[i]-=1

L['sink'] = L.pop('source')

L['source'] = 0

return L

计算一个平衡过的流网络从各节点到汇的流距离

def flowDistanceToSink(G): #input a balanced nx graph

#---------initialize flow distance dict------

L = dict((i,1) for i in G.nodes())

#---------prepare weighted out-degree dict------

T = G.out_degree(weight='weight')

#---------iterate until converge------------

ls = np.array(L.values())

delta = len(L)*0.001 + 1

k=0

while delta > len(L)*0.001:

k+=1

if k>20:

break

for i in L:

l=1

for m,n in G.edges(i):

l+=L[n]*G[m][n].values()[0]/float(T[m])

L[i]=l

delta = sum(np.abs(np.array(L.values()) - ls))

ls = np.array(L.values())

for i in L:

L[i]-=1

return L

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值