机器学习100期三色球数据

package com.example.demo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CaiPiaoController {
   //预测号码
   String ddfsss="6,1,8";
   
   int[][] arr = {
{8,5,8},
{4,4,8},
{1,1,9},
{5,6,7},
{7,7,9},
{4,7,3},
{2,7,5},
{8,1,5},
{8,6,1},
{7,8,5},
{2,4,6},
{6,1,6},
{3,6,9},
{8,7,9},
{7,5,8},
{5,4,9},
{2,6,6},
{5,1,9},
{9,0,3},
{1,8,6},
{5,1,3},
{0,9,9},
{6,6,8},
{6,7,1},
{1,0,2},
{2,3,1},
{2,0,9},
{1,3,6},
{2,4,1},
{2,4,3},
{0,3,8},
{6,6,5},
{9,1,9},
{2,7,0},
{3,4,5},
{5,7,3},
{2,2,2},
{7,1,7},
{8,4,7},
{3,3,6},
{6,6,3},
{6,9,2},
{2,1,8},
{9,4,0},
{7,2,0},
{9,5,5},
{6,1,5},
{4,4,1},
{9,9,9},
{0,0,3},
{0,4,3},
{9,3,0},
{9,7,8},
{4,9,8},
{7,6,1},
{1,8,1},
{9,0,6},
{6,8,9},
{8,3,2},
{3,6,1},
{0,2,0},
{0,3,3},
{3,5,6},
{2,4,5},
{2,9,7},
{9,6,6},
{6,9,1},
{6,1,7},
{6,9,8},
{6,4,3},
{5,3,7},
{0,6,8},
{4,4,7},
{1,3,3},
{5,3,6},
{9,3,7},
{4,5,4},
{0,9,1},
{3,8,6},
{5,6,9},
{6,3,6},
{0,8,7},
{9,2,3},
{3,3,5},
{5,1,4},
{2,5,8},
{3,2,9},
{6,6,9},
{2,8,9},
{3,2,4},
{0,2,2},
{3,6,3},
{3,4,0},
{4,7,7},
{7,9,4},
{1,3,7},
{8,6,9},
{1,5,7},
{9,0,2},
{9,4,1}
,{4,8,9}
,{1,7,4}
,{5,3,0}
,{1,1,7}
,{8,1,9}
,{5,1,3}
,{3,4,6}
};
    @RequestMapping("/getdata")
    public   int[][] home() {
       int dd=arr.length*3;
       int[][] res=new int[dd][3];
       int count0=0;
       int count1=0;
       int count2=0;
       int count3=0;
       int count4=0;
       int count5=0;
       int count6=0;
       int count7=0;
       int count8=0;
       int count9=0;
       int count =0;
       for(int i=0;i<arr.length;i++) {
          //i代表的时第几期,是x轴数据
          for(int j=0;j<arr[i].length;j++) {
             
             switch(arr[i][j]) {
             case 0:
                count0++;
                res[count][0]=0;
                res[count][1]=i;
                res[count][2]=count0;
                break;
             case 1:
                count1++;
                res[count][0]=1;
                res[count][1]=i;
                res[count][2]=count1;
                break;
             case 2:
                count2++;
                res[count][0]=2;
                res[count][1]=i;
                res[count][2]=count2;
                
                break;
             case 3:
                count3++;
                res[count][0]=3;
                res[count][1]=i;
                res[count][2]=count3;
                break;
             case 4:
                count4++;

                res[count][0]=4;
                res[count][1]=i;
                res[count][2]=count4;
                
                break;
             case 5:
                count5++;
                res[count][0]=5;
                res[count][1]=i;
                res[count][2]=count5;
                
                break;
             case 6:
                count6++;
                res[count][0]=6;
                res[count][1]=i;
                res[count][2]=count6;
                
                break;
             case 7:
                count7++;
                res[count][0]=7;
                res[count][1]=i;
                res[count][2]=count7;
                
                break;
             case 8:
                count8++;
                res[count][0]=8;
                res[count][1]=i;
                res[count][2]=count8;
                
                break;
             case 9:
                count9++;
                res[count][0]=9;
                res[count][1]=i;
                res[count][2]=count9;
                
                break;
             }
             count++;
          }
          
       }
          
           return res;
       }
    
    
    @RequestMapping("/getallarray")
    public  int[][] getallarray() throws IOException {
       int dd=arr.length*3;
       int[][] res=new int[dd][3];
       int count0=0;
       int count1=0;
       int count2=0;
       int count3=0;
       int count4=0;
       int count5=0;
       int count6=0;
       int count7=0;
       int count8=0;
       int count9=0;
       int count =0;
       for(int i=0;i<arr.length;i++) {
          //i代表的时第几期,是x轴数据
          for(int j=0;j<arr[i].length;j++) {
             
             switch(arr[i][j]) {
             case 0:
                count0++;
                res[count][0]=0;
                res[count][1]=i;
                res[count][2]=count0;
                break;
             case 1:
                count1++;
                res[count][0]=1;
                res[count][1]=i;
                res[count][2]=count1;
                break;
             case 2:
                count2++;
                res[count][0]=2;
                res[count][1]=i;
                res[count][2]=count2;
                
                break;
             case 3:
                count3++;
                res[count][0]=3;
                res[count][1]=i;
                res[count][2]=count3;
                break;
             case 4:
                count4++;

                res[count][0]=4;
                res[count][1]=i;
                res[count][2]=count4;
                
                break;
             case 5:
                count5++;
                res[count][0]=5;
                res[count][1]=i;
                res[count][2]=count5;
                
                break;
             case 6:
                count6++;
                res[count][0]=6;
                res[count][1]=i;
                res[count][2]=count6;
                
                break;
             case 7:
                count7++;
                res[count][0]=7;
                res[count][1]=i;
                res[count][2]=count7;
                
                break;
             case 8:
                count8++;
                res[count][0]=8;
                res[count][1]=i;
                res[count][2]=count8;
                
                break;
             case 9:
                count9++;
                res[count][0]=9;
                res[count][1]=i;
                res[count][2]=count9;
                
                break;
             }
             count++;
          }
          
       }
       int y=0;
       y=y<count0?count0:y;
       y=y<count1?count1:y;
       y=y<count2?count2:y;
       y=y<count3?count3:y;
       y=y<count4?count4:y;
       y=y<count5?count5:y;
       y=y<count6?count6:y;
       y=y<count7?count7:y;
       y=y<count8?count8:y;
       y=y<count9?count9:y;
       int x=arr.length;
       int[][] allArray=new int[y+1][x];
       for(int i=0;i<res.length;i++) {
          //i代表的时第几期,是x轴数据
          int x1=res[i][1];//第几期
          int y1=res[i][2];//出现了多少次
          allArray[y1][x1]=res[i][0]==0?10:res[i][0];
                     
       }
       String resStr="";
       int min=10000;
       min=min>count0?count0:min;
       min=min>count1?count1:min;
       min=min>count2?count2:min;
       min=min>count3?count3:min;
       min=min>count4?count4:min;
       min=min>count5?count5:min;
       min=min>count6?count6:min;
       min=min>count7?count7:min;
       min=min>count8?count8:min;
       min=min>count9?count9:min;
       System.out.println(y);
       System.out.println(min);
       
//     FileWriter fw=new FileWriter(new File("D:\\caipiao.csv"));
//         //写入中文字符时会出现乱码
//         BufferedWriter  bw=new BufferedWriter(fw);
//         //BufferedWriter  bw=new BufferedWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8")));
//         for(int i=0;i<allArray.length;i++) {
//           String row="";
//           //i代表的时第几期,是x轴数据
//           for(int j=0;j<allArray[i].length;j++) {
//              if(j==(allArray[i].length-1)) {
//                 row=row+allArray[i][j];
//              }else {
//                 row=row+allArray[i][j]+",";
//              }
//              
//           }
//           bw.write(row+"\t\n");
//        }
//         bw.close();
//         fw.close();
       
           return allArray;
       }
    
    
    @RequestMapping("/getdata1")
    public   int[] home1(int qishu) {
       int dd=arr.length*3;
       int[] res=new int[10];
       int count0=0;
       int count1=0;
       int count2=0;
       int count3=0;
       int count4=0;
       int count5=0;
       int count6=0;
       int count7=0;
       int count8=0;
       int count9=0;
       for(int i=arr.length-qishu;i<arr.length;i++) {
          //i代表的时第几期,是x轴数据
          for(int j=0;j<arr[i].length;j++) {
             for(int m=0;m<arr[i].length;m++) {
                if(arr[i-1][m]==arr[i][j]) {
                   switch(arr[i][j]) {
                   case 0:
                      count0++;
                      break;
                   case 1:
                      count1++;
                      break;
                   case 2:
                      count2++;
                      
                      break;
                   case 3:
                      count3++;
                      break;
                   case 4:
                      count4++;

                      
                      break;
                   case 5:
                      count5++;
                      
                      break;
                   case 6:
                      count6++;
                      
                      break;
                   case 7:
                      count7++;
                      
                      break;
                   case 8:
                      count8++;
                      
                      break;
                   case 9:
                      count9++;
                      
                      break;
                   }
                }
             }
          }
       }
       
       res[0]=count0;
       res[1]=count1;
       res[2]=count2;
       res[3]=count3;
       res[4]=count4;
       res[5]=count5;
       res[6]=count6;
       res[7]=count7;
       res[8]=count8;
       res[9]=count9;
       return res;
    }
    @RequestMapping("/getdata2")
    public   int[] home2(int qishu) {
       int dd=arr.length*3;
       int[] res=new int[10];
       int count0=0;
       int count1=0;
       int count2=0;
       int count3=0;
       int count4=0;
       int count5=0;
       int count6=0;
       int count7=0;
       int count8=0;
       int count9=0;
       for(int i=arr.length-qishu;i<arr.length;i++) {
          //i代表的时第几期,是x轴数据
          for(int j=0;j<arr[i].length;j++) {
                   switch(arr[i][j]) {
                   case 0:
                      count0=i;
                      break;
                   case 1:
                      count1=i;
                      break;
                   case 2:
                      count2=i;
                      
                      break;
                   case 3:
                      count3=i;
                      break;
                   case 4:
                      count4=i;

                      
                      break;
                   case 5:
                      count5=i;
                      
                      break;
                   case 6:
                      count6=i;
                      
                      break;
                   case 7:
                      count7=i;
                      
                      break;
                   case 8:
                      count8=i;
                      
                      break;
                   case 9:
                      count9=i;
                      
                      break;
                   }
          }
       }
       
       res[0]=arr.length-1-count0;
       res[1]=arr.length-1-count1;
       res[2]=arr.length-1-count2;
       res[3]=arr.length-1-count3;
       res[4]=arr.length-1-count4;
       res[5]=arr.length-1-count5;
       res[6]=arr.length-1-count6;
       res[7]=arr.length-1-count7;
       res[8]=arr.length-1-count8;
       res[9]=arr.length-1-count9;
       return res;
    }
}

