Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Machine Learnng - Top Trumps - www.101computing.net-->
<H1>Machine Learning Top Trumps Demonstration</H1>
This codepen purpose is to demonstrate a basic example of how machine learning works.
<br/><br/>
In this example, the computer learns how to play a game of Top Trumps more effectively to increase its chance of winning the game.
<br/><br/>
View full instructions on <a href="www.101Computing.net/machine-learning-top-trumps-game/" target="_blank">www.101Computing.net/machine-learning-top-trumps-game/</a>
<br/><br/>
<input type="Button" class=button value="Learning Sequence (10 iterations)"  onClick="learningPhase(10);">
<input type="Button" class=button value="Learning Sequence (100 iterations)"  onClick="learningPhase(100);">
<input type="Button" class=button value="Learning Sequence (1000 iterations)"  onClick="learningPhase(1000);">
<input type="Button" class=button value="Reset Knowledge Base"  onClick="resetStatistics();">
<hr/>
<H1>Knowledge Base</H1>
This knowledge base shows the "learned probabilities" of winning for each category of each card.<br/><br/>

<div id="statistics">
  <div id=card0 class=card></div>
  <div id=card1 class=card></div>
  <div id=card2 class=card></div>
  <div id=card3 class=card></div>
  <div id=card4 class=card></div>
  <div id=card5 class=card></div>
  <div id=card6 class=card></div>
  <div id=card7 class=card></div>
  <div id=card8 class=card></div>
 
</div>
              
            
!

CSS

              
                BODY {
  background-color: #000000;
  color:#ffffff;
  font-family: arial;
  font-size: 0.8em;
}

H2 {
  margin: 0px;
}

A {
  color: #DD44DD;
}
.button {
  margin: 4px;
  width: 250px;
  padding: 6px;
  border-radius: 10px;
}

.card {
  float: left;
  margin: 10px 10px;
}

.barChart {
  margin-left: 10px;
  width: 200px;
  height: 10px;
  border: 1px #aaaaaa solid;
  border-radius: 3px;
}

.barChartData0 {
  background-color: #FFFF00;
  height:10px;
}
.barChartData1 {
  background-color: #00FFFF;
  height:10px;
}
.barChartData2 {
  background-color: #FF00FF;
  height:10px;
}
.barChartData3 {
  background-color: #55FF55;
  height:10px;
}

              
            
!

JS

              
                //Copyright 101Computing.net

var cards=[["Mercury",57.9,4879,88,0],
["Venus",108.2,12104,224.7,0],
["Earth",149.6,12756,365.2,1],
["Mars",227.9,6792,687,2],
["Jupiter",778.6,142984,4331,67],
["Saturn",1433.5,120536,10747,62],
["Uranus",2872.5,51118,30589,27],
["Neptune",4495.1,49528,59800,14],
["Pluto",5906.4,2370,90560,5]];

var statistics=[[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]]];

var categories=["Distance from the Sun","Diameter","Orbital Period","Number Of Moons"];

function displayStatistics() {
  for (var i = 0; i < 9; i++) { 
    var card = document.getElementById("card" + i);
    var html = "<h2>" + cards[i][0] + "</h2><br/>";
    for (var category = 0; category < 4; category++) { 
        html = html + "&nbsp;&nbsp;" + categories[category] + "<br/>";
    var percent=0;
    if (statistics[i][category][1]>0) {
      percent = parseInt(statistics[i][category][0]*100/statistics[i][category][1]);
    }
    html = html + "<div class='barChart'><div class='barChartData" +category + "' style='width:" + (2*percent) + "px;'></div></div></br>"
    }
    html = html + "<br/><br/>";
    card.innerHTML=html;
  }
}

function learningPhase(numberOfIterations) {
  for (i = 0; i < numberOfIterations; i++) { 
   var cardIndex1 = Math.floor(9*Math.random());
   var cardIndex2 = Math.floor(9*Math.random());
   while (cardIndex2==cardIndex1) {
     cardIndex2 = Math.floor(9*Math.random());
   }
   var category = Math.floor(4*Math.random());
   if (cards[cardIndex1][category+1]>cards[cardIndex2][category+1]) {
     statistics[cardIndex1][category][0]++;
   } else {
     statistics[cardIndex2][category][0]++;
   }
   
   statistics[cardIndex1][category][1]++;
   statistics[cardIndex2][category][1]++;  
   displayStatistics(); 
  }  
}

function resetStatistics() {
  statistics=[[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]]];
   displayStatistics(); 
}

displayStatistics();
              
            
!
999px

Console