LoginSignup
3
4

More than 1 year has passed since last update.

【Python】ロト6とロト7の当選データから見る傾向(次にきそうな数字とは?)

Last updated at Posted at 2022-11-26

1.はじめに

コロナ禍からテレワーク。
通勤時間にかける時間を別の事に使う時間が増えた。
少し前から始めたロト6。
気まぐれな攪拌機から飛び出す番号のかかれたボール。
何度も見送った高額当選。
前回記事とは違う角度の「前回出た数字から次回でそうな数字」の傾向を調べてみることにした。
今回はロト7の傾向も見れるようにしたい。

2.流れ

2.1

ロト6かロト7のどちらの傾向が知りたいか入力
(ロト6なら6を、ロト7なら7を入力)

2.2

開催回の範囲(開始回と終了回)を入力
ロト6の傾向を見る場合はloto6_2.csvを読み込む。
ロト7の傾向を見る場合はloto6_2.csvを読み込む。
(loto6_2.csv:ロト6の当選結果番号のcsvファイル、
loto6_7.csv:ロト7の当選結果番号のcsvファイル)
指定した範囲でdataframeを作成しておく

2.3

各数字の出現回数の辞書の作成

2.4

前回数字と今回数字の組み合わせと、2.2の範囲の出現回数の関係辞書(dict_tmp)を 表す関数の作成

表示したいタイプをロト6とした場合で、
第N回の出現数字(1,2,3,4,5,6)
第N+1回の出現数字(1,2,7,12,15,38)
の場合、組み合わせは
(1,1),(1,2),,,,(2,1),(2,2),...(6,15),(6,38)となる。
上記辞書の値を出現回数(カウント)とする。
上記組み合わせが、辞書に含まれる場合は出現回数を⁺1し、
存在しない場合は出現回数を1とする。

2.5

下記の項目で出力するcsvファイルを作成する。

前回数字 今回数字 カウント 前回数字の出現回数 割合
1 4 4 30 13
2 4 5 40 13
・・ ・・ ・・ ・・ ・・
・・ ・・ ・・ ・・ ・・
4 5 6 30 20

カウント:前回数字と今回数字の組み合わせの出現回数
割合:前回数字の出現回数におけるカウントの割合

3.ソース

開発環境:Jupyter Notebook
OS:Windows10 64bit

loto67_afternumber.py
import pandas as pd
import datetime as dt

select_type=input("loto6、loto7のどちらの傾向が知りたいですか?loto6は6,loto7は7で指定してください")
:loto67_afternumber.py
#各数字と出現回数の辞書
def make_dictnum(new_data0,type0):
  row0=new_data0.shape[0]  
  dict_tmp={}
  for i in range(0,row0-1):
    for j in range(1,int(type0)+1):
      key0=str(new_data0.iloc[i,j])
      if key0 in dict_tmp:
        dict_tmp[key0]=int(dict_tmp[key0])+1
      else:
        dict_tmp[key0]=1
  return dict_tmp  
loto67_afternumber.py
#今回出た数字と、次回出た数字の組み合わせとその出現回数の辞書
def make_dictafter(new_data0,type0):

  row0=new_data0.shape[0]  
  dict_tmp={}
  for i in range(0,row0-1):
    for j in range(1,int(type0)+1):
      for k in range(1,int(type0)+1):
        key0=str(new_data0.iloc[i,j])+","+str(new_data0.iloc[i+1,k])        

        if key0 in dict_tmp:
          dict_tmp[key0]=int(dict_tmp[key0])+1
        else:
          dict_tmp[key0]=1
 
  return dict_tmp
loto67_afternumber.py
# dict0:組み合わせとカウント数の辞書
# dict1:nstart0~nend0回における各数字の出現数の辞書
# type0:loto6 or loto7
def make_csvfile(dict0,dict1,nstart0,nend0,type0):  
  
  #空のデータフレーム作成
  new_df2=pd.DataFrame()
  keys0=dict0.keys()

  #カウント数を入れる配列
  list_val=[]
  for k in keys0:
    val0=dict0[k]
    if not val0 in list_val:
      list_val.append(val0)
      
  print(list_val)
  
  row1=0
  for k in keys0:
    splitstr=k.split(',')
    col1=0
    #組み合わせの出現回数
    val=dict0[k]
    #前回数字の出現回数
    val1=dict1[splitstr[0]]
    #valの出現割合
    val2=int(val*100/val1)

   
    tmpnum = pd.Series([splitstr[0], splitstr[1],str(val),str(val1),str(val2)],index=['前回数字','今回数字','カウント数','前回数字出現回数','割合'])
    new_df2=new_df2.append(tmpnum,ignore_index=True)
    row1=row1+1
    
  print(new_df2)
  csvfile="loto"+type0+"after_from"+str(nstart0)+"_to"+str(nend0)+".csv"
  print(csvfile)
  new_df2.to_csv(csvfile,encoding="cp932",index=False)    
  
loto67_afternumber.py
if select_type.isdecimal()==True:
  if int(select_type)!=6 and int(select_type)!=7:
    print("6か7で指定してください")
  else:
    #文字コードを指定して読み込む
    if int(select_type)==6:
      df = pd.read_csv("loto6_2.csv",encoding="sjis")
      new_data=df[['開催回','第1数字','第2数字','第3数字','第4数字','第5数字','第6数字']]
    else:
      df = pd.read_csv("loto7_2.csv",encoding="sjis")
      new_data=df[['開催回','第1数字','第2数字','第3数字','第4数字','第5数字','第6数字','第7数字']]

    row=df.shape[0]
    print(df)
    print(row)
    nstart=input("nstart(集計開始回)\n") 
    nend=input("nend(カウント集計終了回)\n")    

    if nstart.isdecimal()==True and nend.isdecimal()==True:

      #開始が0より大きくrow未満かつ開始が終了未満、終了がrow以下  
      if int(nstart)>0 and int(nstart) < row and int(nstart)<int(nend) and int(nend) <= row:

        new_data3=new_data[int(nstart)-1:int(nend)]     
        dict_afternum=make_dictafter(new_data3,select_type)
        print(new_data3)
        make_csvfile(dict_afternum,int(nstart),int(nend),select_type)        
      else:
        print("nstartは1以上"+str(row)+"未満で、nendはnstartより大きく"+str(row)+"以下で指定")
    else:
      print("nstart、nendは数値を指定")
else:
  print("6か7で指定してください")  
    

4.出力結果

loto7,nstart=1,nend=498とした場合

loto6、loto7のどちらの傾向が知りたいですか?loto6は6,loto7は7で指定してください7
498
nstart(集計開始回)
1
nend(カウント集計終了回)
498
     開催回  第1数字  第2数字  第3数字  第4数字  第5数字  第6数字  第7数字
0      1     7    10    12    17    23    28    34
1      2    20    24    29    31    33    34    35
2      3     2     7     8    11    14    23    31
3      4    12    13    22    23    24    28    29
4      5     1     3     4     5    16    21    28
..   ...   ...   ...   ...   ...   ...   ...   ...
493  494     7     9    12    22    28    29    32
494  495     2    16    19    20    22    33    36
495  496     5     8    15    18    25    27    36
496  497    18    20    24    26    27    30    33
497  498     6     8    12    20    21    24    29

[498 rows x 8 columns]
[20, 18, 21, 19, 23, 17, 14, 16, 13, 10, 25, 11, 15, 22, 24, 7, 9, 28, 12, 27, 26, 31, 29, 8, 32, 30, 6]
     前回数字 今回数字 カウント数 前回数字出現回数  割合
0       7   20    20       96  20
1       7   24    20       96  20
2       7   29    18       96  18
3       7   31    18       96  18
4       7   33    21       96  21
...   ...  ...   ...      ...  ..
1364   31   26    16      102  15
1365   17   17    15       96  15
1366    1   33    10       88  11
1367   30   37    12      107  11
1368   33   20    12       79  15

[1369 rows x 5 columns]
loto7after_from1_to498.csv

5.参考資料

KYO's LOTO
当選番号データだけではなく、各数字の出現回数など様々なデータを収集されています。

6.さいごに

各数字の出現数は偏りがないが、
数字の組み合わせによって、出やすいものと出にくいものがあることがわかる。

例えばロト7において第1回から第498回までのデータを見ると
5の次に15が出る割合が32%、18の次に8が出る割合が6%となっている。

32%というと可能性が高く思えるが、
5の次に15以外の数字が出る確率が68%
ということも忘れてはならない。

やはり一攫千金の道は険しく、謎多き撹拌機はよくできているという事なのだろう。

しかし、買わなければチャンスはないのだ。
サッカードイツ戦の波に乗って、またチャレンジするつもりだ。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4