前端展现数据走向

<!DOCTYPE html>
<html lang="zh_cn">
<head>
    <meta charset="UTF-8">
    <title id="urlTitle"></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/>
    <link rel="icon" href="images/favicon.ico" type="image/x-icon"/>

    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>

<script src="js/jquery-1.11.2.min.js"></script>
    <script src="js/echarts.js"></script>
</head>
<body style="background:white;">
<span style="font-size:18px;" style="white-space: nowrap;">    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->  
    <div id="main" style="width: 3500px;height:1700px;display: inline-block;"></div>  
    <div style="display: inline-block;" ><table id="data1"></table></div>
    <div style="display: inline-block;" ><table id="data111"></table></div>
    <div style="display: inline-block;" ><table id="data11"></table></div>
    <div style="display: inline-block;" ><table id="data12"></table></div>
    <div style="display: inline-block;" ><table id="data13"></table></div>
     <div style="display: inline-block;"><table id="data2"></table></div>
     <script type="text/javascript"> 
     $.get("http://localhost:8080/getdata1?qishu=20", function(result){
        var strdd='<tr><td>球号</td><td>连号次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data1').append(strdd);
     });
     $.get("http://localhost:8080/getdata1?qishu=19", function(result){
        var strdd='<tr><td>球号</td><td>连号次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data111').append(strdd);
     });
     $.get("http://localhost:8080/getdata1?qishu=40", function(result){
        var strdd='<tr><td>球号</td><td>连号次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data11').append(strdd);
     });
     $.get("http://localhost:8080/getdata1?qishu=60", function(result){
        var strdd='<tr><td>球号</td><td>连号次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data12').append(strdd);
     });
     $.get("http://localhost:8080/getdata1?qishu=80", function(result){
        var strdd='<tr><td>球号</td><td>连号次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data13').append(strdd);
     });
     $.get("http://localhost:8080/getdata2?qishu=20", function(result){
        var strdd='<tr><td>球号</td><td>遗忘次数</td></tr>';
        var arg=[];
        for(i in result){
           arg.push([i,result[i]])
        }
        for(j=0;j<arg.length-1;j++){
           for(i=1;i< arg.length-j;i++){
               if(arg[i-1][1]<arg[i][1]){
                  var ddf=arg[i-1];
                  arg[i-1]=arg[i];
                  arg[i]=ddf;
               }
            }
        }
        for(i in arg){
           strdd=strdd+'<tr><td>'+arg[i][0]+'</td><td>'+arg[i][1]+'</td></tr>';
        }
        $('#data2').append(strdd);
     });
     </script>
    <script type="text/javascript">  
        // 基于准备好的dom,初始化echarts实例  
        var myChart = echarts.init(document.getElementById('main')); 
        var data0=[];var data1=[];var data2=[];var data3=[];var data4=[];
        var data5=[];var data6=[];var data7=[];var data8=[];var data9=[];
        var datamid=[];
        
        function dssss(){
           option = {
                   xAxis: {
                      name:'期数',
                       scale: true
                   },
                   yAxis: {
                      name:'出现次数',
                       scale: true
                   },
                   legend: {
                       data:['0','1','2','3','4','5','6','7','8','9','边界']
                   },
                   series: [{
                      name:'0',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '0'; 
                                    }}
                            }
                        },
                       data: data0
                   },{
                      name:'1',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '1'; 
                                    }}
                            }
                        },
                       data: data1
                   },{
                      name:'2',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '2'; 
                                    }}
                            }
                        },
                       data:data2
                   },{
                      name:'3',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '3'; 
                                    }}
                            }
                        },
                       data: data3
                   },{
                      name:'4',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '4'; 
                                    }}
                            }
                        },
                       data: data4
                   },{
                      name:'5',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '5'; 
                                    }}
                            }
                        },
                       data:data5
                   },{
                      name:'6',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '6'; 
                                    }}
                            }
                        },
                       data: data6
                   },{
                      name:'7',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '7'; 
                                    }}
                            }
                        },
                       data:data7
                   },{
                      name:'8',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '8'; 
                                    }}
                            }
                        },
                       data:data8
                   }, {
                      name:'9',
                       type: 'scatter',
                       symbolSize: 20,
                       itemStyle: {
                            normal: {
                                //color: '#C1232B',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '9'; 
                                    }}
                            }
                        },
                       data: data9
                   }, {
                      name:'边界',
                       type: 'scatter',
                       itemStyle: {
                            normal: {
                                color: '#030303',
                                label : {show: true,
                                   formatter: function(params) {  
                                        return '边界'; 
                                    }}
                            }
                        },
                       data: datamid
                   }]
               };
            
      
            // 使用刚指定的配置项和数据显示图表。  
            myChart.setOption(option);
        }
        
        
        $.get("http://localhost:8080/getdata", function(result){
            for(var i in result){
               var x=result[i][1];
              var y=result[i][2];
              var item=[x,y];
               switch(result[i][0]){
               case 0:
                  data0.push(item);
                  break;
               case 1:
                  data1.push(item);
                  break;
               case 2:
                  data2.push(item);
                  break;
               case 3:
                  data3.push(item);
                  break;
               case 4:
                  data4.push(item);
                  break;
               case 5:
                  data5.push(item);
                  break;
               case 6:
                  data6.push(item);
                  break;
               case 7:
                  data7.push(item);
                  break;
               case 8:
                  data8.push(item);
                  break;
               case 9:
                  data9.push(item);
                  break;
               }
            }
            var max0=0;
            for(var i in data0){
               if(data0[i][1]>max0){
                  max0=data0[i][1];
               }
            }
            var max1=0;
            for(var i in data1){
               if(data1[i][1]>max1){
                  max1=data1[i][1];
               }
            }
            var max1=0;
            for(var i in data1){
               if(data1[i][1]>max1){
                  max1=data1[i][1];
               }
            }
            var max2=0;
            for(var i in data2){
               if(data2[i][1]>max2){
                  max2=data2[i][1];
               }
            }
            var max3=0;
            for(var i in data3){
               if(data3[i][1]>max3){
                  max3=data3[i][1];
               }
            }
            var max4=0;
            for(var i in data4){
               if(data4[i][1]>max4){
                  max4=data4[i][1];
               }
            }
            var max5=0;
            for(var i in data5){
               if(data5[i][1]>max5){
                  max5=data5[i][1];
               }
            }
            var max6=0;
            for(var i in data6){
               if(data6[i][1]>max6){
                  max6=data6[i][1];
               }
            }
            var max7=0;
            for(var i in data7){
               if(data7[i][1]>max7){
                  max7=data7[i][1];
               }
            }
            var max8=0;
            for(var i in data8){
               if(data8[i][1]>max8){
                  max8=data8[i][1];
               }
            }
            var max9=0;
            for(var i in data9){
               if(data9[i][1]>max9){
                  max9=data9[i][1];
               }
            }
            var max=0;
            var min=10000;
            for(i=0;i<10;i++){
               if(eval('max'+i+'>max')){
                  eval('max=max'+i);
               }
               if(eval('max'+i+'<min')){
                  eval('min=max'+i);
               }
            }
            datamid.push([99,min]);
            datamid.push([99,max]);
            datamid.push([99,(min+max)/2]);
            dssss();
          });
          
    </script></span>  
</body>
<script >



</script>
</html>

 

将D:\\caipiao.csv数据转成图片

from PIL import Image #PIL pakage name is Pillow
import numpy as np
import pandas as pd
# im = Image.open("baidu.jpg")
# print(im.format, im.size, im.mode)
# im.show()
#
# arr = np.array(im)
# print(arr.shape, arr.dtype)

# im2 = Image.open("111.png")
# im2 = im2.convert("L")
# arr2 = np.array(im2)
# print(arr2.shape, arr2.dtype)
# print(arr2)
# new_im = Image.fromarray(arr2)
# new_im.show()


df = pd.read_csv('d:\caipiao.csv')  # 返回一个DataFrame的对象,这个是pandas的一个数据结构
X = np.array(df,dtype='uint8')
print(X)
print(X.shape, X.dtype)
X.dtype = 'uint8'
print(X.shape, X.dtype)
print(X)
X=X*10
print(X)
new_im = Image.fromarray(X)
new_im.show()
# print(X)

 神经网络学习彩票数据

import tensorflow as tf
import numpy as np

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string("captcha_dir", "./tfrecords/captcha.tfrecords", "验证码数据的路径")
tf.app.flags.DEFINE_integer("batch_size", 100, "每批次训练的样本数")
tf.app.flags.DEFINE_integer("label_num", 4, "每个样本的目标值数量")
tf.app.flags.DEFINE_integer("letter_num", 26, "每个目标值取的字母的可能心个数")
caipiao_file_path = "d:/caipiao.csv"

# 定义一个初始化权重的函数
def weight_variables(shape):
    w = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=1.0))
    return w


# 定义一个初始化偏置的函数
def bias_variables(shape):
    b = tf.Variable(tf.constant(0.0, shape=shape))
    return b


def read_and_decode():
    """
    读取验证码数据API
    :return: image_batch, label_batch
    """
    caipiaoArray = np.loadtxt(caipiao_file_path, delimiter=",", dtype="int")
    x, y = caipiaoArray.shape
    # 50-107       43---43-10
    print(x)
    print(y)
    print(caipiaoArray[42:43, 106:107])
    xflag = 0
    diyici = 1
    tezhengAll = None
    trueTargetAll = None
    for i in range(y, 50, -1):
        if xflag >= 10:
            x = x - 3
            xflag = 0
        item = caipiaoArray[x - 27:x, i - 11:i]
        xflag = xflag + 1
        # 特征值
        tezheng = item[:, :10]
        tezheng = tezheng.reshape([1, -1])
        trueTarget = item[:, 10]
        trueTarget = trueTarget.reshape([1, -1])
        if diyici == 1:
            tezhengAll = tezheng

            trueTargetAll = trueTarget
        else:
            tezhengAll = np.concatenate((tezhengAll, tezheng), axis=0)

            trueTargetAll = np.concatenate((trueTargetAll, trueTarget), axis=0)
        diyici = 0
    yx, yy = trueTargetAll.shape
    aa = None
    diyici1 = 1
    for yxi in range(0, yx, 1):
        a = np.array([0, 0, 0])
        iii = 0
        for yyi in range(0, yy, 1):
            if trueTargetAll[yxi, yyi] != 0:
                a[iii] = trueTargetAll[yxi, yyi]
                iii = iii + 1
        if diyici1 == 1:
            aa = a.reshape([1, -1])
            diyici1 = 0
        else:
            aa = np.concatenate((aa, a.reshape([1, -1])), axis=0)
    print(aa)
    print(aa.shape)
    return trueTargetAll, aa


