def learn(self, edge, strategy): self.display = [] with open('Model.csv', 'w', newline='') as stream: writer = csv.writer(stream) self.get_model().train(input_fn=self.train_input_fn(), steps=100) evaluation_result = self.get_model().evaluate( input_fn=self.test_input_fn()) predictions = list( self.get_model().predict(input_fn=self.test_input_fn())) for pred in predictions: self.test_results_info[3].append( round(pred['probabilities'][0], 2)) self.test_results_info[4].append( round(pred['probabilities'][1], 2)) self.test_results_info[5].append( round(pred['probabilities'][2], 2)) self.test_results_info[6].append(str(pred['classes'])) prediction_result = betting.test_betting_stategy( predictions, self.test_features, self.test_labels, self.test_results_info, edge, strategy) writer.writerow([ evaluation_result['accuracy'], evaluation_result['average_loss'], prediction_result['performance'] ]) self.display.append(prediction_result) self.display.append(evaluation_result) with open('Predictions.csv', 'w', newline='') as out: writer = csv.writer(out) writer.writerow( ["Home", "Away", "Date", "P(H)", "P(D)", "P(A)", "P"]) for j in range(len(self.test_results_info[0])): writer.writerow([ self.test_results_info[0][j], self.test_results_info[1][j], self.test_results_info[2][j].strftime('%d-%m-%Y'), self.test_results_info[3][j], self.test_results_info[4][j], self.test_results_info[5][j], self.test_results_info[6][j].replace("b", "").replace( "['", "").replace("']", "") ]) return self.display
def main(argv): os.system('rm -rf model/*') data = dataset.Dataset('data/liga.csv') train_results_len = int(TRAINING_SET_FRACTION * len(data.processed_results)) train_results = data.processed_results[:train_results_len] test_results = data.processed_results[train_results_len:] def map_results(results): features = {} for result in results: for key in result.keys(): if key not in features: features[key] = [] features[key].append(result[key]) for key in features.keys(): features[key] = np.array(features[key]) return features, features['result'] train_features, train_labels = map_results(train_results) test_features, test_labels = map_results(test_results) train_input_fn = tf.estimator.inputs.numpy_input_fn(x=train_features, y=train_labels, batch_size=500, num_epochs=None, shuffle=True) test_input_fn = tf.estimator.inputs.numpy_input_fn(x=test_features, y=test_labels, num_epochs=1, shuffle=False) feature_columns = [] for mode in ['home', 'away']: feature_columns = feature_columns + [ tf.feature_column.numeric_column(key='{}-wins'.format(mode)), tf.feature_column.numeric_column(key='{}-draws'.format(mode)), tf.feature_column.numeric_column(key='{}-losses'.format(mode)), tf.feature_column.numeric_column(key='{}-goals'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-goals'.format(mode)), tf.feature_column.numeric_column(key='{}-shots'.format(mode)), tf.feature_column.numeric_column( key='{}-shots-on-target'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-shots'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-shots-on-target'.format(mode)), ] model = tf.estimator.DNNClassifier( model_dir='model/', hidden_units=[10], feature_columns=feature_columns, n_classes=3, label_vocabulary=['H', 'D', 'A'], optimizer=tf.train.ProximalAdagradOptimizer( learning_rate=0.1, l1_regularization_strength=0.001)) with open('training-log.csv', 'w') as stream: csvwriter = csv.writer(stream) for i in range(0, 400): model.train(input_fn=train_input_fn, steps=100) evaluation_result = model.evaluate(input_fn=test_input_fn) predictions = list(model.predict(input_fn=test_input_fn)) prediction_result = betting.test_betting_stategy( predictions, test_features, test_labels) csvwriter.writerow([(i + 1) * 100, evaluation_result['accuracy'], evaluation_result['average_loss'], prediction_result['performance']])
def main(argv): data = dataset.Dataset('data/book.csv') train_results_len = int(TRAINING_SET_FRACTION * len(data.processed_results)) train_results = data.processed_results[:train_results_len] test_results = data.processed_results[train_results_len:] team_details = data.team_details[train_results_len:] def map_results(results): features = {} for result in results: for key in result.keys(): if key not in features: features[key] = [] features[key].append(result[key]) for key in features.keys(): features[key] = np.array(features[key]) return features, features['result'] train_features, train_labels = map_results(train_results) test_features, test_labels = map_results(test_results) train_input_fn = tf.estimator.inputs.numpy_input_fn(x=train_features, y=train_labels, batch_size=500, num_epochs=None, shuffle=True) test_input_fn = tf.estimator.inputs.numpy_input_fn(x=test_features, y=test_labels, num_epochs=1, shuffle=False) feature_columns = [] for mode in ['home', 'away']: feature_columns = feature_columns + [ tf.feature_column.numeric_column(key='{}-wins'.format(mode)), tf.feature_column.numeric_column(key='{}-draws'.format(mode)), tf.feature_column.numeric_column(key='{}-losses'.format(mode)), tf.feature_column.numeric_column(key='{}-goals'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-goals'.format(mode)), tf.feature_column.numeric_column(key='{}-shots'.format(mode)), tf.feature_column.numeric_column( key='{}-shots-on-target'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-shots'.format(mode)), tf.feature_column.numeric_column( key='{}-opposition-shots-on-target'.format(mode)), ] model = tf.estimator.DNNClassifier( model_dir='model/', hidden_units=[10], feature_columns=feature_columns, n_classes=3, label_vocabulary=['H', 'D', 'A'], optimizer=tf.train.ProximalAdagradOptimizer( learning_rate=0.1, l1_regularization_strength=0.001)) _accuracy = -1 FILENAME = 'results.csv' with open('training-log.csv', 'w') as stream: csvwriter = csv.writer(stream) for i in range(0, 2): model.train(input_fn=train_input_fn, steps=100) evaluation_result = model.evaluate(input_fn=test_input_fn) predictions = list(model.predict(input_fn=test_input_fn)) prediction_result = betting.test_betting_stategy( predictions, test_features, test_labels) if (evaluation_result['accuracy'] > _accuracy): _accuracy = evaluation_result['accuracy'] f = open(FILENAME, 'w') f.write('HomeTeam,AwayTeam,H,D,A,Prediction\n') for idx, val in enumerate(predictions): lst = team_details[idx] lst.extend(list(val['probabilities'])) lst.append(val['classes'][0].decode('utf-8')) values = list(map(str, lst)) f.write(','.join(values) + '\n') del lst del values f.close() csvwriter.writerow([(i + 1) * 100, evaluation_result['accuracy'], evaluation_result['average_loss'], prediction_result['performance']])