HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!-- The library is included in this pen's javascript settings, but the following is also possible: -->
<!--<script src="https://rawgit.com/StefanieStoppel/d3ML/master/lib/d3ml.min.js"></script>-->
<h1 class="center">K-Nearest Neighbor (KNN)</h1>
<p class="center"><strong>A usage example of the <a href="https://github.com/StefanieStoppel/d3ML" target="_blank" title="d3ML repository on GitHub">d3ML</a> javascript library.</strong></p>
<p class="center">Click on the grey area to create new circles and see how their color changes based on their neighbors.</p>
<div id="knn"></div>
<div class="description">
<p>
In the visualization you can see how the <strong>k-nearest neighbor (KNN)</strong> algorithm
<strong>classifies</strong> new data points based on their <strong>k</strong> nearest neighbors, where the amount of neighbors k is specified using the slider above. The new point will be assigned the <strong>class</strong> that the majority of its k nearest neighbors hold.
<br><br>
The initial circles are our "training" data set. The data set is divided into two classes: <strong>red and blue</strong>.
When we add a new circle, we want to find out which class (aka color) we need to assign it to, depending on its neighbors.
<br><br>
What's going on behind the scenes after you add a circle:
</p>
<ol>
<li>The algorithm goes through all other circles and calculates their distance from your new circle.</li>
<li>It then sorts them by distance from your circle in ascending order, meaning the circles with the smallest distances to the new circle come first.</li>
<li>It picks the first k entries from the result of step 2.</li>
<li>It looks at the k nearest neighbors' classes. If the majority of them are blue, our new point will be blue as well. If most of them are red, then the new point will be red.</li>
</ol>
<h3>The weighted KNN</h3>
<p>
If you check the <strong>"Weighted" checkbox</strong> above, the algorithm is justified, so that the <strong>inverse of the distance</strong> of the k neighbors is taken into account. This is important during "ties", meaning that you chose an even amount of neighbors k and half of them are class red while the other half are blue.
If you don't use the weighted version of KNN in this case, the neighbor with the closest distance will <strong>ALWAYS</strong> win, so that your circle will take on its color.
<p>
<h4>Let's look at an example:</h4>
<p>
Imagine you chose <strong>k=4</strong> and the nearest neighbors are <strong>[(Blue,2), (Blue,500), (Red,3), (Red,4)]</strong>, where the numbers represent the distances from the new circle. In the unweighted KNN, the nearest neighbor's class - in this case Blue - would automatically win. But look at the distances: The two red circles are much closer to the new circle than the second blue circle. It might not be the right decision to just go on the closest circle and not take the others into account!
<br><br>
When using the <strong>weighted KNN</strong>, the way of determining which class to assign to the new circle is different:<br>
The weighted KNN sums up the <strong>inverted distance</strong> of all the nearest neighbors belonging to the <strong>same class</strong>. It then checks which of the classes' <strong>sums is the greatest</strong> and assigns the corresponding class to the new circle.<br><br>
In our example the weights of the classes are:
</p>
<ul>
<li>Weight of Blue = 1/2 + 1/500 = 0.502</li>
<li>Weight of Red = 1/3 + 1/4 = 0.583</li>
</ul>
<p>
Because the weight of class Red is greater than that of class Blue, the new circle will be <strong>red</strong>!<br>
</div>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Helvetica;
background-color: #33353d;
color: white;
}
a {
color: yellow;
}
h1 {
margin-top: 0.4em;
margin-bottom: 0;
}
p {
margin-top: 0.5em;
}
.center {
text-align: center;
}
.d3ml__settings {
display: flex;
justify-content: space-around;
}
.d3ml__settings__group:first-of-type {
display: flex;
flex-direction: column;
}
.description {
width: 80%;
}
const displayWidth = window.innerWidth - 25;
const displayHeight = 450;
const dataSetSize = 250;
const options = {
rootNode: '#knn',
width: displayWidth,
height: displayHeight,
backgroundColor: 'black',
circleFill: '#3fe4h2',
circleStroke: 'white'
};
const types = ['A', 'B'];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createRandomEllipsoidCoordinates(width, height, cx, cy) {
const rho = Math.sqrt(Math.random())
const phi = Math.random() * Math.PI * 2
const rands = {x: getRandomInt(-width/2,width/2), y: getRandomInt(-height/2,height/2)}
const x = (rho * Math.cos(phi) * width/2) + cx + rands.x
const y = (rho * Math.sin(phi) * height/2) + cy + rands.y
return {x, y}
}
function createRandomData() {
const ellipsoidOptions = {
'A': {
width: displayWidth/3,
height: displayWidth/3,
cx: displayWidth/3,
cy: displayHeight/3
},
'B': {
width: displayWidth/2.5,
height: displayWidth/2.5,
cx: displayWidth*0.663,
cy: displayHeight*0.66
}
};
return Array.apply(null, Array(dataSetSize))
.map(d => {
const type = Math.random() > 0.5 ? types[0] : types[1];
const {width, height, cx, cy} = ellipsoidOptions[type]
const {x, y} = createRandomEllipsoidCoordinates(width, height, cx, cy);
return {x, y, type};
}
);
}
const data = createRandomData();
const k = 3;
const vis = new d3ml.KNNVisualization(data, options, types, k);
vis.draw();
Also see: Tab Triggers