Public
Edited
Nov 2, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
labeled_weights
Insert cell
Insert cell
weights
Insert cell
Insert cell
updated_weights
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Distance metric, computes normalized high-dimensional distance between 2 rows in the data table.
// r1, r2 = references to data table rows.
function Distance(r1, r2) {
return d3.sum(
data.attrs.map((attr) => Math.abs(r1[attr] - r2[attr]) * data.weights[attr])
); // weighted L1
//Math.abs(r1[attr] - r2[attr]) / data.stdevs[attr] * data.weights[attr] )); // weighted L1 normalized
//return d3.sum(data.attrs.map(attr =>
// Math.abs(r1[attr] - r2[attr]) / data.stdevs[attr] )); // L1
//return Math.sqrt(d3.sum(data.attrs.map(attr =>
// Math.pow((r1[attr] - r2[attr])/data.stdevs[attr], 2)))); // L2
}
Insert cell
Insert cell
d3.mean(graph.edges, (e) => e.mydistance)
Insert cell
Insert cell
{ // update the MDS based on slider changes
data.weights = w;
graph.edges.forEach( (e, i) => {
e.mydistance = Distance(e.source.row, e.target.row)} );
simulation.force("link")
.distance(e => e.mydistance * scale * distanceScale);
simulation.alpha(1).restart();
}
Insert cell
// MDS force simulator
simulation = d3
.forceSimulation(graph.vertices)
.force(
"link",
d3
.forceLink(graph.edges)
.distance((e) => e.mydistance * distanceScale)
//.strength(e => 10.0 / e.distance)
.strength(0.2)
)
//.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(height / 2.0, height / 2.0))
Insert cell
Insert cell
Insert cell
width
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
scale_to_pix.domain()
Insert cell
scale_to_pix.invert(431)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//manhattan_distance([2,2], [1,8])
Insert cell
Insert cell
//euclidean_distance([1,2], [2,5])
Insert cell
//testing_data = [[24.9, 8],[25.1, 6],[24.8, 3.39]]
Insert cell
//pairwise_distance(testing_data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//x = [[0, 25, 31.88], [25, 0, 16.28], [31.88, 16.28, 0]]
Insert cell
//y = [[0,2.3,5], [2.31,0,2.76], [5.04, 2.76, 0]]
Insert cell
//stress(x, y)
Insert cell
Insert cell
Insert cell
//testing_data = [[1,2], [3,4]]
Insert cell
//testing_weights = [1, 0.5]
Insert cell
Insert cell
Insert cell
Insert cell
attrs.length
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
FileAttachment("Animal_Data_Andromeda2.csv").csv()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more