파이썬 머신러닝ML
파이썬 로또 번호 예측 하기 #4
양기호니
2022. 12. 5. 22:39
728x90
반응형
혼자 공부하려고 정리했어요~
이전 포스팅 링크 참고하세요~!
이번에는 DecisionTreeRegressor를 이용하여 로또 번호를 예측해보겠습니다.
훈련 데이터는 앞서 포스팅한 내용을 참고하세요~
https://gihoni.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%A1%9C%EB%98%90-%EB%B2%88%ED%98%B8-%EC%98%88%EC%B8%A1-%ED%95%98%EA%B8%B0-1
저번 포스팅에는 회차와 추첨년도, 추첨날짜만으로 당첨 번호를 예측해봤습니다.
회차와 추첨년도, 추첨날짜, 당첨번호를 통해 다음 번호 예측하기!
번호를 첫번째, .... 여섯번째 각각 예측해볼 생각입니다.
from sklearn.pipeline import make_pipeline
from sklearn.metrics import r2_score
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split
rows = data.to_numpy()
row_count = len(rows)
pre_counts = row_count
now_counts = row_count-1
x_samples = x_data[1:pre_counts]
y_samples = y_data[0:now_counts]
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['1'], test_size=0.1)
model11 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model11.fit(X_train,y_train)
print(model11.score(X_train,y_train))
print(model11.score(X_test,y_test))
y_tr_pred = model11.predict(X_train)
y_te_pred = model11.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
#출력값
1.0
-1.2862096791782078
정확도 train : 1.0
정확도 test : -1.2862096791782078
학습한 데이터는 잘 맞추지만...
새롭게 등장하는 시험데이터는 잘 맞추지 못하네요...
정확도가 마이너스가 나와버리는군요...ㅎㅎ
다른 번호들도 이어가보겠습니다.
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['2'], test_size=0.1)
model22 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model22.fit(X_train,y_train)
print(model22.score(X_train,y_train))
print(model22.score(X_test,y_test))
y_tr_pred = model22.predict(X_train)
y_te_pred = model22.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['3'], test_size=0.1)
model33 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model33.fit(X_train,y_train)
print(model33.score(X_train,y_train))
print(model33.score(X_test,y_test))
y_tr_pred = model33.predict(X_train)
y_te_pred = model33.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['4'], test_size=0.1)
model44 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model44.fit(X_train,y_train)
print(model44.score(X_train,y_train))
print(model44.score(X_test,y_test))
y_tr_pred = model44.predict(X_train)
y_te_pred = model44.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['5'], test_size=0.1)
model55 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model55.fit(X_train,y_train)
print(model55.score(X_train,y_train))
print(model55.score(X_test,y_test))
y_tr_pred = model55.predict(X_train)
y_te_pred = model55.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['5'], test_size=0.1)
model55 = make_pipeline(StandardScaler(),DecisionTreeRegressor()) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model55.fit(X_train,y_train)
print(model55.score(X_train,y_train))
print(model55.score(X_test,y_test))
y_tr_pred = model55.predict(X_train)
y_te_pred = model55.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
featurename = ["회차","추천날짜","1","2","3","4","5","6","보너스"]
X_train,X_test, y_train, y_test = train_test_split(x_samples[featurename], y_samples['6'], test_size=0.1)
model66 = make_pipeline(StandardScaler(),DecisionTreeRegressor(max_depth=10)) # scaling한 다음 바로 DecisionTreeRegressor() 적용
model66.fit(X_train,y_train)
print(model66.score(X_train,y_train))
print(model66.score(X_test,y_test))
y_tr_pred = model66.predict(X_train)
y_te_pred = model66.predict(X_test)
print(f'정확도 train : {r2_score(y_train,y_tr_pred)}')
print(f'정확도 test : {r2_score(y_test,y_te_pred)}')
#회차 추첨날짜 1 2 3 4 5 6 보너스
ans1 =[[row_count, 1126, 3,5,12,22,26,31,19]]
y_pred_11 = model11.predict(ans1)
y_pred_22 = model22.predict(ans1)
y_pred_33 = model33.predict(ans1)
y_pred_44 = model44.predict(ans1)
y_pred_55 = model55.predict(ans1)
y_pred_66 = model66.predict(ans1)
print(np.round(y_pred_11,0),np.round(y_pred_22,0),np.round(y_pred_33,0),np.round(y_pred_44,0),np.round(y_pred_55,0),np.round(y_pred_66,0))
이번주 당첨되었던 결과를 뽑아볼까요??
음~ 학습되었던 데이터가 포함이 되어있었나보네요.
3등 당첨돼봤으면 좋겠네요 ㅠㅠ
#결과값
[12.] [17.] [20.] [26.] [34.] [36.]
반응형