python 通过历史出现的数字列表,实现在数字 1 到 49 的列表中找出下次数字出现概率及具体数字的问题代码--2023博客之星候选--城市赛道

###1.通过强化训练方法来估计下一个数字出现的概率,并找出具体的数字,可以使用一个简单的计数方法。
首先,我们需要创建一个包含数字 1 到 49 的列表,并初始化计数为 0。然后,遍历历史数字列表,对每个数字进行计数。最后,根据计数结果,计算每个数字出现的概率,并找出具有最高概率的数字。以下是实现上述功能的示例代码:

from collections import Counter

def estimate_next_number(history):
    # 创建包含数字 1 到 49 的列表,并初始化计数为 0
    numbers = list(range(1, 50))
    counts = Counter(numbers)

    # 遍历历史数字列表,对每个数字进行计数
    for num in history:
        counts[num] += 1

    # 计算每个数字出现的概率
    total = sum(counts.values())
    probabilities = {num: count / total for num, count in counts.items()}

    # 找出具有最高概率的数字
    max_probability = max(probabilities.values())
    next_number = [num for num, prob in probabilities.items() if prob == max_probability]

    return next_number, max_probability

history = [32,39,18,39,41,47,26,6,9,9,16,49,15,13,12,36,48,26,32,49,45,7,40,36,9,32,35,2,18,36,11,11,4,47,48,4,21,30,9,31,23,5,39,11,16,30,12,2,13,23,26,21,46,6,21,3,49]
next_number, probability = estimate_next_number(history)

print("下一个数字的概率:", probability)
print("下一个数字可能是:", next_number)

在上述代码中,我们使用 Counter 对象来进行数字计数,并使用字典推导式计算每个数字的概率。然后,找出具有最高概率的数字,作为下一个可能出现的数字。
注意,这只是一个简单的估计方法,并不能保证准确预测下一个数字。实际上,数字的出现是随机的,过去的出现并不能直接反映未来的概率。
根据提供的历史数字列表,运行代码后,输出应该是:

下一个数字的概率: 0.08928571428571429
下一个数字可能是: [32]

这表示下一个数字是32的概率最高,为0.08928571428571429。
请注意,这个结果是基于历史数字的频率计算得出的概率,仅供参考。

###2.使用PBIL算法计算给定数字列表中下次数字出现概率及具体数字。
以下是实现上述功能的示例代码:

import random
import numpy as np

# 给定的历史出现数字列表
history = [32,39,18,39,41,47,26,6,9,9,16,49,15,13,12,36,48,26,32,49,45,7,40,36,9,32,35,2,18,36,11,11,4,47,48,4,21,30,9,31,23,5,39,11,16,30,12,2,13,23,26,21,46,6,21,3,49]

# 参数设置
population_size = 100  # 种群大小
learning_rate = 0.1  # 学习率
mutation_rate = 0.02  # 变异率
num_generations = 100  # 迭代次数

# 初始化概率向量
prob_vector = np.ones(49) * 0.5

# PBIL算法
for generation in range(num_generations):
    population = []
    for _ in range(population_size):
        individual = []
        for i in range(1, 50):
            if random.random() < prob_vector[i-1]:
                individual.append(i)
        population.append(individual)
    
    # 计算适应度
    fitness = np.zeros(population_size)
    for i in range(population_size):
        for j in range(len(history)-1):
            if set(history[j:j+2]).issubset(set(population[i])):
                fitness[i] += 1
    
    # 更新概率向量
    best_individual = population[np.argmax(fitness)]
    for i in range(1, 50):
        prob_vector[i-1] = (1 - learning_rate) * prob_vector[i-1] + learning_rate * (best_individual.count(i) / len(best_individual))
    
    # 变异
    for i in range(1, 50):
        if random.random() < mutation_rate:
            prob_vector[i-1] = random.random()

# 输出下次数字出现概率及具体数字
next_prob = prob_vector / np.sum(prob_vector)
next_number = np.argmax(next_prob) + 1
print("下次数字出现概率:", next_prob)
print("下次数字:", next_number)