def fc_model(image):
    """
    进行预测结果
    :param image: 100图片特征值[100, 20, 80, 3]
    :return: y_predict预测值[100, 4 * 26]
    """
    with tf.variable_scope("model"):
        # 将图片数据形状转换成二维的形状
        image_reshape = tf.reshape(image, [-1, 20 * 80 * 3])

        # 1、随机初始化权重偏置
        # matrix[100, 20 * 80 * 3] * [20 * 80 * 3, 4 * 26] + [104] = [100, 4 * 26]
        weights = weight_variables([20 * 80 * 3, 4 * 26])
        bias = bias_variables([4 * 26])

        # 进行全连接层计算[100, 4 * 26]
        y_predict = tf.matmul(tf.cast(image_reshape, tf.float32), weights) + bias

    return y_predict


def predict_to_onehot(label):
    """
    将读取文件当中的目标值转换成one-hot编码
    :param label: [100, 4]      [[13, 25, 15, 15], [19, 23, 20, 16]......]
    :return: one-hot
    """
    # 进行one_hot编码转换,提供给交叉熵损失计算,准确率计算[100, 4, 26]
    label_onehot = tf.one_hot(label, depth=FLAGS.letter_num, on_value=1.0, axis=2)

    print(label_onehot)

    return label_onehot


