/
NBMulti_ML.py
114 lines (95 loc) · 5.07 KB
/
NBMulti_ML.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import random
import sys
import numpy as np
import sklearn
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
from Blackjack import Blackjack
def datacoll(num_games):
wins = 0
pushes = 0
loses = 0
blackjack = Blackjack()
Data_coll = [] #initialize the data set for my bayes method
for _ in range(num_games):
finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["start"]) #I can name these output variables differently and create my output data differently
hit_count = 0
game_data = [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1]
Data_coll.append(game_data)
while not finished:
action = random.choice([Blackjack.ACTION["hit"], Blackjack.ACTION["hold"]])
finished, result, dealer_cards, player_cards = blackjack.play_game(action)
if action == Blackjack.ACTION["hit"]:
hit_count += 1
Data_coll.append([Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1])
else:
Data_coll.append([Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),result,hit_count,_+1])
wins += 1 if result == Blackjack.RESULT["win"] else 0
pushes += 1 if result == Blackjack.RESULT["push"] else 0
loses += 1 if result == Blackjack.RESULT["lose"] else 0
return (Data_coll)
def dataprocc(data,game): #breaks down the data into game sequence lists
datasize = len(data)
current_game = []
for i in range(datasize): #uses a for loop to break down the data lists into the actions of a game
if game == data[i][4] : #identifies game of the current item.
current_game.append(data[i])
return(current_game)
def gameprocc(game_data):
datasize = len(game_data) #identifies the number of moves made in the game, it is actually one more than the number of moves
outcome = game_data[datasize-1][2] #i am only going to record data for the last move because each player move of hit or hold is independent of each other
player_value = game_data[0][0] #the starting card value of the player
dealer_value = game_data[0][1] #the starting dealer value of his shown card
features = []
if datasize == 2 :
if abs(player_value - game_data[datasize-1][0]) > 0 :
hit = 1
else:
hit = 0
features = [outcome, player_value,dealer_value,hit] #[win(1)/lose(-1)/push(0), player value, dealer value of shown card, hit/hold ]
elif player_value == 21:
features = [outcome, player_value,dealer_value,0]
return(features)
if __name__ == '__main__':
num_games = 10000
currentgame = 1
AllData = datacoll(num_games)
Gamemove = []
for i in range(num_games):
cur_ga_mov = dataprocc(AllData,currentgame)
Gamemove.append(gameprocc(cur_ga_mov))
currentgame += 1
Gamemove = [x for x in Gamemove if x != []] #creates a list of lists which contains the features and attributes of the data
feat_labels = ['Game Outcome' 'Player Card Value' 'Dealer Shown Card Value' 'Move Made' ]
#Training Data
outcome =np.array([item[0] for item in Gamemove])
for i in range(len(Gamemove)):
Gamemove[i].pop(0)
Data = np.array(Gamemove)
# Initialize our classifier
mnb = MultinomialNB()
# Train our classifier
model = mnb.fit(Data, outcome)
# Playing With NB.Multinomial Classifier
num_games_test = 1000
wins = 0
pushes = 0
loses = 0
blackjack = Blackjack()
for _ in range(num_games_test):
finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["start"]) #I can name these output variables differently and create my output data differently
while not finished:
outc_W_hit = [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),1]
outc_Wout_hit = [Blackjack.get_card_value(player_cards),Blackjack.get_card_value([dealer_cards[0]]),0]
preds_W_hit = mnb.predict_proba([outc_W_hit]) #order of this output is [-1 0 1] thanks to mnb.classes_ attribute output
preds_Wout_hit = mnb.predict_proba([outc_Wout_hit])
if preds_W_hit[0][2] > preds_Wout_hit[0][2]:
finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["hit"])
else:
finished, result, dealer_cards, player_cards = blackjack.play_game(Blackjack.ACTION["hold"])
wins += 1 if result == Blackjack.RESULT["win"] else 0
pushes += 1 if result == Blackjack.RESULT["push"] else 0
loses += 1 if result == Blackjack.RESULT["lose"] else 0
# Machine Learning Winning Percentage
Total = wins + loses
print (wins/Total)