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

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

Java数值运算常见陷阱与规避方法

整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...

DingDing机器人群消息推送

文章目录 1 新建机器人2 API文档说明3 代码编写 1 新建机器人 点击群设置 下滑到群管理的机器人&#xff0c;点击进入 添加机器人 选择自定义Webhook服务 点击添加 设置安全设置&#xff0c;详见说明文档 成功后&#xff0c;记录Webhook 2 API文档说明 点击设置说明 查看自…...

Unity UGUI Button事件流程

场景结构 测试代码 public class TestBtn : MonoBehaviour {void Start(){var btn GetComponent<Button>();btn.onClick.AddListener(OnClick);}private void OnClick(){Debug.Log("666");}}当添加事件时 // 实例化一个ButtonClickedEvent的事件 [Formerl…...

「全栈技术解析」推客小程序系统开发:从架构设计到裂变增长的完整解决方案

在移动互联网营销竞争白热化的当下&#xff0c;推客小程序系统凭借其裂变传播、精准营销等特性&#xff0c;成为企业抢占市场的利器。本文将深度解析推客小程序系统开发的核心技术与实现路径&#xff0c;助力开发者打造具有市场竞争力的营销工具。​ 一、系统核心功能架构&…...

永磁同步电机无速度算法--基于卡尔曼滤波器的滑模观测器

一、原理介绍 传统滑模观测器采用如下结构&#xff1a; 传统SMO中LPF会带来相位延迟和幅值衰减&#xff0c;并且需要额外的相位补偿。 采用扩展卡尔曼滤波器代替常用低通滤波器(LPF)&#xff0c;可以去除高次谐波&#xff0c;并且不用相位补偿就可以获得一个误差较小的转子位…...

ubuntu22.04有线网络无法连接,图标也没了

今天突然无法有线网络无法连接任何设备&#xff0c;并且图标都没了 错误案例 往上一顿搜索&#xff0c;试了很多博客都不行&#xff0c;比如 Ubuntu22.04右上角网络图标消失 最后解决的办法 下载网卡驱动&#xff0c;重新安装 操作步骤 查看自己网卡的型号 lspci | gre…...

macOS 终端智能代理检测

&#x1f9e0; 终端智能代理检测&#xff1a;自动判断是否需要设置代理访问 GitHub 在开发中&#xff0c;使用 GitHub 是非常常见的需求。但有时候我们会发现某些命令失败、插件无法更新&#xff0c;例如&#xff1a; fatal: unable to access https://github.com/ohmyzsh/oh…...