def captcharec():
    """
    验证码识别程序
    :return:
    """
    # 1、读取验证码的数据文件 label_btch [100 ,4]
    image_batch, label_batch = read_and_decode()

    # 2、通过输入图片特征数据,建立模型,得出预测结果
    # 一层,全连接神经网络进行预测
    # matrix [100, 20 * 80 * 3] * [20 * 80 * 3, 4 * 26] + [104] = [100, 4 * 26]
    y_predict = fc_model(image_batch)

    #  [100, 4 * 26]
    print(y_predict)

    # 3、先把目标值转换成one-hot编码 [100, 4, 26]
    y_true = predict_to_onehot(label_batch)

    # 4、softmax计算, 交叉熵损失计算
    with tf.variable_scope("soft_cross"):
        # 求平均交叉熵损失 ,y_true [100, 4, 26]--->[100, 4*26]
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(y_true, [FLAGS.batch_size, FLAGS.label_num * FLAGS.letter_num]),
            logits=y_predict))
    # 5、梯度下降优化损失
    with tf.variable_scope("optimizer"):

        train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

    # 6、求出样本的每批次预测的准确率是多少 三维比较
    with tf.variable_scope("acc"):

        # 比较每个预测值和目标值是否位置(4)一样    y_predict [100, 4 * 26]---->[100, 4, 26]
        equal_list = tf.equal(tf.argmax(y_true, 2), tf.argmax(tf.reshape(y_predict, [FLAGS.batch_size, FLAGS.label_num, FLAGS.letter_num]), 2))

        # equal_list  100个样本   [1, 0, 1, 0, 1, 1,..........]
        accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))

    # 定义一个初始化变量的op
    init_op = tf.global_variables_initializer()

    # 开启会话训练
    with tf.Session() as sess:
        sess.run(init_op)

        # 定义线程协调器和开启线程(有数据在文件当中读取提供给模型)
        coord = tf.train.Coordinator()

        # 开启线程去运行读取文件操作
        threads = tf.train.start_queue_runners(sess, coord=coord)

        # 训练识别程序
        for i in range(5000):

            sess.run(train_op)

            print("第%d批次的准确率为:%f" % (i, accuracy.eval()))

        # 回收线程
        coord.request_stop()

        coord.join(threads)

    return None


if __name__ == "__main__":
    captcharec()

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值