这里以双色球为例,分享一个python数据分析的源码:
#!d:/python32/python 2 #coding = <utf-8>
#双色球计算
def __init__(self):
self.quanzhong = [2.618,1.618,4.236]#权重,分别为一周统计数据,一月统计数据,一年统计数据与完全随机权重的比值,表示对最后选出的号码的影响力大小,此数据依据最伟大的黄金分割点制定。
#self.quanzhong = [1,1,1]
self.qishu = [4,13,153]#一周、一月、一年的彩票期数
self.data_long = ['W','M','Y']#期数时间范围的长度表示
self.fle = '123.txt'#打开彩票文件
self.red_dict = {}#红球存储字典
self.blue_dict = {}#篮球字典
self.red_rate = {}#红球出现率字典
self.blue_rate = {}#篮球出现率字典
self.red_qz = []#红球权重
self.blue_qz = []#篮球权重
#初始化存储字典
for i in range(1,34):
self.__initdict__(i,self.red_dict)
self.__initdict__(i,self.red_rate)
for i in range(1,17):
self.__initdict__(i,self.blue_dict)
self.__initdict__(i,self.blue_rate)
#print(self.red_dict,self.blue_dict)
def __initdict__(self,num ,color_dict):
color_dict[str(num)] = {}
for x in range(len(self.data_long)):
color_dict[str(num)][self.data_long[x]] = 0
def __count__(self,num, color_dict, data_long):#统计一个号码出现的次数
#print(num)
38 color_dict[str(num)][data_long] += 1
39 #print (num,data_long)
40
41 def __countlst__(self,ball_lst,data_long):
42 #print(ball_lst)
43 #年数据存储
44 for i in range(6):
45 self.__count__(ball_lst,self.red_dict, data_long)
46 self.__count__(ball_lst[6],self.blue_dict,data_long)
47 #print(self.red_dict)
48 #print(self.blue_dict)
49
50 def __readdata__(self):
51 #从文件读取彩票中奖纪录
52 ball_file = open(self.fle, 'r')
53 ball_lst = ball_file.readline()
54 for i in range(1,self.qishu[2]):
55 ball_lst = ball_file.readline().split()
56 #print(ball_lst)
57 #red = ball_list[5:11]
58 #blue = ball_list[11:]
59 #print(ball_lst)
60 ball_lst = ball_lst[4:]
61 if i <= self.qishu[0]:
62 self.__countlst__(ball_lst,self.data_long[0])
63 if i <= self.qishu[1]:
64 self.__countlst__(ball_lst,self.data_long[1])
65 self.__countlst__(ball_lst,self.data_long[2])
66 ball_file.close()
67 #print(self.red_dict)
68 #print('------------------------')
69 #print(self.blue_dict)
70
71 def __rateone__(self,qishu,data_long):#根据统计的一段时间内(以data_long为依据)的出现次数计算1-33,1-16的出现几率
72 #计算总出现的次数
73 redall = qishu * 6
74 blueall = qishu * 1
75 #计算红球出现率
76 for i in range(1,34):
77 self.red_rate[str(i)][data_long] = self.red_dict[str(i)][data_long]/redall
78 #计算篮球出现率
79 for i in range(1,17):
80 self.blue_rate[str(i)][data_long] = self.blue_dict[str(i)][data_long]/blueall
81 def __rate__(self):
82 for i in range(len(self.data_long)):
83 self.__rateone__(self.qishu,self.data_long)
84 #print(self.red_rate)
85 def __quanzhong__(self,num,color_rate):#计算num号码对应的权重
86 value = (1/len(color_rate)- color_rate[str(num)][self.data_long[0]])* self.quanzhong[0]/sum(self.quanzhong)#一周出现概率高,那么后面,出现概率就会降
87 value += (color_rate[str(num)][self.data_long[1]] - 1/len(color_rate)) * self.quanzhong[1]/sum(self.quanzhong)
88 value += (color_rate[str(num)][self.data_long[2]] - 1/len(color_rate)) * self.quanzhong[2]/sum(self.quanzhong)
89 return value
90
91 def make_quanzhong(self):#生成整个权重的列表
92 for i in range(1,34):
93 self.red_qz.append([self.__quanzhong__(i,self.red_rate),i])
94 for i in range(1,17):
95 self.blue_qz.append([self.__quanzhong__(i,self.blue_rate),i])
96 #print(self.red_qz)
97 #print('--------------------------------------')
98 #print(self.blue_qz)
99
100
101
102 def init_data(self):#初始化读入数据、生成概率和权重数据结构
103 self.__readdata__()
104 self.__rate__()
105 self.make_quanzhong()
106
107 def _str_format(self,s):#格式化输入的字符串。形成数字列表
108 a = re.compile('\d+')
109 b = re.findall(a,s)
110 return b
111
112 def _find_qz(self,color_qz,num):#查找数字num的出现概率
113 for i in range(0,34):
114 if color_qz[1] == num:
115 return color_qz[0]
116
117 def _suiji_gl(self):#计算随机概率
118 sjgl = 1
119 for x in range(28,34):
120 sjgl *= x
121 sjgl = 1/sjgl
122 return sjgl * 1/16
123
124 def gailv(self,s):#计算概率
125 lst = self._str_format(s)
126 #print(lst)
127 lst = [int(x) for x in lst]
128 #print(lst)
129 gl = 1
130 if not lst or len(lst)>7 or len(lst)<7 or max(lst)>33 or min(lst)<1:
131 return None
132 else:
133 for x in lst[:7]:
134 gl *= (1+self._find_qz(self.red_qz,x))
135 return gl * self._suiji_gl()
136
137 def caipiao_random(self):#随机红篮球列表
138 redball = sorted(random.sample(range(1,34),6))
139 blueball = random.sample(range(1,17),1)
140 return redball + blueball
141
142 def print_random(self):#打印随机结果
143 random_ball = self.caipiao_random()
144 print('--------------------------------------------------')
145 print('红球:',random_ball[:6],'篮球:',random_ball[6:7])
146 print('--------------------------------------------------')
147
148 def print_best_number(self):#输出中奖概率最高的号码
149 print('根据规则选出的最可能中奖的号码如下:')
150 print('--------------------------------------------------')
151 print('红球:',sorted(list(x[1] for x in sorted(self.red_qz)[-6:])),'篮球:',sorted(self.blue_qz)[-1][1])
152 print('--------------------------------------------------')
153
154
155 def print_gl(self):
156 print('输入格式为-> \"(1,9,13,21,28,32)(14)\"')
157 c = input('请输入:')
158 if not self.gailv(c):
159 print('输入有误!')
160 return
161 print('--------------------------------------------------')
162 print(' 随机概率为:%0.10f%%'%(self._suiji_gl()*100))
163 print(' -------------------------')
164 print(' 自选概率为:%0.10f%%'%(self.gailv(c)*100))
165 print('--------------------------------------------------')
166 def print_all(shuangseqiu_obj):
167 print('''
168 |----------功能列表-------------|
169 |--1:打印中奖率最高代码---------|
170 |--2:打印随机号码---------------|
171 |--3:自助选取号码,打印中奖概率-|
172 |--q:退出-----------------------|
173 ''')
174
175 while True:
176 a = input('功能选择:')
177 if a == '1':
178 shuangseqiu_obj.print_best_number()
179 elif a == '2':
180 shuangseqiu_obj.print_random()
181 elif a == '3':
182 shuangseqiu_obj.print_gl();
183 elif a == 'q':
184 break;
185 else:
186 print('输入有误')
187 print('谢谢使用')
188
189 if __name__ == '__main__':
190 b = shuangseqiu()
191 b.init_data()
192 print_all(b)