python 生成随机数据以输入CTM算法。以“python2.7 ctmdata.py”运行。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 生成随机数据以输入CTM算法。以“python2.7 ctmdata.py”运行。相关的知识,希望对你有一定的参考价值。

from random import randint
from random import uniform, gauss

def gaussian(mu, sigma = 0.1):
	prob = gauss(mu, sigma)
	if prob < 0:
		return 0.0
	elif prob > 1:
		return 1.0
	else:
		return round(prob, 2)

N = 10 # no. of news
G = 3 # no. of groups of users
K = 5 # no. of users per group

sparse_prob = 0.2 # this times 100% is the percentage sparsity

U = G * K # total no. of users = no. of groups times no. of users per group

groups = {} # map of users to groups
group_ratings = [[0 for i in range(G)] for j in range(N)]  # 2-D array of ratings of the collective group for each news
ratings = [[0 for i in range(U)] for j in range(N)] # 2-D array of ratings of each user for each news

for i in range(G):
	for j in range(i*(U/G), (i+1)*(U/G)):
		groups[j] = i

approxRatings = [0.3, 0.7] # ratings to be given

for i in range(N):
	for j in range(G):
		group_ratings[i][j] = approxRatings[randint(0, len(approxRatings) - 1)]

for n in range(N):
	for u in range(U):
		ratings[n][u] = 0
		if sparse_prob < uniform(0, 1): # probability that the user has rated the 
			ratings[n][u] = gaussian(group_ratings[n][groups[u]])

for r in ratings:
	print r

以上是关于python 生成随机数据以输入CTM算法。以“python2.7 ctmdata.py”运行。的主要内容,如果未能解决你的问题,请参考以下文章

python 生成随机数据 验证算法

Python STL random

有关遗传算法的疑问:“以一定概率进行交叉和变异”的含义?

处理大数据以执行随机森林分类

Python3标准库:random伪随机数生成器

收藏 | 机器学习算法的随机数据生成