社交网络影响力最大化——线性阈值模型(LT模型)算法实现(Python实现)
目录
1、环境配置
2、LT传播模型算法实现
3、LT传播模型算法测试
4、测试文件Wiki-Vote.txt数据
社交网络影响力最大化——线性阈值模型(LT模型)算法实现(Python实现)
1、环境配置
环境配置:Win7 Pycharm Anaconda2
该算法每个节点的阈值设为 0.5
2、LT传播模型算法实现
linear_threshold.py (LT传播模型算法)
# -*- coding: utf-8 -*-
"""
Implement linear threshold models
社交网络影响力最大化 传播模型——线性阈值(LT)模型算法实现
"""
import copy
import itertools
import random
import math
import networkx as nx__all__ = ['linear_threshold']#-------------------------------------------------------------------------
# Some Famous Diffusion Models
#-------------------------------------------------------------------------def linear_threshold(G, seeds, steps=0): #LT线性阈值算法"""Parameters----------G : networkx graph #所有节点构成的图The number of nodes.seeds: list of nodes #子节点集The seed nodes of the graphsteps: int #激活节点的层数(深度),当steps<=0时,返回子节点集能激活的所有节点The number of steps to diffuseWhen steps <= 0, the model diffuses until no more nodescan be activatedReturn------layer_i_nodes : list of list of activated nodeslayer_i_nodes[0]: the seeds #子节点集layer_i_nodes[k]: the nodes activated at the kth diffusion step #该子节点集激活的节点集Notes-----1. Each node is supposed to have an attribute "threshold". If not, thedefault value is given (0.5). #每个节点有一个阈值,这里默认阈值为:0.52. Each edge is supposed to have an attribute "influence". If not, thedefault value is given (1/in_degree) #每个边有一个权重值,这里默认为:1/入度References----------[1] GranovetterMark. Threshold models of collective behavior.The American journal of sociology, 1978."""if type(G) == nx.MultiGraph or type(G) == nx.MultiDiGraph:raise Exception( \"linear_threshold() is not defined for graphs with multiedges.")# make sure the seeds are in the graphfor s in seeds:if s not in G.nodes():raise Exception("seed", s, "is not in graph")# change to directed graphif not G.is_directed():DG = G.to_directed()else:DG = copy.deepcopy(G) # copy.deepcopy 深拷贝 拷贝对象及其子对象# init thresholdsfor n in DG.nodes():if 'threshold' not in DG.node[n]:DG.node[n]['threshold'] = 0.5elif DG.node[n]['threshold'] > 1:raise Exception("node threshold:", DG.node[n]['threshold'], \"cannot be larger than 1")# init influencesin_deg = DG.in_degree() #获取所有节点的入度for e in DG.edges():if 'influence' not in DG[e[0]][e[1]]:DG[e[0]][e[1]]['influence'] = 1.0 / in_deg[e[1]] #计算边的权重elif DG[e[0]][e[1]]['influence'] > 1:raise Exception("edge influence:", DG[e[0]][e[1]]['influence'], \"cannot be larger than 1")# perform diffusionA = copy.deepcopy(seeds)if steps <= 0:# perform diffusion until no more nodes can be activatedreturn _diffuse_all(DG, A)# perform diffusion for at most "steps" rounds onlyreturn _diffuse_k_rounds(DG, A, steps)def _diffuse_all(G, A):layer_i_nodes = [ ]layer_i_nodes.append([i for i in A])while True:len_old = len(A)A, activated_nodes_of_this_round = _diffuse_one_round(G, A)layer_i_nodes.append(activated_nodes_of_this_round)if len(A) == len_old:breakreturn layer_i_nodesdef _diffuse_k_rounds(G, A, steps):layer_i_nodes = [ ]layer_i_nodes.append([i for i in A])while steps > 0 and len(A) < len(G):len_old = len(A)A, activated_nodes_of_this_round = _diffuse_one_round(G, A)layer_i_nodes.append(activated_nodes_of_this_round)if len(A) == len_old:breaksteps -= 1return layer_i_nodesdef _diffuse_one_round(G, A):activated_nodes_of_this_round = set()for s in A:nbs = G.successors(s)for nb in nbs:if nb in A:continueactive_nb = list(set(G.predecessors(nb)).intersection(set(A)))if _influence_sum(G, active_nb, nb) >= G.node[nb]['threshold']:activated_nodes_of_this_round.add(nb)A.extend(list(activated_nodes_of_this_round))return A, list(activated_nodes_of_this_round)def _influence_sum(G, froms, to):influence_sum = 0.0for f in froms:influence_sum += G[f][to]['influence']return influence_sum
3、LT传播模型算法测试
test_linear_threshold.py(LT模型算法测试)
#!/usr/bin/env python
# coding=UTF-8 #支持中文字符需要添加 coding=UTF-8
from nose.tools import *
from networkx import *
from linear_threshold import *
import time
"""Test Diffusion Models
----------------------------
"""
if __name__=='__main__':start=time.clock()datasets=[]f=open("Wiki-Vote.txt","r") #读取文件数据(边的数据)data=f.read()rows=data.split('\n')for row in rows:split_row=row.split('\t')name=(int(split_row[0]),int(split_row[1]))datasets.append(name) #将边的数据以元组的形式存放到列表中G=networkx.DiGraph() #建立一个空的有向图GG.add_edges_from(datasets) #向有向图G中添加边的数据列表layers=linear_threshold(G,[6],2) #调用LT线性阈值算法,返回子节点集和该子节点集的最大激活节点集del layers[-1]length=0for i in range(len(layers)):length =length+len(layers[i])lengths=length-len(layers[0]) #获得子节点的激活节点的个数(长度)end=time.clock()#测试数据输出结果print(layers) #[[25], [33, 3, 6, 8, 55, 80, 50, 19, 54, 23, 75, 28, 29, 30, 35]]print(lengths) #15print('Running time: %s Seconds'%(end-start)) #输出代码运行时间
社交网络影响力最大化(Python实现)及Wiki-Vote数据集资源下载:
社交网络影响力最大化(Python实现)及Wiki-Vote数据集-机器学习文档类资源-CSDN下载
本人博文社交网络影响力最大化项目实战基础学习
1、社交网络影响力最大化(独立级联(IC)模型和线性阈值(LT)模型)介绍
2、社交网络影响力最大化—线性阈值模型(LT模型)算法实现(Python实现)
3、社交网络影响力最大化—贪心算法实现(Python实现)
4、社交网络影响力最大化项目实战源代码和Wiki-Vote数据集下载
交流学习资料共享欢迎入群:955817470(群一),801295159(群二)
社交网络影响力最大化——线性阈值模型(LT模型)算法实现(Python实现)相关推荐
- 一种有效的基于路径的社交网络影响力最大化方法【论文阅读】
文章目录 一.论文的亮点 二.HIPA算法具体过程 三.论文的优缺点 四.自己的收获点 五.未来可能的突破点 [摘要]本文是关于影响力最大化相关内容:内容的是关于论文An efficient path ...
- 【机器学习】隐马尔可夫模型及其三个基本问题(三)模型参数学习算法及python实现
[机器学习]隐马尔可夫模型及其三个基本问题(三)模型参数学习算法及python实现 一.一些概率与期望值的计算 二.非监督学习方法(Baum-Welch算法) 三.python实现 隐马尔可夫模型参数 ...
- 数学建模:微分方程模型—常微分方程数值解算法及 Python 实现
目录 一.显式欧拉 (Euler) 法 二.显式欧拉法的改进 隐式欧拉法 (后退欧拉法) 梯形法 两步欧拉法 (中点法) 预报 - 校正法 (改进欧拉法) 三.龙格 - 库塔 (Runge-Kutta ...
- 社交网络中基于位置的影响力最大化 CIKM2015 译文
社交网络中基于位置的影响力最大化 摘要 这篇文章的目的是通过研究在LBSN平台中基于位置的影响最大化来实现O2O模式上的产品推广.随着O2O环境下存在的消费行为,传统的线上影响力扩散模型不能准确描述产 ...
- 《大数据》2015年第3期“研究”——社交网络影响力传播研究(上)
社交网络影响力传播研究 陈卫 (微软亚洲研究院 北京 100080) 摘 要:随着互联网和大数据的研究应用日益广泛,对社交网络影响力传播的研究成为数据挖掘和社交网络分析中的热点.从影响力传播模型.影响 ...
- 【转】如何从计算视角研究网络传播影响力最大化问题?
电商中对社交网络的两个应用方向,基于社交网络做推荐和基于社交网络做裂变广告.有意思的是做电商的往往做不起社交,电商和社交数据很难打通,而不管做推荐还是做广告,所有的建模和分析都是基于用户行为数据的,不 ...
- Information Sciences 2022 | 利用图嵌入和图神经网络实现社交网络中的影响力最大化
目录 前言 1. 影响力最大化 2. SGNN 2.1 标签生成 2.2 struc2vec 2.3 GNN特征处理 2.4 回归预测 2.5 整体框架 3. 实验 3.1 数据集 3.2 评价指标 ...
- 线性连续时间状态空间模型的离散化及实例
线性连续时间状态空间模型的离散化(Discretization of Linear Continuous-Time State-Space Models) 1 .状态空间模型 非线性连续时间状态空间模 ...
- R语言广义加性模型(GAMs:Generalized Additive Model)建模:数据加载、划分数据、并分别构建线性回归模型和广义线性加性模型GAMs、并比较线性模型和GAMs模型的性能
R语言广义加性模型(GAMs:Generalized Additive Model)建模:数据加载.划分数据.并分别构建线性回归模型和广义线性加性模型GAMs.并比较线性模型和GAMs模型的性能 目录
- R语言glm拟合logistic回归模型:模型评估(模型预测概率的分组密度图、混淆矩阵、准确率、精确度、召回率、ROC、AUC)、PRTPlot函数获取logistic模型最优阈值(改变阈值以优化)
R语言glm拟合logistic回归模型:模型评估(模型预测概率的分组密度图.混淆矩阵.Accuray.Precision.Recall.ROC.AUC).PRTPlot函数可视化获取logistic ...
最新文章
- 1.2.3 OSI参考模型(2)
- LIST OF CITIES FOR POTENTIAL VISIT IN SOUTHERN UK
- 初学者学习Python,掌握这些实用小技巧能快速入门!
- Linux学习之十二-Linux文件属性
- 111. 二叉树的最小深度 golang
- 蓝桥杯 ALGO-83 算法训练 阶乘 java版
- clion中自定义消息msg消息时定义的msg文件有类型提示
- 陈俊龙:从深度强化学习到宽度强化学习—结构,算法,机遇及挑战
- android获取ssid,有关WiFi SSID的获取
- 微软 android启动器,微软启动器Mirosoft Launcher
- 计算机慢怎么解决6,解决电脑运行慢卡顿问题的六种方法
- JS字符串过滤数字_过滤大写数字
- Java判断经纬度点是否在给定区域内
- 澳洲移民 技术移民_满足COVID-19期间移民对语言访问的需求
- 自己做一个table插件 (一)Ajax获取数据后动态生成table
- Windows 10 文件夹越来越大,如何瘦身,删除哪些文件?
- 素描java字母_生成素描图片
- mysql报错:1044 -Access denied for user ‘root‘@‘%‘ to database
- 2020年了才学Python,来得及嘛?
- 北京PM10超8000!怎么测出来的?