def compute_model(BIDDING): ''' Fonction de base calculant le score du réseau''' df = search_biddings(BIDDING) X = df["leader"].values X = np.array([categorize_hand(h) for h in X]) y = (df["lead"] // 13).values y = np.stack(np_utils.to_categorical(y, num_classes=4)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) epochs = 15 batch_size = 256 model = get_model(4) model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2) score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0) print("Accuracy: ", score[1] * 100) Y_pred = model.predict(X_test) y_pred = np.argmax(Y_pred, axis=1) y_ = np.argmax(y_test, axis=1) target_names = ['S', 'H', 'D', 'C'] print("\nCorrélation prédiction / true:\n") print(classification_report(y_, y_pred, target_names=target_names)) print(confusion_matrix(y_, y_pred))
def compute_model_highLow(bid): df = search_biddings(bid) X = df["leader"].values X = np.array([categorize_hand(h) for h in X]) y = (df["lead"]).values X, y = add_color(X, y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) epochs = 10 batch_size = 256 model = get_model(2, 56, (4, 14, 1)) model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2) score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0) print("Accuracy: ", score[1] * 100) Y_pred = model.predict(X_test) y_pred = np.argmax(Y_pred, axis=1) y_ = np.argmax(y_test, axis=1) target_names = ['Low', 'High'] print("\nCorrélation prédiction / true:\n") print(classification_report(y_, y_pred, target_names=target_names)) print(confusion_matrix(y_, y_pred))
def compute_model_8(BIDDING): df = search_biddings(BIDDING) X = df["leader"].values X = np.array([categorize_hand(h) for h in X]) y = (df["lead"]).values y = np.stack( np_utils.to_categorical([classe8(lead) for lead in y], num_classes=8)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) epochs = 20 batch_size = 256 model = get_model(8) model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2) score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0) print("Accuracy: ", score[1] * 100) Y_pred = model.predict(X_test) y_pred = np.argmax(Y_pred, axis=1) y_ = np.argmax(y_test, axis=1) target_names = [ 'S_low', 'S_high', 'H_low', 'H_high', 'D_low', 'D_high', 'C_low', 'C_high' ] print("\nCorrélation prédiction / true:\n") print(classification_report(y_, y_pred, target_names=target_names)) print(confusion_matrix(y_, y_pred))
def main(): BIDDINGS = [["P"]] # ["1N,P,P,P", "P,1N,P,P,P", "P,P,1N,P,P,P", "P,P,P,1N,P,P,P"]] # ["1N,P,P,P", "P,1N,P,P,P"], # ["1N,P,P,P"], # ["1N,P,2C,P,2S,P,3N,P,P,P"], # ["1N,P,2C,P,2H,P,3N,P,P,P"], # ["1N,P,3N,P,P,P", "P,1N,P,3N,P,P,P", "P,P,1N,P,3N,P,P,P", "P,P,P,1N,P,3N,P,P,P"]] # ["1H,P,2H,P,4H,P,P,P"], # ["1C,P,1H,P,1N,P,P,P"], # ["1C,P,1S,P,1N,P,P,P"], # ["2N,P,3N,P,P,P"]] # BIDDINGS = [[x for x in data.enums.BIDSMAP]] for BIDDING in BIDDINGS: print("\n" + " - ".join(BIDDING)) print(len(search_biddings(BIDDING)))
import numpy as np from keras.utils import np_utils from data.parser import search_biddings from data.tools import categorize_hand from data.symetry import permute_shdc from sklearn.model_selection import train_test_split from learning.models import CNN_model BIDDINGS = ["1N,P,P,P", "P,1N,P,P,P", "P,P,1N,P,P,P", "P,P,P,1N,P,P,P"] df = search_biddings(BIDDINGS) print(len(df), " deals") X = df["leader"].values X = np.array([categorize_hand(h) for h in X]) y = (df["lead"]).values res = list(map(lambda x: permute_shdc(*x), zip(X, y))) X = np.array([b for a in res for b in a[0]]) y = np.array([b for a in res for b in a[1]]) y = np.stack(np_utils.to_categorical(y)) print(len(X), "deals after generating equivalent deals") X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) batch_size = 256 epochs = 100
def compute_model_long(BIDDING): df = search_biddings(BIDDING) X = df["leader"].values X = np.array([categorize_hand(h) for h in X]) y = (df["lead"] // 13).values ####################### Sélection de certaines donnes #################################################### # Y_longest = np.array([longest_color(x) for x in X]) # y_longest = np.argmax(Y_longest, axis=1) # # X_ = [] # Y_ = [] # for k in range(len(y_longest)): # if y_longest[k] == y[k]: # X_.append(X[k]) # Y_.append(y[k]) # X = np.array(X_) # y = np.array(Y_) y = np.stack(np_utils.to_categorical(y, num_classes=4)) X, y = all_symetries(X, y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # epochs = max(5, min(ceil(len(X_train) / 500.), 15)) epochs = 20 batch_size = 256 model = get_model(4) model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2) score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0) print("Accuracy: ", score[1] * 100) Y_pred = model.predict(X_test) y_pred = np.argmax(Y_pred, axis=1) y_ = np.argmax(y_test, axis=1) target_names = ['S', 'H', 'D', 'C'] print("\nCorrélation prédiction / true:\n") print(classification_report(y_, y_pred, target_names=target_names)) print(confusion_matrix(y_, y_pred)) ################################ Corrélations avec la longueur ############################################ print("\nCorrélation prédiction / longueur :\n") Y_longest = np.array([longest_color(x) for x in X_test]) print( classification_report(np.argmax(Y_longest, axis=1), y_pred, target_names=target_names)) print(confusion_matrix(np.argmax(Y_longest, axis=1), y_pred)) print("\nCorrélation longueur / vraie classe :\n") y_longest = np.argmax(Y_longest, axis=1) print(classification_report(y_, y_longest, target_names=target_names)) print(confusion_matrix(y_, y_longest)) X_ = [] Y_ = [] for k in range(len(y_longest)): if not y_longest[k] == y_[k]: X_.append(y_pred[k]) Y_.append(y_[k]) X_ = np.array(X_) Y_ = np.array(Y_) print("\nCorrélation prédiction / true lorsque true != longest:\n") print(classification_report(Y_, X_, target_names=target_names)) print(confusion_matrix(Y_, X_)) X_ = [] Y_ = [] for k in range(len(y_longest)): if not y_longest[k] == y_pred[k]: X_.append(y_pred[k]) Y_.append(y_[k]) X_ = np.array(X_) Y_ = np.array(Y_) print("\nCorrélation prédiction / true lorsque pred != longest:\n") print(classification_report(Y_, X_, target_names=target_names)) print(confusion_matrix(Y_, X_)) X_ = [] Y_ = [] for k in range(len(y_longest)): if y_[k] == y_pred[k]: X_.append(y_pred[k]) Y_.append(y_longest[k]) X_ = np.array(X_) Y_ = np.array(Y_) print("\nCorrélation prédiction / longueur lorsque pred == y_true:\n") print(classification_report(Y_, X_, target_names=target_names)) print(confusion_matrix(Y_, X_))