Make soccer prediction from sample data

476 Views Asked by At

I have this code on a node.js playground. I'm experimenting with brain.js and I want to predict the probability that a team will win a match.

NB: I saw this post where the user was trying to do something similar, not sure if the solution was looking something similar to my code

var playground = require("playground")
const brain = require("brain.js")

const network = new brain.NeuralNetwork();



const samples = [
{ homeTeam: "Lazio",
  homeTeamId: 1,
  awayTeam: "Inter",
  awayTeamId: 2,
  matchResult: 1
},
{ homeTeam: "Juventus",
  homeTeamId: 3,
  awayTeam: "Roma",
  awayTeamId: 4,
  matchResult: 1
},
{ homeTeam: "Napoli",
  homeTeamId: 5,
  awayTeam: "Sampdoria",
  awayTeamId: 6,
  matchResult: 0
},
{ homeTeam: "Udinese",
  homeTeamId: 7,
  awayTeam: "Monza",
  awayTeamId: 8,
  matchResult: 1
},
{ homeTeam: "Verona",
  homeTeamId: 9,
  awayTeam: "Milan",
  awayTeamId: 10,
  matchResult: 0
}
]
// network.train([
//   { input: [0, 0, 0], output: [0] },
//   { input: [0, 0, 1], output: [0] },
//   { input: [0, 1, 1], output: [0] },
//   { input: [1, 0, 1], output: [1] },
//   { input: [1, 1, 1], output: [1] }
// ]);

network.train( samples.map( (sample, i) => {
  return {
    input: [sample.homeTeamId, sample.awayTeamId],
    output: [sample.matchResult]
  }
  //{ input: [1, 2], output: [1] }, // Team 2 wins
  //{ input: [1, 3], output: [1] }, // Team 3 wins
  //{ input: [2, 3], output: [0] }, // Team 2 wins
  //{ input: [2, 4], output: [1] }, // Team 4 wins
  //{ input: [1, 2], output: [0] }, // Team 1 wins
  //{ input: [1, 3], output: [0] }, // Team 1 wins
  //{ input: [3, 4], output: [0] } // Team 3 wins

}));

const output = network.run([2, 3]);

console.log(`Prob: ${output}`);

My idea is to pass the results of each single match played in the last two soccer season to train the network.

I want to give to each football team a static id that so I can call the network.run([3,1]) to get a prediction of the next match where the team with the id 3 will play against team with id 1.

At the moment the code seems not working at all, I will always get a prediction of "Prob: 0.9995707273483276" in the console log, also if I pass different teams id eg. network.run([2,4]), network.run([5,7])

How I can corretcly make a prediction?Maybe I'm using the wrong method or miss something?

Will be this possible also with tensorflow.js eventually?

1

There are 1 best solutions below

5
Yunat Amos On

WE are together. Use this to get some ideas https://github.com/lukewduncan/brain-js-predictor/blob/master/routes/index.js

and

const teams = {
  Sofapaka: 0,
  Tusker: 1,
  Posta: 2,
  Wazito: 3,
  Chemelil: 4,
  Nzoia: 5,
  'Western Stima': 6,
  'Zoo Kericho': 7,
  Vihiga: 8,
  'Gor Mahia': 9,
  'Sony Sugar': 10,
  Kakamega: 11,
  Port: 12,
  Leopards: 13,
  Kcb: 14,
  Kariobangi: 15,
  'Ulinzi Stars': 16,
  Mathare: 17
}

const x = teams['Kariobangi']
const y = teams['Nzoia']

// eslint-disable-next-line handle-callback-err
con.query(`SELECT * FROM kenyanleaguematches WHERE result != '' AND result !="" ORDER BY id ASC `, function (err, result, fields) {
  console.log(result.length)
  const scores = []
  for (let i = 0; i < result.length; i++) {
    const score = result[i].result.split(':')
    const homeScore = score[0]
    const awayScore = score[1]
    const whoWon = awayScore === homeScore ? 'd' : homeScore > awayScore ? 'h' : 'a'
    const homeTeam = result[i].home_team
    const awayTeam = result[i].away_team
    // eslint-disable-next-line standard/object-curly-even-spacing
    const r = {input: [teams[homeTeam], teams[awayTeam]], output: [Number(`${whoWon === 'd' ? 0 : whoWon === 'h' ? 1 : 2}`)] }
    scores.push(r)
  }

  // console.log(scores)

  // const data = [
  // {input: [1, 3], output: [1]},
  // {input: [2, 1], output: [2]},
  // {input: [3, 4], output: [1]},
  // {input: [3, 2], output: [1]},
  // {input: [2, 4], output: [1]}
  // ]

// create configuration for training
  const config = {
    iterations: 2000,
    log: true,
    logPeriod: 500,
    layers: [10]
  }

  net.train(scores, config)
// console.log(net.run([1, 4]))
//   const output = net.run([1, 4])
//   console.log(JSON.stringify(output))
  console.log([x, y])
  const output = net.run([x, y])
  console.log(JSON.stringify(output))

  console.log(Object.keys(output))

  console.log('Done')
})