以上代码使用了一个简单的PBIL算法来估计下次数字出现的概率。首先,我们初始化一个概率向量,其中每个数字的初始概率都是0.5。然后,通过迭代的方式,生成一个种群(每个个体是由概率向量确定的数字组成的列表)。计算每个个体的适应度,适应度的计算方式是看历史数字列表中的每两个数字是否出现在个体中。接下来,根据适应度更新概率向量,使得最优个体的数字有更高的概率出现。最后,通过归一化概率向量,得到下次数字出现的概率,并找出具体的数字(概率最高的数字)。
通过以上代码,我们可以对其进行优化,以下是实现优化的示例代码:

import random
import numpy as np


class PBIL:
    def __init__(self, history, population_size=100, learning_rate=0.1, mutation_rate=0.02, num_generations=100):
        """用于初始化算法的参数,包括历史出现数字列表、种群大小、学习率、突变率和迭代次数

        :param history: 历史出现数字列表
        :param population_size: 种群大小
        :param learning_rate: 学习率
        :param mutation_rate: 变异率
        :param num_generations: 迭代次数
        """
        self.history = history
        self.population_size = population_size
        self.learning_rate = learning_rate
        self.mutation_rate = mutation_rate
        self.num_generations = num_generations

    def generate_population(self):
        """用于生成种群,每个个体是一个数字列表,数字的选择基于概率向量进行随机采样

        :return:
        """
        population = []
        for _ in range(self.population_size):
            individual = []
            for i in range(1, 50):
                if random.random() < self.prob_vector[i - 1]:
                    individual.append(i)
            population.append(individual)
        return population

    def calculate_fitness(self, population):
        """用于计算种群中每个个体的适应度,适应度的定义是在历史出现数字列表中,个体的两个连续数字的出现次数。

        :param population:
        :return:
        """
        fitness = np.zeros(self.population_size)
        for i in range(self.population_size):
            for j in range(len(self.history) - 1):
                if set(self.history[j:j + 2]).issubset(set(population[i])):
                    fitness[i] += 1
        return fitness

    def update_prob_vector(self, best_individual):
        """根据当前种群中最优个体的数字分布,更新概率向量。

        :param best_individual:
        :return:
        """
        for i in range(1, 50):
            self.prob_vector[i - 1] = (1 - self.learning_rate) * self.prob_vector[i - 1] + self.learning_rate * (
                    best_individual.count(i) / len(best_individual))

    def mutate_prob_vector(self):
        """用于在概率向量上进行突变操作,以增加种群的多样性。

        :return:
        """
        for i in range(1, 50):
            if random.random() < self.mutation_rate:
                self.prob_vector[i - 1] = random.random()

    def run(self):
        """算法的主要执行逻辑,它根据给定的迭代次数执行迭代过程,每次迭代生成新的种群、计算适应度、更新概率向量和进行突变操作

        :return:
        """
        # 初始化概率向量
        self.prob_vector = np.ones(49) * 0.5

        for generation in range(self.num_generations):
            population = self.generate_population()
            fitness = self.calculate_fitness(population)

            best_individual = population[np.argmax(fitness)]
            self.update_prob_vector(best_individual)

            self.mutate_prob_vector()

        # 输出下次数字出现概率及具体数字
        next_prob = self.prob_vector / np.sum(self.prob_vector)  # 表示下一个数字的出现概率
        next_number = np.argmax(next_prob) + 1  # 表示出现的具体数字
        return next_prob, next_number


# 给定的历史出现数字列表
history_new =[32,39,18,39,41,47,26,6,9,9,16,49,15,13,12,36,48,26,32,49,45,7,40,36,9,32,35,2,18,36,11,11,4,47,48,4,21,30,9,31,23,5,39,11,16,30,12,2,13,23,26,21,46,6,21,3,49]




history=history_new

pbil = PBIL(history)
next_prob, next_number = pbil.run()  # 根据给定的历史出现数字列表,运行代码可以得到下一个数字的出现概率和具体数字的预测结果

print("下次数字出现概率:", next_prob)
print("下次数字:", next_number)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SzetoZeZe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值