当前位置: 首页 > 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…...

反向工程与模型迁移:打造未来商品详情API的可持续创新体系

在电商行业蓬勃发展的当下&#xff0c;商品详情API作为连接电商平台与开发者、商家及用户的关键纽带&#xff0c;其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息&#xff08;如名称、价格、库存等&#xff09;的获取与展示&#xff0c;已难以满足市场对个性化、智能…...

React Native 导航系统实战(React Navigation)

导航系统实战&#xff08;React Navigation&#xff09; React Navigation 是 React Native 应用中最常用的导航库之一&#xff0c;它提供了多种导航模式&#xff0c;如堆栈导航&#xff08;Stack Navigator&#xff09;、标签导航&#xff08;Tab Navigator&#xff09;和抽屉…...

java 实现excel文件转pdf | 无水印 | 无限制

文章目录 目录 文章目录 前言 1.项目远程仓库配置 2.pom文件引入相关依赖 3.代码破解 二、Excel转PDF 1.代码实现 2.Aspose.License.xml 授权文件 总结 前言 java处理excel转pdf一直没找到什么好用的免费jar包工具,自己手写的难度,恐怕高级程序员花费一年的事件,也…...

Java - Mysql数据类型对应

Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...

多模态商品数据接口:融合图像、语音与文字的下一代商品详情体验

一、多模态商品数据接口的技术架构 &#xff08;一&#xff09;多模态数据融合引擎 跨模态语义对齐 通过Transformer架构实现图像、语音、文字的语义关联。例如&#xff0c;当用户上传一张“蓝色连衣裙”的图片时&#xff0c;接口可自动提取图像中的颜色&#xff08;RGB值&…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...

[Java恶补day16] 238.除自身以外数组的乘积

给你一个整数数组 nums&#xff0c;返回 数组 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法&#xff0c;且在 O(n) 时间复杂度…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...