第一个PyQt程序:双色球号码生成器(特色:使用SVM机器学习方法训练历史开奖数据)

 

前言:

      最近一年的时间里,基本每个星期都会买上一注福彩,号码基本就随机的,自己从不会花心思去分析啥走势。早期在500w网站上买,貌似中过一个10块和两个5块的,后来淘宝可以买之后就转移阵地了,买的次数也不少了,可恁是一次都没中过,那叫一个郁闷。

      正在做的项目基本上把主流的机器学习方法都用遍了,某天突发奇想,想用机器学习方法去分析历史开奖数据,找出其中的规律,然后随机出更像开奖数据的号码。(ps:注意我用的是更像一词,而不是更可能

      接触Python语言估计有1年多的时间了吧,但一直没用过它做gui,以前曾经想用其做gui开发的,因为它开发快,但看看tk后选择了逃避。自从得知PyQt的存在后(本科时在Linux下使用过QtGUI开发),我决定使用PyQt实现这个基于机器学习的双色球号码生成器。

     

效果图:

第一个PyQt程序:双色球号码生成器(特色:使用SVM机器学习方法训练历史开奖数据)_第1张图片

 

 

本程序完全是PyQt的练手程序,实现没技术可言,所以不作过多说明,只贴部分代码

 

 

程序的关键部分在于历史开奖数据的抓取,代码如下:

import urllib import re import operator def getSsqHistoryFrom500w(n): """ Get ssq history data from the website http://www.500wan.com/pages/info/datachart/ssq/history/history.shtml. The website "http://www.500wan.com/pages/info/datachart/ssq/history/inc/history.php?limit=1200" can give what we want, and "1200" can be set to any number of the records you want . n : ?limit=n """ if not operator.isNumberType(n) : return if n < 0: return dataurl = r"http://www.500wan.com/pages/info/datachart/ssq/history/inc/history.php?limit=" dataurl = ''.join([dataurl, str(n)]) # webdata = urllib.urlopen(dataurl, proxies={'http' : 'http://127.0.0.1:8081'}).read() webdata = urllib.urlopen(dataurl).read() pattern = r'(/d{5})(/d/d)(/d/d)(/d/d)(/d/d)(/d/d)(/d/d)(/d/d)' regexp = re.compile(pattern) matchdata = regexp.findall(webdata) f = open('ssq_500w.dat','w') for i in range(len(matchdata)): f.write(' '.join(matchdata[i])) f.write('/n') f.close() f = open('last10.dat', 'w') for i in range(10): f.write(' '.join(matchdata[i])) f.write('/n') f.close return matchdata def getSsqHistoryFromZhcw(): """ Get ssq history data from the website http://map2.zhcw.com/ssq/ssq/ssqInc/ssqZongHeFengBuTuAscselect=500.html number of items is 500. """ dataurl = "http://map2.zhcw.com/ssq/ssq/ssqInc/ssqZongHeFengBuTuAscselect=500.html" webdata = urllib.urlopen(dataurl).read() webdata = webdata.replace('/r/n', '') webdata = webdata.replace('/r', '' ) webdata = webdata.replace('/n', '') #print webdata pattern = r' /d/d/d/d-/d/d-/d/d (/d{7}) (/d/d) (/d/d) (/d/d) (/d/d) (/d/d) (/d/d) (/d/d)' regexp = re.compile(pattern) matchdata = regexp.findall(webdata) f = open('ssq_zhcw.dat','w') for i in range(len(matchdata)): f.write(' '.join(matchdata[i])) f.write('/n') f.close() f = open('last10.dat', 'w') for i in range(10): tdata = [] for x in matchdata[len(matchdata)-1-i]: tdata.append(x) tdata[0] = tdata[0][2:] f.write(' '.join(tdata)) f.write('/n') f.close return matchdata

 

Train by SVM,代码如下(使用到QThread):

class TrainThread(QThread): """ """ def __init__(self, choice=1, parent=None): super(TrainThread, self).__init__(parent) self.choice=choice def run(self): ssqdata = [[1, 2, 3, 4, 5, 6, 7]] try: if(self.choice == 0): ssqdata = getSsqHistoryFrom500w(2000) if (self.choice == 1): ssqdata = getSsqHistoryFromZhcw() except Exception, e: self.emit(SIGNAL('done(bool)'), False) return ##prepare train data posData = [] posNum = len(ssqdata) for i in range(posNum): eachData = [] for j in range(1, len(ssqdata[i])): eachData.append((int)(ssqdata[i][j])) posData.append(eachData) posLabel = [1 for i in range(posNum)] negNum = 2 * posNum # num = 0 negData = [] while num < negNum: sample = [] for i in range(6): sample.append(random.randint(1, 33)) sample.append(random.randint(1,16)) if sample not in posData: num = num + 1 negData.append(sample) negLabel = [-1 for i in range(negNum)] ##train by svm prob = svm_problem(posLabel+negLabel, posData+negData) param = svm_parameter(kernel_type = LINEAR, C = 10) svc = svm_model(prob, param) svc.save('ssqsvm.model') self.emit(SIGNAL('done(bool)'), True)

 

其它关于GUI部分的代码就没有贴的必要了

 

在贴两个PyQt开发较实用的东西:

qt designer设计好的窗口ui文件 ==> .py文件

资源问题qrc     ==>  .py文件

import os for root, dirs, files in os.walk('.'): for file in files: if file.endswith('.ui'): os.system('pyuic4 -o ui_%s.py %s' % (file.rsplit('.', 1)[0], file)) elif file.endswith('.qrc'): os.system('pyrcc4 -o %s_rc.py %s' % (file.rsplit('.', 1)[0], file))

 

py2exe打包python程序为exe可执行文件

from distutils.core import setup import py2exe setup(windows=[{'script':'SsqDialog.pyw','icon_resources':[(1,"favicon.ico")]}],options={'py2exe':{'includes':['sip','PyQt4._qt','zope.interface']}})

 

然后执行>python xxx.py py2exe --include sip

 

打包后的文件真大,20、30M,大头在PyQt的库文件,压缩后6、7M

 

 

最后放上双色球号码生成器的exe可执行程序:ssq.rar

 

 

 

 

 

 

 

    

 

 

 

 

 

 

你可能感兴趣的:(第一个PyQt程序:双色球号码生成器(特色:使用SVM机器学习方法训练历史开奖数据))