Skip to content

Instantly share code, notes, and snippets.

@bkey
Created March 25, 2016 03:08
Show Gist options
  • Save bkey/547cb45f14c8449d7428 to your computer and use it in GitHub Desktop.
Save bkey/547cb45f14c8449d7428 to your computer and use it in GitHub Desktop.
data science from scratch line graph
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.title {
font: 16px sans-serif;
}
.variance {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.biasSquared {
fill: none;
stroke: purple;
stroke-width: 1.5px;
stroke-dasharray: 5;
}
.totalError {
fill: none;
stroke: navy;
stroke-width: 1.5px;
stroke-dasharray: 3;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatDate = d3.time.format("%d-%b-%y");
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var varianceLine = d3.svg.line()
.x(function(d) { return x(d.model); })
.y(function(d) { return y(d.variance); });
var biasSquaredLine = d3.svg.line()
.x(function(d) { return x(d.model); })
.y(function(d) { return y(d.bias_squared); });
var totalErrorLine = d3.svg.line()
.x(function(d) { return x(d.model); })
.y(function(d) { return y(d.total_error); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("line_data.tsv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.model; }));
y.domain(d3.extent(data, function(d) { return d.bias_squared; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.attr("class", "title")
.text("The Bias-Variance Tradeoff");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("path")
.datum(data)
.attr("class", "variance")
.attr("data-legend", "Variance")
.attr("d", varianceLine);
svg.append("path")
.datum(data)
.attr("class", "biasSquared")
.attr("data-legend", "Bias^2")
.attr("d", biasSquaredLine);
svg.append("path")
.datum(data)
.attr("class", "totalError")
.attr("data-legend", "Total Error")
.attr("d", totalErrorLine);
});
function type(d) {
d.model = +d.model;
d.variance = +d.variance;
d.bias_squared = +d.bias_squared;
d.total_error = d.variance + d.bias_squared;
return d;
}
</script>
model variance bias_squared
1 1 256
2 2 128
3 4 64
4 8 32
5 16 16
6 32 8
7 64 4
8 128 2
9 256 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment