Created
September 7, 2017 06:58
-
-
Save KimMyungSam/c060830a4356d064257aa3c8f7b379e2 to your computer and use it in GitHub Desktop.
winlotto 번호추출 2017.0.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 로또번호 추정하여 만들기 순서\n", | |
"1. 로또 홈페이지 크롤링\n", | |
"2. 최근 당첨번호 트랜드 분석\n", | |
"3. 번호 선정 로직 생성하기\n", | |
"4. random하게 번호 뽑기\n", | |
"4. 번호 선정후 1~45 경우수와 (결과)선정된 번호의 경우수 비교하기\n", | |
"5. 생성시마다 10개번호 산출" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<font color='red'>\n", | |
" <ul>\n", | |
" <li>1자리~6자리 합계 정규분포에서 상위 95%, 하위 5% 수준으로 분석시 max=188, min=84. 6개 숫자 합의 범위 188 ~ 84로 제한함(정규분포 90%만 인정)</li>\n", | |
" <li>홀수, 짝수 조합분석시 6개모두 홀수, 짝수 일 경우수는 1.82% 임으로 6개모두 짝/홀수 경우는 제외</li>\n", | |
" <li>color band는 3개 또는 4개 조합이 가장 높음.</li>\n", | |
" <li>연속번호 당첨 1,2조합, 1,2,3조합만.</li>\n", | |
" <li>동일한 끝자리수가 3회 이상인 조합은 버림 </li>\n", | |
" </ul>\n", | |
"</font>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<head>mysql> describe winlotto;</head>\n", | |
"<pre>\n", | |
"+------------+-------------+------+-----+---------+-------+\n", | |
"| Field | Type | Null | Key | Default | Extra |\n", | |
"+------------+-------------+------+-----+---------+-------+\n", | |
"| count | int(10) | NO | | NULL | |\n", | |
"| 1 | int(2) | NO | | NULL | |\n", | |
"| 2 | int(2) | NO | | NULL | |\n", | |
"| 3 | int(2) | NO | | NULL | |\n", | |
"| 4 | int(2) | NO | | NULL | |\n", | |
"| 5 | int(2) | NO | | NULL | |\n", | |
"| 6 | int(2) | NO | | NULL | |\n", | |
"| 7 | int(2) | NO | | NULL | |\n", | |
"| persons | int(2) | NO | | NULL | |\n", | |
"| amounts | varchar(20) | NO | | NULL | |\n", | |
"| total | int(5) | NO | | NULL | |\n", | |
"| odd | int(2) | NO | | NULL | |\n", | |
"| even | int(2) | NO | | NULL | |\n", | |
"| yellow | int(2) | NO | | NULL | |\n", | |
"| blue | int(2) | NO | | NULL | |\n", | |
"| red | int(2) | NO | | NULL | |\n", | |
"| green | int(2) | NO | | NULL | |\n", | |
"| gray | int(2) | NO | | NULL | |\n", | |
"| band | int(2) | NO | | NULL | |\n", | |
"| 1continue | int(2) | NO | | NULL | |\n", | |
"| 2continue | int(2) | NO | | NULL | |\n", | |
"| 3continue | int(2) | NO | | NULL | |\n", | |
"| 4continue | int(2) | NO | | NULL | |\n", | |
"| endigDigit | int(2) | NO | | NULL | |\n", | |
"+------------+-------------+------+-----+---------+-------+\n", | |
" </pre>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"#lotto.py\n", | |
"%matplotlib inline\n", | |
"\n", | |
"import requests\n", | |
"from bs4 import BeautifulSoup\n", | |
"import mysql.connector\n", | |
"import sqlalchemy\n", | |
"from sqlalchemy import create_engine\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import pandas as pd\n", | |
"import random\n", | |
"\n", | |
"#웹 크롤링 한 결과를 저장할 리스트\n", | |
"lotto_list = []\n", | |
"\n", | |
"#로또 웹 사이트의 첫 주소\n", | |
"main_url = \"http://www.nlotto.co.kr/gameResult.do?method=byWin\"\n", | |
"\n", | |
"#웹 크롤릴 주소\n", | |
"basic_url = \"http://www.nlotto.co.kr/gameResult.do?method=byWin&drwNo=\"\n", | |
"\n", | |
"def getLast():\n", | |
" resp = requests.get(main_url)\n", | |
" soup = BeautifulSoup(resp.text, \"lxml\") \n", | |
" line = str(soup.find(\"meta\", {\"id\" : \"desc\", \"name\" : \"description\"})['content']) \n", | |
" \n", | |
" begin = line.find(\" \") \n", | |
" end = line.find(\"회\") \n", | |
"\n", | |
" if begin == -1 or end == -1: \n", | |
" print(\"not found last lotto number\") \n", | |
" exit() \n", | |
" return int(line[begin + 1 : end])\n", | |
"\n", | |
"def checkLast():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" sql = \"SELECT MAX(count) FROM winlotto\"\n", | |
" \n", | |
" try:\n", | |
" count = connector.execute(sql)\n", | |
" result = count.fetchone()\n", | |
" if result[0] is None:\n", | |
" result = [1,]\n", | |
" \n", | |
" except Exception as err:\n", | |
" print(str(err))\n", | |
" \n", | |
" connector.close()\n", | |
" \n", | |
" return result[0]\n", | |
"\n", | |
"def crawler(fromPos,toPos):\n", | |
" for i in range(fromPos,toPos + 1):\n", | |
" crawler_url = basic_url + str(i)\n", | |
" \n", | |
" resp = requests.get(crawler_url)\n", | |
" soup = BeautifulSoup(resp.text, \"lxml\")\n", | |
" '''\n", | |
" 개발자 모드로 분석하여 HTML Tag로 찾을때\n", | |
" div_data = soup.find_all('div', class_='lotto_win_number mt12')\n", | |
" p_data = div_data[0].find_all('p',class_='number')\n", | |
" img_number = p_data[0].find_all('img')\n", | |
" '''\n", | |
" line = str(soup.find(\"meta\", {\"id\" : \"desc\", \"name\" : \"description\"})['content'])\n", | |
" print(\"당첨회차: \" + str(i))\n", | |
" \n", | |
" begin = line.find(\"당첨번호\")\n", | |
" begin = line.find(\" \", begin) + 1\n", | |
" end = line.find(\".\", begin)\n", | |
" numbers = line[begin:end] \n", | |
" print(\"당첨번호: \" + numbers)\n", | |
" \n", | |
" begin = line.find(\"총\")\n", | |
" begin = line.find(\" \", begin) + 1\n", | |
" end = line.find(\"명\", begin)\n", | |
" persons = line[begin:end]\n", | |
" print(\"당첨인원: \" + persons)\n", | |
" \n", | |
" begin = line.find(\"당첨금액\")\n", | |
" begin = line.find(\" \", begin) + 1\n", | |
" end = line.find(\"원\", begin)\n", | |
" amount = line[begin:end]\n", | |
" print(\"당첨금액: \" + amount)\n", | |
" \n", | |
" info = {}\n", | |
" info[\"회차\"] = i\n", | |
" info[\"번호\"] = numbers \n", | |
" info[\"당첨자\"] = persons \n", | |
" info[\"금액\"] = amount \n", | |
" \n", | |
" lotto_list.append(info)\n", | |
"\n", | |
"def insert():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
"\n", | |
" for dic in lotto_list:\n", | |
" count = dic[\"회차\"]\n", | |
" numbers = dic[\"번호\"]\n", | |
" persons = dic[\"당첨자\"] \n", | |
" amounts = dic[\"금액\"]\n", | |
" odd = 0 # 홀수\n", | |
" even = 0 # 짝수\n", | |
" yellow = 0 # 1~10\n", | |
" blue = 0 # 11~20\n", | |
" red = 0 # 21~30\n", | |
" green = 0 # 31~40\n", | |
" gray = 0 # 41 ~ 45\n", | |
" band = 0 #숫자 밴드 카운트\n", | |
" winNumbers = []\n", | |
" lotto_continue = 0\n", | |
" lotto_2continue = 0\n", | |
" lotto_3continue = 0\n", | |
" lotto_4continue = 0\n", | |
" \n", | |
" print(\"insert to database at \" + str(count)) \n", | |
" numberlist = str(numbers).split(\",\") \n", | |
" \n", | |
" winNumbers.append(int(numberlist[0]))\n", | |
" winNumbers.append(int(numberlist[1]))\n", | |
" winNumbers.append(int(numberlist[2]))\n", | |
" winNumbers.append(int(numberlist[3]))\n", | |
" winNumbers.append(int(numberlist[4]))\n", | |
" winNumbers.append(int(numberlist[5].split(\"+\")[0]))\n", | |
" winNumbers.append(int(numberlist[5].split(\"+\")[1]))\n", | |
"\n", | |
" persons = int(persons)\n", | |
" total = sum(winNumbers[0:6])\n", | |
" \n", | |
" # 홀수갯수 구하기\n", | |
" for i in range(0,6):\n", | |
" if (winNumbers[i] % 2 != 0):\n", | |
" odd = odd + 1;\n", | |
" even = 6 - odd # 짝수갯수는 6 - 홀수갯수 \n", | |
" \n", | |
" # bamd 구분하기\n", | |
" for i in range(0,6):\n", | |
" if (winNumbers[i] <= 10):\n", | |
" yellow += 1\n", | |
" elif (winNumbers[i] >= 11 and winNumbers[i] <= 20):\n", | |
" blue += 1\n", | |
" elif (winNumbers[i] >= 21 and winNumbers[i] <= 30):\n", | |
" red += 1\n", | |
" elif (winNumbers[i] >= 31 and winNumbers[i] <= 40): \n", | |
" green += 1\n", | |
" elif (winNumbers[i] >= 41 and winNumbers[i] <= 45): \n", | |
" gray += 1\n", | |
" if (yellow > 0):\n", | |
" band += 1\n", | |
" if (blue > 0):\n", | |
" band += 1\n", | |
" if (red > 0):\n", | |
" band += 1\n", | |
" if (green > 0):\n", | |
" band += 1\n", | |
" if (gray > 0):\n", | |
" band += 1\n", | |
" \n", | |
" #continure number 구하기\n", | |
" #1 연번\n", | |
" if (winNumbers[1] - winNumbers[0] == 1):\n", | |
" lotto_continue += 1\n", | |
" elif (winNumbers[2] - winNumbers[1] == 1):\n", | |
" lotto_continue += 1\n", | |
" elif (winNumbers[3] - winNumbers[2] == 1):\n", | |
" lotto_continue += 1\n", | |
" elif (winNumbers[4] - winNumbers[3] == 1):\n", | |
" lotto_continue += 1\n", | |
" elif (winNumbers[5] - winNumbers[4] == 1):\n", | |
" lotto_continue += 1\n", | |
" \n", | |
" #2 연번\n", | |
" if (winNumbers[2] - winNumbers[0] == 2):\n", | |
" lotto_2continue += 1\n", | |
" lotto_continue -= 1\n", | |
" elif (winNumbers[3] - winNumbers[1] == 2):\n", | |
" lotto_2continue += 1\n", | |
" lotto_continue -= 1\n", | |
" elif (winNumbers[4] - winNumbers[2] == 2):\n", | |
" lotto_2continue += 1\n", | |
" lotto_continue -= 1\n", | |
" elif (winNumbers[5] - winNumbers[3] == 2):\n", | |
" lotto_2continue += 1\n", | |
" lotto_continue -= 1\n", | |
" \n", | |
" #3 연번\n", | |
" if (winNumbers[3] - winNumbers[0] == 3):\n", | |
" lotto_3continue += 1\n", | |
" lotto_2continue -= 1\n", | |
" elif (winNumbers[4] - winNumbers[1] == 3):\n", | |
" lotto_3continue += 1\n", | |
" lotto_2continue -= 1\n", | |
" elif (winNumbers[5] - winNumbers[2] == 3):\n", | |
" lotto_3continue += 1\n", | |
" lotto_2continue -= 1\n", | |
" \n", | |
" #4 연번\n", | |
" if (winNumbers[4] - winNumbers[0] == 4):\n", | |
" lotto_4continue += 1\n", | |
" lotto_3continue += 1\n", | |
" elif (winNumbers[5] - winNumbers[1] == 4):\n", | |
" lotto_4continue += 1\n", | |
" lotto_3continue += 1\n", | |
" \n", | |
" #끝자리수 횟수 확인\n", | |
" ending_digit = []\n", | |
" \n", | |
" for i in range(0,6):\n", | |
" if (winNumbers[i] <= 9):\n", | |
" ending_digit.append(winNumbers[i])\n", | |
" elif (winNumbers[i] >= 10 and winNumbers[i] <= 19):\n", | |
" ending_digit.append(winNumbers[i] - 10)\n", | |
" elif (winNumbers[i] >= 20 and winNumbers[i] <= 29):\n", | |
" ending_digit.append(winNumbers[i] - 20)\n", | |
" elif (winNumbers[i] >= 30 and winNumbers[i] <= 39): \n", | |
" ending_digit.append(winNumbers[i] - 30)\n", | |
" elif (winNumbers[i] >= 40 and winNumbers[i] <= 45): \n", | |
" ending_digit.append(winNumbers[i] - 40)\n", | |
" unique_elements, counts_elements = np.unique(ending_digit, return_counts=True)\n", | |
" max_ending_digit_count = int(max(counts_elements)) # max count\n", | |
" \n", | |
" '''\n", | |
" 아래 코드를 사용하면 sql문 에러 발생으로 시행되지 않음\n", | |
" sql = \"INSERT INTO winlotto (count, 1, 2, 3, 4, 5, 6, 7, persons, amounts)\\\n", | |
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\"\n", | |
" connector.execute(sql, count, i1, i2, i3, i4, i5, i6, 7, persons, amounts) \n", | |
" \n", | |
" '''\n", | |
" # sql문 생성시 table name 으로 표기함.\n", | |
" sql = \"INSERT INTO winlotto VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\"\n", | |
"\n", | |
" try: \n", | |
" connector.execute(sql, count,winNumbers[0], winNumbers[1],winNumbers[2],winNumbers[3],winNumbers[4],\\\n", | |
" winNumbers[5],winNumbers[6],persons, amounts, total, odd, even,yellow, blue, red, green, gray, band,\\\n", | |
" lotto_continue, lotto_2continue, lotto_3continue, lotto_4continue, max_ending_digit_count)\n", | |
" except Exception as err:\n", | |
" print(str(err))\n", | |
" break\n", | |
" \n", | |
" connector.close()\n", | |
"\n", | |
"def analysis_max():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" #각 자리수별 뽑힌 숫자들 전체를 조회\n", | |
" for i in range(1,8):\n", | |
" sql = \"select `\"\n", | |
" sql += str(i)\n", | |
" sql += \"` from winlotto\"\n", | |
" \n", | |
" try:\n", | |
" nums = connector.execute(sql)\n", | |
" results = nums.fetchall()\n", | |
"\n", | |
" #해당 숫자의 뽑힌 횟수를 하나씩증가\n", | |
" lottoarray = [0 for i in range(46)]\n", | |
" for row in results:\n", | |
" k = row[0]\n", | |
" count = lottoarray[k]\n", | |
" lottoarray[k] = count + 1\n", | |
" print (i, \"자리 max count 숫자 =\", lottoarray.index(max(lottoarray)))\n", | |
" except Exception as err:\n", | |
" print(str(err))\n", | |
" \n", | |
" connector.close()\n", | |
"\n", | |
"def analysis():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" #0부터 45까지의 배열을 생성하고 0으로 초기화\n", | |
" lottoarray = [0 for i in range(46)]\n", | |
" \n", | |
" #각 자리수별 뽑힌 숫자들 전체를 조회\n", | |
" for i in range(1,8):\n", | |
" sql = \"select `\"\n", | |
" sql += str(i)\n", | |
" sql += \"` from winlotto\"\n", | |
" \n", | |
" try:\n", | |
" nums = connector.execute(sql)\n", | |
" results = nums.fetchall()\n", | |
"\n", | |
" #해당 숫자의 뽑힌 횟수를 하나씩증가\n", | |
"\n", | |
" for row in results:\n", | |
" k = row[0]\n", | |
" count = lottoarray[k]\n", | |
" lottoarray[k] = count + 1\n", | |
" \n", | |
" except Exception as err:\n", | |
" print(str(err))\n", | |
" \n", | |
" print (\"전체 숫자 당첨 카운수\")\n", | |
" for i in range(1, len(lottoarray)): \n", | |
" if (i % 10) == 0: \n", | |
" print(\"\") # 10개 마다 줄 바꾸기\n", | |
" print(\"[\" + str(i) + \":\" + str(lottoarray[i]) + \"]\", end=\" \") \n", | |
" print()\n", | |
" \n", | |
" connector.close()\n", | |
"\n", | |
"def sum_analysis():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" df = pd.read_sql(\"SELECT total FROM winlotto WHERE count > 600\", con = connector)\n", | |
" sum = df.values\n", | |
" print ('max = ', max(sum), 'min = ', min(sum))\n", | |
" \n", | |
" print ('상,하위 분위수별 수 quantile 0.95 = ', quantile(sum, 0.95), 'quantile 0.05 = ', quantile(sum, 0.05))\n", | |
" quantileCount = count(sum, quantile(sum, 0.95),quantile(sum, 0.05)) # 퍼센티지 구하기\n", | |
" print (\"회수별 sum대비 분위수의 점유율 = \",\"%3.2f\" %(quantileCount/len(sum)))\n", | |
" \n", | |
" print ('상,하위 분위수별 수 quantile 0.90 = ', quantile(sum, 0.90), 'quantile 0.1 = ', quantile(sum, 0.1))\n", | |
" quantileCount = count(sum, quantile(sum, 0.90),quantile(sum, 0.1)) # 퍼센티지 구하기\n", | |
" print (\"회수별 sum대비 분위수의 점유율 = \",\"%3.2f\" %(quantileCount/len(sum)))\n", | |
" print ()\n", | |
" \n", | |
" plt.figure(figsize=(18,10))\n", | |
" plt.hist(sum, bins=35, facecolor='red', alpha=0.4, histtype='stepfilled')\n", | |
" plt.hist(sum, bins=40, facecolor='green', alpha=0.4, histtype='stepfilled')\n", | |
" plt.hist(sum, bins=45, facecolor='black', alpha=0.4, histtype='stepfilled')\n", | |
" plt.xlabel('sum')\n", | |
" plt.ylabel('count')\n", | |
" \n", | |
" connector.close()\n", | |
"\n", | |
"def oddEven():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" df = pd.read_sql(\"select odd from winlotto WHERE count > 600\", con = connector)\n", | |
" odd = df.values\n", | |
" unique_elements, counts_elements = np.unique(odd, return_counts=True)\n", | |
" print (\"당첨번호 6개중 홀수 번호가 나온 총 갯수\")\n", | |
" print (np.asarray((unique_elements,counts_elements)), \"전체회차 = \", len(odd))\n", | |
" \n", | |
" df = pd.read_sql(\"select even from winlotto WHERE count > 600\", con = connector)\n", | |
" even = df.values\n", | |
" unique_elements, counts_elements = np.unique(even, return_counts=True)\n", | |
" print (\"당첨번호 6개중 짝수 번호가 나온 총 갯수\")\n", | |
" print (np.asarray((unique_elements,counts_elements)), \"전체회차 = \", len(even))\n", | |
" print ()\n", | |
" \n", | |
" plt.figure(figsize=(18,10))\n", | |
" plt.hist(odd, bins=6)\n", | |
" # plt.hist(even, bins=6)\n", | |
" \n", | |
" connector.close()\n", | |
" \n", | |
" \n", | |
"def count(sum, maxi, mini):\n", | |
" count = 0\n", | |
" \n", | |
" for num in sum:\n", | |
" if (num >= mini and num <= maxi):\n", | |
" count += 1\n", | |
" return count\n", | |
"\n", | |
"def quantile(x,p):\n", | |
" p_index = int(p * len(x)) \n", | |
" return sorted(x)[p_index]\n", | |
"\n", | |
"def mean(x, y):\n", | |
" return sum(y) / len(x)\n", | |
"\n", | |
"def bandCount():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" band = pd.read_sql(\"select band from winlotto\", con = connector)\n", | |
" \n", | |
" unique_elements, counts_elements = np.unique(band, return_counts=True)\n", | |
" print (\"컬러별 밴드의 수\")\n", | |
" print (np.asarray((unique_elements,counts_elements)), \"전체회차 = \", len(band))\n", | |
" print ()\n", | |
" \n", | |
" plt.figure(figsize=(18,10))\n", | |
" plt.hist(band.values)\n", | |
" \n", | |
" connector.close()\n", | |
"\n", | |
"def unUsed():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" count = 10\n", | |
" courrentCount = pd.read_sql(\"SELECT max(count) FROM winlotto\", con = connector)\n", | |
" beginCount = courrentCount - count\n", | |
" begin = int(beginCount.iloc[0])\n", | |
" \n", | |
" used = pd.read_sql(\"SELECT `1`,`2`,`3`,`4`,`5`,`6` FROM winlotto WHERE count >= %s\" %begin, con = connector)\n", | |
" used = np.unique(used)\n", | |
" \n", | |
" # unUsed = used - lotto_number\n", | |
" unUsed = []\n", | |
" for i in range(1,46):\n", | |
" unUsed.append(i)\n", | |
" \n", | |
" for i in used:\n", | |
" unUsed.remove(i)\n", | |
" \n", | |
" print ('조회한 회차수 = ', count)\n", | |
" print ('사용할 번호는 = ', used)\n", | |
" print ('사용할 번호 갯수는 = ', len(used))\n", | |
" print ()\n", | |
" \n", | |
" connector.close()\n", | |
" return used\n", | |
" \n", | |
"def generate(numlist):\n", | |
" ConditionCount = 0\n", | |
" count = 0\n", | |
" lotto_continue = 0\n", | |
" numlist = set(numlist)\n", | |
" targetBand = 3\n", | |
" winNumber = []\n", | |
" result_count = 0 #5개 추출을 위한 카운터\n", | |
" \n", | |
"\n", | |
" while True:\n", | |
" # 예측번호로 부터 6개 뽑아내기\n", | |
" result = sorted(random.sample(numlist,6))\n", | |
" count += 1\n", | |
" \n", | |
" # band 구하기\n", | |
" yellow = 0 # 1~10\n", | |
" blue = 0 # 11~20\n", | |
" red = 0 # 21~30\n", | |
" green = 0 # 31~40\n", | |
" gray = 0 # 41 ~ 45\n", | |
" band = 0 #숫자 밴드 카운트\n", | |
" \n", | |
" for i in range(0,6):\n", | |
" if (result[i] <= 10):\n", | |
" yellow += 1\n", | |
" elif (result[i] >= 11 and result[i] <= 20):\n", | |
" blue += 1\n", | |
" elif (result[i] >= 21 and result[i] <= 30):\n", | |
" red += 1\n", | |
" elif (result[i] >= 31 and result[i] <= 40): \n", | |
" green += 1\n", | |
" elif (result[i] >= 41 and result[i] <= 45): \n", | |
" gray += 1\n", | |
" \n", | |
" #band 카운트\n", | |
" if (yellow > 0):\n", | |
" band += 1\n", | |
" if (blue > 0):\n", | |
" band += 1\n", | |
" if (red > 0):\n", | |
" band += 1\n", | |
" if (green > 0):\n", | |
" band += 1\n", | |
" if (gray > 0):\n", | |
" band += 1\n", | |
"\n", | |
" # 홀수갯수 구하기\n", | |
" odd = 0\n", | |
" for i in range(0,6):\n", | |
" if (result[i] % 2 != 0):\n", | |
" odd = odd + 1;\n", | |
" even = 6 - odd\n", | |
" \n", | |
" # sum 점수 구하기.max=188, min=84\n", | |
" total = sum(result[0:6])\n", | |
" \n", | |
" #3자리 연번이상 확인하기\n", | |
" if (result[3] - result[0] == 3): #3 연번\n", | |
" lotto_continue += 1\n", | |
" elif (result[4] - result[1] == 3):\n", | |
" lotto_continue += 1\n", | |
" elif (result[5] - result[2] == 3):\n", | |
" lotto_continue += 1\n", | |
"\n", | |
" if (result[4] - result[0] == 4): #4 연번\n", | |
" lotto_continue += 1\n", | |
" elif (result[5] - result[1] == 4):\n", | |
" lotto_continue += 1\n", | |
" \n", | |
" #끝수 max count 확인\n", | |
" ending_digit = []\n", | |
"\n", | |
" for i in range(0,6):\n", | |
" if (result[i] <= 9):\n", | |
" ending_digit.append(result[i])\n", | |
" elif (result[i] >= 10 and result[i] <= 19):\n", | |
" ending_digit.append(result[i] - 10)\n", | |
" elif (result[i] >= 20 and result[i] <= 29):\n", | |
" ending_digit.append(result[i] - 20)\n", | |
" elif (result[i] >= 30 and result[i] <= 39): \n", | |
" ending_digit.append(result[i] - 30)\n", | |
" elif (result[i] >= 40 and result[i] <= 45): \n", | |
" ending_digit.append(result[i] - 40)\n", | |
" unique_elements, counts_elements = np.unique(ending_digit, return_counts=True)\n", | |
" max_ending_digit_count = max(counts_elements)\n", | |
" \n", | |
" \n", | |
" #3등에 당첨됨 count\n", | |
" \n", | |
" #4등에 당첨된 Count\n", | |
" \n", | |
" # 1등에 당첨된 count \n", | |
" # band = 3,4 경우, 각 5개 추출\n", | |
" if (odd != 6) and (even != 6) and (targetBand == band) and (total <= 188) and (total >= 84) and (lotto_continue == 0) and (max_ending_digit_count < 3):\n", | |
" ConditionCount += 1\n", | |
" if (ConditionCount > random.randint(100000,1000000)): #십만에서 백만중 하나 추출하여 count횟수가 그만큼 클때 인정\n", | |
" winNumber.append(result)\n", | |
" result_count += 1\n", | |
" ConditionCount = 0 # 0으로 초기화\n", | |
" lotto_continue = 0\n", | |
" # print ('band = %s, sum = %s, result = %s' %(band, total, result))\n", | |
" else:\n", | |
" lotto_continue = 0 # 연번 변수 0으로 초기화\n", | |
" \n", | |
" if result_count > 5: # 5개번호 추출후 band를 4로 변경\n", | |
" targetBand = 4\n", | |
" if result_count > 10: #10개번호 추출후 종료\n", | |
" break\n", | |
" return winNumber\n", | |
"\n", | |
"def continue_number():\n", | |
" pwd = 'rlaehgus1'\n", | |
" engine = create_engine('mysql+mysqlconnector://root:'+pwd+'@localhost/lotto', echo=False)\n", | |
" connector = engine.connect()\n", | |
" \n", | |
" numbers = pd.read_sql(\"SELECT `1continue`,`2continue`,`3continue`,`4continue` FROM winlotto\", con = connector)\n", | |
" \n", | |
" plt.plot(numbers)\n", | |
" \n", | |
" unique_elements, counts_elements = np.unique(numbers, return_counts=True)\n", | |
" print (\"연번의 합계\")\n", | |
" print (np.asarray((unique_elements,counts_elements)), \"전체회차 = \", len(numbers))\n", | |
" print ()\n", | |
" \n", | |
" connector.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"조회한 회차수 = 10\n", | |
"사용할 번호는 = [ 1 3 4 5 7 8 9 10 11 12 15 16 20 21 22 23 24 26 27 29 30 31 32 34 35\n", | |
" 36 38 39 41 42 43 44 45]\n", | |
"사용할 번호 갯수는 = 33\n", | |
"\n", | |
"total = 152, winNumber = [11, 23, 26, 27, 29, 36]\n", | |
"total = 126, winNumber = [5, 7, 22, 23, 27, 42]\n", | |
"total = 101, winNumber = [9, 10, 12, 20, 21, 29]\n", | |
"total = 129, winNumber = [7, 9, 10, 16, 42, 45]\n", | |
"total = 180, winNumber = [21, 22, 29, 31, 35, 42]\n", | |
"total = 127, winNumber = [11, 15, 16, 20, 23, 42]\n", | |
"total = 186, winNumber = [3, 23, 36, 38, 42, 44]\n", | |
"total = 101, winNumber = [1, 7, 10, 15, 29, 39]\n", | |
"total = 172, winNumber = [10, 21, 23, 32, 42, 44]\n", | |
"total = 141, winNumber = [10, 11, 24, 26, 27, 43]\n" | |
] | |
} | |
], | |
"source": [ | |
"def main():\n", | |
" # 최신 추첨 회차 확인\n", | |
" last = getLast()\n", | |
" dblast = checkLast()\n", | |
" \n", | |
" #신규 회차확인시 크롤링 \n", | |
" if dblast < last:\n", | |
" print(\"최신 회차는 \" + str(last) + \" 회 이며, 데이터베이스에는 \" + str(dblast) + \"회 까지 저장되어 있습니다.\") \n", | |
" print(\"업데이트를 시작합니다.\") \n", | |
" crawler(dblast, last)\n", | |
" \n", | |
" #신규 회차 있을때 db update\n", | |
" if len(lotto_list) > 0:\n", | |
" insert()\n", | |
"\n", | |
" # 최근 횟수를 주고 사용되지 않은 숫자 확인\n", | |
" numlist = unUsed()\n", | |
"\n", | |
" # 당첨번호 생성하기\n", | |
" winNumber = generate(numlist) #band 3, band 4\n", | |
" for i in range(0,10):\n", | |
" print ('total = %s, winNumber = %s' %(sum(winNumber[i]), winNumber[i]))\n", | |
" \n", | |
" # 끝수 통계\n", | |
" \n", | |
" '''\n", | |
" # 연번호 확인하기\n", | |
" continue_number()\n", | |
" \n", | |
" #자리별 count\n", | |
" bandCount()\n", | |
"\n", | |
" # 회차별 6개 숫자 sum 확인\n", | |
" sum_analysis()\n", | |
" \n", | |
" # 홀수, 짝수 회차별 갯수 확인\n", | |
" oddEven()\n", | |
"\n", | |
" # 자리수별 max number\n", | |
" analysis_max()\n", | |
" \n", | |
" #1~45 숫자 출현 횟수\n", | |
" analysis()\n", | |
" '''\n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment