当前位置: 首页 > news >正文

双色球预测算法(Java),——森林机器学习、时间序列

最近AI很火,老想着利用AI的什么算法,干点什么有意义的事情。其中之一便想到了双色球,然后让AI给我预测,结果基本都是简单使用随机算法列出了几个数字。

额,,,,咋说呢,双色球确实是随机的,但是,如果只是随机,我用你AI干嘛,直接写个随机数就行了嘛。

于是乎,问了下市面上的一些预测算法,给出了俩,一个是:森林机器学习,一个是时间序列。

然后,我让它给我把这俩算法写出来,给是给了,但是,,,无力吐槽。

于是,在我和它的共同配合下,这俩算法的java版诞生了,仅供参考:

森林机器学习:
package com.ruoyi.web.controller.test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import lombok.val;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;import weka.classifiers.Classifier;
import weka.classifiers.trees.RandomForest;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;public class LotteryPredictor {public static void main(String[] args) throws Exception {String csvFilePath = "D:\\12.csv"; // 请替换为你的CSV文件的绝对路径// Step 1: Read historical data from CSVList<int[]> historicalData = readCSV(csvFilePath);// Step 2: Prepare data for WekaInstances trainingData = prepareTrainingData(historicalData);// Step 3: Train RandomForest modelClassifier model = new RandomForest();model.buildClassifier(trainingData);// Step 4: Make a predictionint[] prediction = predictNextNumbers(model, trainingData);// Output the predictionSystem.out.println("Predicted numbers: ");for (int num : prediction) {System.out.print(num + " ");}}private static List<int[]> readCSV(String csvFilePath) throws Exception {List<int[]> data = new ArrayList<>();try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilePath), StandardCharsets.UTF_8))) {CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withDelimiter(',').withTrim());for (CSVRecord record : csvParser) {if(record.size() == 1) {val rec = record.get(0).split(","); // Remove non-numeric charactersint[] row = new int[rec.length];for (int i = 0; i < rec.length; i++) {String value = rec[i].replaceAll("[^0-9]", ""); // Remove non-numeric charactersif (!value.isEmpty()) {row[i] = Integer.parseInt(value);}}data.add(row);}else {int[] row = new int[record.size()];for (int i = 0; i < record.size(); i++) {String value = record.get(i).replaceAll("[^0-9]", ""); // Remove non-numeric charactersif (!value.isEmpty()) {row[i] = Integer.parseInt(value);}}data.add(row);}}}return data;}private static Instances prepareTrainingData(List<int[]> historicalData) {// Define attributesArrayList<Attribute> attributes = new ArrayList<>();for (int i = 0; i < historicalData.get(0).length; i++) {attributes.add(new Attribute("num" + (i + 1)));}// Create datasetInstances dataset = new Instances("LotteryData", attributes, historicalData.size());dataset.setClassIndex(dataset.numAttributes() - 1);// Add datafor (int[] row : historicalData) {dataset.add(new DenseInstance(1.0, toDoubleArray(row)));}return dataset;}private static double[] toDoubleArray(int[] intArray) {double[] doubleArray = new double[intArray.length];for (int i = 0; i < intArray.length; i++) {doubleArray[i] = intArray[i];}return doubleArray;}private static int[] predictNextNumbers(Classifier model, Instances trainingData) throws Exception {int numAttributes = trainingData.numAttributes();Set<Integer> predictedNumbers = new HashSet<>();while (predictedNumbers.size() < numAttributes) {DenseInstance instance = new DenseInstance(numAttributes);instance.setDataset(trainingData);for (int i = 0; i < numAttributes; i++) {instance.setValue(i, Math.random() * 33 + 1); // Random values for prediction}double prediction = model.classifyInstance(instance);int predictedNumber = (int) Math.round(prediction);// Ensure the predicted number is within the valid range and not a duplicateif (predictedNumber >= 1 && predictedNumber <= 33) {predictedNumbers.add(predictedNumber);}}int[] predictionArray = new int[numAttributes];int index = 0;for (int num : predictedNumbers) {predictionArray[index++] = num;}return predictionArray;}
}
时间序列算法:
package com.ruoyi.web.controller.test;
import lombok.val;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerMinMaxScaler;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.learning.config.Adam;
import org.nd4j.linalg.lossfunctions.LossFunctions;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;public class LotteryPredictor3 {public static void main(String[] args) throws Exception {String csvFilePath = "D:\\12.csv"; // 请替换为你的CSV文件的绝对路径// Step 1: Read historical data from CSVList<int[]> historicalData = readCSV(csvFilePath);// Step 2: Prepare data for time series analysisdouble[][] timeSeriesData = prepareTimeSeriesData(historicalData);// Step 3: Train neural network modelMultiLayerNetwork model = trainModel(timeSeriesData);// Step 4: Make a predictionint[] redBallPrediction = predictRedBalls(model, timeSeriesData);int blueBallPrediction = predictBlueBall(model, timeSeriesData);// Output the predictionSystem.out.println("Predicted numbers: ");for (int num : redBallPrediction) {System.out.print(num + " ");}System.out.println("Blue ball: " + blueBallPrediction);}private static List<int[]> readCSV(String csvFilePath) throws Exception {List<int[]> data = new ArrayList<>();try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilePath), StandardCharsets.UTF_8))) {CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withDelimiter(',').withTrim());for (CSVRecord record : csvParser) {if(record.size() == 1) {val rec = record.get(0).split(","); // Remove non-numeric charactersint[] row = new int[rec.length];for (int i = 0; i < rec.length; i++) {String value = rec[i].replaceAll("[^0-9]", ""); // Remove non-numeric charactersif (!value.isEmpty()) {row[i] = Integer.parseInt(value);}}data.add(row);}else {int[] row = new int[record.size()];for (int i = 0; i < record.size(); i++) {String value = record.get(i).replaceAll("[^0-9]", ""); // Remove non-numeric charactersif (!value.isEmpty()) {row[i] = Integer.parseInt(value);}}data.add(row);}}}return data;}private static double[][] prepareTimeSeriesData(List<int[]> historicalData) {// Flatten the historical data into a 2D arraydouble[][] timeSeriesData = new double[historicalData.size()][];for (int i = 0; i < historicalData.size(); i++) {timeSeriesData[i] = new double[historicalData.get(i).length];for (int j = 0; j < historicalData.get(i).length; j++) {timeSeriesData[i][j] = historicalData.get(i)[j];}}return timeSeriesData;}private static MultiLayerNetwork trainModel(double[][] timeSeriesData) {int numInputs = timeSeriesData[0].length;int numOutputs = numInputs;int numHiddenNodes = 10;MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().updater(new Adam(0.01)).list().layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes).activation(Activation.RELU).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MSE).activation(Activation.IDENTITY).nIn(numHiddenNodes).nOut(numOutputs).build()).build();MultiLayerNetwork model = new MultiLayerNetwork(conf);model.init();model.setListeners(new ScoreIterationListener(10));// Prepare the dataINDArray input = Nd4j.create(timeSeriesData);INDArray output = Nd4j.create(timeSeriesData);DataSet dataSet = new DataSet(input, output);// Normalize the dataNormalizerMinMaxScaler scaler = new NormalizerMinMaxScaler(0, 1);scaler.fit(dataSet);scaler.transform(dataSet);// Train the modelfor (int i = 0; i < 2000; i++) {model.fit(dataSet);}return model;}private static int[] predictRedBalls(MultiLayerNetwork model, double[][] timeSeriesData) {INDArray input = Nd4j.create(timeSeriesData);INDArray output = model.output(input);double[] lastPrediction = output.getRow(output.rows() - 1).toDoubleVector();Set<Integer> predictedNumbers = new HashSet<>();for (double num : lastPrediction) {int scaledNum = (int) Math.round(num * 32) + 1; // Scale back to 1-33 rangeif (scaledNum >= 1 && scaledNum <= 33) {predictedNumbers.add(scaledNum);}if (predictedNumbers.size() == 6) {break;}}// Ensure we have exactly 6 unique numberswhile (predictedNumbers.size() < 6) {int randomNum = (int) (Math.random() * 33) + 1;predictedNumbers.add(randomNum);}int[] predictionArray = new int[6];int index = 0;for (int num : predictedNumbers) {predictionArray[index++] = num;}return predictionArray;}private static int predictBlueBall(MultiLayerNetwork model, double[][] timeSeriesData) {INDArray input = Nd4j.create(timeSeriesData);INDArray output = model.output(input);double lastPrediction = output.getDouble(output.rows() - 1);// Predict blue ball numberint blueBallPrediction = (int) Math.round(lastPrediction * 15) + 1; // Scale back to 1-16 rangeif (blueBallPrediction < 1) blueBallPrediction = 1;if (blueBallPrediction > 16) blueBallPrediction = 16;return blueBallPrediction;}
}

对比了下,时间序列的相对容易让人相信,机器学习,不知道咋评价,大家可以试试。

相关文章:

双色球预测算法(Java),——森林机器学习、时间序列

最近AI很火&#xff0c;老想着利用AI的什么算法&#xff0c;干点什么有意义的事情。其中之一便想到了双色球&#xff0c;然后让AI给我预测&#xff0c;结果基本都是简单使用随机算法列出了几个数字。 额&#xff0c;&#xff0c;&#xff0c;&#xff0c;咋说呢&#xff0c;双…...

【计算机网络篇】数据链路层(11)在数据链路层扩展以太网

文章目录 &#x1f354;使用网桥在数据链路层扩展以太网&#x1f95a;网桥的主要结构和基本工作原理&#x1f388;网桥的主要结构&#x1f50e;网桥转发帧的例子&#x1f50e;网桥丢弃帧的例子&#x1f50e;网桥转发广播帧的例子 &#x1f95a;透明网桥&#x1f50e;透明网桥的…...

Ubuntu20.04 使用scrapy-splash爬取动态网页

我们要先安装splash服务&#xff0c;使用dock安装&#xff0c;如果dock没有安装&#xff0c;请参考我的上一篇博文&#xff1a; 按照官方文档&#xff1a;https://splash.readthedocs.io/en/stable/install.html 1.下载splash sudo docker pull scrapinghub/splash2.安装scrapy…...

Function:控制继电器上下电,上电后adb登录,copy配置文件

import serial import time import datetime import subprocess import osdef append_to_txt(file_path, content):if os.path.exists(file_path):with open(file_path, a) as file: # 使用 a 模式打开文件进行追加file.write(content \n) # 追加内容&#xff0c;并换行else…...

香港电讯高可用网络助力企业变革金融计算

客户背景 客户是一家金融行业知名的量化私募对冲基金公司&#xff0c;专注于股票、期权、期货、债券等主要投资市场&#xff0c;在量化私募管理深耕多年&#xff0c;目前资管规模已达数百亿级&#xff0c;在国内多个城市均设有办公地点。 客户需求 由于客户业务倚重量化技术…...

LDR6020一拖二快充线:多设备充电新选择

随着科技的快速发展&#xff0c;我们的日常生活中越来越多地依赖于智能设备。然而&#xff0c;每当手机、平板或其他移动设备电量告急时&#xff0c;我们总是需要寻找合适的充电线进行充电。为了解决这一痛点&#xff0c;市场上出现了一款备受瞩目的新产品——LDR6020一拖二快充…...

电脑ffmpeg.dll丢失原因解析,找不到ffmpeg.dll的5种解决方法

在数字化时代&#xff0c;多媒体文件的处理已经成为我们日常生活和工作中不可或缺的一部分。在计算机使用过程中&#xff0c;丢失ffmpeg.dll文件是一个特定但常见的问题&#xff0c;尤其是对于那些经常处理视频编解码任务的用户来说。下面小编讲全面分析ffmpeg.dll丢失原因以及…...

手机网站制作软件是哪些

手机网站制作软件是一种用于设计、开发和创建适用于移动设备的网站的软件工具。随着移动互联网时代的到来&#xff0c;越来越多的用户开始使用手机浏览网页和进行在线交流&#xff0c;因此&#xff0c;手机网站制作软件也逐渐成为了市场上的热门工具。 1. Adobe Dreamweaver&am…...

【Kubernetes项目部署】k8s集群+高可用、负载均衡+防火墙

项目架构图 &#xff08;1&#xff09;部署 kubernetes 集群 详见&#xff1a;http://t.csdnimg.cn/RLveS &#xff08;2&#xff09; 在 Kubernetes 环境中&#xff0c;通过yaml文件的方式&#xff0c;创建2个Nginx Pod分别放置在两个不同的节点上&#xff1b; Pod使用hostP…...

IPC工业电脑的现状、发展未来与破局策略

文章目录 全球工业电脑市场概况1.1 市场规模与增长1.2 区域分布与主要市场 工业电脑的技术发展与应用2.1 技术趋势与创新2.2 应用领域扩展2.3 工业自动化与智能化 竞争格局与市场参与者3.1 主要企业与市场竞争3.2 国内外竞争对比3.3 市场集中度与竞争策略 未来发展趋势与市场预…...

深入了解Redis的TYPE命令

Redis作为一个高性能的内存数据库&#xff0c;支持多种数据结构。在管理和操作Redis数据库时&#xff0c;了解键对应的数据类型是至关重要的。本文将深入探讨Redis的TYPE命令&#xff0c;它用于返回存储在指定键中的值的数据类型。 什么是TYPE命令&#xff1f; TYPE命令用于查…...

iptables(3)规则管理

简介 上一篇文章中,我们已经介绍了怎样使用iptables命令查看规则,那么这篇文章我们就来介绍一下,怎样管理规则,即对iptables进行”增、删、改”操作。 注意:在进行iptables实验时,请务必在个人的测试机上进行,不要再有任何业务的机器上进行测试。 在进行测试前,为保障…...

关于addEventListener的使用和注意项

一、addEventListener基本理解 addEventListener 是一个 JavaScript DOM 方法&#xff0c;用于向指定元素添加事件监听器。它接受三个参数&#xff1a; 事件类型&#xff1a;一个字符串&#xff0c;表示要监听的事件类型&#xff0c;如 ‘click’、‘mouseover’、‘keydown’…...

分享一下,如何搭建个人网站的步骤

在这段充满探索与创造的奇妙旅途中&#xff0c;我就像一位耐心的建筑师&#xff0c;在数字世界的荒原上精心雕琢&#xff0c;两周的时光缓缓流淌。每天&#xff0c;我与代码共舞&#xff0c;手执HTML、CSS与JavaScript这三大构建魔杖&#xff0c;一砖一瓦地筑起了梦想中的网络城…...

(7)摄像机和云台

文章目录 前言 1 云台 2 带有MAVLink接口的摄像机 3 相机控制和地理标签 4 视频质量差的常见修复方法 5 详细主题 前言 Copter、Plane 和 Rover 最多支持 3 轴云台&#xff0c;包括自动瞄准感兴趣区域&#xff08;ROI&#xff09;的相机和自动触发相机快门等先进功能。按…...

MicroBlaze IP核中的外设接口和缓冲器接口介绍

MicroBlaze IP核是Xilinx公司提供的一个嵌入式软核处理器&#xff0c;广泛应用于FPGA设计中。在MicroBlaze IP核中&#xff0c;外设接口和缓冲器接口是处理器与外部设备和内存交互的关键部分。 1 外设接口 MicroBlaze处理器中的AXI4 内存映射外设接口AXI4是一种在Xilinx FPGA设…...

Java数据结构与算法(完全背包)

前言: 完全背包问题是背包问题的一个变种&#xff0c;与0/1背包问题不同&#xff0c;在完全背包问题中&#xff0c;每种物品可以被选取多次。问题描述如下&#xff1a; 给定 n 件物品&#xff0c;每件物品有一个重量 wi和一个价值 vi&#xff0c;以及一个背包&#xff0c;它能…...

git merge(3个模式) 与 git rebase 图文详解区别

目录 1 git merge1.1 模式一&#xff1a;fast-forward(–ff)1.2 模式二&#xff1a;non-Fast-forward(–no-ff)1.3 模式三&#xff1a;fast-forward only(–ff-only) 2 git rebase3 区别 1 git merge git merge有好几种不同的模式 默认情况下你直接使用 git merge 命令&#x…...

Eclipse 工作空间:深入解析与高效使用

Eclipse 工作空间:深入解析与高效使用 Eclipse 是一款广受欢迎的集成开发环境(IDE),它为各种编程语言提供了强大的开发工具。在 Eclipse 中,工作空间(Workspace)是一个核心概念,它代表了一个项目的集合,这些项目共享相同的配置和设置。本文将深入探讨 Eclipse 工作空…...

Aspose将doc,ppt转成pdf

1.需要引入的jar包 链接: https://pan.baidu.com/s/1t3wqq7KrHi50K9KX3-Eb9A?pwdu4se 提取码: u4se <dependency><groupId>com.aspose</groupId><artifactId>aspose-words-jdk16</artifactId><version>15.8.0</version><scop…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

AtCoder 第409​场初级竞赛 A~E题解

A Conflict 【题目链接】 原题链接&#xff1a;A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串&#xff0c;只有在同时为 o 时输出 Yes 并结束程序&#xff0c;否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》

在注意力分散、内容高度同质化的时代&#xff0c;情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现&#xff0c;消费者对内容的“有感”程度&#xff0c;正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中&#xff0…...

学校招生小程序源码介绍

基于ThinkPHPFastAdminUniApp开发的学校招生小程序源码&#xff0c;专为学校招生场景量身打造&#xff0c;功能实用且操作便捷。 从技术架构来看&#xff0c;ThinkPHP提供稳定可靠的后台服务&#xff0c;FastAdmin加速开发流程&#xff0c;UniApp则保障小程序在多端有良好的兼…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

leetcodeSQL解题:3564. 季节性销售分析

leetcodeSQL解题&#xff1a;3564. 季节性销售分析 题目&#xff1a; 表&#xff1a;sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

Redis:现代应用开发的高效内存数据存储利器

一、Redis的起源与发展 Redis最初由意大利程序员Salvatore Sanfilippo在2009年开发&#xff0c;其初衷是为了满足他自己的一个项目需求&#xff0c;即需要一个高性能的键值存储系统来解决传统数据库在高并发场景下的性能瓶颈。随着项目的开源&#xff0c;Redis凭借其简单易用、…...